001: /*
002: * The XML:DB Initiative Software License, Version 1.0
003: *
004: *
005: * Copyright (c) 2000-2001 The XML:DB Initiative. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * XML:DB Initiative (http://www.xmldb.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The name "XML:DB Initiative" must not be used to endorse or
028: * promote products derived from this software without prior written
029: * permission. For written permission, please contact info@xmldb.org.
030: *
031: * 5. Products derived from this software may not be called "XML:DB",
032: * nor may "XML:DB" appear in their name, without prior written
033: * permission of the XML:DB Initiative.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the XML:DB Initiative. For more information
051: * on the XML:DB Initiative, please see <http://www.xmldb.org/>.
052: */
053: package test.xmldb;
054:
055: import java.io.*;
056:
057: import junit.framework.*;
058:
059: import org.xmldb.api.*;
060: import org.xmldb.api.base.*;
061: import org.xmldb.api.modules.*;
062:
063: import java.util.Properties;
064:
065: import org.w3c.dom.Node;
066: import org.w3c.dom.Document;
067: import org.w3c.dom.Element;
068: import org.w3c.dom.DOMImplementation;
069: import org.apache.xerces.parsers.DOMParser;
070: import org.xml.sax.InputSource;
071: import org.apache.xml.serialize.OutputFormat;
072: import org.apache.xml.serialize.XMLSerializer;
073: import org.apache.xerces.dom.DOMImplementationImpl;
074:
075: /**
076: * This is the fixture for the other tests
077: */
078: public class XMLDBTestCase extends TestCase {
079: /** the <code>Collection</code> that we use in all tests */
080: protected Collection col;
081:
082: /** File containing the XML to be used for SAX tests*/
083: protected String xmlFileName = "LevelZeroTest.xml";
084:
085: /** a starting <code>Document</code> corresponding to the fileName for DOM tests*/
086: protected Document document;
087:
088: /** the collection used for testing */
089: protected String collectionName;
090:
091: private CollectionStorageHelper collectionStorageHelper;
092:
093: public XMLDBTestCase(String name) {
094: super (name);
095: }
096:
097: public void setUp() throws Exception {
098: System.out
099: .println("\n\n******************** set up ********************");
100:
101: Properties props = loadProps(XMLDBTestSuite.propertiesFileName);
102: String driver = props.getProperty("driverName");
103: collectionName = props.getProperty("collectionName");
104: String collectionURI = props.getProperty("URI")
105: + collectionName;
106: String dbURI = props.getProperty("dbURI");
107:
108: //create and store a new collection in the XML database
109: collectionStorageHelper = new CollectionStorageHelper(dbURI);
110: collectionStorageHelper.createCollection(collectionName);
111:
112: Database database = (Database) Class.forName(driver)
113: .newInstance();
114: // No need to register since this is done automatically when loading the driver
115: //DatabaseManager.registerDatabase(database);
116:
117: // get the root collection
118: col = database.getCollection(collectionURI);
119: assertNotNull(
120: "XMLDBTestCase.setUp() - Collection could not be created",
121: col);
122:
123: document = createXMLFile(xmlFileName);
124: assertNotNull(
125: "XMLDBTestCase.setUp() - failed to create XML file",
126: document);
127: }
128:
129: public void tearDown() {
130: System.out
131: .println("\n******************** tear down ********************");
132: collectionStorageHelper.deleteCollection(collectionName);
133: col.close();
134: assertTrue(
135: "XMLDBTestCase.tearDown() - failed to delete XML file",
136: deleteXMLFile(xmlFileName));
137: }
138:
139: /** convert a <code>Document</code>into a String */
140: protected String toString(Document document) throws Exception {
141: StringWriter writer = new StringWriter();
142: XMLSerializer serializer = new XMLSerializer(writer,
143: new OutputFormat("xml", "UTF-8", true));
144: serializer.serialize(document);
145: writer.flush();
146: return writer.toString();
147: }
148:
149: /** convert a <code>Node</code>into a String */
150: protected String toString(Node node) throws Exception {
151: try {
152: // getOwnerDocument returns null if it is a Document so we check first
153: if (node instanceof Document) {
154: return toString((Document) node);
155: } else {
156: Document doc = node.getOwnerDocument();
157: return toString(doc);
158: }
159: } catch (Exception e) {
160: e.printStackTrace();
161: throw e;
162: }
163: }
164:
165: protected void deleteResource(String id) throws Exception {
166: col.removeResource(col.getResource(id));
167: }
168:
169: /**
170: * Helper method to load the XMLDBTestCase.properties file
171: *
172: * @return the loaded properties
173: */
174: private Properties loadProps(String iniFileName) {
175: Properties defaultProps = new Properties();
176:
177: // load the default parameters for the reference implementation
178: defaultProps.put("driverName",
179: "org.xmldb.api.reference.DatabaseImpl");
180: defaultProps.put("URI", "xmldb:ref:///child1");
181:
182: // TODO: how about userid and password?
183: //defaultProps.put("userId", "foo");
184: //defaultProps.put("passWord", "bar");
185:
186: Properties props = new Properties(defaultProps);
187:
188: // now load the props file
189: try {
190: props.load(new FileInputStream(new File(iniFileName)));
191: } catch (Exception e) {
192: System.out
193: .println("Didn't find props file, using defaults");
194: props.list(System.out);
195: }
196: return props;
197: }
198:
199: protected Document createXMLFile(String fileName) throws Exception {
200: System.out
201: .println("XMLDBTestCase.createXMLFile() - Writing file= "
202: + fileName);
203: FileWriter out = new FileWriter(fileName);
204:
205: DOMImplementation documentCreator = new DOMImplementationImpl();
206: Document doc = documentCreator.createDocument(null,
207: "XMLDBTests", null);
208:
209: Element root = doc.getDocumentElement();
210:
211: Element levelZeroTests = doc.createElement("levelZeroTests");
212: levelZeroTests.setAttribute("complianceLevel", "0");
213: root.appendChild(levelZeroTests);
214:
215: Element testName = doc.createElement("testName");
216: levelZeroTests.appendChild(testName);
217:
218: Node name = doc.createTextNode("testBinary");
219: testName.appendChild(name);
220:
221: testName = doc.createElement("testName");
222: levelZeroTests.appendChild(testName);
223:
224: name = doc.createTextNode("testDOM");
225: testName.appendChild(name);
226:
227: OutputFormat format = new OutputFormat(doc, "UTF-8", true);
228: XMLSerializer serializer = new XMLSerializer(out, format);
229:
230: serializer.serialize(doc);
231: out.close();
232:
233: return doc;
234: }
235:
236: protected boolean deleteXMLFile(String xmlFileName) {
237: File file = new File(xmlFileName);
238: return file.delete();
239: }
240: }
|