25
25
*/
26
26
package com .oracle .svm .test .jmx ;
27
27
28
+ import static org .junit .Assert .assertEquals ;
29
+ import static org .junit .Assert .assertFalse ;
30
+ import static org .junit .Assert .assertNotNull ;
28
31
import static org .junit .Assert .assertTrue ;
29
32
import static org .junit .Assume .assumeTrue ;
30
33
31
- import java .io .File ;
32
34
import java .io .IOException ;
33
35
import java .lang .management .ClassLoadingMXBean ;
34
36
import java .lang .management .GarbageCollectorMXBean ;
41
43
import java .lang .management .RuntimeMXBean ;
42
44
import java .lang .management .ThreadMXBean ;
43
45
import java .nio .file .Files ;
46
+ import java .nio .file .Path ;
44
47
import java .nio .file .attribute .PosixFilePermission ;
45
-
46
48
import java .util .HashMap ;
47
- import java .util .HashSet ;
49
+ import java .util .List ;
48
50
import java .util .Map ;
49
51
import java .util .Set ;
50
- import java .util .List ;
51
52
52
53
import javax .management .MBeanServer ;
53
54
import javax .management .MBeanServerConnection ;
56
57
import javax .management .remote .JMXConnector ;
57
58
import javax .management .remote .JMXConnectorFactory ;
58
59
import javax .management .remote .JMXServiceURL ;
59
-
60
- import org .graalvm .nativeimage .ImageInfo ;
61
-
62
60
import javax .rmi .ssl .SslRMIClientSocketFactory ;
63
61
62
+ import org .graalvm .nativeimage .ImageInfo ;
64
63
import org .junit .Assert ;
65
64
import org .junit .BeforeClass ;
66
65
import org .junit .Test ;
@@ -88,7 +87,6 @@ public class JmxTest {
88
87
static final String SOCKET_FACTORY_PROPERTY = "com.sun.jndi.rmi.factory.socket" ;
89
88
static final String TEST_PORT = "12345" ;
90
89
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" ;
92
90
93
91
@ BeforeClass
94
92
public static void setup () throws IOException {
@@ -102,25 +100,33 @@ public static void setup() throws IOException {
102
100
System .setProperty (SSL_PROPERTY , TRUE );
103
101
System .setProperty (REGISTRY_SSL_PROPERTY , TRUE );
104
102
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
+
105
115
// 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 () );
108
118
109
119
/*
110
120
* 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.
112
122
*/
113
- System .setProperty (KEYSTORE_PROPERTY , JMX_REMOTE_RESOURCES + "/ clientkeystore" );
123
+ System .setProperty (KEYSTORE_PROPERTY , clientkeystore . toString () );
114
124
System .setProperty (KEYSTORE_PASSWORD_PROPERTY , "clientpass" );
115
- System .setProperty (TRUSTSTORE_PROPERTY , JMX_REMOTE_RESOURCES + "/ servertruststore" );
125
+ System .setProperty (TRUSTSTORE_PROPERTY , servertruststore . toString () );
116
126
System .setProperty (TRUSTSTORE_PASSWORD_PROPERTY , "servertrustpass" );
117
127
118
128
// 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 ));
124
130
125
131
try {
126
132
// We need to rerun the startup hook with the correct properties set.
@@ -151,8 +157,8 @@ private static MBeanServerConnection getLocalMBeanServerConnectionStatic() {
151
157
public void testConnection () throws Exception {
152
158
// This simply tests that we can establish a connection between client and server
153
159
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 ());
156
162
}
157
163
158
164
@ Test
@@ -185,7 +191,7 @@ public void testRuntimeMXBeanProxy() {
185
191
}
186
192
187
193
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 ());
189
195
assertTrue ("Start time should be positive" , runtimeMXBean .getStartTime () > 0 );
190
196
}
191
197
@@ -196,7 +202,7 @@ public void testRuntimeMXBeanDirect() throws MalformedObjectNameException {
196
202
ObjectName objectName = new ObjectName ("java.lang:type=Runtime" );
197
203
try {
198
204
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" ));
200
206
assertTrue ("Start time should be positive" , (long ) mbsc .getAttribute (objectName , "StartTime" ) > 0 );
201
207
} catch (Exception e ) {
202
208
Assert .fail ("Remote invocations failed : " + e .getMessage ());
@@ -215,7 +221,7 @@ public void testClassLoadingMXBeanProxy() {
215
221
Assert .fail ("Failed to get ClassLoadingMXBean. : " + e .getMessage ());
216
222
}
217
223
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 ());
219
225
} else {
220
226
assertTrue ("If in java mode, number of loaded classes should be positive: " , classLoadingMXBean .getLoadedClassCount () > 0 );
221
227
}
@@ -293,7 +299,7 @@ public void testGarbageCollectorMXBeanProxy() {
293
299
Assert .fail ("Failed to get GarbageCollectorMXBean. : " + e .getMessage ());
294
300
}
295
301
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 ());
297
303
assertTrue ("Number of GC should not be negative" , gcBean .getCollectionCount () >= 0 );
298
304
}
299
305
}
@@ -310,7 +316,7 @@ public void testOperatingSystemMXBeanProxy() {
310
316
} catch (Exception e ) {
311
317
Assert .fail ("Failed to get OperatingSystemMXBean. : " + e .getMessage ());
312
318
}
313
- assertTrue ("OS version can't be null. " , operatingSystemMXBean .getVersion () != null );
319
+ assertNotNull ("OS version can't be null. " , operatingSystemMXBean .getVersion ());
314
320
}
315
321
316
322
@ Test
@@ -319,7 +325,7 @@ public void testOperatingSystemMXBeanDirect() throws MalformedObjectNameExceptio
319
325
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic ();
320
326
ObjectName objectName = new ObjectName ("java.lang:type=OperatingSystem" );
321
327
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" ));
323
329
} catch (Exception e ) {
324
330
Assert .fail ("Remote invokations failed : " + e .getMessage ());
325
331
}
@@ -337,7 +343,7 @@ public void testMemoryManagerMXBeanProxy() {
337
343
Assert .fail ("Failed to get MemoryManagerMXBean. : " + e .getMessage ());
338
344
}
339
345
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 ());
341
347
}
342
348
}
343
349
@@ -354,7 +360,7 @@ public void testMemoryPoolMXBeanProxy() {
354
360
Assert .fail ("Failed to get MemoryPoolMXBean. : " + e .getMessage ());
355
361
}
356
362
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 ());
358
364
}
359
365
}
360
366
@@ -371,7 +377,7 @@ public void testFlightRecorderMXBeanProxy() {
371
377
Assert .fail ("Failed to get FlightRecorderMXBean. : " + e .getMessage ());
372
378
}
373
379
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 ());
375
381
}
376
382
377
383
@ Test
@@ -383,7 +389,7 @@ public void testFlightRecorderMXBeanDirect() throws MalformedObjectNameException
383
389
mbsc .invoke (objectName , "startRecording" , new Object []{recording }, new String []{"long" });
384
390
mbsc .invoke (objectName , "stopRecording" , new Object []{recording }, new String []{"long" });
385
391
} catch (Exception e ) {
386
- Assert .fail ("Remote invokations failed : " + e .getMessage ());
392
+ Assert .fail ("Remote invocations failed : " + e .getMessage ());
387
393
}
388
394
}
389
395
}
0 commit comments