/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed 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.
*
* CVS $Id: BinaryResourceTest.java,v 1.7 2004/03/20 13:43:23 vgritsenko Exp $
*/
package org.apache.xindice.integration.client.resources;
import org.apache.xindice.integration.client.AbstractXmlDbClientTest;
import org.apache.xindice.integration.client.XmlDbClientSetup;
import org.apache.xindice.xml.dom.DOMParser;
import org.apache.xindice.client.xmldb.services.MetaService;
import org.apache.xindice.core.meta.MetaData;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Resource;
import org.xmldb.api.modules.BinaryResource;
/**
*/
public class BinaryResourceTest extends AbstractXmlDbClientTest {
public void setUp() throws Exception {
String name = "current";
// Override super.setUp(): Need inline meta. See client.createCollection(String, String)
String cfg
= "<collection compressed=\"true\" name=\"" + name + "\" inline-metadata=\"true\">"
+ " <filer class=\"org.apache.xindice.core.filer.BTreeFiler\" />"
+ "</collection>";
this.client.createCollection(XmlDbClientSetup.INSTANCE_NAME + "/" + XmlDbClientSetup.TEST_COLLECTION_NAME,
name, DOMParser.toDocument(cfg));
}
public void testBinaryRoundTrip() throws Exception {
Collection collection = client.getCollection(TEST_COLLECTION_PATH);
if (collection == null) {
throw new Exception("getCollection(" + TEST_COLLECTION_PATH + ") returned null");
}
/*
* Create a binary resource, save it in the 'current' collection,
* then retrieve it and verify that the data comes back right.
*/
Resource newResource = collection.createResource(null, "BinaryResource");
newResource.setContent(new byte[] { 0x00, 0x10, 0x01, 0x11 });
collection.storeResource(newResource);
String id = newResource.getId();
Resource foundResource = collection.getResource(id);
assertNotNull("We know you're in there...", foundResource);
assertEquals("The resource type is supposed be 'BinaryResource'", "BinaryResource", foundResource.getResourceType());
assertTrue("The resource is an instance of BinaryResource", foundResource instanceof BinaryResource);
byte[] newBytes = (byte[]) newResource.getContent();
byte[] foundBytes = (byte[]) foundResource.getContent();
assertEquals("The size of the found and saved resource should be the same...", newBytes.length, foundBytes.length);
for (int i = 0; i < newBytes.length; ++i) {
assertEquals("The resources differ in byte " + i, newBytes[i], foundBytes[i]);
}
}
public void testBinaryList() throws Exception {
Collection collection = client.getCollection(TEST_COLLECTION_PATH);
if (collection == null) {
throw new Exception("getCollection(" + TEST_COLLECTION_PATH + ") returned null");
}
//
// Create a binary resource, save it in the 'current' collection,
// then list resources.
//
Resource newResource = collection.createResource(null, "BinaryResource");
newResource.setContent(new byte[] { 0x00, 0x10, 0x01, 0x11 });
collection.storeResource(newResource);
String id = newResource.getId();
String[] resources = collection.listResources();
assertNotNull("Can not be null", resources);
assertEquals("Should have one resource", 1, resources.length);
assertEquals("Resource ID should match", id, resources[0]);
collection.removeResource(newResource);
}
public void testBinaryRemove() throws Exception {
Collection collection = client.getCollection(TEST_COLLECTION_PATH);
if (collection == null) {
throw new Exception("getCollection(" + TEST_COLLECTION_PATH + ") returned null");
}
/*
* Create a binary resource, save it in the 'current' collection,
* then remove it and verify that it was indeed removed.
*/
Resource newResource = collection.createResource(null, "BinaryResource");
newResource.setContent(new byte[] { 0x00, 0x10, 0x01, 0x11 });
collection.storeResource(newResource);
String id = newResource.getId();
Resource foundResource = collection.getResource(id);
assertNotNull("It should be in there", foundResource);
collection.removeResource(foundResource);
foundResource = collection.getResource(id);
assertNull("It should not be in there anymore", foundResource);
}
public void testBinaryMeta() throws Exception {
Collection collection = client.getCollection(TEST_COLLECTION_PATH);
if (collection == null) {
throw new Exception("getCollection(" + TEST_COLLECTION_PATH + ") returned null");
}
/*
* Create a binary resource, set / get meta data
*/
Resource newResource = collection.createResource(null, "BinaryResource");
newResource.setContent(new byte[] { 0x00, 0x10, 0x01, 0x11 });
collection.storeResource(newResource);
String id = newResource.getId();
MetaService service = (MetaService)collection.getService("MetaService", "1.0");
MetaData meta = service.getMetaData(id);
assertNotNull("Meta should be in there", meta);
meta.setAttribute("test", "attribute");
service.setMetaData(id, meta);
meta = service.getMetaData(id);
assertNotNull("Meta should be in there", meta);
assertEquals("Attribute should not be in there", "attribute", meta.getAttribute("test"));
}
}