You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-users@xml.apache.org by Ke...@bredex.com on 2002/01/21 20:22:12 UTC
Problem removing
Hello all,
I am attempting to create a simplified test case to recreate the problems I
am having with OutOfMemoryException, but I am having problem with
collection.remove(Resource) in my test case testRemoveAll().
Any ideas?
Kevin Ross
==============================================================================
package org.dbxml.examples;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.modules.XPathQueryService;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.modules.XUpdateQueryService;
import org.dbxml.client.xmldb.DatabaseImpl;
import junit.textui.TestRunner;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
/**
*
*/
public class MemoryTest extends TestCase {
private static boolean isDatabaseRegistered = false;
private static final String COLLECTION_XPATH = "/db/root/ocs";
public MemoryTest(java.lang.String testName) throws XMLDBException {
super(testName);
registerDatabase();
}
public static void main(java.lang.String[] args) {
try{
TestRunner.run(suite());
}
catch(Throwable t){
t.printStackTrace();
}
}
protected void runTest() throws Throwable {
try{
System.out.println("\n\nRunning test: " + getName());
super.runTest();
}
catch(XMLDBException x){
System.out.println(org.dbxml.core.FaultCodes.getFaultMessage(x));
throw x;
}
}
public static Test suite() throws XMLDBException {
//TestSuite suite = new TestSuite(MemoryTest.class);
TestSuite suite = new TestSuite();
suite.addTest(new MemoryTest("testQuery"));
suite.addTest(new MemoryTest("testRemoveAll"));
suite.addTest(new MemoryTest("testQuery"));
//
// Initialize and display test document.
//
//suite.addTest(new MemoryTest("testInitializeTestCaseDocument"));
//suite.addTest(new MemoryTest("testQuery"));
//
// You can use this to test one update and see the results.
//
//suite.addTest(new MemoryTest("testUpdateDocument"));
//suite.addTest(new MemoryTest("testQuery"));
//
// Does this leak? So far if only one row is in the collection, it
leaks(?), but not with more than one document(?).
//
//suite.addTest(new MemoryTest
("testUpdateDocumentOneThousandTimes"));
//suite.addTest(new MemoryTest("testQuery"));
//
// The following works without incident
//
//suite.addTest(new MemoryTest("testGetAndCloseCollection"));
//suite.addTest(new MemoryTest
("testGetAndCloseCollectionOneHundredTimes"));
//suite.addTest(new MemoryTest
("testGetAndCloseCollectionInfinitely"));
return suite;
}
protected Collection getCollection(String collectionXpath) throws
XMLDBException {
Collection collection = DatabaseManager.getCollection
("xmldb:dbxml://" + collectionXpath);
return collection;
}
protected void registerDatabase() throws XMLDBException {
if(!isDatabaseRegistered){
DatabaseManager.registerDatabase(new DatabaseImpl());
isDatabaseRegistered = true;
}
}
public void testUpdateDocumentOneThousandTimes() throws XMLDBException{
for(int i=0; i<1000; i++){
updateDocument();
}
}
public void testUpdateDocument() throws XMLDBException {
updateDocument();
}
public void updateDocument() throws XMLDBException {
Collection collection = null;
try {
collection = getCollection(COLLECTION_XPATH);
String xupdate =
"<xu:modifications version=\"1.0\"" +
" xmlns:xu=\"http://www.xmldb.org/xupdate\">" +
" <xu:update select=\"/highKey[@uniqueIdentifier
= 'foo.com']/@value\">" + System.currentTimeMillis() + "</xu:update>" +
"</xu:modifications>";
XUpdateQueryService xupdateService = (XUpdateQueryService)
collection.getService("XUpdateQueryService", "1.0");
xupdateService.update(xupdate);
}
finally {
if (collection != null) {
collection.close();
}
}
}
/**
* to create the initial document
*
*/
public void testInitializeTestCaseDocument() throws XMLDBException {
removeAll();
Collection collection = null;
try{
collection = getCollection(COLLECTION_XPATH);
String documentId = collection.createId();
XMLResource xmlResource
= (XMLResource)collection.createResource(documentId,
XMLResource.RESOURCE_TYPE);
String xml = "<?xml version=\"1.0\"?><highKey uniqueIdentifier
=\"foo.com\" value=\"1\"/>";
xmlResource.setContent(xml);
collection.storeResource(xmlResource);
}
finally{
if (collection != null) {
collection.close();
}
}
}
/**
* Search the database.
*/
public void testQuery() throws XMLDBException {
String xpathQuery = "/highKey[@uniqueIdentifier = 'foo.com']";
ResourceSet resourceSet = getResourceSet(xpathQuery);
ResourceIterator resourceSetIterator = resourceSet.getIterator();
int i = 0;
while (resourceSetIterator.hasMoreResources()) {
Resource resource = resourceSetIterator.nextResource();
System.out.println((++i) + ".\n--------------------------\n" +
(String) resource.getContent() + "\n\n");
}
}
public ResourceSet getResourceSet(Collection collection, String
xpathQuery) throws XMLDBException {
XPathQueryService xpathQueryService = (XPathQueryService)
collection.getService("XPathQueryService", "1.0");
return xpathQueryService.query(xpathQuery);
}
public ResourceSet getResourceSet(String xpathQuery) throws
XMLDBException {
Collection collection = null;
try{
collection = getCollection(COLLECTION_XPATH);
return getResourceSet(collection, xpathQuery);
}
finally{
if (collection != null) {
collection.close();
}
}
}
public void testRemoveAll() throws XMLDBException{
removeAll();
}
public void removeAll() throws XMLDBException {
Collection collection = null;
String xpathQuery = "/highKey[@uniqueIdentifier = 'foo.com']";
try{
int i = 0;
collection = getCollection(COLLECTION_XPATH);
ResourceSet resourceSet = getResourceSet(collection,
xpathQuery);
ResourceIterator resourceIterator = resourceSet.getIterator();
while (resourceIterator.hasMoreResources()) {
i++;
Resource resource = resourceIterator.nextResource();
System.out.println("Attempting to remove resource[" + i +
"]: " + resource.getContent());
collection.removeResource(resource);
}
System.out.println("removeAll(): removed " + i + "
documents.");
}
finally{
if (collection != null) {
collection.close();
}
}
}
/**
*
*/
public void getAndCloseCollection() throws XMLDBException {
Collection collection = null;
try{
collection = getCollection(COLLECTION_XPATH);
}
finally{
if (collection != null) {
collection.close();
}
}
}
public void testGetAndCloseCollectionInfinitely() throws XMLDBException
{
System.out.println("testGetAndCloseCollectionInfinitely():\n");
while(true){
getAndCloseCollection();
System.out.print(".");
}
}
public void testGetAndCloseCollectionOneHundredTimes() throws
XMLDBException {
System.out.println("testGetAndCloseCollectionOneHundredTimes():
\n");
for(int i = 0; i < 100; i++){
getAndCloseCollection();
}
}
public void testGetAndCloseCollection() throws XMLDBException {
getAndCloseCollection();
}
}
Re: Problem removing
Posted by Kimbro Staken <ks...@dbxmlgroup.com>.
It looks right, what's the problem you're seeing?
On Monday, January 21, 2002, at 12:22 PM, KevinRoss@bredex.com wrote:
>
> Hello all,
>
> I am attempting to create a simplified test case to recreate the problems
> I
> am having with OutOfMemoryException, but I am having problem with
> collection.remove(Resource) in my test case testRemoveAll().
>
> Any ideas?
>
>
> Kevin Ross
>
>
>
Kimbro Staken
XML Database Software, Consulting and Writing
http://www.xmldatabases.org/