}
public void testCreateCollection() {
Collection tempcol = null;
try {
// Name of the valid collection to create
String newcolname = "testchild";
// Config XML for valid collection created
String colconf = "<collection compressed=\"true\" name=\"" + newcolname + "\" >" ;
colconf = colconf + "<filer class=\"org.dbxml.core.filer.BTreeFiler\" gzip=\"true\"/>" ;
colconf = colconf + "</collection>" ;
EncodedBuffer colconfbuf = new EncodedBuffer(-1, EmptyBytes, colconf.getBytes() );
// Create the collection
tempcol = colman.createCollection(newcolname, colconfbuf);
// Verify that collection created, and that name was set correctly
assertTrue( tempcol.getName().equals( newcolname ) );
// Now drop the collection
colman.dropCollection( newcolname );
// Attempt to create a collection with an empty configuration
try {
exceptioncaught = false;
String empty = "";
EncodedBuffer emptybuf = new EncodedBuffer(-1, EmptyBytes, empty.getBytes() );
tempcol = colman.createCollection("NullConfig", emptybuf );
} catch( APIException e ) {
exceptioncaught = true;
assertTrue( e.faultCode == FaultCodes.JAVA_RUNTIME_ERROR );
} finally {
assertTrue( exceptioncaught );
}
// Attempt to create a collection with a malformed configuration
try {
exceptioncaught = false;
// Bad configuration file
String malconf = "<bad config>lkjasf";
EncodedBuffer malconfbuf = new EncodedBuffer(-1, EmptyBytes, malconf.getBytes() );
// Attempt to create the collection
tempcol = colman.createCollection("malconf", malconfbuf );
} catch( APIException e ) {
exceptioncaught = true;
assertTrue( e.faultCode == FaultCodes.JAVA_RUNTIME_ERROR );
} finally {
assertTrue( exceptioncaught );
}
// Attempt to create a collection with an invalid configuration, missing name attribute
try {
exceptioncaught = false;
// Bad configuration file
String invalidconf = "<collection>" ;
invalidconf = invalidconf + "<filer class=\"org.dbxml.core.filer.BTreeFiler\" gzip=\"true\"/>" ;
invalidconf = invalidconf + "</collection>" ;
EncodedBuffer invalidconfbuf = new EncodedBuffer(-1, EmptyBytes, invalidconf.getBytes() );
// Attempt to create the collection
tempcol = colman.createCollection("invalid", invalidconfbuf );
// Test that the collection was created by calling getName() on it
assertTrue( tempcol.getName().equals("invalid") );
colman.dropCollection("invalid");
} catch( APIException e ) {
exceptioncaught = true;
assertTrue( e.faultCode == FaultCodes.COL_CANNOT_CREATE );
} finally {
assertTrue( exceptioncaught );
}
// Attempt to create a collection without a filer attribute, this should be allowed
exceptioncaught = false;
String nofilerconf = "<collection name=\"nofiler\">" ;
nofilerconf = nofilerconf + "</collection>" ;
EncodedBuffer nofilerconfbuf = new EncodedBuffer(-1, EmptyBytes, nofilerconf.getBytes() );
// Attempt to create the collection
tempcol = colman.createCollection("nofiler", nofilerconfbuf );
colman.dropCollection("nofiler");
} catch( Exception e ) {
e.printStackTrace();
fail(e.getMessage() );
} finally {
// Remove collection added so that we don't break other tests, release recources for tempcol
tempcol.remove();
}
}