blob: 8ad742f0ca796955ca9d6875ea87f821ab936fea [file] [log] [blame]
Andreas Gampe1bdaf732017-01-09 19:21:06 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.util.Set;
18import java.util.TreeSet;
19
20public class Main {
21 public static void main(String[] args) throws Exception {
Andreas Gampe1bdaf732017-01-09 19:21:06 -080022 doTest();
23 }
24
25 public static void doTest() throws Exception {
26 Set<String> recommendedProperties = getRecommendedProperties();
27
28 System.out.println("Recommended properties:");
29 for (String key : recommendedProperties) {
30 checkProperty(key);
31 }
32
33 Set<String> allProperties = getAllProperties();
34
35 Set<String> retained = new TreeSet<String>(recommendedProperties);
36 retained.retainAll(allProperties);
37 if (!retained.equals(recommendedProperties)) {
38 Set<String> missing = new TreeSet<String>(recommendedProperties);
39 missing.removeAll(retained);
40 System.out.println("Missing recommended properties: " + missing);
41 }
42
43 Set<String> nonRecommended = new TreeSet<String>(allProperties);
44 nonRecommended.removeAll(recommendedProperties);
45
46 System.out.println("Other properties:");
47 for (String key : nonRecommended) {
48 checkProperty(key);
49 }
50
51 System.out.println("Non-specified property:");
52 String key = generate(allProperties);
53 checkProperty(key);
54
55 System.out.println("Non-specified property (2):");
56 String key2 = generateUnique(allProperties);
57 checkProperty(key2);
58 }
59
60 private static Set<String> getRecommendedProperties() {
61 Set<String> keys = new TreeSet<String>();
62 keys.add("java.vm.vendor");
63 keys.add("java.vm.version");
64 keys.add("java.vm.name");
65 keys.add("java.vm.info");
66 keys.add("java.library.path");
67 keys.add("java.class.path");
68 return keys;
69 }
70
71 private static Set<String> getAllProperties() {
72 Set<String> keys = new TreeSet<String>();
73 String[] props = getSystemProperties();
74 for (String p : props) {
75 keys.add(p);
76 }
77 return keys;
78 }
79
80 private static boolean equals(String s1, String s2) {
81 if (s1 == null && s2 == null) {
82 return true;
83 } else if (s1 != null) {
84 return s1.equals(s2);
85 } else {
86 return false;
87 }
88 }
89
90 private static void checkProperty(String key) {
91 System.out.print(" \"" + key + "\": ");
92 String err = null;
93 String value = null;
94 try {
95 value = getSystemProperty(key);
96 } catch (RuntimeException e) {
97 err = e.getMessage();
98 }
99 String sysValue = System.getProperty(key);
100 if (equals(value, sysValue)) {
101 System.out.print("OK");
102 if (err != null) {
103 System.out.println(" !!!" + err);
104 } else {
105 System.out.println();
106 }
107 } else {
108 System.out.println("ERROR !!!" + err);
109 }
110
111 System.out.print(" Setting value to \"abc\": ");
112 try {
113 setSystemProperty(key, "abc");
114 System.out.println("SUCCEEDED");
115 } catch (RuntimeException e) {
116 System.out.println("!!!" + e.getMessage());
117 }
118 }
119
120 private static String generateUnique(Set<String> others) {
121 // Construct something. To be deterministic, just use "a+".
122 StringBuilder sb = new StringBuilder("a");
123 for (;;) {
124 String key = sb.toString();
125 if (!others.contains(key)) {
126 return key;
127 }
128 sb.append('a');
129 }
130 }
131
132 private static String generate(Set<String> others) {
133 // First check for something in the overall System properties.
134 TreeSet<String> sysProps = new TreeSet<String>(System.getProperties().stringPropertyNames());
135 sysProps.removeAll(others);
136 if (!sysProps.isEmpty()) {
137 // Find something that starts with "java" or "os," trying to be platform-independent.
138 for (String s: sysProps) {
139 if (s.startsWith("java.") || s.startsWith("os.")) {
140 return s;
141 }
142 }
143 // Just return the first thing.
144 return sysProps.iterator().next();
145 }
146
147 return generateUnique(others);
148 }
149
150 private static native String[] getSystemProperties();
151 private static native String getSystemProperty(String key);
152 private static native void setSystemProperty(String key, String value);
153}