test/tools/javac/diags/CheckExamples.java

changeset 610
3640b60bd0f6
child 708
c8b4a1e76089
equal deleted inserted replaced
609:13354e1abba7 610:3640b60bd0f6
1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 6968063
27 * @summary provide examples of code that generate diagnostics
28 * @build Example CheckExamples
29 * @run main CheckExamples
30 */
31
32 import java.io.*;
33 import java.util.*;
34
35 /**
36 * Check invariants for a set of examples.
37 * -- each example should exactly declare the keys that will be generated when
38 * it is run.
39 * -- together, the examples should cover the set of resource keys in the
40 * compiler.properties bundle. A list of exceptions may be given in the
41 * not-yet.txt file. Entries on the not-yet.txt list should not be
42 * covered by examples.
43 * When new keys are added to the resource buncle, it is strongly recommended
44 * that corresponding new examples be added here, if at all practical, instead
45 * of simply and lazily being added to the not-yet.txt list.
46 */
47 public class CheckExamples {
48 /**
49 * Standard entry point.
50 */
51 public static void main(String... args) throws Exception {
52 new CheckExamples().run();
53 }
54
55 /**
56 * Run the test.
57 */
58 void run() throws Exception {
59 Set<Example> examples = getExamples();
60
61 Set<String> notYetList = getNotYetList();
62 Set<String> declaredKeys = new TreeSet<String>();
63 for (Example e: examples) {
64 Set<String> e_decl = e.getDeclaredKeys();
65 Set<String> e_actual = e.getActualKeys();
66 for (String k: e_decl) {
67 if (!e_actual.contains(k))
68 error("Example " + e + " declares key " + k + " but does not generate it");
69 }
70 for (String k: e_actual) {
71 if (!e_decl.contains(k))
72 error("Example " + e + " generates key " + k + " but does not declare it");
73 }
74 for (String k: e.getDeclaredKeys()) {
75 if (notYetList.contains(k))
76 error("Example " + e + " declares key " + k + " which is also on the \"not yet\" list");
77 declaredKeys.add(k);
78 }
79 }
80
81 ResourceBundle b =
82 ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler");
83 Set<String> resourceKeys = new TreeSet<String>(b.keySet());
84
85 for (String dk: declaredKeys) {
86 if (!resourceKeys.contains(dk))
87 error("Key " + dk + " is declared in tests but is not a valid key in resource bundle");
88 }
89
90 for (String nk: notYetList) {
91 if (!resourceKeys.contains(nk))
92 error("Key " + nk + " is declared in not-yet list but is not a valid key in resource bundle");
93 }
94
95 for (String rk: resourceKeys) {
96 if (!declaredKeys.contains(rk) && !notYetList.contains(rk))
97 error("Key " + rk + " is declared in resource bundle but is not in tests or not-yet list");
98 }
99
100 System.err.println(examples.size() + " examples checked");
101 System.err.println(notYetList.size() + " keys on not-yet list");
102
103 Counts declaredCounts = new Counts(declaredKeys);
104 Counts resourceCounts = new Counts(resourceKeys);
105 List<String> rows = new ArrayList<String>(Arrays.asList(Counts.prefixes));
106 rows.add("other");
107 rows.add("total");
108 System.err.println();
109 System.err.println(String.format("%-14s %15s %15s %4s",
110 "prefix", "#keys in tests", "#keys in javac", "%"));
111 for (String p: rows) {
112 int d = declaredCounts.get(p);
113 int r = resourceCounts.get(p);
114 System.err.print(String.format("%-14s %15d %15d", p, d, r));
115 if (r != 0)
116 System.err.print(String.format(" %3d%%", (d * 100) / r));
117 System.err.println();
118 }
119
120 if (errors > 0)
121 throw new Exception(errors + " errors occurred.");
122 }
123
124 /**
125 * Get the complete set of examples to be checked.
126 */
127 Set<Example> getExamples() {
128 Set<Example> results = new TreeSet<Example>();
129 File testSrc = new File(System.getProperty("test.src"));
130 File examples = new File(testSrc, "examples");
131 for (File f: examples.listFiles()) {
132 if (f.isDirectory() || f.isFile() && f.getName().endsWith(".java"))
133 results.add(new Example(f));
134 }
135 return results;
136 }
137
138 /**
139 * Get the contents of the "not-yet" list.
140 */
141 Set<String> getNotYetList() {
142 Set<String> results = new TreeSet<String>();
143 File testSrc = new File(System.getProperty("test.src"));
144 File notYetList = new File(testSrc, "examples.not-yet.txt");
145 try {
146 String[] lines = read(notYetList).split("[\r\n]");
147 for (String line: lines) {
148 int hash = line.indexOf("#");
149 if (hash != -1)
150 line = line.substring(0, hash).trim();
151 if (line.matches("[A-Za-z0-9-_.]+"))
152 results.add(line);
153 }
154 } catch (IOException e) {
155 throw new Error(e);
156 }
157 return results;
158 }
159
160 /**
161 * Read the contents of a file.
162 */
163 String read(File f) throws IOException {
164 byte[] bytes = new byte[(int) f.length()];
165 DataInputStream in = new DataInputStream(new FileInputStream(f));
166 try {
167 in.readFully(bytes);
168 } finally {
169 in.close();
170 }
171 return new String(bytes);
172 }
173
174 /**
175 * Report an error.
176 */
177 void error(String msg) {
178 System.err.println("Error: " + msg);
179 errors++;
180 }
181
182 int errors;
183
184 static class Counts {
185 static String[] prefixes = {
186 "compiler.err.",
187 "compiler.warn.",
188 "compiler.note.",
189 "compiler.misc."
190 };
191
192 Counts(Set<String> keys) {
193 nextKey:
194 for (String k: keys) {
195 for (String p: prefixes) {
196 if (k.startsWith(p)) {
197 inc(p);
198 continue nextKey;
199 }
200 }
201 inc("other");
202 }
203 table.put("total", keys.size());
204 }
205
206 int get(String p) {
207 Integer i = table.get(p);
208 return (i == null ? 0 : i);
209 }
210
211 void inc(String p) {
212 Integer i = table.get(p);
213 table.put(p, (i == null ? 1 : i + 1));
214 }
215
216 Map<String,Integer> table = new HashMap<String,Integer>();
217 };
218 }

mercurial