001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007:
008: package test.xmldb.levelzero;
009:
010: import test.xmldb.*;
011: import junit.framework.*;
012:
013: import org.xmldb.api.modules.XMLResource;
014:
015: import org.w3c.dom.Node;
016: import org.w3c.dom.NodeList;
017: import org.w3c.dom.Document;
018: import org.w3c.dom.Element;
019: import org.w3c.dom.Text;
020:
021: /**
022: * @author Per Nyfelt
023: */
024: public class DOMTest extends XMLDBTestCase implements
025: LevelZeroTestConstants {
026:
027: /** Creates new DOMTest */
028: public DOMTest(String name) {
029: super (name);
030: }
031:
032: public static Test suite() {
033: return new TestSuite(DOMTest.class);
034: }
035:
036: /**
037: * test all scenarios for using XML as DOM
038: */
039: public void testDOM() {
040: try {
041: System.out.println("\nLevelZeroTest.testDOM() - started\n");
042: insertDOMDocument(id, super .document);
043: Node node = retrieveDOMNode(id);
044: super .assertNotNull("LevelZeroTest.testDOM()", node);
045:
046: insertDOMDocument(id2, document);
047: Node node2 = retrieveDOMNode(id2);
048: super .assertNotNull("LevelZeroTest.testDOM()", node2);
049:
050: // DOM level 2, npot supported by Ozone yet
051: //super.assertEquals("LevelZeroTest.testDOM()", node.getLocalName(), node2.getLocalName());
052:
053: System.out
054: .println("DOMTest.testDOM() - updateDOMDocument() is uncommented!");
055: //updateDOMDocument(id);
056: node = retrieveDOMNode(id);
057: super .assertNotNull("LevelZeroTest.testDOM()", node);
058:
059: } catch (Exception e) {
060: fail(e.getMessage());
061: }
062: }
063:
064: private void insertDOMDocument(String id, Document document)
065: throws Exception {
066:
067: XMLResource resource = (XMLResource) col.createResource(id,
068: XMLResource.RESOURCE_TYPE);
069:
070: resource.setContentAsDOM(document);
071: col.storeResource(resource);
072:
073: }
074:
075: private void updateDOMDocument(String id) throws Exception {
076: try {
077: XMLResource resource = (XMLResource) col.getResource(id);
078:
079: Document document = (Document) resource.getContentAsDOM();
080: System.out.println("LevelZeroTest.updateDOMDocument() - "
081: + document);
082: assertNotNull("LevelZeroTest.updateDOMDocument()", document);
083: Element root = document.getDocumentElement();
084: // Change document by appending to an element data
085:
086: Text nameNode = document.createTextNode("updateAddition");
087: NodeList list = root.getElementsByTagName("testName");
088: Node parent = list.item(0);
089: parent.appendChild(nameNode);
090:
091: // insert a new element
092: list = root.getElementsByTagName("levelZeroTests");
093: Node levelZeroTests = list.item(0);
094: Element testName = document.createElement("testName");
095: levelZeroTests.appendChild(testName);
096: Node name = document.createTextNode("testSAX");
097: testName.appendChild(name);
098:
099: resource.setContentAsDOM(document);
100: col.storeResource(resource);
101: super .assertEquals(document, (Document) resource
102: .getContentAsDOM());
103: } catch (Exception e) {
104: e.printStackTrace();
105: throw e;
106: }
107: }
108:
109: private Node retrieveDOMNode(String id) throws Exception {
110: XMLResource resource = (XMLResource) col.getResource(id);
111:
112: Node node = (Node) resource.getContentAsDOM();
113: return node;
114:
115: }
116:
117: private void retrieveDOMDocument(String id) throws Exception {
118: XMLResource resource = (XMLResource) col.getResource(id);
119:
120: Document doc = (Document) resource.getContentAsDOM();
121: }
122:
123: }
|