Skip to content

Commit c93f199

Browse files
asotonasormuras
andauthored
Convert Classfile API test to use JUnit Jupiter (openjdk#44)
Co-authored-by: Christian Stein <sormuras@gmail.com>
1 parent 8db63f2 commit c93f199

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+815
-890
lines changed

test/jdk/jdk/classfile/AccessFlagsTest.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,26 @@
2626
/*
2727
* @test
2828
* @summary Testing Classfile AccessFlags.
29-
* @run testng AccessFlagsTest
29+
* @run junit AccessFlagsTest
3030
*/
3131
import java.util.EnumSet;
3232
import java.util.Random;
3333
import java.util.Set;
34+
import java.util.function.Consumer;
3435
import java.util.function.Function;
3536
import java.util.function.IntFunction;
3637
import java.lang.reflect.AccessFlag;
3738
import jdk.classfile.AccessFlags;
38-
import org.testng.annotations.Test;
39-
import static org.testng.Assert.assertEquals;
40-
import org.testng.annotations.DataProvider;
39+
import static org.junit.jupiter.api.Assertions.*;
40+
import org.junit.jupiter.api.Test;
41+
import org.junit.jupiter.params.provider.EnumSource;
42+
import org.junit.jupiter.params.ParameterizedTest;
4143

42-
public class AccessFlagsTest {
44+
class AccessFlagsTest {
4345

44-
@DataProvider(name = "accessFlagsContexts")
45-
public static AccessFlag.Location[] accessFlagsContexts() {
46-
return new AccessFlag.Location[] {AccessFlag.Location.CLASS, AccessFlag.Location.METHOD, AccessFlag.Location.FIELD};
47-
}
48-
49-
@Test(dataProvider = "accessFlagsContexts")
50-
public void testRandomAccessFlagsConverions(AccessFlag.Location ctx) {
46+
@ParameterizedTest
47+
@EnumSource(names = { "CLASS", "METHOD", "FIELD" })
48+
void testRandomAccessFlagsConverions(AccessFlag.Location ctx) {
5149
IntFunction<AccessFlags> intFactory = switch (ctx) {
5250
case CLASS -> AccessFlags::ofClass;
5351
case METHOD -> AccessFlags::ofMethod;
@@ -74,12 +72,16 @@ public void testRandomAccessFlagsConverions(AccessFlag.Location ctx) {
7472
}
7573
}
7674

77-
@Test(dataProvider = "accessFlagsContexts", expectedExceptions = IllegalArgumentException.class)
78-
public void testInvalidFlagsUse(AccessFlag.Location ctx) {
79-
switch (ctx) {
80-
case CLASS -> AccessFlags.ofClass(AccessFlag.values());
81-
case FIELD -> AccessFlags.ofField(AccessFlag.values());
82-
case METHOD -> AccessFlags.ofMethod(AccessFlag.values());
83-
}
75+
@Test
76+
void testInvalidFlagsUse() {
77+
assertAll(
78+
() -> assertThrowsForInvalidFlagsUse(AccessFlags::ofClass),
79+
() -> assertThrowsForInvalidFlagsUse(AccessFlags::ofField),
80+
() -> assertThrowsForInvalidFlagsUse(AccessFlags::ofMethod)
81+
);
82+
}
83+
84+
void assertThrowsForInvalidFlagsUse(Consumer<AccessFlag[]> factory) {
85+
assertThrows(IllegalArgumentException.class, () -> factory.accept(AccessFlag.values()));
8486
}
8587
}

test/jdk/jdk/classfile/AdaptCodeTest.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/*
2727
* @test
2828
* @summary Testing Classfile Code Adaptation.
29-
* @run testng AdaptCodeTest
29+
* @run junit AdaptCodeTest
3030
*/
3131

3232
import java.lang.constant.ConstantDesc;
@@ -44,20 +44,20 @@
4444
import helpers.TestUtil;
4545
import helpers.Transforms;
4646
import jdk.classfile.instruction.ConstantInstruction;
47-
import org.testng.annotations.DataProvider;
48-
import org.testng.annotations.Test;
47+
import org.junit.jupiter.api.Test;
48+
import static org.junit.jupiter.api.Assertions.*;
49+
import org.junit.jupiter.params.ParameterizedTest;
50+
import org.junit.jupiter.params.provider.ValueSource;
4951

50-
import static org.testng.Assert.assertEquals;
51-
52-
@Test()
53-
public class AdaptCodeTest {
52+
class AdaptCodeTest {
5453

5554
static final String testClassName = "AdaptCodeTest$TestClass";
5655
static final Path testClassPath = Paths.get(URI.create(AdaptCodeTest.class.getResource(testClassName + ".class").toString()));
5756
private static final String THIRTEEN = "BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah";
5857
private static final String SEVEN = "BlahBlahBlahBlahBlahBlahBlah";
5958

60-
public void testNullAdaptIterator() throws Exception {
59+
@Test
60+
void testNullAdaptIterator() throws Exception {
6161
ClassModel cm = Classfile.parse(testClassPath);
6262
for (ClassTransform t : Transforms.noops) {
6363
byte[] newBytes = cm.transform(t);
@@ -69,16 +69,22 @@ public void testNullAdaptIterator() throws Exception {
6969
}
7070
}
7171

72-
@Test(dataProvider = "noExceptionClassfiles")
73-
public void testNullAdaptIterator2(String path) throws Exception {
72+
@ParameterizedTest
73+
@ValueSource(strings = {
74+
"modules/java.base/java/util/AbstractCollection.class",
75+
"modules/java.base/java/util/PriorityQueue.class",
76+
"modules/java.base/java/util/ArraysParallelSortHelpers.class"
77+
})
78+
void testNullAdaptIterator2(String path) throws Exception {
7479
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
7580
ClassModel cm = Classfile.parse(fs.getPath(path));
7681
for (ClassTransform t : Transforms.noops) {
7782
byte[] newBytes = cm.transform(t);
7883
}
7984
}
8085

81-
public void testSevenOfThirteenIterator() throws Exception {
86+
@Test
87+
void testSevenOfThirteenIterator() throws Exception {
8288
ClassModel cm = Classfile.parse(testClassPath);
8389

8490
var transform = ClassTransform.transformingMethodBodies((codeB, codeE) -> {
@@ -103,17 +109,8 @@ public void testSevenOfThirteenIterator() throws Exception {
103109
assertEquals(result, SEVEN);
104110
}
105111

106-
@DataProvider(name = "noExceptionClassfiles")
107-
public Object[][] provide() {
108-
return new Object[][] { { "modules/java.base/java/util/AbstractCollection.class" },
109-
{ "modules/java.base/java/util/PriorityQueue.class" },
110-
{ "modules/java.base/java/util/ArraysParallelSortHelpers.class" }
111-
};
112-
}
113-
114-
115-
116-
public void testCopy() throws Exception {
112+
@Test
113+
void testCopy() throws Exception {
117114
ClassModel cm = Classfile.parse(testClassPath);
118115
byte[] newBytes = Classfile.build(cm.thisClass().asSymbol(), cb -> cm.forEachElement(cb));
119116
// TestUtil.writeClass(newBytes, "TestClass.class");

test/jdk/jdk/classfile/AdvancedTransformationsTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/*
2727
* @test
2828
* @summary Testing Classfile advanced transformations.
29-
* @run testng AdvancedTransformationsTest
29+
* @run junit AdvancedTransformationsTest
3030
*/
3131
import helpers.ByteArrayClassLoader;
3232
import java.util.Map;
@@ -41,8 +41,8 @@
4141
import jdk.classfile.impl.StackMapGenerator;
4242
import jdk.classfile.components.ClassRemapper;
4343
import jdk.classfile.components.CodeLocalsShifter;
44-
import org.testng.annotations.Test;
45-
import static org.testng.Assert.*;
44+
import org.junit.jupiter.api.Test;
45+
import static org.junit.jupiter.api.Assertions.*;
4646
import static helpers.TestUtil.assertEmpty;
4747
import java.lang.constant.ClassDesc;
4848
import java.lang.constant.ConstantDescs;
@@ -72,10 +72,10 @@
7272
import java.lang.annotation.Target;
7373
import jdk.classfile.impl.AbstractPseudoInstruction;
7474

75-
public class AdvancedTransformationsTest {
75+
class AdvancedTransformationsTest {
7676

7777
@Test
78-
public void testShiftLocals() throws Exception {
78+
void testShiftLocals() throws Exception {
7979
try (var in = StackMapGenerator.class.getResourceAsStream("StackMapGenerator.class")) {
8080
var clm = Classfile.parse(in.readAllBytes());
8181
var remapped = Classfile.parse(clm.transform((clb, cle) -> {
@@ -107,7 +107,7 @@ public void accept(CodeBuilder builder, CodeElement element) {
107107
}
108108

109109
@Test
110-
public void testRemapClass() throws Exception {
110+
void testRemapClass() throws Exception {
111111
var map = Map.of(
112112
ConstantDescs.CD_List, ClassDesc.of("remapped.List"),
113113
ClassDesc.ofDescriptor(AbstractPseudoInstruction.ExceptionCatchImpl.class.descriptorString()), ClassDesc.of("remapped.ExceptionCatchImpl"),
@@ -166,7 +166,7 @@ public Rec(Foo foo) {
166166
}
167167

168168
@Test
169-
public void testRemapModule() throws Exception {
169+
void testRemapModule() throws Exception {
170170
var foo = ClassDesc.ofDescriptor(Foo.class.descriptorString());
171171
var bar = ClassDesc.ofDescriptor(Bar.class.descriptorString());
172172

@@ -183,7 +183,7 @@ public void testRemapModule() throws Exception {
183183
}
184184

185185
@Test
186-
public void testRemapDetails() throws Exception {
186+
void testRemapDetails() throws Exception {
187187
var foo = ClassDesc.ofDescriptor(Foo.class.descriptorString());
188188
var bar = ClassDesc.ofDescriptor(Bar.class.descriptorString());
189189
var fooAnno = ClassDesc.ofDescriptor(FooAnno.class.descriptorString());
@@ -233,7 +233,7 @@ private static void verifySignature(ClassDesc desc, Signature sig) {
233233
}
234234

235235
@Test
236-
public void testInstrumentClass() throws Exception {
236+
void testInstrumentClass() throws Exception {
237237
var instrumentor = Classfile.parse(AdvancedTransformationsTest.class.getResourceAsStream("AdvancedTransformationsTest$InstrumentorClass.class").readAllBytes());
238238
var target = Classfile.parse(AdvancedTransformationsTest.class.getResourceAsStream("AdvancedTransformationsTest$TargetClass.class").readAllBytes());
239239
var instrumentedBytes = instrument(target, instrumentor, mm -> mm.methodName().stringValue().equals("instrumentedMethod"));

test/jdk/jdk/classfile/AnnotationModelTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@
2626
/*
2727
* @test
2828
* @summary Testing Classfile annotation model.
29-
* @run testng AnnotationModelTest
29+
* @run junit AnnotationModelTest
3030
*/
3131
import jdk.classfile.Classfile;
3232
import jdk.classfile.Attributes;
33-
import org.testng.annotations.Test;
33+
import org.junit.jupiter.api.Test;
3434

3535
import java.io.IOException;
3636
import java.net.URI;
3737
import java.nio.file.FileSystem;
3838
import java.nio.file.FileSystems;
3939
import java.nio.file.Files;
4040

41-
import static org.testng.Assert.*;
41+
import static org.junit.jupiter.api.Assertions.*;
4242

43-
public class AnnotationModelTest {
43+
class AnnotationModelTest {
4444
private static final FileSystem JRT = FileSystems.getFileSystem(URI.create("jrt:/"));
4545
private static final String testClass = "modules/java.base/java/lang/annotation/Target.class";
4646
static byte[] fileBytes;
@@ -54,7 +54,7 @@ public class AnnotationModelTest {
5454
}
5555

5656
@Test
57-
public void readAnnos() {
57+
void readAnnos() {
5858
var model = Classfile.parse(fileBytes);
5959
var annotations = model.findAttribute(Attributes.RUNTIME_VISIBLE_ANNOTATIONS).get().annotations();
6060

test/jdk/jdk/classfile/AnnotationTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/*
2727
* @test
2828
* @summary Testing Classfile annotations.
29-
* @run testng AnnotationTest
29+
* @run junit AnnotationTest
3030
*/
3131
import java.lang.constant.ClassDesc;
3232
import static java.lang.constant.ConstantDescs.*;
@@ -41,21 +41,18 @@
4141
import jdk.classfile.attribute.RuntimeVisibleAnnotationsAttribute;
4242
import jdk.classfile.*;
4343
import jdk.classfile.constantpool.ConstantPoolBuilder;
44-
import org.testng.annotations.Test;
44+
import org.junit.jupiter.api.Test;
4545

4646
import static java.util.stream.Collectors.toList;
4747
import static java.util.stream.Collectors.toSet;
4848
import jdk.classfile.impl.DirectClassBuilder;
4949

50-
import static org.testng.Assert.assertEquals;
51-
import static org.testng.Assert.assertTrue;
52-
import static org.testng.AssertJUnit.fail;
50+
import static org.junit.jupiter.api.Assertions.*;
5351

5452
/**
5553
* AnnotationTest
5654
*/
57-
@Test
58-
public class AnnotationTest {
55+
class AnnotationTest {
5956
enum E {C};
6057

6158
private static Map<String, Object> constants
@@ -128,7 +125,8 @@ private static RuntimeVisibleAnnotationsAttribute buildAnnotationsWithCPB(Consta
128125
return RuntimeVisibleAnnotationsAttribute.of(Annotation.of(constantPoolBuilder.utf8Entry("LAnno;"), elements()));
129126
}
130127

131-
public void testAnnos() {
128+
@Test
129+
void testAnnos() {
132130
byte[] bytes = Classfile.build(ClassDesc.of("Foo"), cb -> {
133131
((DirectClassBuilder) cb).writeAttribute(buildAnnotationsWithCPB(cb.constantPool()));
134132
cb.withMethod("foo", MethodTypeDesc.of(CD_void), 0, mb -> mb.with(buildAnnotationsWithCPB(mb.constantPool())));
@@ -172,7 +170,8 @@ private static RuntimeVisibleAnnotationsAttribute buildAnnotations() {
172170
elements()));
173171
}
174172

175-
public void testAnnosNoCPB() {
173+
@Test
174+
void testAnnosNoCPB() {
176175
byte[] bytes = Classfile.build(ClassDesc.of("Foo"), cb -> {
177176
((DirectClassBuilder) cb).writeAttribute(buildAnnotations());
178177
cb.withMethod("foo", MethodTypeDesc.of(CD_void), 0, mb -> mb.with(buildAnnotations()));

test/jdk/jdk/classfile/ArrayTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/*
2727
* @test
2828
* @summary Testing Classfile arrays.
29-
* @run testng ArrayTest
29+
* @run junit ArrayTest
3030
*/
3131
import jdk.classfile.Classfile;
3232
import jdk.classfile.ClassModel;
@@ -37,22 +37,22 @@
3737
import jdk.classfile.instruction.NewMultiArrayInstruction;
3838
import jdk.classfile.instruction.NewPrimitiveArrayInstruction;
3939
import jdk.classfile.instruction.NewReferenceArrayInstruction;
40-
import org.testng.annotations.Test;
40+
import org.junit.jupiter.api.Test;
4141

42-
import static org.testng.Assert.assertEquals;
42+
import static org.junit.jupiter.api.Assertions.*;
4343

4444
import java.net.URI;
4545
import java.nio.file.Path;
4646
import java.nio.file.Paths;
4747
import java.util.Iterator;
4848

49-
public class ArrayTest {
49+
class ArrayTest {
5050
static final String testClassName = "ArrayTest$TestClass";
5151
static final Path testClassPath = Paths.get(URI.create(ArrayTest.class.getResource(testClassName + ".class").toString()));
5252

5353

5454
@Test
55-
public void testArrayNew() throws Exception {
55+
void testArrayNew() throws Exception {
5656
ClassModel cm = Classfile.parse(testClassPath);
5757

5858
for (MethodModel mm : cm.methods()) {

test/jdk/jdk/classfile/BSMTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/*
2727
* @test
2828
* @summary Testing Classfile bootstrap methods.
29-
* @run testng BSMTest
29+
* @run junit BSMTest
3030
*/
3131
import java.lang.constant.ClassDesc;
3232
import java.lang.constant.MethodTypeDesc;
@@ -45,10 +45,10 @@
4545
import jdk.classfile.constantpool.LoadableConstantEntry;
4646
import jdk.classfile.constantpool.MemberRefEntry;
4747
import jdk.classfile.constantpool.MethodHandleEntry;
48-
import org.testng.annotations.Test;
48+
import org.junit.jupiter.api.Test;
4949

5050
import static java.lang.constant.ConstantDescs.CD_String;
51-
import static org.testng.Assert.assertEquals;
51+
import static org.junit.jupiter.api.Assertions.*;
5252

5353
public class BSMTest {
5454
static final String testClassName = "BSMTest$SomeClass";
@@ -58,8 +58,8 @@ public class BSMTest {
5858
private static final String TWENTY = "BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah";
5959
private static final String TYPE = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/String;";
6060

61-
@Test()
62-
public void testSevenOfThirteenIterator() throws Exception {
61+
@Test
62+
void testSevenOfThirteenIterator() throws Exception {
6363
ClassModel cm = Classfile.parse(testClassPath);
6464
byte[] newBytes = cm.transform((cb, ce) -> {
6565
if (ce instanceof MethodModel mm) {

0 commit comments

Comments
 (0)