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