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

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

mercurial