GetSystemProperties.java revision 1753:16b33372af1c
11558Srgrimes/* 21558Srgrimes * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. 31558Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41558Srgrimes * 51558Srgrimes * This code is free software; you can redistribute it and/or modify it 61558Srgrimes * under the terms of the GNU General Public License version 2 only, as 71558Srgrimes * published by the Free Software Foundation. 81558Srgrimes * 91558Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 101558Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111558Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 121558Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 131558Srgrimes * accompanied this code). 141558Srgrimes * 151558Srgrimes * You should have received a copy of the GNU General Public License version 161558Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171558Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181558Srgrimes * 191558Srgrimes * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 201558Srgrimes * CA 95054 USA or visit www.sun.com if you need additional information or 211558Srgrimes * have any questions. 221558Srgrimes */ 231558Srgrimes 241558Srgrimes/* 251558Srgrimes * @test 261558Srgrimes * @bug 4990512 271558Srgrimes * @summary Basic Test for RuntimeMXBean.getSystemProperties(). 281558Srgrimes * @author Mandy Chung 291558Srgrimes * 30117031Sgordon * @compile -source 1.5 GetSystemProperties.java 311558Srgrimes * @run main GetSystemProperties 321558Srgrimes */ 331558Srgrimes 34import java.lang.management.ManagementFactory; 35import java.lang.management.RuntimeMXBean; 36import java.util.*; 37 38public 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} 121