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
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
4040
4141import 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
4746public 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