1 7 8 package test; 9 10 import org.cyberneko.html.HTMLConfiguration; 11 12 import java.io.*; 13 import java.util.*; 14 15 import org.apache.tools.ant.BuildException; 16 import org.apache.tools.ant.DirectoryScanner; 17 import org.apache.tools.ant.Project; 18 import org.apache.tools.ant.Task; 19 import org.apache.tools.ant.types.FileSet; 20 21 import org.apache.xerces.xni.parser.XMLDocumentFilter; 22 import org.apache.xerces.xni.parser.XMLInputSource; 23 import org.apache.xerces.xni.parser.XMLParserConfiguration; 24 25 33 public class Tester 34 extends Task { 35 36 40 41 protected String fCanonicalDir; 42 43 44 protected String fOutputDir; 45 46 47 protected Vector fFileSets = new Vector(); 48 49 53 54 public void setCanonDir(String canondir) { 55 fCanonicalDir = canondir; 56 } 58 59 public void setOutputDir(String outdir) { 60 fOutputDir = outdir; 61 } 63 64 public void addFileSet(FileSet fileset) { 65 fFileSets.addElement(fileset); 66 } 68 72 73 public void execute() throws BuildException { 74 75 String canonicaldir = fCanonicalDir; 77 if (canonicaldir == null) { 78 canonicaldir = "."; 79 log("Canonical directory not specified. Assuming current directory.", 80 Project.MSG_WARN); 81 } 82 String outputdir = fOutputDir; 83 if (outputdir == null) { 84 outputdir = "."; 85 log("Output directory not specified. Assuming current directory.", 86 Project.MSG_WARN); 87 } 88 if (fFileSets.size() == 0) { 89 throw new BuildException("must specify at least one fileset"); 90 } 91 92 log("Parsing test files and generating output..."); 94 File outdir = new File(outputdir); 95 int size = fFileSets.size(); 96 for (int i = 0; i < size; i++) { 97 FileSet fileset = (FileSet)fFileSets.elementAt(i); 98 DirectoryScanner dirscanner = fileset.getDirectoryScanner(project); 99 File indir = dirscanner.getBasedir(); 100 String [] files = dirscanner.getIncludedFiles(); 101 for (int j = 0; j < files.length; j++) { 102 File infile = new File(indir, files[j]); 103 File outfile = new File(outdir, files[j]); 104 log(" "+outfile, Project.MSG_VERBOSE); 105 OutputStream out = null; 106 try { 107 out = new FileOutputStream(outfile); 109 XMLDocumentFilter[] filters = { new Writer(out) }; 110 111 XMLParserConfiguration parser = new HTMLConfiguration(); 113 114 parser.setProperty("http://cyberneko.org/html/properties/filters", filters); 116 String infilename = infile.toString(); 117 File insettings = new File(infilename+".settings"); 118 if (insettings.exists()) { 119 BufferedReader settings = new BufferedReader(new FileReader(insettings)); 120 String settingline; 121 while ((settingline = settings.readLine()) != null) { 122 StringTokenizer tokenizer = new StringTokenizer(settingline); 123 String type = tokenizer.nextToken(); 124 String id = tokenizer.nextToken(); 125 String value = tokenizer.nextToken(); 126 if (type.equals("feature")) { 127 parser.setFeature(id, value.equals("true")); 128 } 129 else { 130 parser.setProperty(id, value); 131 } 132 } 133 settings.close(); 134 } 135 136 parser.parse(new XMLInputSource(null, infilename, null)); 138 } 139 catch (Exception e) { 140 log(" error parsing input file, "+infile); 141 throw new BuildException(e); 142 } 143 finally { 144 try { 145 out.close(); 146 } 147 catch (Exception e) { 148 log(" error closing output file, "+outfile); 149 throw new BuildException(e); 150 } 151 } 152 } 153 } 154 155 log("Comparing parsed output against canonical output..."); 157 File canondir = new File(canonicaldir); 158 int errors = 0; 159 for (int i = 0; i < size; i++) { 160 FileSet fileset = (FileSet)fFileSets.elementAt(i); 161 DirectoryScanner dirscanner = fileset.getDirectoryScanner(project); 162 File indir = dirscanner.getBasedir(); 163 String [] files = dirscanner.getIncludedFiles(); 164 for (int j = 0; j < files.length; j++) { 165 File canonfile = new File(canondir, files[j]); 166 if (!canonfile.exists()) { 167 errors++; 168 log(" canonical file missing, "+canonfile); 169 continue; 170 } 171 File outfile = new File(outdir, files[j]); 172 if (!outfile.exists()) { 173 errors++; 174 log(" output file missing, "+outfile); 175 continue; 176 } 177 log(" comparing "+canonfile+" and "+outfile, Project.MSG_VERBOSE); 178 try { 179 if (compare(canonfile, outfile)) { 180 errors++; 181 } 182 } 183 catch (IOException e) { 184 errors++; 185 log("i/o error"); 186 } 187 } 188 } 189 190 if (errors > 0) { 192 log("Finished with errors."); 193 throw new BuildException(); 194 } 195 log("Done."); 196 197 } 199 203 204 protected boolean compare(File f1, File f2) throws IOException { 205 BufferedReader i1 = new BufferedReader(new InputStreamReader(new UTF8BOMSkipper(new FileInputStream(f1)), "UTF8")); 206 BufferedReader i2 = new BufferedReader(new InputStreamReader(new FileInputStream(f2), "UTF8")); 207 String l1; 208 String l2; 209 int errors = 0; 210 long n = 0; 211 while ((l1 = i1.readLine()) != null) { 212 n++; 213 if ((l2 = i2.readLine()) == null) { 214 errors++; 215 log(" file lengths don't match ("+f1+")"); 216 break; 217 } 218 if (compare(f1.getName(), n, l1, l2)) { 219 errors++; 220 break; 221 } 222 } 223 if (errors == 0 && (l2 = i2.readLine()) != null) { 224 errors++; 225 log(" file lengths don't match ("+f1+")"); 226 } 227 i1.close(); 228 i2.close(); 229 return errors > 0; 230 } 232 233 protected boolean compare(String f, long n, String s1, String s2) { 234 int l1 = s1.length(); 235 int l2 = s2.length(); 236 boolean error = false; 237 if (l1 < l2) { 238 error = true; 239 log(" "+f+':'+n+" output string too long"); 240 } 241 else if (l1 > l2) { 242 error = true; 243 log(" "+f+':'+n+" output string too short"); 244 } 245 else if (!s1.equals(s2)) { 246 error = true; 247 log(" "+f+':'+n+" strings don't match"); 248 } 249 if (error) { 250 log(" [in: "+s1+']'); 251 log(" [out: "+s2+']'); 252 } 253 return error; 254 } 256 } | Popular Tags |