2525 */
2626package com .oracle .svm .test .jmx ;
2727
28+ import static org .junit .Assert .assertEquals ;
29+ import static org .junit .Assert .assertFalse ;
30+ import static org .junit .Assert .assertNotNull ;
2831import static org .junit .Assert .assertTrue ;
2932import static org .junit .Assume .assumeTrue ;
3033
31- import java .io .File ;
3234import java .io .IOException ;
3335import java .lang .management .ClassLoadingMXBean ;
3436import java .lang .management .GarbageCollectorMXBean ;
4143import java .lang .management .RuntimeMXBean ;
4244import java .lang .management .ThreadMXBean ;
4345import java .nio .file .Files ;
46+ import java .nio .file .Path ;
4447import java .nio .file .attribute .PosixFilePermission ;
45-
4648import java .util .HashMap ;
47- import java .util .HashSet ;
49+ import java .util .List ;
4850import java .util .Map ;
4951import java .util .Set ;
50- import java .util .List ;
5152
5253import javax .management .MBeanServer ;
5354import javax .management .MBeanServerConnection ;
5657import javax .management .remote .JMXConnector ;
5758import javax .management .remote .JMXConnectorFactory ;
5859import javax .management .remote .JMXServiceURL ;
59-
60- import org .graalvm .nativeimage .ImageInfo ;
61-
6260import javax .rmi .ssl .SslRMIClientSocketFactory ;
6361
62+ import org .graalvm .nativeimage .ImageInfo ;
6463import org .junit .Assert ;
6564import org .junit .BeforeClass ;
6665import org .junit .Test ;
@@ -88,7 +87,6 @@ public class JmxTest {
8887 static final String SOCKET_FACTORY_PROPERTY = "com.sun.jndi.rmi.factory.socket" ;
8988 static final String TEST_PORT = "12345" ;
9089 static final String TRUE = "true" ;
91- static final String JMX_REMOTE_RESOURCES = "substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx/jmxremoteresources" ;
9290
9391 @ BeforeClass
9492 public static void setup () throws IOException {
@@ -102,25 +100,33 @@ public static void setup() throws IOException {
102100 System .setProperty (SSL_PROPERTY , TRUE );
103101 System .setProperty (REGISTRY_SSL_PROPERTY , TRUE );
104102
103+ // Copy resources into tempDirectory
104+ Path tempDirectory = Files .createTempDirectory ("jmxtest" );
105+ Path jmxRemoteAccess = tempDirectory .resolve ("jmxremote.access" );
106+ Path jmxRemotePassword = tempDirectory .resolve ("jmxremote.password" );
107+ Path clientkeystore = tempDirectory .resolve ("clientkeystore" );
108+ Path servertruststore = tempDirectory .resolve ("servertruststore" );
109+ // Note: full paths are used to ensure analysis includes the resources automatically
110+ Files .copy (JmxTest .class .getResourceAsStream ("/resources/jmxremote/jmxremote.access" ), jmxRemoteAccess );
111+ Files .copy (JmxTest .class .getResourceAsStream ("/resources/jmxremote/jmxremote.password" ), jmxRemotePassword );
112+ Files .copy (JmxTest .class .getResourceAsStream ("/resources/jmxremote/clientkeystore" ), clientkeystore );
113+ Files .copy (JmxTest .class .getResourceAsStream ("/resources/jmxremote/servertruststore" ), servertruststore );
114+
105115 // The following are dummy password and access files required for testing authentication.
106- System .setProperty (ACCESS_PROPERTY , JMX_REMOTE_RESOURCES + "/jmxremote.access" );
107- System .setProperty (PASSWORD_PROPERTY , JMX_REMOTE_RESOURCES + "/jmxremote.password" );
116+ System .setProperty (ACCESS_PROPERTY , jmxRemoteAccess . toString () );
117+ System .setProperty (PASSWORD_PROPERTY , jmxRemotePassword . toString () );
108118
109119 /*
110120 * The following are dummy SSL keystore and truststore files required for testing connection
111- * using SSL. See jmxremoteresources /README.md for more information.
121+ * using SSL. See resources/jmxremote /README.md for more information.
112122 */
113- System .setProperty (KEYSTORE_PROPERTY , JMX_REMOTE_RESOURCES + "/ clientkeystore" );
123+ System .setProperty (KEYSTORE_PROPERTY , clientkeystore . toString () );
114124 System .setProperty (KEYSTORE_PASSWORD_PROPERTY , "clientpass" );
115- System .setProperty (TRUSTSTORE_PROPERTY , JMX_REMOTE_RESOURCES + "/ servertruststore" );
125+ System .setProperty (TRUSTSTORE_PROPERTY , servertruststore . toString () );
116126 System .setProperty (TRUSTSTORE_PASSWORD_PROPERTY , "servertrustpass" );
117127
118128 // Password file must have restricted access.
119- File file = new File (JMX_REMOTE_RESOURCES + "/jmxremote.password" );
120- Set <PosixFilePermission > perms = new HashSet <>();
121- perms .add (PosixFilePermission .OWNER_READ );
122- perms .add (PosixFilePermission .OWNER_WRITE );
123- Files .setPosixFilePermissions (file .toPath (), perms );
129+ Files .setPosixFilePermissions (jmxRemotePassword , Set .of (PosixFilePermission .OWNER_READ , PosixFilePermission .OWNER_WRITE ));
124130
125131 try {
126132 // We need to rerun the startup hook with the correct properties set.
@@ -151,8 +157,8 @@ private static MBeanServerConnection getLocalMBeanServerConnectionStatic() {
151157 public void testConnection () throws Exception {
152158 // This simply tests that we can establish a connection between client and server
153159 MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic ();
154- assertTrue ("Connection should not be null" , mbsc != null );
155- assertTrue ("Connection default domain should not be empty" , ! mbsc .getDefaultDomain ().isEmpty ());
160+ assertNotNull ("Connection should not be null" , mbsc );
161+ assertFalse ("Connection default domain should not be empty" , mbsc .getDefaultDomain ().isEmpty ());
156162 }
157163
158164 @ Test
@@ -185,7 +191,7 @@ public void testRuntimeMXBeanProxy() {
185191 }
186192
187193 assertTrue ("PID should be positive." , runtimeMXBean .getPid () > 0 );
188- assertTrue ("Class Path should not be null: " , runtimeMXBean .getClassPath () != null );
194+ assertNotNull ("Class Path should not be null: " , runtimeMXBean .getClassPath ());
189195 assertTrue ("Start time should be positive" , runtimeMXBean .getStartTime () > 0 );
190196 }
191197
@@ -196,7 +202,7 @@ public void testRuntimeMXBeanDirect() throws MalformedObjectNameException {
196202 ObjectName objectName = new ObjectName ("java.lang:type=Runtime" );
197203 try {
198204 assertTrue ("Uptime should be positive. " , (long ) mbsc .getAttribute (objectName , "Pid" ) > 0 );
199- assertTrue ("Class Path should not be null: " , mbsc .getAttribute (objectName , "ClassPath" ) != null );
205+ assertNotNull ("Class Path should not be null: " , mbsc .getAttribute (objectName , "ClassPath" ));
200206 assertTrue ("Start time should be positive" , (long ) mbsc .getAttribute (objectName , "StartTime" ) > 0 );
201207 } catch (Exception e ) {
202208 Assert .fail ("Remote invocations failed : " + e .getMessage ());
@@ -215,7 +221,7 @@ public void testClassLoadingMXBeanProxy() {
215221 Assert .fail ("Failed to get ClassLoadingMXBean. : " + e .getMessage ());
216222 }
217223 if (ImageInfo .inImageRuntimeCode ()) {
218- assertTrue ("Loaded Class count should be 0 (hardcoded at 0): " , classLoadingMXBean .getLoadedClassCount () == 0 );
224+ assertEquals ("Loaded Class count should be 0 (hardcoded at 0): " , 0 , classLoadingMXBean .getLoadedClassCount ());
219225 } else {
220226 assertTrue ("If in java mode, number of loaded classes should be positive: " , classLoadingMXBean .getLoadedClassCount () > 0 );
221227 }
@@ -293,7 +299,7 @@ public void testGarbageCollectorMXBeanProxy() {
293299 Assert .fail ("Failed to get GarbageCollectorMXBean. : " + e .getMessage ());
294300 }
295301 for (GarbageCollectorMXBean gcBean : garbageCollectorMXBeans ) {
296- assertTrue ("GC object name should not be null" , gcBean .getObjectName () != null );
302+ assertNotNull ("GC object name should not be null" , gcBean .getObjectName ());
297303 assertTrue ("Number of GC should not be negative" , gcBean .getCollectionCount () >= 0 );
298304 }
299305 }
@@ -310,7 +316,7 @@ public void testOperatingSystemMXBeanProxy() {
310316 } catch (Exception e ) {
311317 Assert .fail ("Failed to get OperatingSystemMXBean. : " + e .getMessage ());
312318 }
313- assertTrue ("OS version can't be null. " , operatingSystemMXBean .getVersion () != null );
319+ assertNotNull ("OS version can't be null. " , operatingSystemMXBean .getVersion ());
314320 }
315321
316322 @ Test
@@ -319,7 +325,7 @@ public void testOperatingSystemMXBeanDirect() throws MalformedObjectNameExceptio
319325 MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic ();
320326 ObjectName objectName = new ObjectName ("java.lang:type=OperatingSystem" );
321327 try {
322- assertTrue ("OS version can't be null. " , mbsc .getAttribute (objectName , "Version" ) != null );
328+ assertNotNull ("OS version can't be null. " , mbsc .getAttribute (objectName , "Version" ));
323329 } catch (Exception e ) {
324330 Assert .fail ("Remote invokations failed : " + e .getMessage ());
325331 }
@@ -337,7 +343,7 @@ public void testMemoryManagerMXBeanProxy() {
337343 Assert .fail ("Failed to get MemoryManagerMXBean. : " + e .getMessage ());
338344 }
339345 for (MemoryManagerMXBean memoryManagerMXBean : memoryManagerMXBeans ) {
340- assertTrue ("Memory pool names should not be null. " , memoryManagerMXBean .getMemoryPoolNames () != null );
346+ assertNotNull ("Memory pool names should not be null. " , memoryManagerMXBean .getMemoryPoolNames ());
341347 }
342348 }
343349
@@ -354,7 +360,7 @@ public void testMemoryPoolMXBeanProxy() {
354360 Assert .fail ("Failed to get MemoryPoolMXBean. : " + e .getMessage ());
355361 }
356362 for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans ) {
357- assertTrue ("Memory Pool name should not be null " , memoryPoolMXBean .getName () != null );
363+ assertNotNull ("Memory Pool name should not be null " , memoryPoolMXBean .getName ());
358364 }
359365 }
360366
@@ -371,7 +377,7 @@ public void testFlightRecorderMXBeanProxy() {
371377 Assert .fail ("Failed to get FlightRecorderMXBean. : " + e .getMessage ());
372378 }
373379 flightRecorderMXBean .newRecording ();
374- assertTrue ("Flight recordings should be available because we just created one." , ! flightRecorderMXBean .getRecordings ().isEmpty ());
380+ assertFalse ("Flight recordings should be available because we just created one." , flightRecorderMXBean .getRecordings ().isEmpty ());
375381 }
376382
377383 @ Test
@@ -383,7 +389,7 @@ public void testFlightRecorderMXBeanDirect() throws MalformedObjectNameException
383389 mbsc .invoke (objectName , "startRecording" , new Object []{recording }, new String []{"long" });
384390 mbsc .invoke (objectName , "stopRecording" , new Object []{recording }, new String []{"long" });
385391 } catch (Exception e ) {
386- Assert .fail ("Remote invokations failed : " + e .getMessage ());
392+ Assert .fail ("Remote invocations failed : " + e .getMessage ());
387393 }
388394 }
389395}
0 commit comments