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