author | jjg |
Fri, 12 Aug 2016 17:45:31 -0700 | |
changeset 40316 | 20b50a99fe8d |
parent 21482 | langtools/test/tools/javac/MethodParameters/Tester.java@21e66f272f98 |
permissions | -rw-r--r-- |
16307 | 1 |
/* |
40316 | 2 |
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. |
16307 | 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 |
*/ |
|
23 |
||
24 |
import java.io.*; |
|
25 |
import java.lang.reflect.Constructor; |
|
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
26 |
import java.nio.charset.StandardCharsets; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
27 |
import java.nio.file.Files; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
28 |
import java.util.ArrayList; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
29 |
import java.util.Arrays; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
30 |
import java.util.List; |
16307 | 31 |
|
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 |
*/ |
|
40316 | 48 |
public class MethodParametersTester { |
16307 | 49 |
|
50 |
final static File classesdir = new File(System.getProperty("test.classes", ".")); |
|
51 |
||
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
52 |
private String classname; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
53 |
private File[] files; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
54 |
private File refFile; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
55 |
private int errors; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
56 |
private int warnings; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
57 |
private int diffGolden; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
58 |
|
16307 | 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 |
}; |
|
72 |
||
73 |
/** |
|
74 |
* Test-driver expect a single classname as argument. |
|
75 |
*/ |
|
76 |
public static void main(String... args) throws Exception { |
|
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
77 |
if (args.length != 2) { |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
78 |
throw new Error("A single class name and a golden file are expected as argument"); |
16307 | 79 |
} |
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
80 |
String testSrc = System.getProperty("test.src"); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
81 |
String testName = args[0]; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
82 |
String testGoldenFile = args[1]; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
83 |
final String pattern = testName + ".*\\.class"; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
84 |
File refFile = new File(testSrc, testGoldenFile); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
85 |
File[] files = classesdir.listFiles(new FileFilter() { |
16307 | 86 |
public boolean accept(File f) { |
87 |
return f.getName().matches(pattern); |
|
88 |
} |
|
89 |
}); |
|
90 |
if (files.length == 0) { |
|
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
91 |
File file = new File(classesdir, testName + ".class"); |
16307 | 92 |
throw new Error(file.getPath() + " not found"); |
93 |
} |
|
94 |
||
40316 | 95 |
new MethodParametersTester(testName, files, refFile).run(); |
16307 | 96 |
} |
97 |
||
40316 | 98 |
public MethodParametersTester(String name, File[] files, File refFile) { |
16307 | 99 |
this.classname = name; |
100 |
this.files = files; |
|
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
101 |
this.refFile = refFile; |
16307 | 102 |
} |
103 |
||
104 |
void run() throws Exception { |
|
105 |
||
106 |
// Test with each visitor |
|
107 |
for (Class<Visitor> vclass : visitors) { |
|
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
108 |
boolean compResult = false; |
16307 | 109 |
try { |
110 |
String vname = vclass.getName(); |
|
40316 | 111 |
Constructor c = vclass.getConstructor(MethodParametersTester.class); |
16307 | 112 |
|
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 |
} |
|
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
126 |
String output = sb.toString(); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
127 |
info(output); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
128 |
compResult = compareOutput(refFile, output); |
16307 | 129 |
} catch(ReflectiveOperationException e) { |
130 |
warn("Class " + vclass.getName() + " ignored, not a Visitor"); |
|
131 |
continue; |
|
132 |
} |
|
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
133 |
if (!compResult) { |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
134 |
diffGolden++; |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
135 |
error("The output from " + vclass.getName() + " did not match golden file."); |
16307 | 136 |
} |
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
137 |
} |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
138 |
|
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
139 |
if (0 != diffGolden) |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
140 |
throw new Exception("Test output is not equal with golden file."); |
16307 | 141 |
|
142 |
if(0 != warnings) |
|
143 |
System.err.println("Test generated " + warnings + " warnings"); |
|
144 |
||
145 |
if(0 != errors) |
|
146 |
throw new Exception("Tester test failed with " + |
|
147 |
errors + " errors"); |
|
148 |
} |
|
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
149 |
// Check if test output matches the golden file. |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
150 |
boolean compareOutput(File refFile, String sb) |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
151 |
throws FileNotFoundException, IOException { |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
152 |
|
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
153 |
List<String> refFileList = Files.readAllLines(refFile.toPath(), StandardCharsets.UTF_8); |
21482
21e66f272f98
8026931: MethodParameters tests failing on Windows
ksrini
parents:
21044
diff
changeset
|
154 |
List<String> sbList = Arrays.asList(sb.split("[\r\n]+")); |
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
155 |
// Check if test output contains unexpected lines or is missing expected lines. |
21482
21e66f272f98
8026931: MethodParameters tests failing on Windows
ksrini
parents:
21044
diff
changeset
|
156 |
List<String> sbOnly = new ArrayList<>(sbList); |
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
157 |
sbOnly.removeAll(refFileList); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
158 |
for (String line: sbOnly) |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
159 |
error("unexpected line found: " + line); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
160 |
|
21482
21e66f272f98
8026931: MethodParameters tests failing on Windows
ksrini
parents:
21044
diff
changeset
|
161 |
List<String> refOnly = new ArrayList<>(refFileList); |
21044
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
162 |
refOnly.removeAll(sbList); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
163 |
for (String line: refOnly) |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
164 |
error("expected line not found: " + line); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
165 |
|
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
166 |
return sbOnly.isEmpty() && refOnly.isEmpty(); |
ffba9cdeff1a
8015372: Update tests for Method Parameter Reflection API to check whether a parameter is final
mnunez
parents:
18000
diff
changeset
|
167 |
} |
16307 | 168 |
|
169 |
abstract static class Visitor { |
|
40316 | 170 |
MethodParametersTester tester; |
16307 | 171 |
File classesdir; |
172 |
||
40316 | 173 |
public Visitor(MethodParametersTester tester) { |
16307 | 174 |
this.tester = tester; |
175 |
} |
|
176 |
||
177 |
abstract void visitClass(final String classname, final File cfile, |
|
178 |
final StringBuilder sb) throws Exception; |
|
179 |
||
180 |
public void error(String msg) { |
|
181 |
tester.error(msg); |
|
182 |
} |
|
183 |
||
184 |
public void warn(String msg) { |
|
185 |
tester.warn(msg); |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
void error(String msg) { |
|
190 |
System.err.println("Error: " + msg); |
|
191 |
errors++; |
|
192 |
} |
|
193 |
||
194 |
void warn(String msg) { |
|
195 |
System.err.println("Warning: " + msg); |
|
196 |
warnings++; |
|
197 |
} |
|
198 |
||
199 |
void info(String msg) { |
|
200 |
System.out.println(msg); |
|
201 |
} |
|
202 |
} |