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

     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  */
    24 /*
    25  * @test
    26  * @bug 6968063
    27  * @summary provide examples of code that generate diagnostics
    28  * @build Example CheckExamples
    29  * @run main CheckExamples
    30  */
    32 import java.io.*;
    33 import java.util.*;
    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 bundle, 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     }
    55     /**
    56      * Run the test.
    57      */
    58     void run() throws Exception {
    59         Set<Example> examples = getExamples();
    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         }
    81         ResourceBundle b =
    82             ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler");
    83         Set<String> resourceKeys = new TreeSet<String>(b.keySet());
    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         }
    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         }
    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         }
   100         System.err.println(examples.size() + " examples checked");
   101         System.err.println(notYetList.size() + " keys on not-yet list");
   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         }
   120         if (errors > 0)
   121             throw new Exception(errors + " errors occurred.");
   122     }
   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 (isValidExample(f))
   133                 results.add(new Example(f));
   134         }
   135         return results;
   136     }
   138     boolean isValidExample(File f) {
   139         return (f.isDirectory() && f.list().length > 0) ||
   140                 (f.isFile() && f.getName().endsWith(".java"));
   141     }
   143     /**
   144      * Get the contents of the "not-yet" list.
   145      */
   146     Set<String> getNotYetList() {
   147         Set<String> results = new TreeSet<String>();
   148         File testSrc = new File(System.getProperty("test.src"));
   149         File notYetList = new File(testSrc, "examples.not-yet.txt");
   150         try {
   151             String[] lines = read(notYetList).split("[\r\n]");
   152             for (String line: lines) {
   153                 int hash = line.indexOf("#");
   154                 if (hash != -1)
   155                     line = line.substring(0, hash).trim();
   156                 if (line.matches("[A-Za-z0-9-_.]+"))
   157                     results.add(line);
   158             }
   159         } catch (IOException e) {
   160             throw new Error(e);
   161         }
   162         return results;
   163     }
   165     /**
   166      * Read the contents of a file.
   167      */
   168     String read(File f) throws IOException {
   169         byte[] bytes = new byte[(int) f.length()];
   170         DataInputStream in = new DataInputStream(new FileInputStream(f));
   171         try {
   172             in.readFully(bytes);
   173         } finally {
   174             in.close();
   175         }
   176         return new String(bytes);
   177     }
   179     /**
   180      * Report an error.
   181      */
   182     void error(String msg) {
   183         System.err.println("Error: " + msg);
   184         errors++;
   185     }
   187     int errors;
   189     static class Counts {
   190         static String[] prefixes = {
   191             "compiler.err.",
   192             "compiler.warn.",
   193             "compiler.note.",
   194             "compiler.misc."
   195         };
   197         Counts(Set<String> keys) {
   198             nextKey:
   199             for (String k: keys) {
   200                 for (String p: prefixes) {
   201                     if (k.startsWith(p)) {
   202                         inc(p);
   203                         continue nextKey;
   204                     }
   205                 }
   206                 inc("other");
   207             }
   208             table.put("total", keys.size());
   209         }
   211         int get(String p) {
   212              Integer i = table.get(p);
   213              return (i == null ? 0 : i);
   214         }
   216         void inc(String p) {
   217             Integer i = table.get(p);
   218             table.put(p, (i == null ? 1 : i + 1));
   219         }
   221         Map<String,Integer> table = new HashMap<String,Integer>();
   222     };
   223 }

mercurial