test/tools/javac/diags/CheckExamples.java

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 795
7b99f98b3035
child 1179
1e2f4f4fb9f7
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

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

mercurial