KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > xmldb > XMLDBTestCase


1 /*
2  * The XML:DB Initiative Software License, Version 1.0
3  *
4  *
5  * Copyright (c) 2000-2001 The XML:DB Initiative. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * XML:DB Initiative (http://www.xmldb.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The name "XML:DB Initiative" must not be used to endorse or
28  * promote products derived from this software without prior written
29  * permission. For written permission, please contact info@xmldb.org.
30  *
31  * 5. Products derived from this software may not be called "XML:DB",
32  * nor may "XML:DB" appear in their name, without prior written
33  * permission of the XML:DB Initiative.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the XML:DB Initiative. For more information
51  * on the XML:DB Initiative, please see <http://www.xmldb.org/>.
52  */

53 package test.xmldb;
54
55 import java.io.*;
56
57 import junit.framework.*;
58
59 import org.xmldb.api.base.*;
60
61 import java.util.Properties JavaDoc;
62 import java.net.URL JavaDoc;
63
64 import org.w3c.dom.Node JavaDoc;
65 import org.w3c.dom.Document JavaDoc;
66 import org.w3c.dom.Element JavaDoc;
67 import org.w3c.dom.DOMImplementation JavaDoc;
68 import org.apache.xml.serialize.OutputFormat;
69 import org.apache.xml.serialize.XMLSerializer;
70 import org.apache.xerces.dom.DOMImplementationImpl;
71 import org.apache.log4j.Logger;
72
73
74 /**
75  * This is the fixture for the other tests
76  */

