1 package test.openxml; 2 3 import javax.xml.parsers.DocumentBuilder ; 4 import javax.xml.parsers.DocumentBuilderFactory ; 5 6 import junit.framework.Test; 7 import junit.framework.TestSuite; 8 import org.ozoneDB.ExternalTransaction; 9 import org.ozoneDB.xml.util.XMLContainer; 10 import org.w3c.dom.Document ; 11 12 18 19 public class XML2ObjTest extends OpenXmlTestCase { 20 21 public static Test suite() { 22 TestSuite suite = new TestSuite(); 23 suite.addTestSuite(XML2ObjTest.class); 24 return suite; 25 } 26 27 public XML2ObjTest(String name) { 28 super(name); 29 } 30 31 public void testAddAndRemove() { 32 insertDocument(); 33 removeDocument(); 34 } 35 36 public void testDOMUpdate() { 37 insertDocument(); 38 ExternalTransaction tx = db.newTransaction(); 39 try { 40 XMLContainer container = XMLContainer.forName(db, xmlTestDataFileName); 41 assertNotNull(container); 42 tx.begin(); 43 container.storeDOM(null, getTestDocument()); 44 tx.commit(); 45 removeDocument(); 46 } catch (Exception e) { 47 try { 48 tx.rollback(); 49 } catch (Exception e1) { 50 } finally { 52 fail(e.getMessage()); 53 } 54 } 55 } 56 57 private Document getTestDocument() { 58 try { 59 DocumentBuilderFactory builderFactory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl(); 60 DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); 61 return documentBuilder.parse(xmlTestDataFileName); 62 } catch (Exception e) { 63 fail(e.getMessage()); 64 return null; 65 } 66 } 67 68 private void insertDocument() { 69 ExternalTransaction tx = db.newTransaction(); 70 try { 71 tx.begin(); 72 XMLContainer xmlContainer = XMLContainer.forName(db, xmlTestDataFileName); 73 if (xmlContainer != null) { 74 xmlContainer.delete(); 75 } 76 xmlContainer = XMLContainer.newContainer(db, xmlTestDataFileName); 77 xmlContainer.storeDOM(getTestDocument()); 78 assertNotNull(XMLContainer.forName(db, xmlTestDataFileName)); 79 tx.commit(); 80 } catch (Exception e) { 81 try { 82 tx.rollback(); 83 } catch (Exception e1) { 84 } finally { 86 fail(e.getMessage()); 87 } 88 } 89 } 90 91 private void removeDocument() { 92 ExternalTransaction tx = db.newTransaction(); 93 try { 94 tx.begin(); 95 XMLContainer container = XMLContainer.forName(db, xmlTestDataFileName); 96 assertNotNull("XML2ObjTest.removeDocument() - No such document " + xmlTestDataFileName, container); 97 container.delete(); 98 tx.commit(); 99 assertNull(XMLContainer.forName(db, xmlTestDataFileName)); 100 } catch (Exception e) { 101 try { 102 tx.rollback(); 103 } catch (Exception e1) { 104 } finally { 106 fail(e.getMessage()); 107 } 108 } 109 } 110 } 111 | Popular Tags |