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

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

mercurial