1 /*
   2  * Copyright (c) 2004, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4990512
  27  * @summary Basic Test for RuntimeMXBean.getSystemProperties().
  28  * @author  Mandy Chung
  29  */
  30 
  31 import java.lang.management.ManagementFactory;
  32 import java.lang.management.RuntimeMXBean;
  33 import java.util.*;
  34 
  35 public class GetSystemProperties {
  36     private static final String KEY1   = "test.property.key1";
  37     private static final String VALUE1 = "test.property.value1";
  38     private static final String KEY2   = "test.property.key2";
  39     private static final String VALUE2 = "test.property.value2";
  40 
  41     // system properties to be omitted
  42     private static final String KEY3   = "test.property.key3";
  43     private static final Long VALUE3   = new Long(0);;
  44 
  45     private static final Object KEY4   = new Object();
  46     private static final String VALUE4 = "test.property.value4";
  47 
  48     public static void main(String[] argv) throws Exception {
  49         // Save a copy of the original system properties
  50         Properties props = System.getProperties();
  51 
  52         try {
  53             // replace the system Properties object for any modification
  54             // in case jtreg caches a copy
  55             System.setProperties(new Properties(props));
  56             runTest();
  57         } finally {
  58             // restore original system properties
  59             System.setProperties(props);
  60         }
  61     }
  62 
  63     private static void runTest() throws Exception {
  64         RuntimeMXBean mbean = ManagementFactory.getRuntimeMXBean();
  65 
  66         // Print all system properties
  67         Map<String,String> props = mbean.getSystemProperties();
  68         printProperties(props);
  69 
  70         // Add new system properties
  71         System.setProperty(KEY1, VALUE1);
  72         System.setProperty(KEY2, VALUE2);
  73 
  74         Map<String,String> props1 = mbean.getSystemProperties();
  75         String value1 = props1.get(KEY1);
  76         if (value1 == null || !value1.equals(VALUE1)) {
  77             throw new RuntimeException(KEY1 + " property found" +
  78                  " with value = " + value1 +
  79                  " but expected to be " + VALUE1);
  80         }
  81 
  82         String value2 = props1.get(KEY2);
  83         if (value2 == null || !value2.equals(VALUE2)) {
  84             throw new RuntimeException(KEY2 + " property found" +
  85                  " with value = " + value2 +
  86                  " but expected to be " + VALUE2);
  87         }
  88 
  89         String value3 = props1.get(KEY3);
  90         if (value3 != null) {
  91             throw new RuntimeException(KEY3 + " property found" +
  92                  " but should not exist" );
  93         }
  94 
  95         // Add new system properties but are not Strings
  96         Properties sp = System.getProperties();
  97         sp.put(KEY3, VALUE3);
  98         sp.put(KEY4, VALUE4);
  99 
 100         Map<String,String> props2 = mbean.getSystemProperties();
 101         // expect the system properties returned should be
 102         // same as the one before adding KEY3 and KEY4
 103         if (!props1.equals(props2)) {
 104             throw new RuntimeException("Two copies of system properties " +
 105                 "are expected to be equal");
 106         }
 107 
 108         System.out.println("Test passed.");
 109     }
 110 
 111     private static void printProperties(Map<String,String> props) {
 112         Set<Map.Entry<String,String>> set = props.entrySet();
 113         for (Map.Entry<String,String> p : set) {
 114             System.out.println(p.getKey() + ": " + p.getValue());
 115         }
 116     }
 117 }