KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > xmldb > other > ConnectionTest


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
package test.xmldb.other;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.ozoneDB.ExternalDatabase;
13 import org.ozoneDB.OzoneInterface;
14 import org.ozoneDB.xml.core.XMLCollection;
15 import org.ozoneDB.xml.core.XMLCollectionImpl;
16 import org.xmldb.api.base.Collection;
17 import org.xmldb.api.base.Database;
18
19 /**
20  * This tests the Connection and root collection creation,
21  * retrieval and deleting.
22  * @author Per Nyfelt and SMB
23  */

24 public class ConnectionTest extends TestCase {
25
26     ExternalDatabase db = null;
27     Database xmlDB = null;
28     String JavaDoc databaseClass = "org.ozoneDB.xml.cli.DatabaseImpl";
29     String JavaDoc dbURI = "ozonedb:remote://localhost:3333";
30     String JavaDoc collectionName="per";
31
32     // url schema:
33
// xmldb:ozonexml:<database>?<rootcoll>
34
//
35
// <database> can either be a directory in the local filesystem or a remote
36
// ip address (host:port)
37
// <rootcoll> specifies the name of the root collection to load
38
// Example:
39
//Collection rootCol = xmlDB.getCollection( "xmldb:ozonexml:/home/lars/xmltest?root" );
40
String JavaDoc collectionURI = "xmldb:ozonexml://localhost:3333?" + collectionName;
41
42     /**
43      * @param name the name given to the test
44      */

45     public ConnectionTest(String JavaDoc name) {
46         super(name);
47     }
48
49     public static Test suite() {
50         return new TestSuite(ConnectionTest.class);
51     }
52     /*
53     public void testStub() {
54         try {
55
56         } catch (Exception e) {
57             fail( e.getUserName( ) );
58         }
59     }
60     */

61     /**
62      * Test the whole cycle of create, get and delete a collection
63      */

64     public void testCycle() {
65         try {
66             create();
67             get();
68             delete();
69         } catch (Exception JavaDoc e) {
70             fail( e.getMessage( ) );
71         }
72     }
73
74     private void connect() throws Exception JavaDoc {
75         if (db == null || !db.isOpen()) {
76                 // load a database instance
77
xmlDB = (Database)Class.forName(databaseClass).newInstance();
78                 db = ExternalDatabase.openDatabase(dbURI);
79                 db.reloadClasses();
80         }
81     }
82     private void create() throws Exception JavaDoc {
83         connect();
84         assertNotNull(db);
85         // create a new Collection
86
XMLCollection root = (XMLCollection)db.createObject( XMLCollectionImpl.class.getName(), OzoneInterface.Public, collectionName);
87         root.setName(collectionName);
88         db.close();
89         assertTrue(!db.isOpen());
90     }
91     private void delete() throws Exception JavaDoc{
92         connect();
93         assertNotNull(db);
94         db.deleteObject(db.objectForName(collectionName));
95         db.close();
96         assertTrue(!db.isOpen());
97     }
98     private void get() throws Exception JavaDoc {
99         connect();
100         assertNotNull(db);
101         Collection rootCol = xmlDB.getCollection(collectionURI);
102
103         // check the name of the collection
104
assertTrue(rootCol instanceof org.ozoneDB.xml.cli.CollectionImpl);
105         assertEquals(collectionName, rootCol.getName());
106     }
107
108 }
Popular Tags