KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > driver > XmlcBasicTest


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: XmlcBasicTest.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.driver;
25
26 import java.io.File JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.enhydra.xml.driver.TestError;
31 import org.enhydra.xml.driver.TestException;
32 import org.enhydra.xml.driver.TestFileOps;
33 import org.enhydra.xml.io.OutputOptions;
34 import org.enhydra.xml.xmlc.XMLObject;
35
36 /**
37  * Class to run a basic set of XMLC tests given a set of parameters.
38  * This class is extended to create HTML- and XML-specified basic
39  * test, and those are extended to implement specific tests.
40  *
41  * <UL>
42  * <LI> Compile a page with the specified set of parameters.
43  * <LI> Compare output of compile dump of DOM to expected
44  * <LI> Load the compiled document class.
45  * <LI> Compare dump of compiled DOM to expected
46  * <LI> Output the document.
47  * <LI> Compiled the new document.
48  * <LI> Compare output of compile dump of DOM to expected.
49  * </UL>
50  *
51  * The following general parameters are use to reuse the specific tests
52  * with different overall configurations of XMLC. These define the tests
53  * ids and output classes used.
54  * <UL>
55  * <LI> parser - The HTML parser to use: tidy or swing.
56  * <LI> dom - The HTML DOM implementation to use.
57  * <LI> autoload - Should auto compilation/loading testing be done?
58  * </UL>
59  */

