test/tools/javac/MethodParameters/Tester.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2013, 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 import java.io.*;
    25 import java.lang.reflect.Constructor;
    26 import java.nio.charset.StandardCharsets;
    27 import java.nio.file.Files;
    28 import java.util.ArrayList;
    29 import java.util.Arrays;
    30 import java.util.List;
    32 /**
    33  * Test driver for MethodParameters testing.
    34  * <p>
    35  * The intended use of this driver is to run it, giving the name of
    36  * a single class compiled with -parameters as argument. The driver
    37  * will test the specified class, and any nested classes it finds.
    38  * <p>
    39  * Each class is tested in two way. By refelction, and by directly
    40  * checking MethodParameters attributes in the classfile. The checking
    41  * is done using two visitor classes {@link ClassFileVisitor} and
    42  * {@link ReflectionVisitor}.
    43  * <p>
    44  * The {@code ReflectionVisitor} test logically belongs with library tests.
    45  * we wish to reuse the same test-cases, so both test are committed together,
    46  * under langtools. The tests, may be duplicated in the jdk repository.
    47  */
    48 public class Tester {
    50     final static File classesdir = new File(System.getProperty("test.classes", "."));
    52     private String classname;
    53     private File[] files;
    54     private File refFile;
    55     private int errors;
    56     private int warnings;
    57     private int diffGolden;
    59     /**
    60      * The visitor classes that does the actual checking are referenced
    61      * statically, to force compilations, without having to reference
    62      * them in individual test cases.
    63      * <p>
    64      * This makes it easy to change the set of visitors, without
    65      * complicating the design with dynamic discovery and compilation
    66      * of visitor classes.
    67      */
    68     static final Class visitors[] = {
    69         ClassFileVisitor.class,
    70         ReflectionVisitor.class
    71     };
    73     /**
    74      * Test-driver expect a single classname as argument.
    75      */
    76     public static void main(String... args) throws Exception {
    77         if (args.length != 2) {
    78             throw new Error("A single class name and a golden file are expected as argument");
    79         }
    80         String testSrc = System.getProperty("test.src");
    81         String testName = args[0];
    82         String testGoldenFile = args[1];
    83         final String pattern = testName + ".*\\.class";
    84         File refFile = new File(testSrc, testGoldenFile);
    85         File[] files = classesdir.listFiles(new FileFilter() {
    86                 public boolean accept(File f) {
    87                     return f.getName().matches(pattern);
    88                 }
    89             });
    90         if (files.length == 0) {
    91             File file = new File(classesdir, testName + ".class");
    92             throw new Error(file.getPath() + " not found");
    93         }
    95         new Tester(testName, files, refFile).run();
    96     }
    98     public Tester(String name, File[] files, File refFile) {
    99         this.classname = name;
   100         this.files = files;
   101         this.refFile = refFile;
   102     }
   104     void run() throws Exception {
   106         // Test with each visitor
   107         for (Class<Visitor> vclass : visitors) {
   108             boolean compResult = false;
   109             try {
   110                 String vname = vclass.getName();
   111                 Constructor c = vclass.getConstructor(Tester.class);
   113                 info("\nRun " + vname + " for " + classname + "\n");
   114                 StringBuilder sb = new StringBuilder();
   115                 for (File f : files) {
   116                     String fname = f.getName();
   117                     fname = fname.substring(0, fname.length() - 6);
   118                     Visitor v = (Visitor) c.newInstance(this);
   119                     try {
   120                         v.visitClass(fname, f,  sb);
   121                     } catch(Exception e) {
   122                         error("Uncaught exception in visitClass()");
   123                         e.printStackTrace();
   124                     }
   125                 }
   126                 String output = sb.toString();
   127                 info(output);
   128                 compResult = compareOutput(refFile, output);
   129             } catch(ReflectiveOperationException e) {
   130                 warn("Class " + vclass.getName() + " ignored, not a Visitor");
   131                 continue;
   132             }
   133             if (!compResult) {
   134                 diffGolden++;
   135                 error("The output from " + vclass.getName() + " did not match golden file.");
   136         }
   137         }
   139         if (0 != diffGolden)
   140             throw new Exception("Test output is not equal with golden file.");
   142         if(0 != warnings)
   143                 System.err.println("Test generated " + warnings + " warnings");
   145         if(0 != errors)
   146             throw new Exception("Tester test failed with " +
   147                                 errors + " errors");
   148     }
   149     // Check if test output matches the golden file.
   150     boolean compareOutput(File refFile, String sb)
   151             throws FileNotFoundException, IOException {
   153         List<String> refFileList = Files.readAllLines(refFile.toPath(), StandardCharsets.UTF_8);
   154         List<String> sbList = Arrays.asList(sb.split("[\r\n]+"));
   155         // Check if test output contains unexpected lines or is missing expected lines.
   156         List<String> sbOnly = new ArrayList<>(sbList);
   157         sbOnly.removeAll(refFileList);
   158         for (String line: sbOnly)
   159             error("unexpected line found: " + line);
   161         List<String> refOnly = new ArrayList<>(refFileList);
   162         refOnly.removeAll(sbList);
   163         for (String line: refOnly)
   164             error("expected line not found: " + line);
   166         return sbOnly.isEmpty() && refOnly.isEmpty();
   167     }
   169     abstract static  class Visitor {
   170         Tester tester;
   171         File classesdir;
   173         public Visitor(Tester tester) {
   174             this.tester = tester;
   175         }
   177         abstract void visitClass(final String classname, final File  cfile,
   178                 final StringBuilder sb) throws Exception;
   180         public void error(String msg) {
   181             tester.error(msg);
   182         }
   184         public void warn(String msg) {
   185             tester.warn(msg);
   186         }
   187     }
   189     void error(String msg) {
   190         System.err.println("Error: " + msg);
   191         errors++;
   192     }
   194     void warn(String msg) {
   195         System.err.println("Warning: " + msg);
   196         warnings++;
   197     }
   199     void info(String msg) {
   200         System.out.println(msg);
   201     }
   202 }

mercurial