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