1 package test.ozoneDB.xml.dom4j; 9 10 import junit.framework.TestCase; 11 import org.dom4j.Document; 12 import org.dom4j.DocumentException; 13 import org.dom4j.Element; 14 import org.dom4j.io.SAXReader; 15 import org.ozoneDB.ExternalDatabase; 16 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 17 18 import java.io.File ; 19 import java.io.FileReader ; 20 import java.net.URL ; 21 22 25 public class LoadFromExternalTest extends TestCase { 26 27 File file; 28 29 final String addressXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 30 "<addresses>" + 31 "<address name=\"Andreas\"><town>New York</town></address>" + 32 "<address name=\"Lars\"><town>Los Angeles</town></address>" + 33 "</addresses>"; 34 35 ExternalDatabase db; 36 37 public LoadFromExternalTest(String methodName) { 38 super(methodName); 39 file = new File ("../../test.xml"); 40 } 41 42 public void testLoadFromExternal() { 43 try { 44 db = ExternalDatabase.openDatabase("ozonedb:remote://localhost:3333"); 45 O3DocumentHelper.configure(db); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 fail(e.toString()); 50 } 51 loadString(); 52 loadFile(); 53 parseFileIntoString(); 54 try { 55 db.close(); 56 } catch (Exception e) { 57 fail(e.toString()); 58 } 59 } 60 61 public void loadString() { 62 try { 63 Document doc; 64 System.out.println("Parsing string"); 65 long start = System.currentTimeMillis(); 66 doc = O3DocumentHelper.parseText(addressXML); 67 long end = System.currentTimeMillis(); 68 System.out.println("[testLoadString] parsing took " + Double.toString((end - start) / 1000) + " seconds"); 69 70 Element root = doc.getRootElement(); 71 assertEquals("Root element is not correct", "addresses", root.getName()); 72 assertEquals("expected root to have 2 children", root.elements().size(), 2); 73 Element address = (Element) root.elements("address").get(0); 74 assertEquals("attribute name is not Andreas", address.attributeValue("name"), "Andreas"); 75 76 O3DocumentHelper.deleteDocument(doc); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 fail(e.toString()); 80 } 81 82 } 83 84 public void loadFile() { 85 try { 86 System.out.println("parsing file from client"); 87 long start = System.currentTimeMillis(); 88 SAXReader xmlReader = O3DocumentHelper.getSAXReader(); 89 Document doc = xmlReader.read(file); 90 long end = System.currentTimeMillis(); 91 System.out.println("[testLoadFile] parsing took " + Double.toString((end - start) / 1000) + " seconds"); 92 O3DocumentHelper.deleteDocument(doc); 94 95 } catch (Exception e) { 96 e.printStackTrace(); 97 fail(e.toString()); 98 } 99 } 100 101 public void parseFileIntoString() { 102 try { 103 System.out.println("parsing file into string"); 104 long start = System.currentTimeMillis(); 105 Document doc = O3DocumentHelper.parse(new FileReader (file)); 106 long end = System.currentTimeMillis(); 107 System.out.println("[testParseFileIntoString] parsing took " + Double.toString((end - start) / 1000) + " seconds"); 108 O3DocumentHelper.deleteDocument(doc); 109 } catch (Exception e) { 110 e.printStackTrace(); 111 fail(e.toString()); 112 } 113 } 114 115 116 131 137 private Document parseWithSAX(URL aURL) throws DocumentException { 138 SAXReader xmlReader = O3DocumentHelper.getSAXReader(); 139 return xmlReader.read(aURL); 140 } 141 142 143 } 144 | Popular Tags |