60 public class XmlcBasicTest {
61     /** Test case we are assocaited with. */
62     private XmlcTestCaseBase fTest;
63
64     /** Source file being compiled */
65     private File JavaDoc fSrcFile;
66     
67     /**
68      * Extra source files (DTDs, etc) that need to be copied to the
69      * same directory as the source file for second compile pass or
70      * for reloading tests. Null, if none are specified,
71      */

72     private ArrayList JavaDoc fExtraSrc;
73
74     /** Doc type extension (e.g. html, xml) */
75     private String JavaDoc fDocFileExt;
76
77     /** Is the compiled step expected to fail? */
78     private boolean fCompileShouldFail;
79
80     /** Option options to use instead of defaults */
81     private OutputOptions fOutputOptions;
82
83     /** Write tracing if not null */
84     private PrintWriter JavaDoc fVerboseOut;
85
86     /** Compile object for first compile of document */
87     private ExecXmlc fComp1Xmlc;
88
89     /** Compile object for compile of generated document */
90     private ExecXmlc fComp2Xmlc;
91
92     /** Encoding that should in in the document */
93     private String JavaDoc fExpectedEncoding;
94
95     /** Should toDocument() be used to write the object */
96     private boolean fUseToDoc;
97
98     /** Should the reloading tests edit the source or just touch it? */
99     private boolean fEditReloadSource = true;
100
101     /** Flag indicated if expected files should be diffed or saved */
102     private boolean fDiffExpected = true;
103
104     /**
105      * Constructor.
106      * @param unqualTestId Id for test
107      */

108     public XmlcBasicTest(XmlcTestCaseBase test,
109                          String JavaDoc docFileExt,
110                          File JavaDoc srcFile) {
111         fTest = test;
112         fDocFileExt = docFileExt;
113         fSrcFile = srcFile;
114         fComp1Xmlc = new ExecXmlc(test);
115         fComp1Xmlc.setSrcFile(fSrcFile);
116
117         fComp2Xmlc = new ExecXmlc(test);
118     }
119
120     /** Get the test case we are associated with */
121     public XmlcTestCaseBase getTestCase() {
122         return fTest;
123     }
124
125     /**
126      * Used to indicate if expected files should be diffed and saved. The
127      * default is yes, but for some tests, the results are so large that it's
128      * not worth the overhead of keeping them.
129      */

130     public void disableExpectedFileDiff() {
131         fDiffExpected = false;
132     }
133
134     /**
135      * Indicate if the edited source should be edited or touched. In some
136      * cases (e.g. a document of all entities), the editing doesn't actually
137      * change anything, causing a check to fail. So we don't edit or do the
138      * check, just touch.
139      */

140     public void setEditReloadSource(boolean val) {
141         fEditReloadSource = val;
142     }
143
144     /**
145      * Add an associated file that need to be in the same directory as
146      * the document to recompile it. These are files such as DTDs, SSIs,
147      * etc. These are also used for reloading tests.
148      */

149     public void addExtraSrcFile(File JavaDoc extraSrcFile) {
150         if (fExtraSrc == null) {
151             fExtraSrc = new ArrayList JavaDoc();
152         }
153         fExtraSrc.add(extraSrcFile);
154         if (fTest.getParams().getReloading()) {
155             fComp1Xmlc.addExtraSrcFile(extraSrcFile);
156         }
157     }
158
159     /**
160      * Set the encoding that should in in the document
161      */

162     public void setExpectedEncoding(String JavaDoc encoding) {
163         fExpectedEncoding = encoding;
164     }
165
166     /**
167      * Get the output options associate with the object. If none exist,
168      * they are created.
169      */

170     public OutputOptions getOutputOptions() {
171         if (fOutputOptions == null) {
172             fOutputOptions = new OutputOptions();
173         }
174         return fOutputOptions;
175     }
176
177     /** Indicate that toDocument() should used to write the object */
178     public void setUseToDocument() {
179         fUseToDoc = true;
180     }
181
182     /** Add an option to the first compile pass */
183     public void addFirst(String JavaDoc opt) {
184         fComp1Xmlc.addOpt(opt);
185     }
186
187     /** Add and option and argument to the first compile pass */
188     public void addFirst(String JavaDoc opt,
189                          String JavaDoc arg) {
190         fComp1Xmlc.addOpt(opt, arg);
191     }
192
193     /** Add an option and two arguments to the first compile pass */
194     public void addFirst(String JavaDoc opt,
195                          String JavaDoc arg1,
196                          String JavaDoc arg2) {
197         fComp1Xmlc.addOpt(opt, arg1, arg2);
198     }
199
200     /** Add an option to the both compile passes */
201     public void addBoth(String JavaDoc opt) {
202         fComp1Xmlc.addOpt(opt);
203         if (fComp2Xmlc != null) {
204             fComp2Xmlc.addOpt(opt);
205         }
206     }
207
208     /** Add and option and argument to the both compile passes */
209     public void addBoth(String JavaDoc opt,
210                         String JavaDoc arg) {
211         fComp1Xmlc.addOpt(opt, arg);
212         if (fComp2Xmlc != null) {
213             fComp2Xmlc.addOpt(opt, arg);
214         }
215     }
216
217     /** Add and option and argument to the both compile passes */
218     public void addBoth(String JavaDoc opt,
219                         File JavaDoc arg) {
220         fComp1Xmlc.addOpt(opt, arg);
221         if (fComp2Xmlc != null) {
222             fComp2Xmlc.addOpt(opt, arg);
223         }
224     }
225
226     /** Add an option and two arguments to the both compile passes */
227     public void addBoth(String JavaDoc opt,
228                         String JavaDoc arg1,
229                         String JavaDoc arg2) {
230         fComp1Xmlc.addOpt(opt, arg1, arg2);
231         if (fComp2Xmlc != null) {
232             fComp2Xmlc.addOpt(opt, arg1, arg2);
233         }
234     }
235
236     /** Specify the -dom or -domfactory option, preventing automatic setting */
237     public void setDomOpt(String JavaDoc opt,
238                           String JavaDoc arg) {
239         fComp1Xmlc.setDomOpt(opt, arg);
240         fComp2Xmlc.setDomOpt(opt, arg);
241     }
242
243     /**
244      * Add an XMLC option or metadata file.
245      */

246     public void addOptionFile(File JavaDoc file) {
247         fComp1Xmlc.addOptionFile(file);
248         if (fComp2Xmlc != null) {
249             fComp2Xmlc.addOptionFile(file);
250         }
251     }
252
253     /**
254      * Set the compile-should-fail flag.
255      */

256     public void setCompileShouldFail() {
257         fCompileShouldFail = true;
258     }
259
260     /**
261      * Build a ExecXmlc object with argument required for the initial compile
262      * and the recompile/dump. This is implemented by the derived class.
263      */

264     protected void addStdOpts(ExecXmlc execXmlc) {
265     }
266
267     /**
268      * Handle the compile failing
269      */

270     private void handleCompileFail(TestException testExcept) {
271         if (fCompileShouldFail) {
272             Throwable JavaDoc cause = testExcept.getCause();
273             // Failure was expected, make sure cause is not a another test
274
// exception or has not cause
275
if ((cause == null) || (cause instanceof TestException)
276                 || (cause instanceof TestError)) {
277                 throw new TestException("compile that should fail, failed in test code",
278                                         testExcept);
279             }
280         } else {
281             // Should not have failed
282
throw testExcept;
283         }
284     }
285
286
287     /**
288      * Do initial compile, creating log file and dump file
289      */

290     public void compileOriginalStep() {
291         fTest.getGenClassFile().delete();
292         addStdOpts(fComp1Xmlc);
293         if (fTest.getParams().getReloading()) {
294             fComp1Xmlc.addOpt(ExecXmlc.OPT_FOR_RECOMP);
295         }
296         fComp1Xmlc.addOpt(ExecXmlc.OPT_DEST_DIR, fTest.getClassRoot());
297         fComp1Xmlc.addOpt(ExecXmlc.OPT_CLASS, fTest.getTestClass());
298         fComp1Xmlc.addOpt(ExecXmlc.OPT_METHODS);
299         fComp1Xmlc.addOpt(ExecXmlc.OPT_DUMP);
300
301         // Compile with checking for expected failure
302
File JavaDoc result = fTest.getResultFile(XmlcTestCaseBase.COMPILE_OUTPUT_EXT);
303         boolean failed = false;
304         try {
305             TestFileOps.ensureFileDir(result);
306             fComp1Xmlc.compile(result);
307         } catch (TestException testExcept) {
308             failed = true;
309             handleCompileFail(testExcept);
310         }
311         if (fCompileShouldFail && !failed) {
312             // Should have failed but didn't
313
throw new TestException("compile should have failed");
314         }
315         if (fDiffExpected) {
316             File JavaDoc expected = fTest.getLoadInvarExpectedFile(XmlcTestCaseBase.COMPILE_OUTPUT_EXT);
317             fTest.getDiffer().diff(expected, result);
318         }
319     }
320
321     /*
322      * Get the path to the file that was generated from outputing the compiled
323      * document.
324      */

325     public File JavaDoc getGenerateFile() {
326         return fTest.getResultFile("gen." + fDocFileExt);
327     }
328
329     /** Get path to file generated by the -docout on the second compile */
330     public File JavaDoc getRegenerateFile() {
331         return fTest.getResultFile("regen." + fDocFileExt);
332     }
333
334     /**
335      * Output the compiled document.
336      */

337     private void outputDocument(XMLObject doc) {
338         if ((fOutputOptions != null) && fUseToDoc) {
339             throw new TestError("bug: both output options and toDocument() are specified");
340         }
341         if (fUseToDoc) {
342             OutputDocument.writeWithToDoc(doc, getGenerateFile());
343         } else {
344             // Uses default options if output options was not set.
345
OutputDocument.write(doc, getGenerateFile(), fOutputOptions);
346         }
347     }
348
349     /**
350      * Parse the generated file using xmlc, dumping the DOM and other
351      * information. Since the generate files are written into directories
352      * that are dependent on the DOM and parser, they can end up with
353      * location-specific paths in the compiler output, causing failures on
354      * diff. To compensate for this, we filter the files with regular
355      * expressions first.
356      */

357     private void parseGenerated() {
358         addStdOpts(fComp2Xmlc);
359         fComp2Xmlc.addOpt(ExecXmlc.OPT_NO_COMPILE);
360         fComp2Xmlc.addOpt(ExecXmlc.OPT_METHODS);
361         fComp2Xmlc.addOpt(ExecXmlc.OPT_DUMP);
362         fComp2Xmlc.addOpt(ExecXmlc.OPT_DOC_OUT, getRegenerateFile().getPath());
363         fComp2Xmlc.setSrcFile(getGenerateFile());
364
365         File JavaDoc out = fTest.getResultFile(XmlcTestCaseBase.PARSE_GEN_OUTPUT_EXT);
366         fComp2Xmlc.compile(out);
367
368         if (fDiffExpected) {
369             File JavaDoc expected = fTest.getExpectedFile(XmlcTestCaseBase.PARSE_GEN_OUTPUT_EXT);
370             fTest.getDiffer().diff(expected, out);
371         }
372     }
373
374     /**
375      * Output the generated class and then parse the source.
376      */

377     public void reparseStep(XMLObject doc) {
378         // output and verify DOM first..
379
if (fDiffExpected) {
380             fTest.dumpVerifyDom(doc, XmlcTestCaseBase.LOAD_DOM_EXT);
381         } else {
382             fTest.dumpDom(doc, XmlcTestCaseBase.LOAD_DOM_EXT);
383         }
384
385         if ((fExpectedEncoding != null)
386             && !fExpectedEncoding.equals(doc.getEncoding())) {
387             throw new TestException("expected document encoding of \""
388                                     + fExpectedEncoding +" \", got \""
389                                     + doc.getEncoding() + "\"");
390         }
391
392         if (fExtraSrc != null) {
393             TestFileOps.copyFilesToDir(fExtraSrc, fTest.getResultsDir());
394         }
395         outputDocument(doc);
396         parseGenerated();
397     }
398
399     /**
400      * Output the genreated class and then parse the source.
401      */

402     public void reparseStep() {
403         reparseStep(fTest.loadTestDocument(fTest.getTestClass()));
404     }
405
406     /**
407      * Execute the standard `basic test'.
408      */

409     public void basicTest() {
410         compileOriginalStep();
411         if (fCompileShouldFail) {
412             return; // should have failed
413
}
414     reparseStep();
415     }
416 }
417
Popular Tags