KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > Tester


1 /*
2  * (C) Copyright 2002-2005, Andy Clark. All rights reserved.
3  *
4  * This file is distributed under an Apache style license. Please
5  * refer to the LICENSE file for specific details.
6  */

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 /**
26  * A simple regression tester written as an Ant task. This task
27  * generates canonical output using the <code>Writer</code> class
28  * and compares it against the expected canonical output. Simple
29  * as that.
30  *
31  * @author Andy Clark
32  */

33 public class Tester
34     extends Task {
35
36     //
37
// Data
38
//
39

40     /** Canonical test directory. */
41     protected String JavaDoc fCanonicalDir;
42
43     /** Output directory for generated files. */
44     protected String JavaDoc fOutputDir;
45
46     /** List of test filesets. */
47     protected Vector fFileSets = new Vector();
48
49     //
50
// Public methods
51
//
52

53     /** Sets the canonical test directory. */
54     public void setCanonDir(String JavaDoc canondir) {
55         fCanonicalDir = canondir;
56     } // setCanonDir(String)
57

58     /** Sets the output directory for generated files. */
59     public void setOutputDir(String JavaDoc outdir) {
60         fOutputDir = outdir;
61     } // setOutputDir(String)
62

63     /** Adds a fileset to the list of test filesets. */
64     public void addFileSet(FileSet fileset) {
65         fFileSets.addElement(fileset);
66     } // addFileSet(FileSet)
67

68     //
69
// Task methods
70
//
71

72     /** Performs the test. */
73     public void execute() throws BuildException {
74
75         // check params
76
String JavaDoc canonicaldir = fCanonicalDir;
77         if (canonicaldir == null) {
78             canonicaldir = ".";
79             log("Canonical directory not specified. Assuming current directory.",
80                 Project.MSG_WARN);
81         }
82         String JavaDoc 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         // parse input files and produce output files
93
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 JavaDoc[] 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                     // create filters
108
out = new FileOutputStream(outfile);
109                     XMLDocumentFilter[] filters = { new Writer(out) };
110                     
111                     // create parser
112
XMLParserConfiguration parser = new HTMLConfiguration();
113
114                     // parser settings
115
parser.setProperty("http://cyberneko.org/html/properties/filters", filters);
116                     String JavaDoc infilename = infile.toString();
117                     File insettings = new File(infilename+".settings");
118                     if (insettings.exists()) {
119                         BufferedReader settings = new BufferedReader(new FileReader(insettings));
120                         String JavaDoc settingline;
121                         while ((settingline = settings.readLine()) != null) {
122                             StringTokenizer tokenizer = new StringTokenizer(settingline);
123                             String JavaDoc type = tokenizer.nextToken();
124                             String JavaDoc id = tokenizer.nextToken();
125                             String JavaDoc 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                     // parse
137
parser.parse(new XMLInputSource(null, infilename, null));
138                 }
139                 catch (Exception JavaDoc 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 JavaDoc e) {
148                         log(" error closing output file, "+outfile);
149                         throw new BuildException(e);
150                     }
151                 }
152             }
153         }
154
155         // compare against canonical output
156
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 JavaDoc[] 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         // finished
191
if (errors > 0) {
192             log("Finished with errors.");
193             throw new BuildException();
194         }
195         log("Done.");
196
197     } // execute()
198

199     //
200
// Protected methods
201
//
202

203     /** Compares two files. */
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 JavaDoc l1;
208         String JavaDoc 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     } // compare(File,File):boolean
231

232     /** Compares two strings. */
233     protected boolean compare(String JavaDoc f, long n, String JavaDoc s1, String JavaDoc 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     } // compare(String,long,String,String):boolean
255

256 } // class Tester
257
Popular Tags