6891701: test/java/lang/management/RuntimeMXBean/GetSystemProperties should restore the system property
authormchung
Wed, 14 Oct 2009 20:16:02 -0700
changeset 4046 871e26a824cc
parent 4045 7c72ce9392fc
child 4048 9b32f343b75c
child 4049 f02d7c56d8f8
6891701: test/java/lang/management/RuntimeMXBean/GetSystemProperties should restore the system property Summary: Restore the system properties when the test finishes Reviewed-by: jjg
jdk/test/java/lang/management/RuntimeMXBean/GetSystemProperties.java
jdk/test/java/lang/management/RuntimeMXBean/PropertiesTest.java
--- a/jdk/test/java/lang/management/RuntimeMXBean/GetSystemProperties.java	Wed Oct 14 09:47:22 2009 -0400
+++ b/jdk/test/java/lang/management/RuntimeMXBean/GetSystemProperties.java	Wed Oct 14 20:16:02 2009 -0700
@@ -49,6 +49,21 @@
     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 @@
         Map<String,String> 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.");
     }
--- a/jdk/test/java/lang/management/RuntimeMXBean/PropertiesTest.java	Wed Oct 14 09:47:22 2009 -0400
+++ b/jdk/test/java/lang/management/RuntimeMXBean/PropertiesTest.java	Wed Oct 14 20:16:02 2009 -0700
@@ -40,8 +40,21 @@
 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 @@
             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());
         }
     }