Skip to content

Commit 8395795

Browse files
committed
Simplify JmxTest.
- Avoid redundant exception handling - Drop `final` from locals
1 parent ad436e9 commit 8395795

File tree

1 file changed

+56
-153
lines changed
  • substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx

1 file changed

+56
-153
lines changed

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx/JmxTest.java

Lines changed: 56 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454

5555
import javax.management.MBeanServer;
5656
import javax.management.MBeanServerConnection;
57-
import javax.management.MalformedObjectNameException;
5857
import javax.management.ObjectName;
5958
import javax.management.remote.JMXConnector;
6059
import javax.management.remote.JMXConnectorFactory;
@@ -63,7 +62,6 @@
6362

6463
import org.graalvm.nativeimage.ImageInfo;
6564
import org.junit.AfterClass;
66-
import org.junit.Assert;
6765
import org.junit.BeforeClass;
6866
import org.junit.Test;
6967

@@ -101,7 +99,7 @@ public class JmxTest {
10199
private static Path tempDirectory;
102100

103101
@BeforeClass
104-
public static void setup() throws IOException {
102+
public static void setup() throws Exception {
105103
assumeTrue("skipping JMX tests", !ImageInfo.inImageCode() ||
106104
(VMInspectionOptions.hasJmxClientSupport() && VMInspectionOptions.hasJmxServerSupport()));
107105

@@ -139,13 +137,9 @@ public static void setup() throws IOException {
139137
// Password file must have restricted access.
140138
Files.setPosixFilePermissions(jmxRemotePassword, Set.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE));
141139

142-
try {
143-
// We need to rerun the startup hook with the correct properties set.
144-
ManagementAgentStartupHook startupHook = new ManagementAgentStartupHook();
145-
startupHook.execute(false);
146-
} catch (Exception e) {
147-
Assert.fail("Failed to start server. Cause: " + e.getMessage());
148-
}
140+
// We need to rerun the startup hook with the correct properties set.
141+
ManagementAgentStartupHook startupHook = new ManagementAgentStartupHook();
142+
startupHook.execute(false);
149143
}
150144

151145
@AfterClass
@@ -166,7 +160,7 @@ private static void delete(Path file) throws IOException {
166160
Files.deleteIfExists(file);
167161
}
168162

169-
private static void createClientKey() throws IOException {
163+
private static void createClientKey() throws Exception {
170164
runCommand(List.of("keytool", "-genkey",
171165
"-keystore", KEYSTORE_FILENAME,
172166
"-alias", "clientkey",
@@ -177,53 +171,44 @@ private static void createClientKey() throws IOException {
177171
"-keyalg", "rsa"));
178172
}
179173

180-
private static void createClientCert() throws IOException {
174+
private static void createClientCert() throws Exception {
181175
runCommand(List.of("keytool", "-exportcert",
182176
"-keystore", KEYSTORE_FILENAME,
183177
"-alias", "clientkey",
184178
"-storepass", KEYSTORE_PASSWORD,
185179
"-file", "client.cer"));
186180
}
187181

188-
private static void createServerTrustStore() throws IOException {
182+
private static void createServerTrustStore() throws Exception {
189183
runCommand(List.of("keytool", "-importcert",
190184
"-noprompt",
191185
"-file", "client.cer",
192186
"-keystore", TRUSTSTORE_FILENAME,
193187
"-storepass", TRUSTSTORE_PASSWORD));
194188
}
195189

196-
private static void runCommand(List<String> command) throws IOException {
190+
private static void runCommand(List<String> command) throws Exception {
197191
ProcessBuilder pb = new ProcessBuilder().command(command);
198192
pb.directory(tempDirectory.toFile());
199-
final Process process = pb.start();
200-
try {
201-
process.waitFor(5, TimeUnit.SECONDS);
202-
} catch (InterruptedException e) {
203-
throw new IOException("Keytool execution error");
204-
}
193+
Process process = pb.start();
194+
process.waitFor(5, TimeUnit.SECONDS);
205195
if (process.exitValue() > 0) {
206-
final String processError = String.join(" \\ ", FileUtils.readAllLines(process.getErrorStream()));
207-
final String processOutput = String.join(" \\ ", FileUtils.readAllLines(process.getInputStream()));
196+
String processError = String.join(" \\ ", FileUtils.readAllLines(process.getErrorStream()));
197+
String processOutput = String.join(" \\ ", FileUtils.readAllLines(process.getInputStream()));
208198
throw new IOException(
209199
"Keytool execution error: " + processError + ", output: " + processOutput + ", command: " + command);
210200
}
211201
}
212202

213-
private static MBeanServerConnection getLocalMBeanServerConnectionStatic() {
214-
try {
215-
JMXServiceURL jmxUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + "localhost" + ":" + TEST_PORT + "/jmxrmi");
216-
Map<String, Object> env = new HashMap<>();
217-
String[] credentials = {TEST_ROLE, TEST_ROLE_PASSWORD};
218-
env.put(JMXConnector.CREDENTIALS, credentials);
219-
// Include below if protecting registry with SSL
220-
env.put(SOCKET_FACTORY_PROPERTY, new SslRMIClientSocketFactory());
221-
JMXConnector connector = JMXConnectorFactory.connect(jmxUrl, env);
222-
return connector.getMBeanServerConnection();
223-
} catch (IOException e) {
224-
Assert.fail("Failed to establish connection Cause: " + e.getMessage());
225-
}
226-
return null;
203+
private static MBeanServerConnection getLocalMBeanServerConnectionStatic() throws IOException {
204+
JMXServiceURL jmxUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + "localhost" + ":" + TEST_PORT + "/jmxrmi");
205+
Map<String, Object> env = new HashMap<>();
206+
String[] credentials = {TEST_ROLE, TEST_ROLE_PASSWORD};
207+
env.put(JMXConnector.CREDENTIALS, credentials);
208+
// Include below if protecting registry with SSL
209+
env.put(SOCKET_FACTORY_PROPERTY, new SslRMIClientSocketFactory());
210+
JMXConnector connector = JMXConnectorFactory.connect(jmxUrl, env);
211+
return connector.getMBeanServerConnection();
227212
}
228213

229214
@Test
@@ -240,59 +225,38 @@ public void testRegistration() throws Exception {
240225
// from the client via the connection
241226
ObjectName objectName = new ObjectName("com.jmx.test.basic:type=basic,name=simple");
242227
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
243-
try {
244-
server.getDefaultDomain();
245-
server.registerMBean(new Simple(), objectName);
246-
247-
} catch (Exception e) {
248-
Assert.fail("Failed to register bean. Cause: " + e.getMessage());
249-
}
228+
server.getDefaultDomain();
229+
server.registerMBean(new Simple(), objectName);
250230
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
251231
assertTrue("Expected bean is not registered.", mbsc.isRegistered(objectName));
252232

253233
}
254234

255235
@Test
256-
public void testRuntimeMXBeanProxy() {
236+
public void testRuntimeMXBeanProxy() throws IOException {
257237
// This test checks to make sure we are able to get the MXBean and do simple things with it.
258238
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
259-
RuntimeMXBean runtimeMXBean = null;
260-
try {
261-
runtimeMXBean = ManagementFactory.getPlatformMXBean(mbsc, RuntimeMXBean.class);
262-
} catch (IOException e) {
263-
Assert.fail("Failed to get RuntimeMXBean. : " + e.getMessage());
264-
}
265-
239+
RuntimeMXBean runtimeMXBean = ManagementFactory.getPlatformMXBean(mbsc, RuntimeMXBean.class);
266240
assertTrue("PID should be positive.", runtimeMXBean.getPid() > 0);
267241
assertNotNull("Class Path should not be null: ", runtimeMXBean.getClassPath());
268242
assertTrue("Start time should be positive", runtimeMXBean.getStartTime() > 0);
269243
}
270244

271245
@Test
272-
public void testRuntimeMXBeanDirect() throws MalformedObjectNameException {
246+
public void testRuntimeMXBeanDirect() throws Exception {
273247
// Basic test to make sure reflective accesses are set up correctly.
274248
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
275249
ObjectName objectName = new ObjectName("java.lang:type=Runtime");
276-
try {
277-
assertTrue("Uptime should be positive. ", (long) mbsc.getAttribute(objectName, "Pid") > 0);
278-
assertNotNull("Class Path should not be null: ", mbsc.getAttribute(objectName, "ClassPath"));
279-
assertTrue("Start time should be positive", (long) mbsc.getAttribute(objectName, "StartTime") > 0);
280-
} catch (Exception e) {
281-
Assert.fail("Remote invocations failed : " + e.getMessage());
282-
}
250+
assertTrue("Uptime should be positive. ", (long) mbsc.getAttribute(objectName, "Pid") > 0);
251+
assertNotNull("Class Path should not be null: ", mbsc.getAttribute(objectName, "ClassPath"));
252+
assertTrue("Start time should be positive", (long) mbsc.getAttribute(objectName, "StartTime") > 0);
283253
}
284254

285255
@Test
286-
public void testClassLoadingMXBeanProxy() {
256+
public void testClassLoadingMXBeanProxy() throws IOException {
287257
// This test checks to make sure we are able to get the MXBean and do simple things with it.
288258
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
289-
290-
ClassLoadingMXBean classLoadingMXBean = null;
291-
try {
292-
classLoadingMXBean = ManagementFactory.getPlatformMXBean(mbsc, ClassLoadingMXBean.class);
293-
} catch (IOException e) {
294-
Assert.fail("Failed to get ClassLoadingMXBean. : " + e.getMessage());
295-
}
259+
ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getPlatformMXBean(mbsc, ClassLoadingMXBean.class);
296260
if (ImageInfo.inImageRuntimeCode()) {
297261
assertEquals("Loaded Class count should be 0 (hardcoded at 0): ", 0, classLoadingMXBean.getLoadedClassCount());
298262
} else {
@@ -301,15 +265,10 @@ public void testClassLoadingMXBeanProxy() {
301265
}
302266

303267
@Test
304-
public void testThreadMXBeanProxy() {
268+
public void testThreadMXBeanProxy() throws IOException {
305269
// This test checks to make sure we are able to get the MXBean and do simple things with it.
306270
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
307-
ThreadMXBean threadMXBean = null;
308-
try {
309-
threadMXBean = ManagementFactory.getPlatformMXBean(mbsc, ThreadMXBean.class);
310-
} catch (IOException e) {
311-
Assert.fail("Failed to get ThreadMXBean. : " + e.getMessage());
312-
}
271+
ThreadMXBean threadMXBean = ManagementFactory.getPlatformMXBean(mbsc, ThreadMXBean.class);
313272
int count = threadMXBean.getPeakThreadCount();
314273
assertTrue("Peak thread count should be positive ", count > 0);
315274
threadMXBean.resetPeakThreadCount();
@@ -320,149 +279,93 @@ public void testThreadMXBeanProxy() {
320279
}
321280

322281
@Test
323-
public void testThreadMXBeanDirect() throws MalformedObjectNameException {
282+
public void testThreadMXBeanDirect() throws Exception {
324283
// Basic test to make sure reflective accesses are set up correctly.
325284
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
326285
ObjectName objectName = new ObjectName("java.lang:type=Threading");
327-
try {
328-
mbsc.invoke(objectName, "resetPeakThreadCount", null, null);
329-
assertTrue("Peak thread count should be positive ", (int) mbsc.getAttribute(objectName, "PeakThreadCount") > 0);
330-
} catch (Exception e) {
331-
Assert.fail("Remote invocations failed : " + e.getMessage());
332-
}
286+
mbsc.invoke(objectName, "resetPeakThreadCount", null, null);
287+
assertTrue("Peak thread count should be positive ", (int) mbsc.getAttribute(objectName, "PeakThreadCount") > 0);
333288
}
334289

335290
@Test
336-
public void testMemoryMXBeanProxy() {
291+
public void testMemoryMXBeanProxy() throws IOException {
337292
// This test checks to make sure we are able to get the MXBean and do simple things with it.
338293
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
339-
MemoryMXBean memoryMXBean = null;
340-
try {
341-
memoryMXBean = ManagementFactory.getPlatformMXBean(mbsc, MemoryMXBean.class);
342-
343-
} catch (Exception e) {
344-
Assert.fail("Failed to get MemoryMXBean. : " + e.getMessage());
345-
}
294+
MemoryMXBean memoryMXBean = ManagementFactory.getPlatformMXBean(mbsc, MemoryMXBean.class);
346295
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
347296
assertTrue("Memory usage should be positive: ", memoryUsage.getUsed() > 0);
348-
349297
}
350298

351299
@Test
352-
public void testMemoryMXBeanDirect() throws MalformedObjectNameException {
300+
public void testMemoryMXBeanDirect() throws Exception {
353301
// Basic test to make sure reflective accesses are set up correctly.
354302
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
355303
ObjectName objectName = new ObjectName("java.lang:type=Memory");
356-
try {
357-
mbsc.invoke(objectName, "gc", null, null);
358-
} catch (Exception e) {
359-
Assert.fail("Remote invocations failed : " + e.getMessage());
360-
}
304+
mbsc.invoke(objectName, "gc", null, null);
361305
}
362306

363307
@Test
364-
public void testGarbageCollectorMXBeanProxy() {
308+
public void testGarbageCollectorMXBeanProxy() throws IOException {
365309
// This test checks to make sure we are able to get the MXBean and do simple things with it.
366310
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
367-
List<GarbageCollectorMXBean> garbageCollectorMXBeans = null;
368-
try {
369-
garbageCollectorMXBeans = ManagementFactory.getPlatformMXBeans(mbsc, GarbageCollectorMXBean.class);
370-
371-
} catch (Exception e) {
372-
Assert.fail("Failed to get GarbageCollectorMXBean. : " + e.getMessage());
373-
}
311+
List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getPlatformMXBeans(mbsc, GarbageCollectorMXBean.class);
374312
for (GarbageCollectorMXBean gcBean : garbageCollectorMXBeans) {
375313
assertNotNull("GC object name should not be null", gcBean.getObjectName());
376314
assertTrue("Number of GC should not be negative", gcBean.getCollectionCount() >= 0);
377315
}
378316
}
379317

380318
@Test
381-
public void testOperatingSystemMXBeanProxy() {
319+
public void testOperatingSystemMXBeanProxy() throws IOException {
382320
// This test checks to make sure we are able to get the MXBean and do simple things with it.
383321
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
384-
385-
OperatingSystemMXBean operatingSystemMXBean = null;
386-
try {
387-
operatingSystemMXBean = ManagementFactory.getPlatformMXBean(mbsc, OperatingSystemMXBean.class);
388-
389-
} catch (Exception e) {
390-
Assert.fail("Failed to get OperatingSystemMXBean. : " + e.getMessage());
391-
}
322+
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getPlatformMXBean(mbsc, OperatingSystemMXBean.class);
392323
assertNotNull("OS version can't be null. ", operatingSystemMXBean.getVersion());
393324
}
394325

395326
@Test
396-
public void testOperatingSystemMXBeanDirect() throws MalformedObjectNameException {
327+
public void testOperatingSystemMXBeanDirect() throws Exception {
397328
// Basic test to make sure reflective accesses are set up correctly.
398329
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
399330
ObjectName objectName = new ObjectName("java.lang:type=OperatingSystem");
400-
try {
401-
assertNotNull("OS version can't be null. ", mbsc.getAttribute(objectName, "Version"));
402-
} catch (Exception e) {
403-
Assert.fail("Remote invokations failed : " + e.getMessage());
404-
}
331+
assertNotNull("OS version can't be null. ", mbsc.getAttribute(objectName, "Version"));
405332
}
406333

407334
@Test
408-
public void testMemoryManagerMXBeanProxy() {
335+
public void testMemoryManagerMXBeanProxy() throws IOException {
409336
// This test checks to make sure we are able to get the MXBean and do simple things with it.
410337
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
411-
List<MemoryManagerMXBean> memoryManagerMXBeans = null;
412-
try {
413-
memoryManagerMXBeans = ManagementFactory.getPlatformMXBeans(mbsc, MemoryManagerMXBean.class);
414-
415-
} catch (Exception e) {
416-
Assert.fail("Failed to get MemoryManagerMXBean. : " + e.getMessage());
417-
}
338+
List<MemoryManagerMXBean> memoryManagerMXBeans = ManagementFactory.getPlatformMXBeans(mbsc, MemoryManagerMXBean.class);
418339
for (MemoryManagerMXBean memoryManagerMXBean : memoryManagerMXBeans) {
419340
assertNotNull("Memory pool names should not be null. ", memoryManagerMXBean.getMemoryPoolNames());
420341
}
421342
}
422343

423344
@Test
424-
public void testMemoryPoolMXBeanProxy() {
345+
public void testMemoryPoolMXBeanProxy() throws IOException {
425346
// This test checks to make sure we are able to get the MXBean and do simple things with it.
426347
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
427-
428-
List<MemoryPoolMXBean> memoryPoolMXBeans = null;
429-
try {
430-
memoryPoolMXBeans = ManagementFactory.getPlatformMXBeans(mbsc, MemoryPoolMXBean.class);
431-
432-
} catch (Exception e) {
433-
Assert.fail("Failed to get MemoryPoolMXBean. : " + e.getMessage());
434-
}
348+
List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getPlatformMXBeans(mbsc, MemoryPoolMXBean.class);
435349
for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
436350
assertNotNull("Memory Pool name should not be null ", memoryPoolMXBean.getName());
437351
}
438352
}
439353

440354
@Test
441-
public void testFlightRecorderMXBeanProxy() {
355+
public void testFlightRecorderMXBeanProxy() throws IOException {
442356
// This test checks to make sure we are able to get the MXBean and do simple things with it.
443357
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
444-
445-
FlightRecorderMXBean flightRecorderMXBean = null;
446-
try {
447-
flightRecorderMXBean = ManagementFactory.getPlatformMXBean(mbsc, FlightRecorderMXBean.class);
448-
449-
} catch (Exception e) {
450-
Assert.fail("Failed to get FlightRecorderMXBean. : " + e.getMessage());
451-
}
358+
FlightRecorderMXBean flightRecorderMXBean = ManagementFactory.getPlatformMXBean(mbsc, FlightRecorderMXBean.class);
452359
flightRecorderMXBean.newRecording();
453360
assertFalse("Flight recordings should be available because we just created one.", flightRecorderMXBean.getRecordings().isEmpty());
454361
}
455362

456363
@Test
457-
public void testFlightRecorderMXBeanDirect() throws MalformedObjectNameException {
364+
public void testFlightRecorderMXBeanDirect() throws Exception {
458365
MBeanServerConnection mbsc = getLocalMBeanServerConnectionStatic();
459366
ObjectName objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder");
460-
try {
461-
long recording = (long) mbsc.invoke(objectName, "newRecording", null, null);
462-
mbsc.invoke(objectName, "startRecording", new Object[]{recording}, new String[]{"long"});
463-
mbsc.invoke(objectName, "stopRecording", new Object[]{recording}, new String[]{"long"});
464-
} catch (Exception e) {
465-
Assert.fail("Remote invocations failed : " + e.getMessage());
466-
}
367+
long recording = (long) mbsc.invoke(objectName, "newRecording", null, null);
368+
mbsc.invoke(objectName, "startRecording", new Object[]{recording}, new String[]{"long"});
369+
mbsc.invoke(objectName, "stopRecording", new Object[]{recording}, new String[]{"long"});
467370
}
468371
}

0 commit comments

Comments
 (0)