Wed, 07 Nov 2012 17:20:12 -0800
8003134: CheckResourceKeys issues
Reviewed-by: jjh, bpatel
jjg@610 | 1 | /* |
jjh@1305 | 2 | * Copyright (c) 2010, 2012, 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@610 | 28 | * @build Example CheckExamples |
jjh@1179 | 29 | * @run main/othervm CheckExamples |
jjh@1179 | 30 | */ |
jjh@1179 | 31 | /* |
jjh@1179 | 32 | * See CR 7127924 for info on why othervm is used. |
jjg@610 | 33 | */ |
jjg@610 | 34 | |
jjg@610 | 35 | import java.io.*; |
jjg@610 | 36 | import java.util.*; |
jjg@610 | 37 | |
jjg@610 | 38 | /** |
jjg@610 | 39 | * Check invariants for a set of examples. |
jjg@610 | 40 | * -- each example should exactly declare the keys that will be generated when |
jjg@610 | 41 | * it is run. |
jjg@610 | 42 | * -- together, the examples should cover the set of resource keys in the |
jjg@610 | 43 | * compiler.properties bundle. A list of exceptions may be given in the |
jjg@610 | 44 | * not-yet.txt file. Entries on the not-yet.txt list should not be |
jjg@610 | 45 | * covered by examples. |
jjg@708 | 46 | * When new keys are added to the resource bundle, it is strongly recommended |
jjg@610 | 47 | * that corresponding new examples be added here, if at all practical, instead |
jjg@610 | 48 | * of simply and lazily being added to the not-yet.txt list. |
jjg@610 | 49 | */ |
jjg@610 | 50 | public class CheckExamples { |
jjg@610 | 51 | /** |
jjg@610 | 52 | * Standard entry point. |
jjg@610 | 53 | */ |
jjg@610 | 54 | public static void main(String... args) throws Exception { |
jjg@610 | 55 | new CheckExamples().run(); |
jjg@610 | 56 | } |
jjg@610 | 57 | |
jjg@610 | 58 | /** |
jjg@610 | 59 | * Run the test. |
jjg@610 | 60 | */ |
jjg@610 | 61 | void run() throws Exception { |
jjg@610 | 62 | Set<Example> examples = getExamples(); |
jjg@610 | 63 | |
jjg@610 | 64 | Set<String> notYetList = getNotYetList(); |
jjg@610 | 65 | Set<String> declaredKeys = new TreeSet<String>(); |
jjg@610 | 66 | for (Example e: examples) { |
jjg@610 | 67 | Set<String> e_decl = e.getDeclaredKeys(); |
jjg@610 | 68 | Set<String> e_actual = e.getActualKeys(); |
jjg@610 | 69 | for (String k: e_decl) { |
jjg@610 | 70 | if (!e_actual.contains(k)) |
jjg@610 | 71 | error("Example " + e + " declares key " + k + " but does not generate it"); |
jjg@610 | 72 | } |
jjg@610 | 73 | for (String k: e_actual) { |
jjg@610 | 74 | if (!e_decl.contains(k)) |
jjg@610 | 75 | error("Example " + e + " generates key " + k + " but does not declare it"); |
jjg@610 | 76 | } |
jjg@610 | 77 | for (String k: e.getDeclaredKeys()) { |
jjg@610 | 78 | if (notYetList.contains(k)) |
jjg@610 | 79 | error("Example " + e + " declares key " + k + " which is also on the \"not yet\" list"); |
jjg@610 | 80 | declaredKeys.add(k); |
jjg@610 | 81 | } |
jjg@610 | 82 | } |
jjg@610 | 83 | |
jjg@610 | 84 | ResourceBundle b = |
jjg@610 | 85 | ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler"); |
jjg@610 | 86 | Set<String> resourceKeys = new TreeSet<String>(b.keySet()); |
jjg@610 | 87 | |
jjg@610 | 88 | for (String dk: declaredKeys) { |
jjg@610 | 89 | if (!resourceKeys.contains(dk)) |
jjg@610 | 90 | error("Key " + dk + " is declared in tests but is not a valid key in resource bundle"); |
jjg@610 | 91 | } |
jjg@610 | 92 | |
jjg@610 | 93 | for (String nk: notYetList) { |
jjg@610 | 94 | if (!resourceKeys.contains(nk)) |
jjg@610 | 95 | error("Key " + nk + " is declared in not-yet list but is not a valid key in resource bundle"); |
jjg@610 | 96 | } |
jjg@610 | 97 | |
jjg@610 | 98 | for (String rk: resourceKeys) { |
jjg@610 | 99 | if (!declaredKeys.contains(rk) && !notYetList.contains(rk)) |
jjg@610 | 100 | error("Key " + rk + " is declared in resource bundle but is not in tests or not-yet list"); |
jjg@610 | 101 | } |
jjg@610 | 102 | |
jjg@610 | 103 | System.err.println(examples.size() + " examples checked"); |
jjg@610 | 104 | System.err.println(notYetList.size() + " keys on not-yet list"); |
jjg@610 | 105 | |
jjg@610 | 106 | Counts declaredCounts = new Counts(declaredKeys); |
jjg@610 | 107 | Counts resourceCounts = new Counts(resourceKeys); |
jjg@610 | 108 | List<String> rows = new ArrayList<String>(Arrays.asList(Counts.prefixes)); |
jjg@610 | 109 | rows.add("other"); |
jjg@610 | 110 | rows.add("total"); |
jjg@610 | 111 | System.err.println(); |
jjg@610 | 112 | System.err.println(String.format("%-14s %15s %15s %4s", |
jjg@610 | 113 | "prefix", "#keys in tests", "#keys in javac", "%")); |
jjg@610 | 114 | for (String p: rows) { |
jjg@610 | 115 | int d = declaredCounts.get(p); |
jjg@610 | 116 | int r = resourceCounts.get(p); |
jjg@610 | 117 | System.err.print(String.format("%-14s %15d %15d", p, d, r)); |
jjg@610 | 118 | if (r != 0) |
jjg@610 | 119 | System.err.print(String.format(" %3d%%", (d * 100) / r)); |
jjg@610 | 120 | System.err.println(); |
jjg@610 | 121 | } |
jjg@610 | 122 | |
jjg@610 | 123 | if (errors > 0) |
jjg@610 | 124 | throw new Exception(errors + " errors occurred."); |
jjg@610 | 125 | } |
jjg@610 | 126 | |
jjg@610 | 127 | /** |
jjg@610 | 128 | * Get the complete set of examples to be checked. |
jjg@610 | 129 | */ |
jjg@610 | 130 | Set<Example> getExamples() { |
jjg@610 | 131 | Set<Example> results = new TreeSet<Example>(); |
jjg@610 | 132 | File testSrc = new File(System.getProperty("test.src")); |
jjg@610 | 133 | File examples = new File(testSrc, "examples"); |
jjg@610 | 134 | for (File f: examples.listFiles()) { |
mcimadamore@795 | 135 | if (isValidExample(f)) |
jjg@610 | 136 | results.add(new Example(f)); |
jjg@610 | 137 | } |
jjg@610 | 138 | return results; |
jjg@610 | 139 | } |
jjg@610 | 140 | |
mcimadamore@795 | 141 | boolean isValidExample(File f) { |
mcimadamore@795 | 142 | return (f.isDirectory() && f.list().length > 0) || |
mcimadamore@795 | 143 | (f.isFile() && f.getName().endsWith(".java")); |
mcimadamore@795 | 144 | } |
mcimadamore@795 | 145 | |
jjg@610 | 146 | /** |
jjg@610 | 147 | * Get the contents of the "not-yet" list. |
jjg@610 | 148 | */ |
jjg@610 | 149 | Set<String> getNotYetList() { |
jjg@610 | 150 | Set<String> results = new TreeSet<String>(); |
jjg@610 | 151 | File testSrc = new File(System.getProperty("test.src")); |
jjg@610 | 152 | File notYetList = new File(testSrc, "examples.not-yet.txt"); |
jjg@610 | 153 | try { |
jjg@610 | 154 | String[] lines = read(notYetList).split("[\r\n]"); |
jjg@610 | 155 | for (String line: lines) { |
jjg@610 | 156 | int hash = line.indexOf("#"); |
jjg@610 | 157 | if (hash != -1) |
jjg@610 | 158 | line = line.substring(0, hash).trim(); |
jjg@610 | 159 | if (line.matches("[A-Za-z0-9-_.]+")) |
jjg@610 | 160 | results.add(line); |
jjg@610 | 161 | } |
jjg@610 | 162 | } catch (IOException e) { |
jjg@610 | 163 | throw new Error(e); |
jjg@610 | 164 | } |
jjg@610 | 165 | return results; |
jjg@610 | 166 | } |
jjg@610 | 167 | |
jjg@610 | 168 | /** |
jjg@610 | 169 | * Read the contents of a file. |
jjg@610 | 170 | */ |
jjg@610 | 171 | String read(File f) throws IOException { |
jjg@610 | 172 | byte[] bytes = new byte[(int) f.length()]; |
jjg@610 | 173 | DataInputStream in = new DataInputStream(new FileInputStream(f)); |
jjg@610 | 174 | try { |
jjg@610 | 175 | in.readFully(bytes); |
jjg@610 | 176 | } finally { |
jjg@610 | 177 | in.close(); |
jjg@610 | 178 | } |
jjg@610 | 179 | return new String(bytes); |
jjg@610 | 180 | } |
jjg@610 | 181 | |
jjg@610 | 182 | /** |
jjg@610 | 183 | * Report an error. |
jjg@610 | 184 | */ |
jjg@610 | 185 | void error(String msg) { |
jjg@610 | 186 | System.err.println("Error: " + msg); |
jjg@610 | 187 | errors++; |
jjg@610 | 188 | } |
jjg@610 | 189 | |
jjg@610 | 190 | int errors; |
jjg@610 | 191 | |
jjg@610 | 192 | static class Counts { |
jjg@610 | 193 | static String[] prefixes = { |
jjg@610 | 194 | "compiler.err.", |
jjg@610 | 195 | "compiler.warn.", |
jjg@610 | 196 | "compiler.note.", |
jjg@610 | 197 | "compiler.misc." |
jjg@610 | 198 | }; |
jjg@610 | 199 | |
jjg@610 | 200 | Counts(Set<String> keys) { |
jjg@610 | 201 | nextKey: |
jjg@610 | 202 | for (String k: keys) { |
jjg@610 | 203 | for (String p: prefixes) { |
jjg@610 | 204 | if (k.startsWith(p)) { |
jjg@610 | 205 | inc(p); |
jjg@610 | 206 | continue nextKey; |
jjg@610 | 207 | } |
jjg@610 | 208 | } |
jjg@610 | 209 | inc("other"); |
jjg@610 | 210 | } |
jjg@610 | 211 | table.put("total", keys.size()); |
jjg@610 | 212 | } |
jjg@610 | 213 | |
jjg@610 | 214 | int get(String p) { |
jjg@610 | 215 | Integer i = table.get(p); |
jjg@610 | 216 | return (i == null ? 0 : i); |
jjg@610 | 217 | } |
jjg@610 | 218 | |
jjg@610 | 219 | void inc(String p) { |
jjg@610 | 220 | Integer i = table.get(p); |
jjg@610 | 221 | table.put(p, (i == null ? 1 : i + 1)); |
jjg@610 | 222 | } |
jjg@610 | 223 | |
jjg@610 | 224 | Map<String,Integer> table = new HashMap<String,Integer>(); |
jjg@610 | 225 | }; |
jjg@610 | 226 | } |