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