21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.io.*; |
24 import java.io.*; |
25 import java.lang.reflect.Constructor; |
25 import java.lang.reflect.Constructor; |
|
26 import java.nio.charset.StandardCharsets; |
|
27 import java.nio.file.Files; |
|
28 import java.util.ArrayList; |
|
29 import java.util.Arrays; |
|
30 import java.util.Collections; |
|
31 import java.util.List; |
26 |
32 |
27 /** |
33 /** |
28 * Test driver for MethodParameters testing. |
34 * Test driver for MethodParameters testing. |
29 * <p> |
35 * <p> |
30 * The intended use of this driver is to run it, giving the name of |
36 * The intended use of this driver is to run it, giving the name of |
42 */ |
48 */ |
43 public class Tester { |
49 public class Tester { |
44 |
50 |
45 final static File classesdir = new File(System.getProperty("test.classes", ".")); |
51 final static File classesdir = new File(System.getProperty("test.classes", ".")); |
46 |
52 |
|
53 private String classname; |
|
54 private File[] files; |
|
55 private File refFile; |
|
56 private int errors; |
|
57 private int warnings; |
|
58 private int diffGolden; |
|
59 |
47 /** |
60 /** |
48 * The visitor classes that does the actual checking are referenced |
61 * The visitor classes that does the actual checking are referenced |
49 * statically, to force compilations, without having to reference |
62 * statically, to force compilations, without having to reference |
50 * them in individual test cases. |
63 * them in individual test cases. |
51 * <p> |
64 * <p> |
60 |
73 |
61 /** |
74 /** |
62 * Test-driver expect a single classname as argument. |
75 * Test-driver expect a single classname as argument. |
63 */ |
76 */ |
64 public static void main(String... args) throws Exception { |
77 public static void main(String... args) throws Exception { |
65 if (args.length != 1) { |
78 if (args.length != 2) { |
66 throw new Error("A single class name is expected as argument"); |
79 throw new Error("A single class name and a golden file are expected as argument"); |
67 } |
80 } |
68 final String pattern = args[0] + ".*\\.class"; |
81 String testSrc = System.getProperty("test.src"); |
69 File files[] = classesdir.listFiles(new FileFilter() { |
82 String testName = args[0]; |
|
83 String testGoldenFile = args[1]; |
|
84 final String pattern = testName + ".*\\.class"; |
|
85 File refFile = new File(testSrc, testGoldenFile); |
|
86 File[] files = classesdir.listFiles(new FileFilter() { |
70 public boolean accept(File f) { |
87 public boolean accept(File f) { |
71 return f.getName().matches(pattern); |
88 return f.getName().matches(pattern); |
72 } |
89 } |
73 }); |
90 }); |
74 if (files.length == 0) { |
91 if (files.length == 0) { |
75 File file = new File(classesdir, args[0] + ".class"); |
92 File file = new File(classesdir, testName + ".class"); |
76 throw new Error(file.getPath() + " not found"); |
93 throw new Error(file.getPath() + " not found"); |
77 } |
94 } |
78 |
95 |
79 new Tester(args[0], files).run(); |
96 new Tester(testName, files, refFile).run(); |
80 } |
97 } |
81 |
98 |
82 public Tester(String name, File files[]) { |
99 public Tester(String name, File[] files, File refFile) { |
83 this.classname = name; |
100 this.classname = name; |
84 this.files = files; |
101 this.files = files; |
|
102 this.refFile = refFile; |
85 } |
103 } |
86 |
104 |
87 void run() throws Exception { |
105 void run() throws Exception { |
88 |
106 |
89 // Test with each visitor |
107 // Test with each visitor |
90 for (Class<Visitor> vclass : visitors) { |
108 for (Class<Visitor> vclass : visitors) { |
|
109 boolean compResult = false; |
91 try { |
110 try { |
92 String vname = vclass.getName(); |
111 String vname = vclass.getName(); |
93 Constructor c = vclass.getConstructor(Tester.class); |
112 Constructor c = vclass.getConstructor(Tester.class); |
94 |
113 |
95 info("\nRun " + vname + " for " + classname + "\n"); |
114 info("\nRun " + vname + " for " + classname + "\n"); |
103 } catch(Exception e) { |
122 } catch(Exception e) { |
104 error("Uncaught exception in visitClass()"); |
123 error("Uncaught exception in visitClass()"); |
105 e.printStackTrace(); |
124 e.printStackTrace(); |
106 } |
125 } |
107 } |
126 } |
108 info(sb.toString()); |
127 String output = sb.toString(); |
|
128 info(output); |
|
129 compResult = compareOutput(refFile, output); |
109 } catch(ReflectiveOperationException e) { |
130 } catch(ReflectiveOperationException e) { |
110 warn("Class " + vclass.getName() + " ignored, not a Visitor"); |
131 warn("Class " + vclass.getName() + " ignored, not a Visitor"); |
111 continue; |
132 continue; |
112 } |
133 } |
113 } |
134 if (!compResult) { |
|
135 diffGolden++; |
|
136 error("The output from " + vclass.getName() + " did not match golden file."); |
|
137 } |
|
138 } |
|
139 |
|
140 if (0 != diffGolden) |
|
141 throw new Exception("Test output is not equal with golden file."); |
114 |
142 |
115 if(0 != warnings) |
143 if(0 != warnings) |
116 System.err.println("Test generated " + warnings + " warnings"); |
144 System.err.println("Test generated " + warnings + " warnings"); |
117 |
145 |
118 if(0 != errors) |
146 if(0 != errors) |
119 throw new Exception("Tester test failed with " + |
147 throw new Exception("Tester test failed with " + |
120 errors + " errors"); |
148 errors + " errors"); |
121 } |
149 } |
|
150 // Check if test output matches the golden file. |
|
151 boolean compareOutput(File refFile, String sb) |
|
152 throws FileNotFoundException, IOException { |
|
153 |
|
154 List<String> refFileList = Files.readAllLines(refFile.toPath(), StandardCharsets.UTF_8); |
|
155 List<String> sbList = Arrays.asList(sb.split(System.getProperty("line.separator"))); |
|
156 // Check if test output contains unexpected lines or is missing expected lines. |
|
157 List<String> sbOnly = new ArrayList<String>(sbList); |
|
158 sbOnly.removeAll(refFileList); |
|
159 for (String line: sbOnly) |
|
160 error("unexpected line found: " + line); |
|
161 |
|
162 List<String> refOnly = new ArrayList<String>(refFileList); |
|
163 refOnly.removeAll(sbList); |
|
164 for (String line: refOnly) |
|
165 error("expected line not found: " + line); |
|
166 |
|
167 return sbOnly.isEmpty() && refOnly.isEmpty(); |
|
168 } |
122 |
169 |
123 abstract static class Visitor { |
170 abstract static class Visitor { |
124 Tester tester; |
171 Tester tester; |
125 File classesdir; |
172 File classesdir; |
126 |
173 |