// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
package test.xmldb.other;
import junit.framework.*;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.ozoneDB.ExternalDatabase;
import org.ozoneDB.OzoneInterface;
import org.ozoneDB.xml.core.XMLCollectionImpl;
import org.ozoneDB.xml.core.XMLCollection;
/**
* This tests the Connection and root collection creation,
* retrieval and deleting.
* @author Per Nyfelt and SMB
*/
public class ConnectionTest extends TestCase {
ExternalDatabase db = null;
Database xmlDB = null;
String databaseClass = "org.ozoneDB.xml.cli.DatabaseImpl";
String dbURI = "ozonedb:remote://localhost:3333";
String collectionName="per";
// url schema:
// xmldb:ozonexml:<database>?<rootcoll>
//
// <database> can either be a directory in the local filesystem or a remote
// ip address (host:port)
// <rootcoll> specifies the name of the root collection to load
// Example:
//Collection rootCol = xmlDB.getCollection( "xmldb:ozonexml:/home/lars/xmltest?root" );
String collectionURI = "xmldb:ozonexml://localhost:3333?" + collectionName;
/**
* @param name the name given to the test
*/
public ConnectionTest(String name) {
super(name);
}
public static Test suite() {
return new TestSuite(ConnectionTest.class);
}
/*
public void testStub() {
try {
} catch (Exception e) {
fail( e.getMessage( ) );
}
}
*/
/**
* Test the whole cycle of create, get and delete a collection
*/
public void testCycle() {
try {
create();
get();
delete();
} catch (Exception e) {
fail( e.getMessage( ) );
}
}
private void connect() throws Exception {
if (db == null || !db.isOpen()) {
// load a database instance
xmlDB = (Database)Class.forName(databaseClass).newInstance();
db = ExternalDatabase.openDatabase(dbURI);
db.reloadClasses();
}
}
private void create() throws Exception {
connect();
assertNotNull(db);
// create a new Collection
XMLCollection root = (XMLCollection)db.createObject( XMLCollectionImpl.class.getName(), OzoneInterface.Public, collectionName);
root.setName(collectionName);
db.close();
assert(!db.isOpen());
}
private void delete() throws Exception{
connect();
assertNotNull(db);
db.deleteObject((XMLCollection)db.objectForName(collectionName));
db.close();
assert(!db.isOpen());
}
private void get() throws Exception {
connect();
assertNotNull(db);
Collection rootCol = xmlDB.getCollection(collectionURI);
// check the name of the collection
assert(rootCol instanceof org.ozoneDB.xml.cli.CollectionImpl);
assertEquals(collectionName, rootCol.getName());
}
}