KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > ozoneDB > xml > dom4j > InsertAndRetrievalTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: InsertAndRetrievalTest.java,v 1.3 2003/06/23 14:41:48 per_nyfelt Exp $
8
package test.ozoneDB.xml.dom4j;
9
10 import junit.framework.TestCase;
11 import org.dom4j.Document;
12 import org.dom4j.Element;
13 import org.ozoneDB.ExternalDatabase;
14 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
15
16 /**
17  * $Id: InsertAndRetrievalTest.java,v 1.3 2003/06/23 14:41:48 per_nyfelt Exp $
18  */

19 public class InsertAndRetrievalTest extends TestCase {
20
21     ExternalDatabase db;
22
23     public InsertAndRetrievalTest(String JavaDoc methodName) {
24         super(methodName);
25     }
26
27     private void init() throws Exception JavaDoc {
28         db = ExternalDatabase.openDatabase("ozonedb:remote://localhost:3333");
29         O3DocumentHelper.configure(db);
30     }
31
32     public void testInsertAndRetrieval() {
33
34         try {
35             init();
36             Document doc = O3DocumentHelper.fetchDocument("testDoc");
37             if (doc != null) {
38                 System.out.println("deleting existing testDoc");
39                 O3DocumentHelper.deleteDocument(doc);
40             }
41             doc = O3DocumentHelper.createDocument("testDoc");
42             assertNotNull("Creating document", doc);
43             //db.nameObject((OzoneRemote) doc, "testDoc");
44
Element root = O3DocumentHelper.createElement("addresses");
45             assertNotNull("Creating adresses element", root);
46             assertEquals("addresses", root.getName());
47             doc.setRootElement(root);
48             assertEquals("adding root element to doc ", root, doc.getRootElement());
49
50             Element el1 = O3DocumentHelper.createElement("address");
51             assertEquals("Creating element address 1","address", el1.getName() );
52             //Attribute att = O3DocumentHelper.createAttribute(null, "name", "Andreas");
53
//el1.add(att);
54
el1.addAttribute("name", "Andreas");
55             assertEquals("Adding name attribute to el1", "Andreas", el1.attributeValue("name"));
56
57             Element town1 = O3DocumentHelper.createElement("town");
58             town1.setText("New York");
59             assertEquals("setting text new york", "New York", town1.getText() );
60             el1.add(town1);
61             root.add(el1);
62
63             //System.out.println("[DocumentHelperTest] - Creating element address 2");
64
Element el2 = O3DocumentHelper.createElement("address");
65             //System.out.println("[DocumentHelperTest] - Adding name attribute to el2");
66
O3DocumentHelper.createAttribute(el2, "name", "Lars");
67             //el2.addAttribute("name","Lars");
68
//System.out.println("[DocumentHelperTest] - creating element town 2");
69
Element town2 = O3DocumentHelper.createElement("town");
70             //System.out.println("[DocumentHelperTest] - setting text to Los Angeles");
71
town2.setText("Los Angeles");
72             //System.out.println("[DocumentHelperTest] - Adding town2 to el2");
73
el2.add(town2);
74             //System.out.println("[DocumentHelperTest] - Adding el2 to root");
75
root.add(el2);
76             assertEquals("Second element should have a town element callled Los Angeles",
77                     ((Element)root.elements().get(1)).element("town").getText(),
78                     "Los Angeles");
79
80             String JavaDoc docXML = doc.asXML();
81             //System.out.println("created document " + docXML);
82
db.close();
83             init();
84             Document doc2 = O3DocumentHelper.fetchDocument("testDoc");
85             String JavaDoc doc2XML = doc2.asXML();
86             //System.out.println("found document " + doc2XML);
87
db.close();
88             init();
89             assertEquals("matching asXML output", docXML, doc2XML);
90             O3DocumentHelper.deleteDocument(doc2);
91             db.close();
92         } catch (Exception JavaDoc e) {
93             e.printStackTrace();
94             fail(e.toString());
95         }
96     }
97
98     public void testChainedBuilding() {
99         try {
100             init();
101             Document document = null;
102             try {
103                 document = O3DocumentHelper.createDocument();
104             } catch (Exception JavaDoc e) {
105                 fail(e.toString());
106             }
107             Element root = document.addElement("root");
108
109             root.addElement("author")
110                     .addAttribute("name", "Toby")
111                     .addAttribute("location", "Germany")
112                     .addText("Tobias Rademacher");
113
114             assertEquals("First attribute should be name, ",
115                          "name",
116                          root.element("author").attribute(0).getQName().getName());
117
118             root.addElement("author")
119                     .addAttribute("name", "James")
120                     .addAttribute("location", "UK")
121                     .addText("James Strachan");
122
123             assertEquals("Author text should be James Strachan, ",
124                          "James Strachan",
125                          ((Element)root.elements().get(1)).getText());
126             String JavaDoc xmlDoc = document.asXML();
127             assertNotNull(xmlDoc);
128             //System.out.println("document created: " + xmlDoc);
129

130             O3DocumentHelper.deleteDocument(document);
131             db.close();
132         } catch (Exception JavaDoc e) {
133             e.printStackTrace();
134             fail(e.toString());
135         }
136     }
137
138 }
139
Popular Tags