1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/diags/CheckExamples.java Thu Jul 22 11:02:54 2010 -0700 1.3 @@ -0,0 +1,218 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6968063 1.30 + * @summary provide examples of code that generate diagnostics 1.31 + * @build Example CheckExamples 1.32 + * @run main CheckExamples 1.33 + */ 1.34 + 1.35 +import java.io.*; 1.36 +import java.util.*; 1.37 + 1.38 +/** 1.39 + * Check invariants for a set of examples. 1.40 + * -- each example should exactly declare the keys that will be generated when 1.41 + * it is run. 1.42 + * -- together, the examples should cover the set of resource keys in the 1.43 + * compiler.properties bundle. A list of exceptions may be given in the 1.44 + * not-yet.txt file. Entries on the not-yet.txt list should not be 1.45 + * covered by examples. 1.46 + * When new keys are added to the resource buncle, it is strongly recommended 1.47 + * that corresponding new examples be added here, if at all practical, instead 1.48 + * of simply and lazily being added to the not-yet.txt list. 1.49 + */ 1.50 +public class CheckExamples { 1.51 + /** 1.52 + * Standard entry point. 1.53 + */ 1.54 + public static void main(String... args) throws Exception { 1.55 + new CheckExamples().run(); 1.56 + } 1.57 + 1.58 + /** 1.59 + * Run the test. 1.60 + */ 1.61 + void run() throws Exception { 1.62 + Set<Example> examples = getExamples(); 1.63 + 1.64 + Set<String> notYetList = getNotYetList(); 1.65 + Set<String> declaredKeys = new TreeSet<String>(); 1.66 + for (Example e: examples) { 1.67 + Set<String> e_decl = e.getDeclaredKeys(); 1.68 + Set<String> e_actual = e.getActualKeys(); 1.69 + for (String k: e_decl) { 1.70 + if (!e_actual.contains(k)) 1.71 + error("Example " + e + " declares key " + k + " but does not generate it"); 1.72 + } 1.73 + for (String k: e_actual) { 1.74 + if (!e_decl.contains(k)) 1.75 + error("Example " + e + " generates key " + k + " but does not declare it"); 1.76 + } 1.77 + for (String k: e.getDeclaredKeys()) { 1.78 + if (notYetList.contains(k)) 1.79 + error("Example " + e + " declares key " + k + " which is also on the \"not yet\" list"); 1.80 + declaredKeys.add(k); 1.81 + } 1.82 + } 1.83 + 1.84 + ResourceBundle b = 1.85 + ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler"); 1.86 + Set<String> resourceKeys = new TreeSet<String>(b.keySet()); 1.87 + 1.88 + for (String dk: declaredKeys) { 1.89 + if (!resourceKeys.contains(dk)) 1.90 + error("Key " + dk + " is declared in tests but is not a valid key in resource bundle"); 1.91 + } 1.92 + 1.93 + for (String nk: notYetList) { 1.94 + if (!resourceKeys.contains(nk)) 1.95 + error("Key " + nk + " is declared in not-yet list but is not a valid key in resource bundle"); 1.96 + } 1.97 + 1.98 + for (String rk: resourceKeys) { 1.99 + if (!declaredKeys.contains(rk) && !notYetList.contains(rk)) 1.100 + error("Key " + rk + " is declared in resource bundle but is not in tests or not-yet list"); 1.101 + } 1.102 + 1.103 + System.err.println(examples.size() + " examples checked"); 1.104 + System.err.println(notYetList.size() + " keys on not-yet list"); 1.105 + 1.106 + Counts declaredCounts = new Counts(declaredKeys); 1.107 + Counts resourceCounts = new Counts(resourceKeys); 1.108 + List<String> rows = new ArrayList<String>(Arrays.asList(Counts.prefixes)); 1.109 + rows.add("other"); 1.110 + rows.add("total"); 1.111 + System.err.println(); 1.112 + System.err.println(String.format("%-14s %15s %15s %4s", 1.113 + "prefix", "#keys in tests", "#keys in javac", "%")); 1.114 + for (String p: rows) { 1.115 + int d = declaredCounts.get(p); 1.116 + int r = resourceCounts.get(p); 1.117 + System.err.print(String.format("%-14s %15d %15d", p, d, r)); 1.118 + if (r != 0) 1.119 + System.err.print(String.format(" %3d%%", (d * 100) / r)); 1.120 + System.err.println(); 1.121 + } 1.122 + 1.123 + if (errors > 0) 1.124 + throw new Exception(errors + " errors occurred."); 1.125 + } 1.126 + 1.127 + /** 1.128 + * Get the complete set of examples to be checked. 1.129 + */ 1.130 + Set<Example> getExamples() { 1.131 + Set<Example> results = new TreeSet<Example>(); 1.132 + File testSrc = new File(System.getProperty("test.src")); 1.133 + File examples = new File(testSrc, "examples"); 1.134 + for (File f: examples.listFiles()) { 1.135 + if (f.isDirectory() || f.isFile() && f.getName().endsWith(".java")) 1.136 + results.add(new Example(f)); 1.137 + } 1.138 + return results; 1.139 + } 1.140 + 1.141 + /** 1.142 + * Get the contents of the "not-yet" list. 1.143 + */ 1.144 + Set<String> getNotYetList() { 1.145 + Set<String> results = new TreeSet<String>(); 1.146 + File testSrc = new File(System.getProperty("test.src")); 1.147 + File notYetList = new File(testSrc, "examples.not-yet.txt"); 1.148 + try { 1.149 + String[] lines = read(notYetList).split("[\r\n]"); 1.150 + for (String line: lines) { 1.151 + int hash = line.indexOf("#"); 1.152 + if (hash != -1) 1.153 + line = line.substring(0, hash).trim(); 1.154 + if (line.matches("[A-Za-z0-9-_.]+")) 1.155 + results.add(line); 1.156 + } 1.157 + } catch (IOException e) { 1.158 + throw new Error(e); 1.159 + } 1.160 + return results; 1.161 + } 1.162 + 1.163 + /** 1.164 + * Read the contents of a file. 1.165 + */ 1.166 + String read(File f) throws IOException { 1.167 + byte[] bytes = new byte[(int) f.length()]; 1.168 + DataInputStream in = new DataInputStream(new FileInputStream(f)); 1.169 + try { 1.170 + in.readFully(bytes); 1.171 + } finally { 1.172 + in.close(); 1.173 + } 1.174 + return new String(bytes); 1.175 + } 1.176 + 1.177 + /** 1.178 + * Report an error. 1.179 + */ 1.180 + void error(String msg) { 1.181 + System.err.println("Error: " + msg); 1.182 + errors++; 1.183 + } 1.184 + 1.185 + int errors; 1.186 + 1.187 + static class Counts { 1.188 + static String[] prefixes = { 1.189 + "compiler.err.", 1.190 + "compiler.warn.", 1.191 + "compiler.note.", 1.192 + "compiler.misc." 1.193 + }; 1.194 + 1.195 + Counts(Set<String> keys) { 1.196 + nextKey: 1.197 + for (String k: keys) { 1.198 + for (String p: prefixes) { 1.199 + if (k.startsWith(p)) { 1.200 + inc(p); 1.201 + continue nextKey; 1.202 + } 1.203 + } 1.204 + inc("other"); 1.205 + } 1.206 + table.put("total", keys.size()); 1.207 + } 1.208 + 1.209 + int get(String p) { 1.210 + Integer i = table.get(p); 1.211 + return (i == null ? 0 : i); 1.212 + } 1.213 + 1.214 + void inc(String p) { 1.215 + Integer i = table.get(p); 1.216 + table.put(p, (i == null ? 1 : i + 1)); 1.217 + } 1.218 + 1.219 + Map<String,Integer> table = new HashMap<String,Integer>(); 1.220 + }; 1.221 +}