77 public class XMLDBTestCase extends TestCase {
78     /** the <code>Collection</code> that we use in all tests */
79     protected Collection col;
80
81     /** File containing the XML to be used for SAX tests*/
82     protected String JavaDoc xmlFileName = "LevelZeroTest.xml";
83
84     /** a starting <code>Document</code> corresponding to the fileName for DOM tests*/
85     protected Document JavaDoc document;
86
87     /** the collection used for testing */
88     protected String JavaDoc collectionName;
89
90     private CollectionStorageHelper collectionStorageHelper;
91
92     private Logger logger = Logger.getLogger(XMLDBTestCase.class);
93
94     public XMLDBTestCase(String JavaDoc name) {
95         super(name);
96     }
97
98     public void setUp() throws Exception JavaDoc {
99         logger.debug("******************** set up ********************");
100         Properties JavaDoc props;
101         if (XMLDBTestSuite.propertiesFileName == null) {
102             // Load the props file from the jar
103
String JavaDoc defaultPropsFileLocation = "test/xmldb/XMLDBTestSuite.properties";
104             URL JavaDoc url = this.getClass().getClassLoader().getResource(defaultPropsFileLocation);
105             if (url == null) {
106                 throw new Exception JavaDoc("failed to find default props file at " + defaultPropsFileLocation);
107             }
108             props = loadProps(url.openConnection().getInputStream());
109
110         } else {
111             props = loadProps(XMLDBTestSuite.propertiesFileName);
112         }
113         String JavaDoc driver = props.getProperty("driverName");
114         collectionName = props.getProperty("collectionName");
115         String JavaDoc collectionURI = props.getProperty("URI") + collectionName;
116         String JavaDoc dbURI = props.getProperty("dbURI");
117
118         //create and store a new collection in the XML database
119
collectionStorageHelper = new CollectionStorageHelper(dbURI);
120         collectionStorageHelper.createCollection(collectionName);
121
122         Database database = (Database) Class.forName(driver).newInstance();
123         // No need to register since this is done automatically when loading the driver
124
//DatabaseManager.registerDatabase(database);
125

126         // get the root collection
127
col = database.getCollection(collectionURI);
128         assertNotNull("XMLDBTestCase.setUp() - Collection could not be created", col);
129
130         document = createXMLFile(xmlFileName);
131         assertNotNull("XMLDBTestCase.setUp() - failed to create XML file", document);
132     }
133
134     public void tearDown() {
135         logger.debug("******************** tear down ********************");
136         collectionStorageHelper.deleteCollection(collectionName);
137         col.close();
138         assertTrue("XMLDBTestCase.tearDown() - failed to delete XML file", deleteXMLFile(xmlFileName));
139     }
140
141     /** convert a <code>Document</code>into a String */
142     protected String JavaDoc toString(Document JavaDoc document) throws Exception JavaDoc {
143         StringWriter writer = new StringWriter();
144         XMLSerializer serializer = new XMLSerializer(writer, new OutputFormat("xml", "UTF-8", true));
145         serializer.serialize(document);
146         writer.flush();
147         return writer.toString();
148     }
149
150     /** convert a <code>Node</code>into a String */
151     protected String JavaDoc toString(Node JavaDoc node) throws Exception JavaDoc {
152         try {
153             // getOwnerDocument returns null if it is a Document so we check first
154
if (node instanceof Document JavaDoc) {
155                 return toString((Document JavaDoc) node);
156             } else {
157                 Document JavaDoc doc = node.getOwnerDocument();
158                 return toString(doc);
159             }
160         } catch (Exception JavaDoc e) {
161             e.printStackTrace();
162             throw e;
163         }
164     }
165
166     protected void deleteResource(String JavaDoc id) throws Exception JavaDoc {
167         col.removeResource(col.getResource(id));
168     }
169
170     private Properties JavaDoc loadProps(InputStream in) {
171         Properties JavaDoc props = createDefaultProps();
172         try {
173             props.load(in);
174         } catch (Exception JavaDoc e) {
175             logger.warn("Didn't find props file, using defaults");
176             props.list(System.out);
177         }
178         return props;
179
180     }
181
182     private Properties JavaDoc loadProps(String JavaDoc iniFileName) {
183         Properties JavaDoc props = createDefaultProps();
184         // now load the props file
185
try {
186             props.load(new FileInputStream(new File(iniFileName)));
187         } catch (Exception JavaDoc e) {
188             logger.warn("Didn't find props file, using defaults");
189             props.list(System.out);
190         }
191         return props;
192     }
193
194     /**
195      * Helper method to load the XMLDBTestCase.properties file
196      *
197      * @return the loaded properties
198      */

199     private Properties JavaDoc createDefaultProps() {
200         Properties JavaDoc defaultProps = new Properties JavaDoc();
201
202         // load the default parameters for the reference implementation
203
defaultProps.put("driverName", "org.xmldb.api.reference.DatabaseImpl");
204         defaultProps.put("URI", "xmldb:ref:///child1");
205
206         // TODO: how about userid and password?
207
//defaultProps.put("userId", "foo");
208
//defaultProps.put("passWord", "bar");
209

210         Properties JavaDoc props = new Properties JavaDoc(defaultProps);
211         return props;
212     }
213
214     protected Document JavaDoc createXMLFile(String JavaDoc fileName) throws Exception JavaDoc {
215         logger.debug("XMLDBTestCase.createXMLFile() - Writing file= " + fileName);
216         FileWriter out = new FileWriter(fileName);
217
218         DOMImplementation JavaDoc documentCreator = new DOMImplementationImpl();
219         Document JavaDoc doc = documentCreator.createDocument(null, "XMLDBTests", null);
220
221         Element JavaDoc root = doc.getDocumentElement();
222
223         Element JavaDoc levelZeroTests = doc.createElement("levelZeroTests");
224         levelZeroTests.setAttribute("complianceLevel", "0");
225         root.appendChild(levelZeroTests);
226
227         Element JavaDoc testName = doc.createElement("testName");
228         levelZeroTests.appendChild(testName);
229
230         Node JavaDoc name = doc.createTextNode("testBinary");
231         testName.appendChild(name);
232
233         testName = doc.createElement("testName");
234         levelZeroTests.appendChild(testName);
235
236         name = doc.createTextNode("testDOM");
237         testName.appendChild(name);
238
239         OutputFormat format = new OutputFormat(doc, "UTF-8", true);
240         XMLSerializer serializer = new XMLSerializer(out, format);
241
242         serializer.serialize(doc);
243         out.close();
244
245         return doc;
246     }
247
248     protected boolean deleteXMLFile(String JavaDoc xmlFileName) {
249         File file = new File(xmlFileName);
250         return file.delete();
251     }
252 }
253
Popular Tags