1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/MethodParameters/Tester.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,202 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.*; 1.28 +import java.lang.reflect.Constructor; 1.29 +import java.nio.charset.StandardCharsets; 1.30 +import java.nio.file.Files; 1.31 +import java.util.ArrayList; 1.32 +import java.util.Arrays; 1.33 +import java.util.List; 1.34 + 1.35 +/** 1.36 + * Test driver for MethodParameters testing. 1.37 + * <p> 1.38 + * The intended use of this driver is to run it, giving the name of 1.39 + * a single class compiled with -parameters as argument. The driver 1.40 + * will test the specified class, and any nested classes it finds. 1.41 + * <p> 1.42 + * Each class is tested in two way. By refelction, and by directly 1.43 + * checking MethodParameters attributes in the classfile. The checking 1.44 + * is done using two visitor classes {@link ClassFileVisitor} and 1.45 + * {@link ReflectionVisitor}. 1.46 + * <p> 1.47 + * The {@code ReflectionVisitor} test logically belongs with library tests. 1.48 + * we wish to reuse the same test-cases, so both test are committed together, 1.49 + * under langtools. The tests, may be duplicated in the jdk repository. 1.50 + */ 1.51 +public class Tester { 1.52 + 1.53 + final static File classesdir = new File(System.getProperty("test.classes", ".")); 1.54 + 1.55 + private String classname; 1.56 + private File[] files; 1.57 + private File refFile; 1.58 + private int errors; 1.59 + private int warnings; 1.60 + private int diffGolden; 1.61 + 1.62 + /** 1.63 + * The visitor classes that does the actual checking are referenced 1.64 + * statically, to force compilations, without having to reference 1.65 + * them in individual test cases. 1.66 + * <p> 1.67 + * This makes it easy to change the set of visitors, without 1.68 + * complicating the design with dynamic discovery and compilation 1.69 + * of visitor classes. 1.70 + */ 1.71 + static final Class visitors[] = { 1.72 + ClassFileVisitor.class, 1.73 + ReflectionVisitor.class 1.74 + }; 1.75 + 1.76 + /** 1.77 + * Test-driver expect a single classname as argument. 1.78 + */ 1.79 + public static void main(String... args) throws Exception { 1.80 + if (args.length != 2) { 1.81 + throw new Error("A single class name and a golden file are expected as argument"); 1.82 + } 1.83 + String testSrc = System.getProperty("test.src"); 1.84 + String testName = args[0]; 1.85 + String testGoldenFile = args[1]; 1.86 + final String pattern = testName + ".*\\.class"; 1.87 + File refFile = new File(testSrc, testGoldenFile); 1.88 + File[] files = classesdir.listFiles(new FileFilter() { 1.89 + public boolean accept(File f) { 1.90 + return f.getName().matches(pattern); 1.91 + } 1.92 + }); 1.93 + if (files.length == 0) { 1.94 + File file = new File(classesdir, testName + ".class"); 1.95 + throw new Error(file.getPath() + " not found"); 1.96 + } 1.97 + 1.98 + new Tester(testName, files, refFile).run(); 1.99 + } 1.100 + 1.101 + public Tester(String name, File[] files, File refFile) { 1.102 + this.classname = name; 1.103 + this.files = files; 1.104 + this.refFile = refFile; 1.105 + } 1.106 + 1.107 + void run() throws Exception { 1.108 + 1.109 + // Test with each visitor 1.110 + for (Class<Visitor> vclass : visitors) { 1.111 + boolean compResult = false; 1.112 + try { 1.113 + String vname = vclass.getName(); 1.114 + Constructor c = vclass.getConstructor(Tester.class); 1.115 + 1.116 + info("\nRun " + vname + " for " + classname + "\n"); 1.117 + StringBuilder sb = new StringBuilder(); 1.118 + for (File f : files) { 1.119 + String fname = f.getName(); 1.120 + fname = fname.substring(0, fname.length() - 6); 1.121 + Visitor v = (Visitor) c.newInstance(this); 1.122 + try { 1.123 + v.visitClass(fname, f, sb); 1.124 + } catch(Exception e) { 1.125 + error("Uncaught exception in visitClass()"); 1.126 + e.printStackTrace(); 1.127 + } 1.128 + } 1.129 + String output = sb.toString(); 1.130 + info(output); 1.131 + compResult = compareOutput(refFile, output); 1.132 + } catch(ReflectiveOperationException e) { 1.133 + warn("Class " + vclass.getName() + " ignored, not a Visitor"); 1.134 + continue; 1.135 + } 1.136 + if (!compResult) { 1.137 + diffGolden++; 1.138 + error("The output from " + vclass.getName() + " did not match golden file."); 1.139 + } 1.140 + } 1.141 + 1.142 + if (0 != diffGolden) 1.143 + throw new Exception("Test output is not equal with golden file."); 1.144 + 1.145 + if(0 != warnings) 1.146 + System.err.println("Test generated " + warnings + " warnings"); 1.147 + 1.148 + if(0 != errors) 1.149 + throw new Exception("Tester test failed with " + 1.150 + errors + " errors"); 1.151 + } 1.152 + // Check if test output matches the golden file. 1.153 + boolean compareOutput(File refFile, String sb) 1.154 + throws FileNotFoundException, IOException { 1.155 + 1.156 + List<String> refFileList = Files.readAllLines(refFile.toPath(), StandardCharsets.UTF_8); 1.157 + List<String> sbList = Arrays.asList(sb.split("[\r\n]+")); 1.158 + // Check if test output contains unexpected lines or is missing expected lines. 1.159 + List<String> sbOnly = new ArrayList<>(sbList); 1.160 + sbOnly.removeAll(refFileList); 1.161 + for (String line: sbOnly) 1.162 + error("unexpected line found: " + line); 1.163 + 1.164 + List<String> refOnly = new ArrayList<>(refFileList); 1.165 + refOnly.removeAll(sbList); 1.166 + for (String line: refOnly) 1.167 + error("expected line not found: " + line); 1.168 + 1.169 + return sbOnly.isEmpty() && refOnly.isEmpty(); 1.170 + } 1.171 + 1.172 + abstract static class Visitor { 1.173 + Tester tester; 1.174 + File classesdir; 1.175 + 1.176 + public Visitor(Tester tester) { 1.177 + this.tester = tester; 1.178 + } 1.179 + 1.180 + abstract void visitClass(final String classname, final File cfile, 1.181 + final StringBuilder sb) throws Exception; 1.182 + 1.183 + public void error(String msg) { 1.184 + tester.error(msg); 1.185 + } 1.186 + 1.187 + public void warn(String msg) { 1.188 + tester.warn(msg); 1.189 + } 1.190 + } 1.191 + 1.192 + void error(String msg) { 1.193 + System.err.println("Error: " + msg); 1.194 + errors++; 1.195 + } 1.196 + 1.197 + void warn(String msg) { 1.198 + System.err.println("Warning: " + msg); 1.199 + warnings++; 1.200 + } 1.201 + 1.202 + void info(String msg) { 1.203 + System.out.println(msg); 1.204 + } 1.205 +}