Package test.openxml

Source Code of test.openxml.XML2ObjTest

package test.openxml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.ozoneDB.ExternalTransaction;
import org.ozoneDB.xml.util.XMLContainer;
import org.w3c.dom.Document;

/*
* User: per
* Date: Feb 8, 2002
* Time: 4:52:37 PM
* Copyright Nordic Wave Inc, All rights reserved
*/

public class XML2ObjTest extends OpenXmlTestCase {

    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(XML2ObjTest.class);
        return suite;
    }

    public XML2ObjTest(String name) {
        super(name);
    }

    public void testAddAndRemove() {
        insertDocument();
        removeDocument();
    }

    public void testDOMUpdate() {
        insertDocument();
        ExternalTransaction tx = db.newTransaction();
        try {
            XMLContainer container = XMLContainer.forName(db, xmlTestDataFileName);
            assertNotNull(container);
            tx.begin();
            container.storeDOM(null, getTestDocument());
            tx.commit();
            removeDocument();
        } catch (Exception e) {
            try {
                tx.rollback();
            } catch (Exception e1) {
                // leave it for finally
            } finally {
                fail(e.getMessage());
            }
        }
    }

    private Document getTestDocument() {
        try {
            DocumentBuilderFactory builderFactory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
            DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
            return documentBuilder.parse(xmlTestDataFileName);
        } catch (Exception e) {
            fail(e.getMessage());
            return null;
        }
    }

    private void insertDocument() {
        ExternalTransaction tx = db.newTransaction();
        try {
            tx.begin();
            XMLContainer container = XMLContainer.newContainer(db, xmlTestDataFileName);
            container.storeDOM(getTestDocument());
            tx.commit();
        } catch (Exception e) {
            try {
                tx.rollback();
            } catch (Exception e1) {
                // leave it for finally
            } finally {
                fail(e.getMessage());
            }
        }
    }
    private void removeDocument() {
        ExternalTransaction tx = db.newTransaction();
        try {
            tx.begin();
            XMLContainer container = XMLContainer.forName(db, xmlTestDataFileName);
            if (container == null) {
                fail("XML2ObjTest.removeDocument() - No such document " + xmlTestDataFileName);
            }
            container.delete();
            tx.commit();
            assertNull(XMLContainer.forName(db, xmlTestDataFileName));
        } catch (Exception e) {
            try {
                tx.rollback();
            } catch (Exception e1) {
                // leave it for finally
            } finally {
                fail(e.getMessage());
            }
        }
    }
}
TOP

Related Classes of test.openxml.XML2ObjTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.