KickJava   Java API By Example, From Geeks To Geeks.

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


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: DocumentHelperTest.java,v 1.2 2003/07/07 10:30:30 per_nyfelt Exp $
8
package test.ozoneDB.xml.dom4j;
9
10 import junit.framework.TestCase;
11 import org.dom4j.*;
12 import org.ozoneDB.ExternalDatabase;
13 import org.ozoneDB.OzoneRemote;
14 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
15 import org.ozoneDB.xml.dom4j.o3impl.OzoneAttributeImpl;
16
17 /**
18  *
19  * <br> Date: Jul 28, 2002
20  * <br> Copyright Nordic Wave Inc, All rights reserved
21  * @author Per Nyfelt
22  */

23 public class DocumentHelperTest extends TestCase {
24
25     ExternalDatabase db;
26     String JavaDoc dbDir = "testDB";
27     Document doc;
28
29     public DocumentHelperTest(String JavaDoc methodName) {
30         super(methodName);
31         init();
32     }
33
34     private void init() {
35         try {
36 // LocalDatabase db1 = new LocalDatabase();
37
// if (!db1.exists(dbDir)) {
38
// db1.create(dbDir);
39
// }
40
// db = db1;
41
} catch (Exception JavaDoc e) {
42             e.printStackTrace();
43         }
44     }
45
46     public void setUp() {
47         try {
48             // Open the database
49
//db.open(dbDir, OzoneDebugLevel.INFO_STR);
50
db = ExternalDatabase.openDatabase("ozonedb:remote://localhost:3333");
51         } catch (Exception JavaDoc e) {
52             e.printStackTrace();
53         }
54     }
55
56     public void testDocumentHelper() {
57         try {
58
59             O3DocumentHelper.configure(db);
60             doc = O3DocumentHelper.fetchDocument("testDoc");
61             if (doc == null) {
62                 System.out.println("[DocumentHelperTest] - Creating document");
63                 doc = O3DocumentHelper.createDocument();
64                 db.nameObject((OzoneRemote) doc, "testDoc");
65                 System.out.println("[DocumentHelperTest] - Document created: " + doc);
66             }
67             System.out.println("[DocumentHelperTest] - Refetching");
68             doc = O3DocumentHelper.fetchDocument("testDoc");
69             System.out.println("[DocumentHelperTest] - doc is " + doc);
70             printContent();
71
72             //QNameImpl qname = O3DocumentHelper.createQName("test");
73
//Element testElem = O3DocumentHelper.createElement(qname);
74
doc = O3DocumentHelper.createDocument();
75             Element testElem = O3DocumentHelper.createElement("test");
76
77             System.out.println("[DocumentHelperTest] - Created test element " + testElem);
78             assertNotNull("test element is null", testElem);
79             System.out.println("[DocumentHelperTest] - adding content to element");
80             testElem.addText("some text");
81
82             System.out.println("[DocumentHelperTest] - testElem.getName() " + testElem.getName());
83             doc.setRootElement(testElem);
84             System.out.println("[DocumentHelperTest] - testElem " + doc.getRootElement().getName() + " associated with document!");
85             System.out.println("[DocumentHelperTest] - getNamespace = " + doc.getRootElement().getNamespace());
86
87             printContent();
88
89
90             Comment comment = O3DocumentHelper.createComment("Comment text...");
91             testElem.add(comment);
92             System.out.println("Comment added to testElem");
93             printContent();
94
95             doc.addComment("A document comment");
96             System.out.println("Comment added to Document");
97
98             QName name = O3DocumentHelper.createQName("name");
99             System.out.println("Created qname");
100
101             Attribute att = OzoneAttributeImpl.create(db, name, "per");
102             System.out.println("Created attribute 1");
103             testElem.add(att);
104             System.out.println("Added a name attribute");
105
106             Attribute att2 = O3DocumentHelper.createAttribute(testElem, "name2", "per2");
107             System.out.println("Created attribute 2");
108             att2.setValue("jabba");
109             System.out.println("Value set for attribute 2");
110             //testElem.add(att2.detach());
111
//System.out.println("Added a name2 attribute");
112

113             O3DocumentHelper.createAttribute(testElem, "name3", "per3");
114             System.out.println("Created attribute 3");
115             testElem.addAttribute("phone", "09808432");
116             System.out.println("Added a phone attribute");
117
118             printContent();
119
120         } catch (Exception JavaDoc e) {
121             e.printStackTrace();
122             fail(e.toString());
123         }
124     }
125
126     private void printContent() {
127         System.out.println("[DocumentHelperTest] - Here's the content: ");
128         System.out.println(doc.asXML());
129     }
130
131     protected void tearDown() throws Exception JavaDoc {
132         if (db != null) {
133             System.out.println("[DocumentHelperTest] - Deleting object " + doc);
134             O3DocumentHelper.deleteDocument(doc);
135             db.close();
136         }
137     }
138 }
139
Popular Tags