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