001: package test.openxml;
002:
003: import javax.xml.parsers.DocumentBuilder;
004: import javax.xml.parsers.DocumentBuilderFactory;
005:
006: import junit.framework.Test;
007: import junit.framework.TestSuite;
008: import org.ozoneDB.ExternalTransaction;
009: import org.ozoneDB.xml.util.XMLContainer;
010: import org.w3c.dom.Document;
011:
012: /*
013: * User: per
014: * Date: Feb 8, 2002
015: * Time: 4:52:37 PM
016: * Copyright Nordic Wave Inc, All rights reserved
017: */
018:
019: public class XML2ObjTest extends OpenXmlTestCase {
020:
021: public static Test suite() {
022: TestSuite suite = new TestSuite();
023: suite.addTestSuite(XML2ObjTest.class);
024: return suite;
025: }
026:
027: public XML2ObjTest(String name) {
028: super (name);
029: }
030:
031: public void testAddAndRemove() {
032: insertDocument();
033: removeDocument();
034: }
035:
036: public void testDOMUpdate() {
037: insertDocument();
038: ExternalTransaction tx = db.newTransaction();
039: try {
040: XMLContainer container = XMLContainer.forName(db,
041: xmlTestDataFileName);
042: assertNotNull(container);
043: tx.begin();
044: container.storeDOM(null, getTestDocument());
045: tx.commit();
046: removeDocument();
047: } catch (Exception e) {
048: try {
049: tx.rollback();
050: } catch (Exception e1) {
051: // leave it for finally
052: } finally {
053: fail(e.getMessage());
054: }
055: }
056: }
057:
058: private Document getTestDocument() {
059: try {
060: DocumentBuilderFactory builderFactory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
061: DocumentBuilder documentBuilder = builderFactory
062: .newDocumentBuilder();
063: return documentBuilder.parse(xmlTestDataFileName);
064: } catch (Exception e) {
065: fail(e.getMessage());
066: return null;
067: }
068: }
069:
070: private void insertDocument() {
071: ExternalTransaction tx = db.newTransaction();
072: try {
073: tx.begin();
074: XMLContainer container = XMLContainer.newContainer(db,
075: xmlTestDataFileName);
076: container.storeDOM(getTestDocument());
077: tx.commit();
078: } catch (Exception e) {
079: try {
080: tx.rollback();
081: } catch (Exception e1) {
082: // leave it for finally
083: } finally {
084: fail(e.getMessage());
085: }
086: }
087: }
088:
089: private void removeDocument() {
090: ExternalTransaction tx = db.newTransaction();
091: try {
092: tx.begin();
093: XMLContainer container = XMLContainer.forName(db,
094: xmlTestDataFileName);
095: if (container == null) {
096: fail("XML2ObjTest.removeDocument() - No such document "
097: + xmlTestDataFileName);
098: }
099: container.delete();
100: tx.commit();
101: assertNull(XMLContainer.forName(db, xmlTestDataFileName));
102: } catch (Exception e) {
103: try {
104: tx.rollback();
105: } catch (Exception e1) {
106: // leave it for finally
107: } finally {
108: fail(e.getMessage());
109: }
110: }
111: }
112: }
|