Thu, 20 Mar 2014 10:42:00 -0700
Merge
jjg@610 | 1 | /* |
jjg@1669 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@610 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@610 | 4 | * |
jjg@610 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@610 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@610 | 7 | * published by the Free Software Foundation. |
jjg@610 | 8 | * |
jjg@610 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@610 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@610 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@610 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@610 | 13 | * accompanied this code). |
jjg@610 | 14 | * |
jjg@610 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@610 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@610 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@610 | 18 | * |
jjg@610 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@610 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@610 | 21 | * questions. |
jjg@610 | 22 | */ |
jjg@610 | 23 | |
jjg@610 | 24 | /* |
jjg@610 | 25 | * @test |
jjh@1182 | 26 | * @bug 6968063 7127924 |
jjg@610 | 27 | * @summary provide examples of code that generate diagnostics |
jjg@1409 | 28 | * @build Example CheckExamples DocCommentProcessor |
jjh@1179 | 29 | * @run main/othervm CheckExamples |
jjh@1179 | 30 | */ |
jjg@1409 | 31 | |
jjh@1179 | 32 | /* |
jjh@1179 | 33 | * See CR 7127924 for info on why othervm is used. |
jjg@610 | 34 | */ |
jjg@610 | 35 | |
jjg@610 | 36 | import java.io.*; |
jjg@1669 | 37 | import java.nio.file.*; |
jjg@1669 | 38 | import java.nio.file.attribute.BasicFileAttributes; |
jjg@610 | 39 | import java.util.*; |
jjg@610 | 40 | |
jjg@610 | 41 | /** |
jjg@610 | 42 | * Check invariants for a set of examples. |
jjg@610 | 43 | * -- each example should exactly declare the keys that will be generated when |
jjg@610 | 44 | * it is run. |
jjg@610 | 45 | * -- together, the examples should cover the set of resource keys in the |
jjg@610 | 46 | * compiler.properties bundle. A list of exceptions may be given in the |
jjg@610 | 47 | * not-yet.txt file. Entries on the not-yet.txt list should not be |
jjg@610 | 48 | * covered by examples. |
jjg@708 | 49 | * When new keys are added to the resource bundle, it is strongly recommended |
jjg@610 | 50 | * that corresponding new examples be added here, if at all practical, instead |
jjg@610 | 51 | * of simply and lazily being added to the not-yet.txt list. |
jjg@610 | 52 | */ |
jjg@610 | 53 | public class CheckExamples { |
jjg@610 | 54 | /** |
jjg@610 | 55 | * Standard entry point. |
jjg@610 | 56 | */ |
jjg@610 | 57 | public static void main(String... args) throws Exception { |
jjg@1669 | 58 | boolean jtreg = (System.getProperty("test.src") != null); |
jjg@1669 | 59 | Path tmpDir; |
jjg@1669 | 60 | boolean deleteOnExit; |
jjg@1669 | 61 | if (jtreg) { |
jjg@1669 | 62 | // use standard jtreg scratch directory: the current directory |
jjg@1669 | 63 | tmpDir = Paths.get(System.getProperty("user.dir")); |
jjg@1669 | 64 | deleteOnExit = false; |
jjg@1669 | 65 | } else { |
jjg@1669 | 66 | tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), |
jjg@1669 | 67 | CheckExamples.class.getName()); |
jjg@1669 | 68 | deleteOnExit = true; |
jjg@1669 | 69 | } |
jjg@1669 | 70 | Example.setTempDir(tmpDir.toFile()); |
jjg@1669 | 71 | |
jjg@1669 | 72 | try { |
jjg@1669 | 73 | new CheckExamples().run(); |
jjg@1669 | 74 | } finally { |
jjg@1669 | 75 | if (deleteOnExit) { |
jjg@1669 | 76 | clean(tmpDir); |
jjg@1669 | 77 | } |
jjg@1669 | 78 | } |
jjg@610 | 79 | } |
jjg@610 | 80 | |
jjg@610 | 81 | /** |
jjg@610 | 82 | * Run the test. |
jjg@610 | 83 | */ |
jjg@610 | 84 | void run() throws Exception { |
jjg@610 | 85 | Set<Example> examples = getExamples(); |
jjg@610 | 86 | |
jjg@610 | 87 | Set<String> notYetList = getNotYetList(); |
jjg@610 | 88 | Set<String> declaredKeys = new TreeSet<String>(); |
jjg@610 | 89 | for (Example e: examples) { |
jjg@610 | 90 | Set<String> e_decl = e.getDeclaredKeys(); |
jjg@610 | 91 | Set<String> e_actual = e.getActualKeys(); |
jjg@610 | 92 | for (String k: e_decl) { |
jjg@610 | 93 | if (!e_actual.contains(k)) |
jjg@610 | 94 | error("Example " + e + " declares key " + k + " but does not generate it"); |
jjg@610 | 95 | } |
jjg@610 | 96 | for (String k: e_actual) { |
jjg@610 | 97 | if (!e_decl.contains(k)) |
jjg@610 | 98 | error("Example " + e + " generates key " + k + " but does not declare it"); |
jjg@610 | 99 | } |
jjg@610 | 100 | for (String k: e.getDeclaredKeys()) { |
jjg@610 | 101 | if (notYetList.contains(k)) |
jjg@610 | 102 | error("Example " + e + " declares key " + k + " which is also on the \"not yet\" list"); |
jjg@610 | 103 | declaredKeys.add(k); |
jjg@610 | 104 | } |
jjg@610 | 105 | } |
jjg@610 | 106 | |
jjg@610 | 107 | ResourceBundle b = |
jjg@610 | 108 | ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler"); |
jjg@610 | 109 | Set<String> resourceKeys = new TreeSet<String>(b.keySet()); |
jjg@610 | 110 | |
jjg@610 | 111 | for (String dk: declaredKeys) { |
jjg@610 | 112 | if (!resourceKeys.contains(dk)) |
jjg@610 | 113 | error("Key " + dk + " is declared in tests but is not a valid key in resource bundle"); |
jjg@610 | 114 | } |
jjg@610 | 115 | |
jjg@610 | 116 | for (String nk: notYetList) { |
jjg@610 | 117 | if (!resourceKeys.contains(nk)) |
jjg@610 | 118 | error("Key " + nk + " is declared in not-yet list but is not a valid key in resource bundle"); |
jjg@610 | 119 | } |
jjg@610 | 120 | |
jjg@610 | 121 | for (String rk: resourceKeys) { |
jjg@610 | 122 | if (!declaredKeys.contains(rk) && !notYetList.contains(rk)) |
jjg@610 | 123 | error("Key " + rk + " is declared in resource bundle but is not in tests or not-yet list"); |
jjg@610 | 124 | } |
jjg@610 | 125 | |
jjg@610 | 126 | System.err.println(examples.size() + " examples checked"); |
jjg@610 | 127 | System.err.println(notYetList.size() + " keys on not-yet list"); |
jjg@610 | 128 | |
jjg@610 | 129 | Counts declaredCounts = new Counts(declaredKeys); |
jjg@610 | 130 | Counts resourceCounts = new Counts(resourceKeys); |
jjg@610 | 131 | List<String> rows = new ArrayList<String>(Arrays.asList(Counts.prefixes)); |
jjg@610 | 132 | rows.add("other"); |
jjg@610 | 133 | rows.add("total"); |
jjg@610 | 134 | System.err.println(); |
jjg@610 | 135 | System.err.println(String.format("%-14s %15s %15s %4s", |
jjg@610 | 136 | "prefix", "#keys in tests", "#keys in javac", "%")); |
jjg@610 | 137 | for (String p: rows) { |
jjg@610 | 138 | int d = declaredCounts.get(p); |
jjg@610 | 139 | int r = resourceCounts.get(p); |
jjg@610 | 140 | System.err.print(String.format("%-14s %15d %15d", p, d, r)); |
jjg@610 | 141 | if (r != 0) |
jjg@610 | 142 | System.err.print(String.format(" %3d%%", (d * 100) / r)); |
jjg@610 | 143 | System.err.println(); |
jjg@610 | 144 | } |
jjg@610 | 145 | |
jjg@610 | 146 | if (errors > 0) |
jjg@610 | 147 | throw new Exception(errors + " errors occurred."); |
jjg@610 | 148 | } |
jjg@610 | 149 | |
jjg@610 | 150 | /** |
jjg@610 | 151 | * Get the complete set of examples to be checked. |
jjg@610 | 152 | */ |
jjg@610 | 153 | Set<Example> getExamples() { |
jjg@610 | 154 | Set<Example> results = new TreeSet<Example>(); |
jjg@610 | 155 | File testSrc = new File(System.getProperty("test.src")); |
jjg@610 | 156 | File examples = new File(testSrc, "examples"); |
jjg@610 | 157 | for (File f: examples.listFiles()) { |
mcimadamore@795 | 158 | if (isValidExample(f)) |
jjg@610 | 159 | results.add(new Example(f)); |
jjg@610 | 160 | } |
jjg@610 | 161 | return results; |
jjg@610 | 162 | } |
jjg@610 | 163 | |
mcimadamore@795 | 164 | boolean isValidExample(File f) { |
mcimadamore@795 | 165 | return (f.isDirectory() && f.list().length > 0) || |
mcimadamore@795 | 166 | (f.isFile() && f.getName().endsWith(".java")); |
mcimadamore@795 | 167 | } |
mcimadamore@795 | 168 | |
jjg@610 | 169 | /** |
jjg@610 | 170 | * Get the contents of the "not-yet" list. |
jjg@610 | 171 | */ |
jjg@610 | 172 | Set<String> getNotYetList() { |
jjg@610 | 173 | Set<String> results = new TreeSet<String>(); |
jjg@610 | 174 | File testSrc = new File(System.getProperty("test.src")); |
jjg@610 | 175 | File notYetList = new File(testSrc, "examples.not-yet.txt"); |
jjg@610 | 176 | try { |
jjg@610 | 177 | String[] lines = read(notYetList).split("[\r\n]"); |
jjg@610 | 178 | for (String line: lines) { |
jjg@610 | 179 | int hash = line.indexOf("#"); |
jjg@610 | 180 | if (hash != -1) |
jjg@610 | 181 | line = line.substring(0, hash).trim(); |
jjg@610 | 182 | if (line.matches("[A-Za-z0-9-_.]+")) |
jjg@610 | 183 | results.add(line); |
jjg@610 | 184 | } |
jjg@610 | 185 | } catch (IOException e) { |
jjg@610 | 186 | throw new Error(e); |
jjg@610 | 187 | } |
jjg@610 | 188 | return results; |
jjg@610 | 189 | } |
jjg@610 | 190 | |
jjg@610 | 191 | /** |
jjg@610 | 192 | * Read the contents of a file. |
jjg@610 | 193 | */ |
jjg@610 | 194 | String read(File f) throws IOException { |
jjg@610 | 195 | byte[] bytes = new byte[(int) f.length()]; |
jjg@610 | 196 | DataInputStream in = new DataInputStream(new FileInputStream(f)); |
jjg@610 | 197 | try { |
jjg@610 | 198 | in.readFully(bytes); |
jjg@610 | 199 | } finally { |
jjg@610 | 200 | in.close(); |
jjg@610 | 201 | } |
jjg@610 | 202 | return new String(bytes); |
jjg@610 | 203 | } |
jjg@610 | 204 | |
jjg@610 | 205 | /** |
jjg@610 | 206 | * Report an error. |
jjg@610 | 207 | */ |
jjg@610 | 208 | void error(String msg) { |
jjg@610 | 209 | System.err.println("Error: " + msg); |
jjg@610 | 210 | errors++; |
jjg@610 | 211 | } |
jjg@610 | 212 | |
jjg@610 | 213 | int errors; |
jjg@610 | 214 | |
jjg@1669 | 215 | /** |
jjg@1669 | 216 | * Clean the contents of a directory. |
jjg@1669 | 217 | */ |
jjg@1669 | 218 | static void clean(Path dir) throws IOException { |
jjg@1669 | 219 | Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { |
jjg@1669 | 220 | @Override |
jjg@1669 | 221 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
jjg@1669 | 222 | Files.delete(file); |
jjg@1669 | 223 | return super.visitFile(file, attrs); |
jjg@1669 | 224 | } |
jjg@1669 | 225 | |
jjg@1669 | 226 | @Override |
jjg@1669 | 227 | public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
jjg@1669 | 228 | if (exc == null) Files.delete(dir); |
jjg@1669 | 229 | return super.postVisitDirectory(dir, exc); |
jjg@1669 | 230 | } |
jjg@1669 | 231 | }); |
jjg@1669 | 232 | } |
jjg@1669 | 233 | |
jjg@610 | 234 | static class Counts { |
jjg@610 | 235 | static String[] prefixes = { |
jjg@610 | 236 | "compiler.err.", |
jjg@610 | 237 | "compiler.warn.", |
jjg@610 | 238 | "compiler.note.", |
jjg@610 | 239 | "compiler.misc." |
jjg@610 | 240 | }; |
jjg@610 | 241 | |
jjg@610 | 242 | Counts(Set<String> keys) { |
jjg@610 | 243 | nextKey: |
jjg@610 | 244 | for (String k: keys) { |
jjg@610 | 245 | for (String p: prefixes) { |
jjg@610 | 246 | if (k.startsWith(p)) { |
jjg@610 | 247 | inc(p); |
jjg@610 | 248 | continue nextKey; |
jjg@610 | 249 | } |
jjg@610 | 250 | } |
jjg@610 | 251 | inc("other"); |
jjg@610 | 252 | } |
jjg@610 | 253 | table.put("total", keys.size()); |
jjg@610 | 254 | } |
jjg@610 | 255 | |
jjg@610 | 256 | int get(String p) { |
jjg@610 | 257 | Integer i = table.get(p); |
jjg@610 | 258 | return (i == null ? 0 : i); |
jjg@610 | 259 | } |
jjg@610 | 260 | |
jjg@610 | 261 | void inc(String p) { |
jjg@610 | 262 | Integer i = table.get(p); |
jjg@610 | 263 | table.put(p, (i == null ? 1 : i + 1)); |
jjg@610 | 264 | } |
jjg@610 | 265 | |
jjg@610 | 266 | Map<String,Integer> table = new HashMap<String,Integer>(); |
jjg@610 | 267 | }; |
jjg@610 | 268 | } |