1.1 --- a/test/tools/javac/MethodParameters/Tester.java Wed Oct 16 16:33:04 2013 -0400 1.2 +++ b/test/tools/javac/MethodParameters/Tester.java Thu Oct 17 13:27:36 2013 +0200 1.3 @@ -23,6 +23,12 @@ 1.4 1.5 import java.io.*; 1.6 import java.lang.reflect.Constructor; 1.7 +import java.nio.charset.StandardCharsets; 1.8 +import java.nio.file.Files; 1.9 +import java.util.ArrayList; 1.10 +import java.util.Arrays; 1.11 +import java.util.Collections; 1.12 +import java.util.List; 1.13 1.14 /** 1.15 * Test driver for MethodParameters testing. 1.16 @@ -44,6 +50,13 @@ 1.17 1.18 final static File classesdir = new File(System.getProperty("test.classes", ".")); 1.19 1.20 + private String classname; 1.21 + private File[] files; 1.22 + private File refFile; 1.23 + private int errors; 1.24 + private int warnings; 1.25 + private int diffGolden; 1.26 + 1.27 /** 1.28 * The visitor classes that does the actual checking are referenced 1.29 * statically, to force compilations, without having to reference 1.30 @@ -62,32 +75,38 @@ 1.31 * Test-driver expect a single classname as argument. 1.32 */ 1.33 public static void main(String... args) throws Exception { 1.34 - if (args.length != 1) { 1.35 - throw new Error("A single class name is expected as argument"); 1.36 + if (args.length != 2) { 1.37 + throw new Error("A single class name and a golden file are expected as argument"); 1.38 } 1.39 - final String pattern = args[0] + ".*\\.class"; 1.40 - File files[] = classesdir.listFiles(new FileFilter() { 1.41 + String testSrc = System.getProperty("test.src"); 1.42 + String testName = args[0]; 1.43 + String testGoldenFile = args[1]; 1.44 + final String pattern = testName + ".*\\.class"; 1.45 + File refFile = new File(testSrc, testGoldenFile); 1.46 + File[] files = classesdir.listFiles(new FileFilter() { 1.47 public boolean accept(File f) { 1.48 return f.getName().matches(pattern); 1.49 } 1.50 }); 1.51 if (files.length == 0) { 1.52 - File file = new File(classesdir, args[0] + ".class"); 1.53 + File file = new File(classesdir, testName + ".class"); 1.54 throw new Error(file.getPath() + " not found"); 1.55 } 1.56 1.57 - new Tester(args[0], files).run(); 1.58 + new Tester(testName, files, refFile).run(); 1.59 } 1.60 1.61 - public Tester(String name, File files[]) { 1.62 + public Tester(String name, File[] files, File refFile) { 1.63 this.classname = name; 1.64 this.files = files; 1.65 + this.refFile = refFile; 1.66 } 1.67 1.68 void run() throws Exception { 1.69 1.70 // Test with each visitor 1.71 for (Class<Visitor> vclass : visitors) { 1.72 + boolean compResult = false; 1.73 try { 1.74 String vname = vclass.getName(); 1.75 Constructor c = vclass.getConstructor(Tester.class); 1.76 @@ -105,12 +124,21 @@ 1.77 e.printStackTrace(); 1.78 } 1.79 } 1.80 - info(sb.toString()); 1.81 + String output = sb.toString(); 1.82 + info(output); 1.83 + compResult = compareOutput(refFile, output); 1.84 } catch(ReflectiveOperationException e) { 1.85 warn("Class " + vclass.getName() + " ignored, not a Visitor"); 1.86 continue; 1.87 } 1.88 + if (!compResult) { 1.89 + diffGolden++; 1.90 + error("The output from " + vclass.getName() + " did not match golden file."); 1.91 } 1.92 + } 1.93 + 1.94 + if (0 != diffGolden) 1.95 + throw new Exception("Test output is not equal with golden file."); 1.96 1.97 if(0 != warnings) 1.98 System.err.println("Test generated " + warnings + " warnings"); 1.99 @@ -119,6 +147,25 @@ 1.100 throw new Exception("Tester test failed with " + 1.101 errors + " errors"); 1.102 } 1.103 + // Check if test output matches the golden file. 1.104 + boolean compareOutput(File refFile, String sb) 1.105 + throws FileNotFoundException, IOException { 1.106 + 1.107 + List<String> refFileList = Files.readAllLines(refFile.toPath(), StandardCharsets.UTF_8); 1.108 + List<String> sbList = Arrays.asList(sb.split(System.getProperty("line.separator"))); 1.109 + // Check if test output contains unexpected lines or is missing expected lines. 1.110 + List<String> sbOnly = new ArrayList<String>(sbList); 1.111 + sbOnly.removeAll(refFileList); 1.112 + for (String line: sbOnly) 1.113 + error("unexpected line found: " + line); 1.114 + 1.115 + List<String> refOnly = new ArrayList<String>(refFileList); 1.116 + refOnly.removeAll(sbList); 1.117 + for (String line: refOnly) 1.118 + error("expected line not found: " + line); 1.119 + 1.120 + return sbOnly.isEmpty() && refOnly.isEmpty(); 1.121 + } 1.122 1.123 abstract static class Visitor { 1.124 Tester tester; 1.125 @@ -153,9 +200,4 @@ 1.126 void info(String msg) { 1.127 System.out.println(msg); 1.128 } 1.129 - 1.130 - int errors; 1.131 - int warnings; 1.132 - String classname; 1.133 - File files[]; 1.134 }