Tue, 31 May 2016 10:20:43 -0700
Merge
strarup@1594 | 1 | /* |
strarup@1594 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
strarup@1594 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
strarup@1594 | 4 | * |
strarup@1594 | 5 | * This code is free software; you can redistribute it and/or modify it |
strarup@1594 | 6 | * under the terms of the GNU General Public License version 2 only, as |
strarup@1594 | 7 | * published by the Free Software Foundation. |
strarup@1594 | 8 | * |
strarup@1594 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
strarup@1594 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
strarup@1594 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
strarup@1594 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
strarup@1594 | 13 | * accompanied this code). |
strarup@1594 | 14 | * |
strarup@1594 | 15 | * You should have received a copy of the GNU General Public License version |
strarup@1594 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
strarup@1594 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
strarup@1594 | 18 | * |
strarup@1594 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
strarup@1594 | 20 | * or visit www.oracle.com if you need additional information or have any |
strarup@1594 | 21 | * questions. |
strarup@1594 | 22 | */ |
strarup@1594 | 23 | |
strarup@1594 | 24 | import java.io.*; |
strarup@1594 | 25 | import java.lang.reflect.Constructor; |
mnunez@2137 | 26 | import java.nio.charset.StandardCharsets; |
mnunez@2137 | 27 | import java.nio.file.Files; |
mnunez@2137 | 28 | import java.util.ArrayList; |
mnunez@2137 | 29 | import java.util.Arrays; |
mnunez@2137 | 30 | import java.util.List; |
strarup@1594 | 31 | |
strarup@1594 | 32 | /** |
strarup@1594 | 33 | * Test driver for MethodParameters testing. |
strarup@1594 | 34 | * <p> |
strarup@1594 | 35 | * The intended use of this driver is to run it, giving the name of |
strarup@1594 | 36 | * a single class compiled with -parameters as argument. The driver |
strarup@1594 | 37 | * will test the specified class, and any nested classes it finds. |
strarup@1594 | 38 | * <p> |
strarup@1594 | 39 | * Each class is tested in two way. By refelction, and by directly |
strarup@1594 | 40 | * checking MethodParameters attributes in the classfile. The checking |
strarup@1594 | 41 | * is done using two visitor classes {@link ClassFileVisitor} and |
strarup@1594 | 42 | * {@link ReflectionVisitor}. |
strarup@1594 | 43 | * <p> |
strarup@1594 | 44 | * The {@code ReflectionVisitor} test logically belongs with library tests. |
strarup@1594 | 45 | * we wish to reuse the same test-cases, so both test are committed together, |
strarup@1594 | 46 | * under langtools. The tests, may be duplicated in the jdk repository. |
strarup@1594 | 47 | */ |
strarup@1594 | 48 | public class Tester { |
strarup@1594 | 49 | |
strarup@1594 | 50 | final static File classesdir = new File(System.getProperty("test.classes", ".")); |
strarup@1594 | 51 | |
mnunez@2137 | 52 | private String classname; |
mnunez@2137 | 53 | private File[] files; |
mnunez@2137 | 54 | private File refFile; |
mnunez@2137 | 55 | private int errors; |
mnunez@2137 | 56 | private int warnings; |
mnunez@2137 | 57 | private int diffGolden; |
mnunez@2137 | 58 | |
strarup@1594 | 59 | /** |
strarup@1594 | 60 | * The visitor classes that does the actual checking are referenced |
strarup@1594 | 61 | * statically, to force compilations, without having to reference |
strarup@1594 | 62 | * them in individual test cases. |
strarup@1594 | 63 | * <p> |
strarup@1594 | 64 | * This makes it easy to change the set of visitors, without |
strarup@1594 | 65 | * complicating the design with dynamic discovery and compilation |
strarup@1594 | 66 | * of visitor classes. |
strarup@1594 | 67 | */ |
strarup@1594 | 68 | static final Class visitors[] = { |
strarup@1594 | 69 | ClassFileVisitor.class, |
strarup@1594 | 70 | ReflectionVisitor.class |
strarup@1594 | 71 | }; |
strarup@1594 | 72 | |
strarup@1594 | 73 | /** |
strarup@1594 | 74 | * Test-driver expect a single classname as argument. |
strarup@1594 | 75 | */ |
strarup@1594 | 76 | public static void main(String... args) throws Exception { |
mnunez@2137 | 77 | if (args.length != 2) { |
mnunez@2137 | 78 | throw new Error("A single class name and a golden file are expected as argument"); |
strarup@1594 | 79 | } |
mnunez@2137 | 80 | String testSrc = System.getProperty("test.src"); |
mnunez@2137 | 81 | String testName = args[0]; |
mnunez@2137 | 82 | String testGoldenFile = args[1]; |
mnunez@2137 | 83 | final String pattern = testName + ".*\\.class"; |
mnunez@2137 | 84 | File refFile = new File(testSrc, testGoldenFile); |
mnunez@2137 | 85 | File[] files = classesdir.listFiles(new FileFilter() { |
strarup@1594 | 86 | public boolean accept(File f) { |
strarup@1594 | 87 | return f.getName().matches(pattern); |
strarup@1594 | 88 | } |
strarup@1594 | 89 | }); |
strarup@1594 | 90 | if (files.length == 0) { |
mnunez@2137 | 91 | File file = new File(classesdir, testName + ".class"); |
strarup@1594 | 92 | throw new Error(file.getPath() + " not found"); |
strarup@1594 | 93 | } |
strarup@1594 | 94 | |
mnunez@2137 | 95 | new Tester(testName, files, refFile).run(); |
strarup@1594 | 96 | } |
strarup@1594 | 97 | |
mnunez@2137 | 98 | public Tester(String name, File[] files, File refFile) { |
strarup@1594 | 99 | this.classname = name; |
strarup@1594 | 100 | this.files = files; |
mnunez@2137 | 101 | this.refFile = refFile; |
strarup@1594 | 102 | } |
strarup@1594 | 103 | |
strarup@1594 | 104 | void run() throws Exception { |
strarup@1594 | 105 | |
strarup@1594 | 106 | // Test with each visitor |
strarup@1594 | 107 | for (Class<Visitor> vclass : visitors) { |
mnunez@2137 | 108 | boolean compResult = false; |
strarup@1594 | 109 | try { |
strarup@1594 | 110 | String vname = vclass.getName(); |
strarup@1594 | 111 | Constructor c = vclass.getConstructor(Tester.class); |
strarup@1594 | 112 | |
strarup@1594 | 113 | info("\nRun " + vname + " for " + classname + "\n"); |
strarup@1594 | 114 | StringBuilder sb = new StringBuilder(); |
strarup@1594 | 115 | for (File f : files) { |
strarup@1594 | 116 | String fname = f.getName(); |
strarup@1594 | 117 | fname = fname.substring(0, fname.length() - 6); |
strarup@1594 | 118 | Visitor v = (Visitor) c.newInstance(this); |
strarup@1594 | 119 | try { |
strarup@1594 | 120 | v.visitClass(fname, f, sb); |
strarup@1594 | 121 | } catch(Exception e) { |
strarup@1594 | 122 | error("Uncaught exception in visitClass()"); |
strarup@1594 | 123 | e.printStackTrace(); |
strarup@1594 | 124 | } |
strarup@1594 | 125 | } |
mnunez@2137 | 126 | String output = sb.toString(); |
mnunez@2137 | 127 | info(output); |
mnunez@2137 | 128 | compResult = compareOutput(refFile, output); |
strarup@1594 | 129 | } catch(ReflectiveOperationException e) { |
strarup@1594 | 130 | warn("Class " + vclass.getName() + " ignored, not a Visitor"); |
strarup@1594 | 131 | continue; |
strarup@1594 | 132 | } |
mnunez@2137 | 133 | if (!compResult) { |
mnunez@2137 | 134 | diffGolden++; |
mnunez@2137 | 135 | error("The output from " + vclass.getName() + " did not match golden file."); |
strarup@1594 | 136 | } |
mnunez@2137 | 137 | } |
mnunez@2137 | 138 | |
mnunez@2137 | 139 | if (0 != diffGolden) |
mnunez@2137 | 140 | throw new Exception("Test output is not equal with golden file."); |
strarup@1594 | 141 | |
strarup@1594 | 142 | if(0 != warnings) |
strarup@1594 | 143 | System.err.println("Test generated " + warnings + " warnings"); |
strarup@1594 | 144 | |
strarup@1594 | 145 | if(0 != errors) |
strarup@1594 | 146 | throw new Exception("Tester test failed with " + |
strarup@1594 | 147 | errors + " errors"); |
strarup@1594 | 148 | } |
mnunez@2137 | 149 | // Check if test output matches the golden file. |
mnunez@2137 | 150 | boolean compareOutput(File refFile, String sb) |
mnunez@2137 | 151 | throws FileNotFoundException, IOException { |
mnunez@2137 | 152 | |
mnunez@2137 | 153 | List<String> refFileList = Files.readAllLines(refFile.toPath(), StandardCharsets.UTF_8); |
ksrini@2151 | 154 | List<String> sbList = Arrays.asList(sb.split("[\r\n]+")); |
mnunez@2137 | 155 | // Check if test output contains unexpected lines or is missing expected lines. |
ksrini@2151 | 156 | List<String> sbOnly = new ArrayList<>(sbList); |
mnunez@2137 | 157 | sbOnly.removeAll(refFileList); |
mnunez@2137 | 158 | for (String line: sbOnly) |
mnunez@2137 | 159 | error("unexpected line found: " + line); |
mnunez@2137 | 160 | |
ksrini@2151 | 161 | List<String> refOnly = new ArrayList<>(refFileList); |
mnunez@2137 | 162 | refOnly.removeAll(sbList); |
mnunez@2137 | 163 | for (String line: refOnly) |
mnunez@2137 | 164 | error("expected line not found: " + line); |
mnunez@2137 | 165 | |
mnunez@2137 | 166 | return sbOnly.isEmpty() && refOnly.isEmpty(); |
mnunez@2137 | 167 | } |
strarup@1594 | 168 | |
strarup@1594 | 169 | abstract static class Visitor { |
strarup@1594 | 170 | Tester tester; |
strarup@1594 | 171 | File classesdir; |
strarup@1594 | 172 | |
strarup@1594 | 173 | public Visitor(Tester tester) { |
strarup@1594 | 174 | this.tester = tester; |
strarup@1594 | 175 | } |
strarup@1594 | 176 | |
strarup@1594 | 177 | abstract void visitClass(final String classname, final File cfile, |
strarup@1594 | 178 | final StringBuilder sb) throws Exception; |
strarup@1594 | 179 | |
strarup@1594 | 180 | public void error(String msg) { |
strarup@1594 | 181 | tester.error(msg); |
strarup@1594 | 182 | } |
strarup@1594 | 183 | |
strarup@1594 | 184 | public void warn(String msg) { |
strarup@1594 | 185 | tester.warn(msg); |
strarup@1594 | 186 | } |
strarup@1594 | 187 | } |
strarup@1594 | 188 | |
strarup@1594 | 189 | void error(String msg) { |
strarup@1594 | 190 | System.err.println("Error: " + msg); |
strarup@1594 | 191 | errors++; |
strarup@1594 | 192 | } |
strarup@1594 | 193 | |
strarup@1594 | 194 | void warn(String msg) { |
strarup@1594 | 195 | System.err.println("Warning: " + msg); |
strarup@1594 | 196 | warnings++; |
strarup@1594 | 197 | } |
strarup@1594 | 198 | |
strarup@1594 | 199 | void info(String msg) { |
strarup@1594 | 200 | System.out.println(msg); |
strarup@1594 | 201 | } |
strarup@1594 | 202 | } |