KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestDOMSource.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.driver;
25 import java.io.File JavaDoc;
26
27 import org.enhydra.xml.driver.TestCaseBase;
28 import org.enhydra.xml.xmlc.XMLCFactory;
29 import org.enhydra.xml.xmlc.XMLCStdFactory;
30 import org.enhydra.xml.xmlc.XMLObject;
31 import org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory;
32
33 //FIXME: turn into cache of compiled objects based on attributes. and file
34

35 /**
36  * Object used to create instances of test documents that are used
37  * in API testing. This will compile a document once for a set of
38  * test parameters. Other requests for instances of the document are
39  * created from the same class. An instance of this object is created per
40  * document and set of parameters and is often shared by multiple tests.
41  */

42 public class TestDOMSource {
43     /** Test case we are assocaited with. */
44     private TestCaseBase fTest;
45
46     /** Object containing test parameters */
47     private XmlcTestParams fParams;
48
49     /** Information about the document */
50     File JavaDoc fInputFile;
51     String JavaDoc fUnqualClassName;
52     Class JavaDoc fImpls;
53
54     /**
55      * Factory use to create instances of the class. Created on first use;
56      * class will only be compiled onces per test case execution. Also the
57      * class name of the document class.
58      */

59     private XMLCFactory fFactory;
60     private String JavaDoc fClassName;
61
62     /**
63      * Constructor.
64      * @params params may differ from test parms.
65      * @param impls Interface to implement, maybe null.
66      */

67     public TestDOMSource(TestCaseBase test,
68                          XmlcTestParams params,
69                          File JavaDoc inputFile,
70                          String JavaDoc unqualClassName,
71                          Class JavaDoc impls) {
72         fTest = test;
73         fParams = params;
74         fInputFile = inputFile;
75         fUnqualClassName = unqualClassName;
76         fClassName = fTest.getTestPackage() + "." + unqualClassName;
77         fImpls = impls;
78     }
79
80     /** Get the input file name */
81     public File JavaDoc getInputFile() {
82         return fInputFile;
83     }
84
85     /*
86      * Compile the source file and setup the factory to create instances of
87      * it.
88      */

89     private void compile() {
90         File JavaDoc outFile = fTest.getResultFile(fUnqualClassName,
91                                            XmlcTestCaseBase.COMPILE_OUTPUT_EXT);
92         File JavaDoc expectFile = fTest.getExpectedFile(fUnqualClassName,
93                                                 XmlcTestCaseBase.COMPILE_OUTPUT_EXT);
94         
95         ExecXmlc execXmlc = new ExecXmlc(fTest, fParams);
96         execXmlc.addOpt(ExecXmlc.OPT_CLASS, fClassName);
97         if (fImpls != null) {
98             execXmlc.addOpt(ExecXmlc.OPT_IMPLEMENTS,
99                             fImpls.getName());
100         }
101         execXmlc.addOpt(ExecXmlc.OPT_DEST_DIR, fTest.getClassRoot());
102         execXmlc.addClassPath(fTest.getClassRoot());
103         execXmlc.setSrcFile(fInputFile);
104         execXmlc.compile(outFile);
105         fTest.getDiffer().diff(expectFile, outFile);
106     }
107
108     /**
109      * Get an instance of the document object, compile if needed.
110      */

111     public synchronized XMLObject create() {
112         if (fFactory == null) {
113             compile();
114
115         if (fParams.getIsDeferredParsing()) {
116         java.io.PrintWriter JavaDoc writer
117             = new java.io.PrintWriter JavaDoc(new java.io.OutputStreamWriter JavaDoc(System.err));
118         org.enhydra.xml.xmlc.StreamXMLCLogger logger
119             = new org.enhydra.xml.xmlc.StreamXMLCLogger(writer, writer, writer);
120
121         fFactory = new XMLCDeferredParsingFactory (null, fTest.createClassLoader(), null); // use reloading loader
122
} else {
123         fFactory = new XMLCStdFactory(fTest.createClassLoader(), null); // use standard loader
124
}
125     }
126     return fFactory.create(fClassName);
127     }
128 }
129
Popular Tags