test/tools/javac/diags/CheckExamples.java

Mon, 21 Jan 2013 20:13:56 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:13:56 +0000
changeset 1510
7873d37f5b37
parent 1409
33abf479f202
child 1669
d3648557391b
permissions
-rw-r--r--

8005244: Implement overload resolution as per latest spec EDR
Summary: Add support for stuck expressions and provisional applicability
Reviewed-by: jjg

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

mercurial