/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: CollectionTest.java 512591 2007-02-28 03:35:44Z vgritsenko $
*/
package org.apache.xindice.integration.client.basic;
import org.apache.xindice.core.FaultCodes;
import org.apache.xindice.integration.client.AbstractXmlDbClientTest;
import org.apache.xindice.integration.client.XmlDbClientSetup;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.XMLDBException;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
/**
* @version $Revision: 512591 $, $Date: 2007-02-27 22:35:44 -0500 (Tue, 27 Feb 2007) $
* @author Vladimir R. Bossicard <vladimir@apache.org>
*/
public class CollectionTest extends AbstractXmlDbClientTest {
public void testGetDatabase() throws Exception {
Collection col = this.client.getCollection(XmlDbClientSetup.INSTANCE_NAME);
assertNotNull(col);
}
public void testGetUnknownDatabase() throws Exception {
try {
Collection col = this.client.getCollection("doesnotexist");
fail("Expected ErrorCodes.NO_SUCH_DATABASE (" + ErrorCodes.NO_SUCH_DATABASE + "), got collection: " + col);
} catch (XMLDBException e) {
assertEquals("ErrorCodes.NO_SUCH_DATABASE",
ErrorCodes.NO_SUCH_DATABASE, e.errorCode);
}
}
public void testListInitialCollections() throws Exception {
Collection db = this.client.getCollection(XmlDbClientSetup.INSTANCE_NAME);
String[] collections = db.listChildCollections();
List collist = new Vector(Arrays.asList(collections));
if (collections.length == 2) {
assertTrue(collist.contains(XmlDbClientSetup.TEST_COLLECTION_NAME));
assertTrue(collist.contains("system"));
} else if (collections.length == 3) {
assertTrue(collist.contains(XmlDbClientSetup.TEST_COLLECTION_NAME));
assertTrue(collist.contains("system"));
assertTrue(collist.contains("meta"));
} else {
fail("Initial number of collections should be 2, or 3 if meta enabled");
}
}
public void testCreateNestedCollection() throws Exception {
Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
Collection nested = this.client.createCollection(TEST_COLLECTION_PATH, "nested");
assertEquals(1, col.getChildCollectionCount());
// Test create collection with path
this.client.createCollection(TEST_COLLECTION_PATH, "nested/child2", "child2");
this.client.createCollection(TEST_COLLECTION_PATH + "/nested", "child3");
assertEquals(2, nested.getChildCollectionCount());
this.client.dropCollection(TEST_COLLECTION_PATH + "/nested", "child2");
assertEquals(1, nested.getChildCollectionCount());
this.client.dropCollection(TEST_COLLECTION_PATH + "/nested", "child3");
assertEquals(0, nested.getChildCollectionCount());
this.client.dropCollection(TEST_COLLECTION_PATH, "nested");
}
public void testCreateDuplicateCollection() throws Exception {
this.client.createCollection(TEST_COLLECTION_PATH, "duplicate");
try {
this.client.createCollection(TEST_COLLECTION_PATH, "duplicate");
} catch (XMLDBException e) {
assertEquals("FaultCodes.COL_DUPLICATE_COLLECTION",
FaultCodes.COL_DUPLICATE_COLLECTION, e.vendorErrorCode);
}
this.client.dropCollection(TEST_COLLECTION_PATH, "duplicate");
}
public void testCreateCollectionEmptyName() throws Exception {
try {
this.client.createCollection(TEST_COLLECTION_PATH, "emptyname", "");
fail();
} catch (XMLDBException e) {
assertEquals("FaultCodes.COL_CANNOT_CREATE",
FaultCodes.COL_CANNOT_CREATE, e.vendorErrorCode);
}
}
public void testCreateCollectionNullName() throws Exception {
try {
this.client.createCollection(TEST_COLLECTION_PATH, "nullname", (String) null);
fail();
} catch (XMLDBException e) {
assertEquals("FaultCodes.COL_CANNOT_CREATE",
FaultCodes.COL_CANNOT_CREATE, e.vendorErrorCode);
}
}
public void testCreateCollectionInvalidName() throws Exception {
try {
this.client.createCollection(TEST_COLLECTION_PATH, "invalidname", "my/collection");
fail();
} catch (XMLDBException e) {
assertEquals("FaultCodes.COL_CANNOT_CREATE",
FaultCodes.COL_CANNOT_CREATE, e.vendorErrorCode);
}
}
public void testDropCollectionTwice() throws Exception {
this.client.createCollection(TEST_COLLECTION_PATH, "droptwice");
this.client.dropCollection(TEST_COLLECTION_PATH, "droptwice");
try {
this.client.dropCollection(TEST_COLLECTION_PATH, "droptwice");
fail();
} catch (XMLDBException e) {
assertEquals("FaultCodes.COL_COLLECTION_NOT_FOUND",
FaultCodes.COL_COLLECTION_NOT_FOUND, e.vendorErrorCode);
}
}
public void testDropCollectionNullName() throws Exception {
try {
this.client.dropCollection(TEST_COLLECTION_PATH, null);
fail();
} catch (XMLDBException e) {
assertEquals("FaultCodes.COL_COLLECTION_NOT_FOUND",
FaultCodes.COL_COLLECTION_NOT_FOUND, e.vendorErrorCode);
}
}
public void testDropCollectionEmptyName() throws Exception {
try {
this.client.dropCollection(TEST_COLLECTION_PATH, "");
fail("Dropping a collection with empty name should throw an exception.");
} catch (XMLDBException e) {
assertEquals("FaultCodes.COL_COLLECTION_NOT_FOUND",
FaultCodes.COL_COLLECTION_NOT_FOUND, e.vendorErrorCode);
}
}
public void testGetCollectionCount() throws Exception {
Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
this.client.createCollection(TEST_COLLECTION_PATH, "count");
assertEquals(1, col.getChildCollectionCount());
this.client.dropCollection(TEST_COLLECTION_PATH, "count");
assertEquals(0, col.getChildCollectionCount());
}
public void testListCollections() throws Exception {
Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
String[] collections = col.listChildCollections();
assertEquals(0, collections.length);
this.client.createCollection(TEST_COLLECTION_PATH, "child1");
this.client.createCollection(TEST_COLLECTION_PATH, "child2");
collections = col.listChildCollections();
assertEquals(2, collections.length);
List collist = new Vector(Arrays.asList(collections));
assertTrue(collist.contains("child1"));
assertTrue(collist.contains("child2"));
this.client.dropCollection(TEST_COLLECTION_PATH, "child1");
collections = col.listChildCollections();
assertEquals(1, collections.length);
assertEquals("child2", collections[0]);
this.client.dropCollection(TEST_COLLECTION_PATH, "child2");
collections = col.listChildCollections();
assertEquals(0, collections.length);
}
public void testGetName() throws Exception {
this.client.createCollection(TEST_COLLECTION_PATH, "getname");
assertEquals("getname", this.client.getName(TEST_COLLECTION_PATH + "/getname"));
this.client.dropCollection(TEST_COLLECTION_PATH, "getname");
}
public void testGetNameEndWithSlash() throws Exception {
this.client.createCollection(TEST_COLLECTION_PATH, "getname");
assertEquals("getname", this.client.getName(TEST_COLLECTION_PATH + "/getname/"));
this.client.dropCollection(TEST_COLLECTION_PATH, "getname");
}
public void testGetParentCollection() throws Exception {
Collection col = this.client.createCollection(TEST_COLLECTION_PATH, "childcol");
Collection parent = col.getParentCollection();
assertNotNull(parent);
assertEquals("current", parent.getName());
parent = parent.getParentCollection();
assertNotNull(parent);
assertEquals("testing", parent.getName());
parent = parent.getParentCollection();
// Must return root database collection
assertNotNull(parent);
assertEquals("db", parent.getName());
// Database (== root collection) has no parent
assertNull(parent.getParentCollection());
this.client.dropCollection(TEST_COLLECTION_PATH, "childcol");
}
public void testGetCollectionUnknown() throws Exception {
assertNull(this.client.getCollection(TEST_COLLECTION_PATH + "/unknown"));
}
public void testGetChildCollectionEndsWithSlash() throws Exception {
Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
try {
col.getChildCollection("/db");
fail("Expected ErrorCodes.INVALID_COLLECTION");
} catch (XMLDBException e) {
assertEquals("ErrorCodes.INVALID_COLLECTION", ErrorCodes.INVALID_COLLECTION, e.errorCode);
}
}
public void testClose() throws Exception {
Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
col.close();
}
}