1 /*
   2  * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4990512
  27  * @summary Basic Test for RuntimeMXBean.getSystemProperties().
  28  * @author  Mandy Chung
  29  *
  30  * @compile -source 1.5 GetSystemProperties.java
  31  * @run main GetSystemProperties
  32  */
  33 
  34 import java.lang.management.ManagementFactory;
  35 import java.lang.management.RuntimeMXBean;
  36 import java.util.*;
  37 
  38 public class GetSystemProperties {
  39     private static final String KEY1   = "test.property.key1";
  40     private static final String VALUE1 = "test.property.value1";
  41     private static final String KEY2   = "test.property.key2";
  42     private static final String VALUE2 = "test.property.value2";
  43 
  44     // system properties to be omitted
  45     private static final String KEY3   = "test.property.key3";
  46     private static final Long VALUE3   = new Long(0);;
  47 
  48     private static final Object KEY4   = new Object();
  49     private static final String VALUE4 = "test.property.value4";
  50 
  51     public static void main(String[] argv) throws Exception {
  52         // Save a copy of the original system properties
  53         Properties props = System.getProperties();
  54 
  55         try {
  56             // replace the system Properties object for any modification
  57             // in case jtreg caches a copy
  58             System.setProperties(new Properties(props));
  59             runTest();
  60         } finally {
  61             // restore original system properties
  62             System.setProperties(props);
  63         }
  64     }
  65 
  66     private static void runTest() throws Exception {
  67         RuntimeMXBean mbean = ManagementFactory.getRuntimeMXBean();
  68 
  69         // Print all system properties
  70         Map<String,String> props = mbean.getSystemProperties();
  71         printProperties(props);
  72 
  73         // Add new system properties
  74         System.setProperty(KEY1, VALUE1);
  75         System.setProperty(KEY2, VALUE2);
  76 
  77         Map<String,String> props1 = mbean.getSystemProperties();
  78         String value1 = props1.get(KEY1);
  79         if (value1 == null || !value1.equals(VALUE1)) {
  80             throw new RuntimeException(KEY1 + " property found" +
  81                  " with value = " + value1 +
  82                  " but expected to be " + VALUE1);
  83         }
  84 
  85         String value2 = props1.get(KEY2);
  86         if (value2 == null || !value2.equals(VALUE2)) {
  87             throw new RuntimeException(KEY2 + " property found" +
  88                  " with value = " + value2 +
  89                  " but expected to be " + VALUE2);
  90         }
  91 
  92         String value3 = props1.get(KEY3);
  93         if (value3 != null) {
  94             throw new RuntimeException(KEY3 + " property found" +
  95                  " but should not exist" );
  96         }
  97 
  98         // Add new system properties but are not Strings
  99         Properties sp = System.getProperties();
 100         sp.put(KEY3, VALUE3);
 101         sp.put(KEY4, VALUE4);
 102 
 103         Map<String,String> props2 = mbean.getSystemProperties();
 104         // expect the system properties returned should be
 105         // same as the one before adding KEY3 and KEY4
 106         if (!props1.equals(props2)) {
 107             throw new RuntimeException("Two copies of system properties " +
 108                 "are expected to be equal");
 109         }
 110 
 111         System.out.println("Test passed.");
 112     }
 113 
 114     private static void printProperties(Map<String,String> props) {
 115         Set<Map.Entry<String,String>> set = props.entrySet();
 116         for (Map.Entry<String,String> p : set) {
 117             System.out.println(p.getKey() + ": " + p.getValue());
 118         }
 119     }
 120 }