KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > xml > ExtendsTests


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

23
24 package org.enhydra.xml.xmlc.xml;
25 import java.io.File JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27
28 import junit.framework.Test;
29
30 import org.enhydra.xml.xmlc.XMLObject;
31 import org.enhydra.xml.xmlc.driver.ExecXmlc;
32
33 /**
34  * Tests of generating classes where super class or interfaces are specified.
35  * Also tests of -domfactory
36  */

37 public class ExtendsTests extends XmlTestCaseBase {
38     /** Interfaces a generated interface must be an instanceof */
39     private final String JavaDoc[] INTERFACE_INSTANCEOF = {
40     "org.enhydra.xml.xmlc.XMLObject",
41     "org.w3c.dom.Document"
42     };
43
44     /** Interfaces a generated interface must not be an instanceof */
45     private final String JavaDoc[] INTERFACE_NOT_INSTANCEOF = {
46     "org.enhydra.xml.xmlc.XMLObjectImpl",
47     };
48
49     /** Interfaces/classes a generated class must be an instanceof */
50     private final String JavaDoc[] IMPLEMENTATION_INSTANCEOF = {
51     "org.enhydra.xml.xmlc.XMLObject",
52     "org.enhydra.xml.xmlc.XMLObjectImpl",
53     "org.w3c.dom.Document"
54     };
55
56     /** Factory method to create suite of these tests */
57     public static Test suite() {
58         return createSuite(ExtendsTests.class, null);
59     }
60     
61     /** Constructor */
62     public ExtendsTests(Method JavaDoc method) {
63         super(method);
64     }
65
66     /**
67      * Do standard verification for an generate class.
68      */

69     private void verifyClass(XMLObject doc,
70                              Class JavaDoc xtraImpl) {
71         verifyIsClass(doc.getClass());
72         verifyInstanceOf(doc.getClass(),
73                               IMPLEMENTATION_INSTANCEOF);
74         if (xtraImpl != null) {
75             verifyInstanceOf(doc.getClass(), xtraImpl);
76         }
77     }
78
79     /**
80      * Do standard verification for an generate interface.
81      */

82     private void verifyInterface(Class JavaDoc docClass,
83                                  Class JavaDoc xtraImpl) {
84         verifyIsInterface(docClass);
85         verifyInstanceOf(docClass,
86                          INTERFACE_INSTANCEOF);
87         verifyNotInstanceOf(docClass,
88                             INTERFACE_NOT_INSTANCEOF);
89         if (xtraImpl != null) {
90             verifyInstanceOf(docClass, xtraImpl);
91         }
92     }
93
94     /**
95      * Craete the test class used in most of these tests
96      */

97     private XmlBasicTest createTest() {
98         return new XmlBasicTest(this, getInputFile("wml/Login.wml"),
99                                 getInputFile("wml/wml_1.1.test.xml"));
100     }
101
102     /*
103      * Test 1: -implements
104      */

105     public void test1() {
106         XmlBasicTest test = createTest();
107         test.addFirst(ExecXmlc.OPT_IMPLEMENTS, WMLLogin.class.getName());
108         
109         Class JavaDoc expectDocClass;
110         if (fParams.getDom().equals(ExecXmlc.XERCES_DOM)) {
111             test.setDomOpt(ExecXmlc.OPT_DOM_FACTORY,
112                            TestXercesDOMFactory.class.getName());
113             expectDocClass = TestXercesDOMDocument.class;
114         } else {
115             test.setDomOpt(ExecXmlc.OPT_DOM_FACTORY,
116                            TestLazyDOMFactory.class.getName());
117             expectDocClass = TestLazyDOMDocument.class;
118         }
119         test.basicTest();
120
121         XMLObject doc = loadTestDocument();
122         verifyClass(doc, WMLLogin.class);
123         verifyInstanceOf(doc.getDocument().getClass(),
124                          expectDocClass);
125     }
126
127     /*
128      * Test 2: -extends
129      */

130     public void test2() {
131         XmlBasicTest test = createTest();
132         test.addFirst(ExecXmlc.OPT_EXTENDS, XMLObjectBase.class.getName());
133         
134         Class JavaDoc expectDocClass;
135         if (fParams.getDom().equals(ExecXmlc.XERCES_DOM)) {
136             test.setDomOpt(ExecXmlc.OPT_DOM_FACTORY,
137                            TestXercesDOMFactory.class.getName());
138             expectDocClass = TestXercesDOMDocument.class;
139         } else {
140             test.setDomOpt(ExecXmlc.OPT_DOM_FACTORY,
141                            TestLazyDOMFactory.class.getName());
142             expectDocClass = TestLazyDOMDocument.class;
143         }
144         test.basicTest();
145
146         XMLObject doc = loadTestDocument();
147         verifyClass(doc, XMLObjectBase.class);
148         verifyInstanceOf(doc.getDocument().getClass(),
149                          expectDocClass);
150     }
151
152     /*
153      * Test 3: generating an interfaces and an implementation, and
154      * implementing an interface.
155      */

156     public void test3() {
157         XmlBasicTest test = createTest();
158         setUsingImplInterface();
159         test.addFirst(ExecXmlc.OPT_GENERATE, ExecXmlc.GENERATE_BOTH);
160         test.addFirst(ExecXmlc.OPT_IMPLEMENTS,
161                       WMLLogin.class.getName());
162         test.basicTest();
163         
164         verifyInterface(loadDocClassInterface(), WMLLogin.class);
165         verifyClass(loadTestDocument(), WMLLogin.class);
166     }
167
168     /*
169      * Test 4: generating an interfaces and an implementation, extending
170      * a base class
171      */

172     public void test4() {
173         XmlBasicTest test = createTest();
174         setUsingImplInterface();
175         test.addFirst(ExecXmlc.OPT_GENERATE, ExecXmlc.GENERATE_BOTH);
176         test.addFirst(ExecXmlc.OPT_EXTENDS,
177                       XMLObjectBase.class.getName());
178         test.basicTest();
179         
180         verifyInterface(loadDocClassInterface(), null);
181         verifyClass(loadTestDocument(), XMLObjectBase.class);
182     }
183
184     /*
185      * Test 5: - not sure what was special about this test
186      */

187     public void test5() {
188         XmlBasicTest test = createTest();
189         setUsingImplInterface();
190         test.addFirst(ExecXmlc.OPT_EXTENDS,
191                       XMLObjectBase.class.getName());
192         test.addFirst(ExecXmlc.OPT_GENERATE, ExecXmlc.GENERATE_BOTH);
193         test.basicTest();
194         verifyInterface(loadDocClassInterface(), null);
195         verifyClass(loadTestDocument(), XMLObjectBase.class);
196     }
197
198     /*
199      * Test 6: generate an interface and an implementation seperately
200      */

201     public void test6() {
202         XmlBasicTest test
203             = new XmlBasicTest(this, getInputFile("bioml/insulin3.bioml"),
204                                getInputFile("bioml/bioml.dtd"));
205         setUsingImplInterface();
206         
207         // Compile the interface
208
test.addFirst(ExecXmlc.OPT_GENERATE, ExecXmlc.GENERATE_INTERFACE);
209         test.compileOriginalStep();
210
211         // now, compile the implementation
212
ExecXmlc execXmlc = new ExecXmlc(this);
213         execXmlc.addOpt(ExecXmlc.OPT_GENERATE,
214                         ExecXmlc.GENERATE_IMPLEMENTATION);
215         execXmlc.addOpt(ExecXmlc.OPT_CLASS, getTestClass());
216         execXmlc.addOpt(ExecXmlc.OPT_DEST_DIR, getClassRoot());
217         execXmlc.addClassPath(getClassRoot());
218         execXmlc.setSrcFile(getInputFile("bioml/insulin3.bioml"));
219
220         File JavaDoc outFile2 = getResultFile("compile2.out");
221         File JavaDoc expectFile2 = getExpectedFile("compile2.out");
222         execXmlc.compile(outFile2);
223         getDiffer().diff(expectFile2, outFile2);
224         
225         // Verify interface and implementation. The test object used to
226
// compile the interface is used to load both so that we get the
227
// same classloader.
228
Class JavaDoc faceClass = loadDocClassInterface();
229         verifyInterface(faceClass, null);
230         verifyClass(loadTestDocument(faceClass), faceClass);
231
232         // finish test now that interface is compiled
233
test.reparseStep();
234     }
235 }
236
Popular Tags