GetSystemProperties.java revision 0:37a05a11f281
1192811Srmacklem/* 2192811Srmacklem * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. 3192811Srmacklem * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4192811Srmacklem * 5192811Srmacklem * This code is free software; you can redistribute it and/or modify it 6192811Srmacklem * under the terms of the GNU General Public License version 2 only, as 7192811Srmacklem * published by the Free Software Foundation. 8192811Srmacklem * 9192811Srmacklem * This code is distributed in the hope that it will be useful, but WITHOUT 10192811Srmacklem * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11192811Srmacklem * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12192811Srmacklem * version 2 for more details (a copy is included in the LICENSE file that 13192811Srmacklem * accompanied this code). 14192811Srmacklem * 15192811Srmacklem * You should have received a copy of the GNU General Public License version 16192811Srmacklem * 2 along with this work; if not, write to the Free Software Foundation, 17192811Srmacklem * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18192811Srmacklem * 19192811Srmacklem * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20192811Srmacklem * CA 95054 USA or visit www.sun.com if you need additional information or 21192811Srmacklem * have any questions. 22192811Srmacklem */ 23192811Srmacklem 24192811Srmacklem/* 25192811Srmacklem * @test 26192811Srmacklem * @bug 4990512 27192811Srmacklem * @summary Basic Test for RuntimeMXBean.getSystemProperties(). 28192811Srmacklem * @author Mandy Chung 29192811Srmacklem * 30192811Srmacklem * @compile -source 1.5 GetSystemProperties.java 31192811Srmacklem * @run main GetSystemProperties 32192811Srmacklem */ 33192811Srmacklem 34192811Srmacklemimport java.lang.management.ManagementFactory; 35192811Srmacklemimport java.lang.management.RuntimeMXBean; 36192811Srmacklemimport java.util.*; 37192811Srmacklem 38192811Srmacklempublic class GetSystemProperties { 39192811Srmacklem private static final String KEY1 = "test.property.key1"; 40192811Srmacklem private static final String VALUE1 = "test.property.value1"; 41192811Srmacklem private static final String KEY2 = "test.property.key2"; 42192811Srmacklem private static final String VALUE2 = "test.property.value2"; 43192811Srmacklem 44192811Srmacklem // system properties to be omitted 45192811Srmacklem private static final String KEY3 = "test.property.key3"; 46192811Srmacklem private static final Long VALUE3 = new Long(0);; 47192811Srmacklem 48192811Srmacklem private static final Object KEY4 = new Object(); 49192811Srmacklem private static final String VALUE4 = "test.property.value4"; 50192811Srmacklem 51192811Srmacklem public static void main(String[] argv) throws Exception { 52192811Srmacklem RuntimeMXBean mbean = ManagementFactory.getRuntimeMXBean(); 53192811Srmacklem 54192811Srmacklem // Print all system properties 55192811Srmacklem Map<String,String> props = mbean.getSystemProperties(); 56192811Srmacklem printProperties(props); 57192811Srmacklem 58192811Srmacklem // Add new system properties 59192811Srmacklem System.setProperty(KEY1, VALUE1); 60192811Srmacklem System.setProperty(KEY2, VALUE2); 61192811Srmacklem 62192811Srmacklem Map<String,String> props1 = mbean.getSystemProperties(); 63192811Srmacklem String value1 = props1.get(KEY1); 64192811Srmacklem if (value1 == null || !value1.equals(VALUE1)) { 65 throw new RuntimeException(KEY1 + " property found" + 66 " with value = " + value1 + 67 " but expected to be " + VALUE1); 68 } 69 70 String value2 = props1.get(KEY2); 71 if (value2 == null || !value2.equals(VALUE2)) { 72 throw new RuntimeException(KEY2 + " property found" + 73 " with value = " + value2 + 74 " but expected to be " + VALUE2); 75 } 76 77 String value3 = props1.get(KEY3); 78 if (value3 != null) { 79 throw new RuntimeException(KEY3 + " property found" + 80 " but should not exist" ); 81 } 82 83 // Add new system properties but are not Strings 84 Properties sp = System.getProperties(); 85 sp.put(KEY3, VALUE3); 86 sp.put(KEY4, VALUE4); 87 88 Map<String,String> props2 = mbean.getSystemProperties(); 89 // expect the system properties returned should be 90 // same as the one before adding KEY3 and KEY4 91 props1.equals(props2); 92 93 System.out.println("Test passed."); 94 } 95 96 private static void printProperties(Map<String,String> props) { 97 Set<Map.Entry<String,String>> set = props.entrySet(); 98 for (Map.Entry<String,String> p : set) { 99 System.out.println(p.getKey() + ": " + p.getValue()); 100 } 101 } 102} 103