001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: package test.xmldb.other;
008:
009: import junit.framework.*;
010: import org.xmldb.api.DatabaseManager;
011: import org.xmldb.api.base.Collection;
012: import org.xmldb.api.base.Database;
013: import org.ozoneDB.ExternalDatabase;
014: import org.ozoneDB.OzoneInterface;
015: import org.ozoneDB.xml.core.XMLCollectionImpl;
016: import org.ozoneDB.xml.core.XMLCollection;
017:
018: /**
019: * This tests the Connection and root collection creation,
020: * retrieval and deleting.
021: * @author Per Nyfelt and SMB
022: */
023: public class ConnectionTest extends TestCase {
024:
025: ExternalDatabase db = null;
026: Database xmlDB = null;
027: String databaseClass = "org.ozoneDB.xml.cli.DatabaseImpl";
028: String dbURI = "ozonedb:remote://localhost:3333";
029: String collectionName = "per";
030:
031: // url schema:
032: // xmldb:ozonexml:<database>?<rootcoll>
033: //
034: // <database> can either be a directory in the local filesystem or a remote
035: // ip address (host:port)
036: // <rootcoll> specifies the name of the root collection to load
037: // Example:
038: //Collection rootCol = xmlDB.getCollection( "xmldb:ozonexml:/home/lars/xmltest?root" );
039: String collectionURI = "xmldb:ozonexml://localhost:3333?"
040: + collectionName;
041:
042: /**
043: * @param name the name given to the test
044: */
045: public ConnectionTest(String name) {
046: super (name);
047: }
048:
049: public static Test suite() {
050: return new TestSuite(ConnectionTest.class);
051: }
052:
053: /*
054: public void testStub() {
055: try {
056:
057: } catch (Exception e) {
058: fail( e.getMessage( ) );
059: }
060: }
061: */
062: /**
063: * Test the whole cycle of create, get and delete a collection
064: */
065: public void testCycle() {
066: try {
067: create();
068: get();
069: delete();
070: } catch (Exception e) {
071: fail(e.getMessage());
072: }
073: }
074:
075: private void connect() throws Exception {
076: if (db == null || !db.isOpen()) {
077: // load a database instance
078: xmlDB = (Database) Class.forName(databaseClass)
079: .newInstance();
080: db = ExternalDatabase.openDatabase(dbURI);
081: db.reloadClasses();
082: }
083: }
084:
085: private void create() throws Exception {
086: connect();
087: assertNotNull(db);
088: // create a new Collection
089: XMLCollection root = (XMLCollection) db.createObject(
090: XMLCollectionImpl.class.getName(),
091: OzoneInterface.Public, collectionName);
092: root.setName(collectionName);
093: db.close();
094: assert (!db.isOpen());
095: }
096:
097: private void delete() throws Exception {
098: connect();
099: assertNotNull(db);
100: db.deleteObject((XMLCollection) db
101: .objectForName(collectionName));
102: db.close();
103: assert (!db.isOpen());
104: }
105:
106: private void get() throws Exception {
107: connect();
108: assertNotNull(db);
109: Collection rootCol = xmlDB.getCollection(collectionURI);
110:
111: // check the name of the collection
112: assert (rootCol instanceof org.ozoneDB.xml.cli.CollectionImpl);
113: assertEquals(collectionName, rootCol.getName());
114: }
115:
116: }
|