jjg@610: /* jjg@610: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. jjg@610: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@610: * jjg@610: * This code is free software; you can redistribute it and/or modify it jjg@610: * under the terms of the GNU General Public License version 2 only, as jjg@610: * published by the Free Software Foundation. jjg@610: * jjg@610: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@610: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@610: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@610: * version 2 for more details (a copy is included in the LICENSE file that jjg@610: * accompanied this code). jjg@610: * jjg@610: * You should have received a copy of the GNU General Public License version jjg@610: * 2 along with this work; if not, write to the Free Software Foundation, jjg@610: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@610: * jjg@610: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@610: * or visit www.oracle.com if you need additional information or have any jjg@610: * questions. jjg@610: */ jjg@610: jjg@610: /* jjg@610: * @test jjg@610: * @bug 6968063 jjg@610: * @summary provide examples of code that generate diagnostics jjg@610: * @build Example CheckExamples jjg@610: * @run main CheckExamples jjg@610: */ jjg@610: jjg@610: import java.io.*; jjg@610: import java.util.*; jjg@610: jjg@610: /** jjg@610: * Check invariants for a set of examples. jjg@610: * -- each example should exactly declare the keys that will be generated when jjg@610: * it is run. jjg@610: * -- together, the examples should cover the set of resource keys in the jjg@610: * compiler.properties bundle. A list of exceptions may be given in the jjg@610: * not-yet.txt file. Entries on the not-yet.txt list should not be jjg@610: * covered by examples. jjg@708: * When new keys are added to the resource bundle, it is strongly recommended jjg@610: * that corresponding new examples be added here, if at all practical, instead jjg@610: * of simply and lazily being added to the not-yet.txt list. jjg@610: */ jjg@610: public class CheckExamples { jjg@610: /** jjg@610: * Standard entry point. jjg@610: */ jjg@610: public static void main(String... args) throws Exception { jjg@610: new CheckExamples().run(); jjg@610: } jjg@610: jjg@610: /** jjg@610: * Run the test. jjg@610: */ jjg@610: void run() throws Exception { jjg@610: Set examples = getExamples(); jjg@610: jjg@610: Set notYetList = getNotYetList(); jjg@610: Set declaredKeys = new TreeSet(); jjg@610: for (Example e: examples) { jjg@610: Set e_decl = e.getDeclaredKeys(); jjg@610: Set e_actual = e.getActualKeys(); jjg@610: for (String k: e_decl) { jjg@610: if (!e_actual.contains(k)) jjg@610: error("Example " + e + " declares key " + k + " but does not generate it"); jjg@610: } jjg@610: for (String k: e_actual) { jjg@610: if (!e_decl.contains(k)) jjg@610: error("Example " + e + " generates key " + k + " but does not declare it"); jjg@610: } jjg@610: for (String k: e.getDeclaredKeys()) { jjg@610: if (notYetList.contains(k)) jjg@610: error("Example " + e + " declares key " + k + " which is also on the \"not yet\" list"); jjg@610: declaredKeys.add(k); jjg@610: } jjg@610: } jjg@610: jjg@610: ResourceBundle b = jjg@610: ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler"); jjg@610: Set resourceKeys = new TreeSet(b.keySet()); jjg@610: jjg@610: for (String dk: declaredKeys) { jjg@610: if (!resourceKeys.contains(dk)) jjg@610: error("Key " + dk + " is declared in tests but is not a valid key in resource bundle"); jjg@610: } jjg@610: jjg@610: for (String nk: notYetList) { jjg@610: if (!resourceKeys.contains(nk)) jjg@610: error("Key " + nk + " is declared in not-yet list but is not a valid key in resource bundle"); jjg@610: } jjg@610: jjg@610: for (String rk: resourceKeys) { jjg@610: if (!declaredKeys.contains(rk) && !notYetList.contains(rk)) jjg@610: error("Key " + rk + " is declared in resource bundle but is not in tests or not-yet list"); jjg@610: } jjg@610: jjg@610: System.err.println(examples.size() + " examples checked"); jjg@610: System.err.println(notYetList.size() + " keys on not-yet list"); jjg@610: jjg@610: Counts declaredCounts = new Counts(declaredKeys); jjg@610: Counts resourceCounts = new Counts(resourceKeys); jjg@610: List rows = new ArrayList(Arrays.asList(Counts.prefixes)); jjg@610: rows.add("other"); jjg@610: rows.add("total"); jjg@610: System.err.println(); jjg@610: System.err.println(String.format("%-14s %15s %15s %4s", jjg@610: "prefix", "#keys in tests", "#keys in javac", "%")); jjg@610: for (String p: rows) { jjg@610: int d = declaredCounts.get(p); jjg@610: int r = resourceCounts.get(p); jjg@610: System.err.print(String.format("%-14s %15d %15d", p, d, r)); jjg@610: if (r != 0) jjg@610: System.err.print(String.format(" %3d%%", (d * 100) / r)); jjg@610: System.err.println(); jjg@610: } jjg@610: jjg@610: if (errors > 0) jjg@610: throw new Exception(errors + " errors occurred."); jjg@610: } jjg@610: jjg@610: /** jjg@610: * Get the complete set of examples to be checked. jjg@610: */ jjg@610: Set getExamples() { jjg@610: Set results = new TreeSet(); jjg@610: File testSrc = new File(System.getProperty("test.src")); jjg@610: File examples = new File(testSrc, "examples"); jjg@610: for (File f: examples.listFiles()) { mcimadamore@795: if (isValidExample(f)) jjg@610: results.add(new Example(f)); jjg@610: } jjg@610: return results; jjg@610: } jjg@610: mcimadamore@795: boolean isValidExample(File f) { mcimadamore@795: return (f.isDirectory() && f.list().length > 0) || mcimadamore@795: (f.isFile() && f.getName().endsWith(".java")); mcimadamore@795: } mcimadamore@795: jjg@610: /** jjg@610: * Get the contents of the "not-yet" list. jjg@610: */ jjg@610: Set getNotYetList() { jjg@610: Set results = new TreeSet(); jjg@610: File testSrc = new File(System.getProperty("test.src")); jjg@610: File notYetList = new File(testSrc, "examples.not-yet.txt"); jjg@610: try { jjg@610: String[] lines = read(notYetList).split("[\r\n]"); jjg@610: for (String line: lines) { jjg@610: int hash = line.indexOf("#"); jjg@610: if (hash != -1) jjg@610: line = line.substring(0, hash).trim(); jjg@610: if (line.matches("[A-Za-z0-9-_.]+")) jjg@610: results.add(line); jjg@610: } jjg@610: } catch (IOException e) { jjg@610: throw new Error(e); jjg@610: } jjg@610: return results; jjg@610: } jjg@610: jjg@610: /** jjg@610: * Read the contents of a file. jjg@610: */ jjg@610: String read(File f) throws IOException { jjg@610: byte[] bytes = new byte[(int) f.length()]; jjg@610: DataInputStream in = new DataInputStream(new FileInputStream(f)); jjg@610: try { jjg@610: in.readFully(bytes); jjg@610: } finally { jjg@610: in.close(); jjg@610: } jjg@610: return new String(bytes); jjg@610: } jjg@610: jjg@610: /** jjg@610: * Report an error. jjg@610: */ jjg@610: void error(String msg) { jjg@610: System.err.println("Error: " + msg); jjg@610: errors++; jjg@610: } jjg@610: jjg@610: int errors; jjg@610: jjg@610: static class Counts { jjg@610: static String[] prefixes = { jjg@610: "compiler.err.", jjg@610: "compiler.warn.", jjg@610: "compiler.note.", jjg@610: "compiler.misc." jjg@610: }; jjg@610: jjg@610: Counts(Set keys) { jjg@610: nextKey: jjg@610: for (String k: keys) { jjg@610: for (String p: prefixes) { jjg@610: if (k.startsWith(p)) { jjg@610: inc(p); jjg@610: continue nextKey; jjg@610: } jjg@610: } jjg@610: inc("other"); jjg@610: } jjg@610: table.put("total", keys.size()); jjg@610: } jjg@610: jjg@610: int get(String p) { jjg@610: Integer i = table.get(p); jjg@610: return (i == null ? 0 : i); jjg@610: } jjg@610: jjg@610: void inc(String p) { jjg@610: Integer i = table.get(p); jjg@610: table.put(p, (i == null ? 1 : i + 1)); jjg@610: } jjg@610: jjg@610: Map table = new HashMap(); jjg@610: }; jjg@610: }