Path: blob/master/test/jdk/java/lang/management/RuntimeMXBean/GetSystemProperties.java
41154 views
/*1* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 499051226* @summary Basic Test for RuntimeMXBean.getSystemProperties().27* @author Mandy Chung28*/2930import java.lang.management.ManagementFactory;31import java.lang.management.RuntimeMXBean;32import java.util.*;3334public class GetSystemProperties {35private static final String KEY1 = "test.property.key1";36private static final String VALUE1 = "test.property.value1";37private static final String KEY2 = "test.property.key2";38private static final String VALUE2 = "test.property.value2";3940// system properties to be omitted41private static final String KEY3 = "test.property.key3";42private static final Long VALUE3 = new Long(0);;4344private static final Object KEY4 = new Object();45private static final String VALUE4 = "test.property.value4";4647public static void main(String[] argv) throws Exception {48// Save a copy of the original system properties49Properties props = System.getProperties();5051try {52// replace the system Properties object for any modification53// in case jtreg caches a copy54System.setProperties(new Properties(props));55runTest();56} finally {57// restore original system properties58System.setProperties(props);59}60}6162private static void runTest() throws Exception {63RuntimeMXBean mbean = ManagementFactory.getRuntimeMXBean();6465// Print all system properties66Map<String,String> props = mbean.getSystemProperties();67printProperties(props);6869// Add new system properties70System.setProperty(KEY1, VALUE1);71System.setProperty(KEY2, VALUE2);7273Map<String,String> props1 = mbean.getSystemProperties();74String value1 = props1.get(KEY1);75if (value1 == null || !value1.equals(VALUE1)) {76throw new RuntimeException(KEY1 + " property found" +77" with value = " + value1 +78" but expected to be " + VALUE1);79}8081String value2 = props1.get(KEY2);82if (value2 == null || !value2.equals(VALUE2)) {83throw new RuntimeException(KEY2 + " property found" +84" with value = " + value2 +85" but expected to be " + VALUE2);86}8788String value3 = props1.get(KEY3);89if (value3 != null) {90throw new RuntimeException(KEY3 + " property found" +91" but should not exist" );92}9394// Add new system properties but are not Strings95Properties sp = System.getProperties();96sp.put(KEY3, VALUE3);97sp.put(KEY4, VALUE4);9899Map<String,String> props2 = mbean.getSystemProperties();100// expect the system properties returned should be101// same as the one before adding KEY3 and KEY4102if (!props1.equals(props2)) {103throw new RuntimeException("Two copies of system properties " +104"are expected to be equal");105}106107System.out.println("Test passed.");108}109110private static void printProperties(Map<String,String> props) {111Set<Map.Entry<String,String>> set = props.entrySet();112for (Map.Entry<String,String> p : set) {113System.out.println(p.getKey() + ": " + p.getValue());114}115}116}117118119