1 /* 2 * Copyright (c) 2010, 2016, 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 6968063 7127924 27 * @summary provide examples of code that generate diagnostics 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.file 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.compiler/com.sun.tools.javac.resources:open 32 * jdk.compiler/com.sun.tools.javac.util 33 * @build Example CheckExamples DocCommentProcessor 34 * @run main/othervm CheckExamples 35 */ 36 37 /* 38 * See CR 7127924 for info on why othervm is used. 39 */ 40 41 import java.io.*; 42 import java.nio.file.*; 43 import java.nio.file.attribute.BasicFileAttributes; 44 import java.util.*; 45 46 /** 47 * Check invariants for a set of examples. 48 * -- each example should exactly declare the keys that will be generated when 49 * it is run. 50 * -- together, the examples should cover the set of resource keys in the 51 * compiler.properties bundle. A list of exceptions may be given in the 52 * not-yet.txt file. Entries on the not-yet.txt list should not be 53 * covered by examples. 54 * When new keys are added to the resource bundle, it is strongly recommended 55 * that corresponding new examples be added here, if at all practical, instead 56 * of simply and lazily being added to the not-yet.txt list. 57 */ 58 public class CheckExamples { 59 /** 60 * Standard entry point. 61 */ main(String... args)62 public static void main(String... args) throws Exception { 63 boolean jtreg = (System.getProperty("test.src") != null); 64 Path tmpDir; 65 boolean deleteOnExit; 66 if (jtreg) { 67 // use standard jtreg scratch directory: the current directory 68 tmpDir = Paths.get(System.getProperty("user.dir")); 69 deleteOnExit = false; 70 } else { 71 tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), 72 CheckExamples.class.getName()); 73 deleteOnExit = true; 74 } 75 Example.setTempDir(tmpDir.toFile()); 76 77 try { 78 new CheckExamples().run(); 79 } finally { 80 if (deleteOnExit) { 81 clean(tmpDir); 82 } 83 } 84 } 85 86 /** 87 * Run the test. 88 */ run()89 void run() throws Exception { 90 Set<Example> examples = getExamples(); 91 92 Set<String> notYetList = getNotYetList(); 93 Set<String> declaredKeys = new TreeSet<String>(); 94 for (Example e: examples) { 95 Set<String> e_decl = e.getDeclaredKeys(); 96 Set<String> e_actual = e.getActualKeys(); 97 for (String k: e_decl) { 98 if (!e_actual.contains(k)) 99 error("Example " + e + " declares key " + k + " but does not generate it"); 100 } 101 for (String k: e_actual) { 102 if (!e_decl.contains(k)) 103 error("Example " + e + " generates key " + k + " but does not declare it"); 104 } 105 for (String k: e.getDeclaredKeys()) { 106 if (notYetList.contains(k)) 107 error("Example " + e + " declares key " + k + " which is also on the \"not yet\" list"); 108 declaredKeys.add(k); 109 } 110 } 111 112 Module jdk_compiler = ModuleLayer.boot().findModule("jdk.compiler").get(); 113 ResourceBundle b = 114 ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler", jdk_compiler); 115 Set<String> resourceKeys = new TreeSet<String>(b.keySet()); 116 117 for (String dk: declaredKeys) { 118 if (!resourceKeys.contains(dk)) 119 error("Key " + dk + " is declared in tests but is not a valid key in resource bundle"); 120 } 121 122 for (String nk: notYetList) { 123 if (!resourceKeys.contains(nk)) 124 error("Key " + nk + " is declared in not-yet list but is not a valid key in resource bundle"); 125 } 126 127 for (String rk: resourceKeys) { 128 if (!declaredKeys.contains(rk) && !notYetList.contains(rk)) 129 error("Key " + rk + " is declared in resource bundle but is not in tests or not-yet list"); 130 } 131 132 System.err.println(examples.size() + " examples checked"); 133 System.err.println(notYetList.size() + " keys on not-yet list"); 134 135 Counts declaredCounts = new Counts(declaredKeys); 136 Counts resourceCounts = new Counts(resourceKeys); 137 List<String> rows = new ArrayList<String>(Arrays.asList(Counts.prefixes)); 138 rows.add("other"); 139 rows.add("total"); 140 System.err.println(); 141 System.err.println(String.format("%-14s %15s %15s %4s", 142 "prefix", "#keys in tests", "#keys in javac", "%")); 143 for (String p: rows) { 144 int d = declaredCounts.get(p); 145 int r = resourceCounts.get(p); 146 System.err.print(String.format("%-14s %15d %15d", p, d, r)); 147 if (r != 0) 148 System.err.print(String.format(" %3d%%", (d * 100) / r)); 149 System.err.println(); 150 } 151 152 if (errors > 0) 153 throw new Exception(errors + " errors occurred."); 154 } 155 156 /** 157 * Get the complete set of examples to be checked. 158 */ getExamples()159 Set<Example> getExamples() { 160 Set<Example> results = new TreeSet<Example>(); 161 File testSrc = new File(System.getProperty("test.src")); 162 File examples = new File(testSrc, "examples"); 163 for (File f: examples.listFiles()) { 164 if (isValidExample(f)) 165 results.add(new Example(f)); 166 } 167 return results; 168 } 169 isValidExample(File f)170 boolean isValidExample(File f) { 171 return (f.isDirectory() && f.list().length > 0) || 172 (f.isFile() && f.getName().endsWith(".java")); 173 } 174 175 /** 176 * Get the contents of the "not-yet" list. 177 */ getNotYetList()178 Set<String> getNotYetList() { 179 Set<String> results = new TreeSet<String>(); 180 File testSrc = new File(System.getProperty("test.src")); 181 File notYetList = new File(testSrc, "examples.not-yet.txt"); 182 try { 183 String[] lines = read(notYetList).split("[\r\n]"); 184 for (String line: lines) { 185 int hash = line.indexOf("#"); 186 if (hash != -1) 187 line = line.substring(0, hash).trim(); 188 if (line.matches("[A-Za-z0-9-_.]+")) 189 results.add(line); 190 } 191 } catch (IOException e) { 192 throw new Error(e); 193 } 194 return results; 195 } 196 197 /** 198 * Read the contents of a file. 199 */ read(File f)200 String read(File f) throws IOException { 201 byte[] bytes = new byte[(int) f.length()]; 202 DataInputStream in = new DataInputStream(new FileInputStream(f)); 203 try { 204 in.readFully(bytes); 205 } finally { 206 in.close(); 207 } 208 return new String(bytes); 209 } 210 211 /** 212 * Report an error. 213 */ error(String msg)214 void error(String msg) { 215 System.err.println("Error: " + msg); 216 errors++; 217 } 218 219 int errors; 220 221 /** 222 * Clean the contents of a directory. 223 */ clean(Path dir)224 static void clean(Path dir) throws IOException { 225 Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { 226 @Override 227 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 228 Files.delete(file); 229 return super.visitFile(file, attrs); 230 } 231 232 @Override 233 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 234 if (exc == null) Files.delete(dir); 235 return super.postVisitDirectory(dir, exc); 236 } 237 }); 238 } 239 240 static class Counts { 241 static String[] prefixes = { 242 "compiler.err.", 243 "compiler.warn.", 244 "compiler.note.", 245 "compiler.misc." 246 }; 247 Counts(Set<String> keys)248 Counts(Set<String> keys) { 249 nextKey: 250 for (String k: keys) { 251 for (String p: prefixes) { 252 if (k.startsWith(p)) { 253 inc(p); 254 continue nextKey; 255 } 256 } 257 inc("other"); 258 } 259 table.put("total", keys.size()); 260 } 261 get(String p)262 int get(String p) { 263 Integer i = table.get(p); 264 return (i == null ? 0 : i); 265 } 266 inc(String p)267 void inc(String p) { 268 Integer i = table.get(p); 269 table.put(p, (i == null ? 1 : i + 1)); 270 } 271 272 Map<String,Integer> table = new HashMap<String,Integer>(); 273 }; 274 } 275