From 04ae560f25c53bd42001fef85f6aac90cefdf365 Mon Sep 17 00:00:00 2001 From: mchung Date: Wed, 14 Oct 2009 20:16:02 -0700 Subject: [PATCH] 6891701: test/java/lang/management/RuntimeMXBean/GetSystemProperties should restore the system property Summary: Restore the system properties when the test finishes Reviewed-by: jjg --- .../RuntimeMXBean/GetSystemProperties.java | 20 ++++++++++++++++++- .../RuntimeMXBean/PropertiesTest.java | 19 +++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/test/java/lang/management/RuntimeMXBean/GetSystemProperties.java b/test/java/lang/management/RuntimeMXBean/GetSystemProperties.java index 6a324bfcd..5a6553784 100644 --- a/test/java/lang/management/RuntimeMXBean/GetSystemProperties.java +++ b/test/java/lang/management/RuntimeMXBean/GetSystemProperties.java @@ -49,6 +49,21 @@ public class GetSystemProperties { private static final String VALUE4 = "test.property.value4"; public static void main(String[] argv) throws Exception { + // Save a copy of the original system properties + Properties props = System.getProperties(); + + try { + // replace the system Properties object for any modification + // in case jtreg caches a copy + System.setProperties(new Properties(props)); + runTest(); + } finally { + // restore original system properties + System.setProperties(props); + } + } + + private static void runTest() throws Exception { RuntimeMXBean mbean = ManagementFactory.getRuntimeMXBean(); // Print all system properties @@ -88,7 +103,10 @@ public class GetSystemProperties { Map props2 = mbean.getSystemProperties(); // expect the system properties returned should be // same as the one before adding KEY3 and KEY4 - props1.equals(props2); + if (!props1.equals(props2)) { + throw new RuntimeException("Two copies of system properties " + + "are expected to be equal"); + } System.out.println("Test passed."); } diff --git a/test/java/lang/management/RuntimeMXBean/PropertiesTest.java b/test/java/lang/management/RuntimeMXBean/PropertiesTest.java index 2900b3c3d..047f291df 100644 --- a/test/java/lang/management/RuntimeMXBean/PropertiesTest.java +++ b/test/java/lang/management/RuntimeMXBean/PropertiesTest.java @@ -40,8 +40,21 @@ import java.lang.management.RuntimeMXBean; public class PropertiesTest { private static int NUM_MYPROPS = 3; public static void main(String[] argv) throws Exception { - Properties sysProps = System.getProperties(); + // Save a copy of the original system properties + Properties props = System.getProperties(); + try { + // replace the system Properties object for any modification + // in case jtreg caches a copy + System.setProperties(new Properties(props)); + runTest(props.size()); + } finally { + // restore original system properties + System.setProperties(props); + } + } + + private static void runTest(int sysPropsCount) throws Exception { // Create a new system properties using the old one // as the defaults Properties myProps = new Properties( System.getProperties() ); @@ -65,10 +78,10 @@ public class PropertiesTest { System.out.println(i++ + ": " + key + " : " + value); } - if (props.size() != NUM_MYPROPS + sysProps.size()) { + if (props.size() != NUM_MYPROPS + sysPropsCount) { throw new RuntimeException("Test Failed: " + "Expected number of properties = " + - NUM_MYPROPS + sysProps.size() + + NUM_MYPROPS + sysPropsCount + " but found = " + props.size()); } } -- GitLab