Skip to content

Commit bbb4b0d

Browse files
committed
8376277: Migrate java/lang/reflect tests away from TestNG
Reviewed-by: alanb
1 parent 64b0ae6 commit bbb4b0d

28 files changed

Lines changed: 513 additions & 708 deletions

test/jdk/java/lang/reflect/AccessibleObject/CanAccessTest.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,11 +21,11 @@
2121
* questions.
2222
*/
2323

24-
/**
24+
/*
2525
* @test
2626
* @build CanAccessTest
2727
* @modules java.base/jdk.internal.misc:+open
28-
* @run testng/othervm CanAccessTest
28+
* @run junit/othervm CanAccessTest
2929
* @summary Test AccessibleObject::canAccess method
3030
*/
3131

@@ -34,31 +34,29 @@
3434
import java.security.SecureClassLoader;
3535

3636
import jdk.internal.misc.Unsafe;
37-
import org.testng.annotations.Test;
38-
import static org.testng.Assert.*;
37+
import static org.junit.jupiter.api.Assertions.*;
38+
import org.junit.jupiter.api.Test;
3939

40-
@Test
4140
public class CanAccessTest {
4241
private static Unsafe INSTANCE = Unsafe.getUnsafe();
4342

4443
/**
4544
* null object parameter for Constructor
4645
*/
46+
@Test
4747
public void testConstructor() throws Exception {
4848
Constructor<?> ctor = Unsafe.class.getDeclaredConstructor();
4949
assertFalse(ctor.canAccess(null));
5050
assertTrue(ctor.trySetAccessible());
5151

52-
try {
53-
// non-null object parameter
54-
ctor.canAccess(INSTANCE);
55-
assertTrue(false);
56-
} catch (IllegalArgumentException expected) {}
52+
// non-null object parameter
53+
assertThrows(IllegalArgumentException.class, () -> ctor.canAccess(INSTANCE));
5754
}
5855

5956
/**
6057
* Test protected constructors
6158
*/
59+
@Test
6260
public void testProtectedConstructor() throws Exception {
6361
TestLoader.testProtectedConstructorNonOpenedPackage();
6462

@@ -69,21 +67,20 @@ public void testProtectedConstructor() throws Exception {
6967
/**
7068
* null object parameter for static members
7169
*/
70+
@Test
7271
public void testStaticMember() throws Exception {
7372
Method m = Unsafe.class.getDeclaredMethod("throwIllegalAccessError");
7473
assertFalse(m.canAccess(null));
7574
assertTrue(m.trySetAccessible());
7675

77-
try {
78-
// non-null object parameter
79-
m.canAccess(INSTANCE);
80-
assertTrue(false);
81-
} catch (IllegalArgumentException expected) { }
76+
// non-null object parameter
77+
assertThrows(IllegalArgumentException.class, () -> m.canAccess(INSTANCE));
8278
}
8379

8480
/**
8581
* Test protected static
8682
*/
83+
@Test
8784
public void testProtectedStatic() throws Exception {
8885
Method m = TestLoader.testProtectedStatic();
8986
assertFalse(m.canAccess(null));
@@ -93,28 +90,24 @@ public void testProtectedStatic() throws Exception {
9390
* the specified object must be an instance of the declaring class
9491
* for instance members
9592
*/
93+
@Test
9694
public void testInstanceMethod() throws Exception {
9795
Method m = Unsafe.class.getDeclaredMethod("allocateMemory0", long.class);
9896
assertFalse(m.canAccess(INSTANCE));
9997

100-
try {
101-
m.canAccess(null);
102-
assertTrue(false);
103-
} catch (IllegalArgumentException expected) { }
98+
assertThrows(IllegalArgumentException.class, () -> m.canAccess(null));
10499
}
105100

106101
/**
107102
* the specified object must be an instance of the declaring class
108103
* for instance members
109104
*/
105+
@Test
110106
public void testInvalidInstanceObject() throws Exception {
111107
Class<?> clazz = Class.forName("sun.security.x509.X500Name");
112108
Method m = clazz.getDeclaredMethod("size");
113109

114-
try {
115-
m.canAccess(INSTANCE);
116-
assertTrue(false);
117-
} catch (IllegalArgumentException expected) { }
110+
assertThrows(IllegalArgumentException.class, () -> m.canAccess(INSTANCE));
118111
}
119112

120113

test/jdk/java/lang/reflect/AccessibleObject/ModuleSetAccessibleTest.java

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,12 +21,12 @@
2121
* questions.
2222
*/
2323

24-
/**
24+
/*
2525
* @test
2626
* @build ModuleSetAccessibleTest
2727
* @modules java.base/java.lang:open
2828
* java.base/jdk.internal.misc:+open
29-
* @run testng/othervm ModuleSetAccessibleTest
29+
* @run junit/othervm ModuleSetAccessibleTest
3030
* @summary Test java.lang.reflect.AccessibleObject with modules
3131
*/
3232

@@ -40,22 +40,19 @@
4040

4141
import jdk.internal.misc.Unsafe;
4242

43-
import org.testng.annotations.Test;
44-
import static org.testng.Assert.*;
43+
import static org.junit.jupiter.api.Assertions.*;
44+
import org.junit.jupiter.api.Test;
4545

46-
@Test
4746
public class ModuleSetAccessibleTest {
4847

4948
/**
5049
* Invoke a private constructor on a public class in an exported package
5150
*/
51+
@Test
5252
public void testPrivateConstructorInExportedPackage() throws Exception {
5353
Constructor<?> ctor = Unsafe.class.getDeclaredConstructor();
5454

55-
try {
56-
ctor.newInstance();
57-
assertTrue(false);
58-
} catch (IllegalAccessException expected) { }
55+
assertThrows(IllegalAccessException.class, () -> ctor.newInstance());
5956

6057
ctor.setAccessible(true);
6158
Unsafe unsafe = (Unsafe) ctor.newInstance();
@@ -65,34 +62,26 @@ public void testPrivateConstructorInExportedPackage() throws Exception {
6562
/**
6663
* Invoke a private method on a public class in an exported package
6764
*/
65+
@Test
6866
public void testPrivateMethodInExportedPackage() throws Exception {
6967
Method m = Unsafe.class.getDeclaredMethod("throwIllegalAccessError");
70-
try {
71-
m.invoke(null);
72-
assertTrue(false);
73-
} catch (IllegalAccessException expected) { }
68+
assertThrows(IllegalAccessException.class, () -> m.invoke(null));
7469

7570
m.setAccessible(true);
76-
try {
77-
m.invoke(null);
78-
assertTrue(false);
79-
} catch (InvocationTargetException e) {
80-
// thrown by throwIllegalAccessError
81-
assertTrue(e.getCause() instanceof IllegalAccessError);
82-
}
71+
InvocationTargetException e = assertThrows(InvocationTargetException.class, () ->
72+
m.invoke(null));
73+
// thrown by throwIllegalAccessError
74+
assertInstanceOf(IllegalAccessError.class, e.getCause());
8375
}
8476

8577

8678
/**
8779
* Access a private field in a public class that is an exported package
8880
*/
81+
@Test
8982
public void testPrivateFieldInExportedPackage() throws Exception {
9083
Field f = Unsafe.class.getDeclaredField("theUnsafe");
91-
92-
try {
93-
f.get(null);
94-
assertTrue(false);
95-
} catch (IllegalAccessException expected) { }
84+
assertThrows(IllegalAccessException.class, () -> f.get(null));
9685

9786
f.setAccessible(true);
9887
Unsafe unsafe = (Unsafe) f.get(null);
@@ -102,19 +91,14 @@ public void testPrivateFieldInExportedPackage() throws Exception {
10291
/**
10392
* Invoke a public constructor on a public class in a non-exported package
10493
*/
94+
@Test
10595
public void testPublicConstructorInNonExportedPackage() throws Exception {
10696
Class<?> clazz = Class.forName("sun.security.x509.X500Name");
10797
Constructor<?> ctor = clazz.getConstructor(String.class);
10898

109-
try {
110-
ctor.newInstance("cn=duke");
111-
assertTrue(false);
112-
} catch (IllegalAccessException expected) { }
99+
assertThrows(IllegalAccessException.class, () -> ctor.newInstance("cn=duke"));
113100

114-
try {
115-
ctor.setAccessible(true);
116-
assertTrue(false);
117-
} catch (InaccessibleObjectException expected) { }
101+
assertThrows(InaccessibleObjectException.class, () -> ctor.setAccessible(true));
118102

119103
ctor.setAccessible(false); // should succeed
120104
}
@@ -123,19 +107,14 @@ public void testPublicConstructorInNonExportedPackage() throws Exception {
123107
/**
124108
* Access a public field in a public class that in a non-exported package
125109
*/
110+
@Test
126111
public void testPublicFieldInNonExportedPackage() throws Exception {
127112
Class<?> clazz = Class.forName("sun.security.x509.X500Name");
128113
Field f = clazz.getField("SERIALNUMBER_OID");
129114

130-
try {
131-
f.get(null);
132-
assertTrue(false);
133-
} catch (IllegalAccessException expected) { }
115+
assertThrows(IllegalAccessException.class, () -> f.get(null));
134116

135-
try {
136-
f.setAccessible(true);
137-
assertTrue(false);
138-
} catch (InaccessibleObjectException expected) { }
117+
assertThrows(InaccessibleObjectException.class, () -> f.setAccessible(true));
139118

140119
f.setAccessible(false); // should succeed
141120
}
@@ -144,6 +123,7 @@ public void testPublicFieldInNonExportedPackage() throws Exception {
144123
/**
145124
* Test that the Class constructor cannot be make accessible.
146125
*/
126+
@Test
147127
public void testJavaLangClass() throws Exception {
148128

149129
// non-public constructor
@@ -152,15 +132,8 @@ public void testJavaLangClass() throws Exception {
152132
ProtectionDomain.class, boolean.class, char.class);
153133
AccessibleObject[] ctors = { ctor };
154134

155-
try {
156-
ctor.setAccessible(true);
157-
assertTrue(false);
158-
} catch (SecurityException expected) { }
159-
160-
try {
161-
AccessibleObject.setAccessible(ctors, true);
162-
assertTrue(false);
163-
} catch (SecurityException expected) { }
135+
assertThrows(SecurityException.class, () -> ctor.setAccessible(true));
136+
assertThrows(SecurityException.class, () -> AccessibleObject.setAccessible(ctors, true));
164137

165138
// should succeed
166139
ctor.setAccessible(false);

0 commit comments

Comments
 (0)