1 /* 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 17 package art; 18 19 import java.util.Set; 20 import java.util.TreeSet; 21 22 public class Test922 { run()23 public static void run() throws Exception { 24 doTest(); 25 } 26 doTest()27 public static void doTest() throws Exception { 28 Set<String> recommendedProperties = getRecommendedProperties(); 29 30 System.out.println("Recommended properties:"); 31 for (String key : recommendedProperties) { 32 checkProperty(key); 33 } 34 35 Set<String> allProperties = getAllProperties(); 36 37 Set<String> retained = new TreeSet<String>(recommendedProperties); 38 retained.retainAll(allProperties); 39 if (!retained.equals(recommendedProperties)) { 40 Set<String> missing = new TreeSet<String>(recommendedProperties); 41 missing.removeAll(retained); 42 System.out.println("Missing recommended properties: " + missing); 43 } 44 45 Set<String> nonRecommended = new TreeSet<String>(allProperties); 46 nonRecommended.removeAll(recommendedProperties); 47 48 System.out.println("Other properties:"); 49 for (String key : nonRecommended) { 50 checkProperty(key); 51 } 52 53 System.out.println("Non-specified property:"); 54 String key = generate(allProperties); 55 checkProperty(key); 56 57 System.out.println("Non-specified property (2):"); 58 String key2 = generateUnique(allProperties); 59 checkProperty(key2); 60 } 61 getRecommendedProperties()62 private static Set<String> getRecommendedProperties() { 63 Set<String> keys = new TreeSet<String>(); 64 keys.add("java.vm.vendor"); 65 keys.add("java.vm.version"); 66 keys.add("java.vm.name"); 67 keys.add("java.vm.info"); 68 keys.add("java.library.path"); 69 keys.add("java.class.path"); 70 return keys; 71 } 72 getAllProperties()73 private static Set<String> getAllProperties() { 74 Set<String> keys = new TreeSet<String>(); 75 String[] props = getSystemProperties(); 76 for (String p : props) { 77 keys.add(p); 78 } 79 return keys; 80 } 81 equals(String s1, String s2)82 private static boolean equals(String s1, String s2) { 83 if (s1 == null && s2 == null) { 84 return true; 85 } else if (s1 != null) { 86 return s1.equals(s2); 87 } else { 88 return false; 89 } 90 } 91 checkProperty(String key)92 private static void checkProperty(String key) { 93 System.out.print(" \"" + key + "\": "); 94 String err = null; 95 String value = null; 96 try { 97 value = getSystemProperty(key); 98 } catch (RuntimeException e) { 99 err = e.getMessage(); 100 } 101 String sysValue = System.getProperty(key); 102 if (equals(value, sysValue)) { 103 System.out.print("OK"); 104 if (err != null) { 105 System.out.println(" !!!" + err); 106 } else { 107 System.out.println(); 108 } 109 } else { 110 System.out.println("ERROR !!!" + err); 111 } 112 113 System.out.print(" Setting value to \"abc\": "); 114 try { 115 setSystemProperty(key, "abc"); 116 System.out.println("SUCCEEDED"); 117 } catch (RuntimeException e) { 118 System.out.println("!!!" + e.getMessage()); 119 } 120 } 121 generateUnique(Set<String> others)122 private static String generateUnique(Set<String> others) { 123 // Construct something. To be deterministic, just use "a+". 124 StringBuilder sb = new StringBuilder("a"); 125 for (;;) { 126 String key = sb.toString(); 127 if (!others.contains(key)) { 128 return key; 129 } 130 sb.append('a'); 131 } 132 } 133 generate(Set<String> others)134 private static String generate(Set<String> others) { 135 // First check for something in the overall System properties. 136 TreeSet<String> sysProps = new TreeSet<String>(System.getProperties().stringPropertyNames()); 137 sysProps.removeAll(others); 138 if (!sysProps.isEmpty()) { 139 // Find something that starts with "java" or "os," trying to be platform-independent. 140 for (String s: sysProps) { 141 if (s.startsWith("java.") || s.startsWith("os.")) { 142 return s; 143 } 144 } 145 // Just return the first thing. 146 return sysProps.iterator().next(); 147 } 148 149 return generateUnique(others); 150 } 151 getSystemProperties()152 private static native String[] getSystemProperties(); getSystemProperty(String key)153 private static native String getSystemProperty(String key); setSystemProperty(String key, String value)154 private static native void setSystemProperty(String key, String value); 155 } 156