Package org.apache.xindice.integration.client.basic

Source Code of org.apache.xindice.integration.client.basic.DocumentTest

/*
* 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: DocumentTest.java,v 1.12 2004/03/30 15:33:52 vgritsenko Exp $
*/

package org.apache.xindice.integration.client.basic;

import org.apache.xindice.integration.client.AbstractXmlDbClientTest;
import org.apache.xindice.tools.command.StringSerializer;
import org.apache.xindice.xml.dom.DOMParser;

import org.custommonkey.xmlunit.XMLAssert;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XMLResource;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;

/**
* @version CVS $Revision: 1.12 $, $Date: 2004/03/30 15:33:52 $
* @author Vladimir R. Bossicard <vladimir@apache.org>
*/
public class DocumentTest extends AbstractXmlDbClientTest {

    private static final String CONTENT = "<?xml version=\"1.0\"?>\n<data><test>test data</test></data>";

    public void testInsertDocument() throws Exception {
        this.client.insertDocument(TEST_COLLECTION_PATH, "testdoc", CONTENT);
        assertEquals(1, this.client.countDocument(TEST_COLLECTION_PATH));

        this.client.removeDocument(TEST_COLLECTION_PATH, "testdoc");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    public void testInsertDocumentAsDOM() throws Exception {
        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);

        XMLResource document = (XMLResource) col.createResource("testdoc", "XMLResource");
        document.setContentAsDOM(DOMParser.toDocument(CONTENT));
        col.storeResource(document);
        assertEquals(1, this.client.countDocument(TEST_COLLECTION_PATH));

        String content = this.client.getDocument(TEST_COLLECTION_PATH, "testdoc");
        assertXMLEqual(CONTENT, content);

        this.client.removeDocument(TEST_COLLECTION_PATH, "testdoc");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    public void testInsertDocumentNullContent() throws Exception {
        try {
            this.client.insertDocument(TEST_COLLECTION_PATH, "testdoc", null);
            fail("Insert null document should fail");
        } catch (XMLDBException e) {
            assertEquals("ErrorCodes.INVALID_RESOURCE", ErrorCodes.INVALID_RESOURCE, e.errorCode);
        }
    }

    public void testRemoveDocumentInvalidName() throws Exception {
        try {
            this.client.removeDocument(TEST_COLLECTION_PATH, "invalidname");
        } catch (XMLDBException e) {
            //assertTrue( e.getMessage().lastIndexOf( "Duplicate Collection" ) > 0 );
            return;
        }
    }

    public void testRemoveDocumentEmptyName() throws Exception {
        try {
            this.client.removeDocument(TEST_COLLECTION_PATH, "");
        } catch (XMLDBException e) {
            //assertTrue( e.getMessage().lastIndexOf( "Duplicate Collection" ) > 0 );
            return;
        }
    }

    public void testRemoveDocumentNullName() throws Exception {
        try {
            this.client.removeDocument(TEST_COLLECTION_PATH, null);
        } catch (XMLDBException e) {
            //assertTrue( e.getMessage().lastIndexOf( "Duplicate Collection" ) > 0 );
            return;
        }
    }

    public void testGetDocument() throws Exception {
        String testDocument = "<?xml version=\"1.0\"?>\n<data><test>test data</test></data>";
        this.client.insertDocument(TEST_COLLECTION_PATH, "doc1", testDocument);

        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc1");
        assertNotNull(doc);
        assertXMLEqual(testDocument, doc);

        this.client.removeDocument(TEST_COLLECTION_PATH, "doc1");
    }

    public void testGetDocumentAsSAX() throws Exception {
        String testDocument = "<?xml version=\"1.0\"?>\n<data><test>test data</test></data>";
        this.client.insertDocument(TEST_COLLECTION_PATH, "doc1", testDocument);

        final StringWriter out = new StringWriter();
        final ContentHandler serializer = createSerializer(out);

        this.client.getDocumentAsSax(TEST_COLLECTION_PATH, "doc1", serializer);
        String doc = out.toString();

        assertNotNull(doc);
        assertXMLEqual(testDocument, doc);

        this.client.removeDocument(TEST_COLLECTION_PATH, "doc1");
    }

    public void testGetInexistantDocument() throws Exception {
        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "ghostdoc");
        assertNull(doc);
    }

    public void testGetDocumentEmptyName() throws Exception {
        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "");
        assertNull(doc);
    }

    public void testGetDocumentNullName() throws Exception {
        String doc = this.client.getDocument(TEST_COLLECTION_PATH, null);
        assertNull(doc);
    }

    public void testListDocuments() throws Exception {
        String[] documents = this.client.listDocuments(TEST_COLLECTION_PATH);
        assertEquals(0, this.client.listDocuments(TEST_COLLECTION_PATH).length);

        this.client.insertDocument(TEST_COLLECTION_PATH, "doc1", "<data>\n<test>\ntest data</test>\n</data>");
        documents = this.client.listDocuments(TEST_COLLECTION_PATH);
        assertEquals(1, documents.length);
        assertEquals("doc1", documents[0]);

        this.client.insertDocument(TEST_COLLECTION_PATH, "doc2", "<data>\n<test>\ntest data2</test>\n</data>");
        documents = this.client.listDocuments(TEST_COLLECTION_PATH);
        assertEquals(2, documents.length);
        assertEquals("doc1", documents[0]);
        assertEquals("doc2", documents[1]);

        this.client.removeDocument(TEST_COLLECTION_PATH, "doc1");
        documents = this.client.listDocuments(TEST_COLLECTION_PATH);
        assertEquals(1, documents.length);
        assertEquals("doc2", documents[0]);

        this.client.removeDocument(TEST_COLLECTION_PATH, "doc2");
        documents = this.client.listDocuments(TEST_COLLECTION_PATH);
        assertEquals(0, documents.length);
    }

    public void testGetDocumentCount() throws Exception {
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));

        this.client.insertDocument(TEST_COLLECTION_PATH, "doc1", "<data>\n<test>\ntest data</test>\n</data>");
        assertEquals(1, this.client.countDocument(TEST_COLLECTION_PATH));

        this.client.insertDocument(TEST_COLLECTION_PATH, "doc2", "<data>\n<test>\ntest data2</test>\n</data>");
        assertEquals(2, this.client.countDocument(TEST_COLLECTION_PATH));

        this.client.removeDocument(TEST_COLLECTION_PATH, "doc1");
        assertEquals(1, this.client.countDocument(TEST_COLLECTION_PATH));

        this.client.removeDocument(TEST_COLLECTION_PATH, "doc2");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    public void testDocumentWithNameSpaces() throws Exception
    {
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));

        final String testDocument = fetchTestDocument("namespaces.xml");
        this.client.insertDocument(TEST_COLLECTION_PATH, "namespaces.xml", testDocument);

        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertNotNull(doc);
        assertXMLEqual(testDocument, doc);

        this.client.removeDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    public void testDocumentWithNameSpacesSAX() throws Exception
    {
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));

        final String testDocument = fetchTestDocument("namespaces.xml");
        this.client.insertDocument(TEST_COLLECTION_PATH, "namespaces.xml", testDocument);

        final StringWriter out = new StringWriter();
        final ContentHandler serializer = createSerializer(out);

        this.client.getDocumentAsSax(TEST_COLLECTION_PATH, "namespaces.xml", serializer);
        String doc = out.toString();

        assertNotNull(doc);
        assertXMLEqual(testDocument, doc);

        this.client.removeDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    public void testDocumentWithSimpleNameSpacePrefixes() throws Exception
    {
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));

        final String testDocument = fetchTestDocument("namespace-simpleprefixes.xml");
        this.client.insertDocument(TEST_COLLECTION_PATH, "namespaces.xml", testDocument);

        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertNotNull(doc);
        assertXMLEqual(testDocument, doc);

        this.client.removeDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    public void testDocumentWithSimpleNameSpacePrefixesSAX() throws Exception
    {
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));

        final String testDocument = fetchTestDocument("namespace-simpleprefixes.xml");
        this.client.insertDocument(TEST_COLLECTION_PATH, "namespaces.xml", testDocument);

        final StringWriter out = new StringWriter();
        final ContentHandler serializer = createSerializer(out);

        this.client.getDocumentAsSax(TEST_COLLECTION_PATH, "namespaces.xml", serializer);
        String doc = out.toString();

        assertNotNull(doc);
        assertXMLEqual(testDocument, doc);

        this.client.removeDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    public void testDocumentWithChangingNameSpacePrefixes() throws Exception
    {
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));

        final String testDocument = fetchTestDocument("namespace-changingprefixes.xml");
        this.client.insertDocument(TEST_COLLECTION_PATH, "namespaces.xml", testDocument);

        String doc = this.client.getDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertNotNull(doc);
        assertXMLEqual(testDocument, doc);

        this.client.removeDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    public void testDocumentWithChangingNameSpacePrefixesSAX() throws Exception
    {
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));

        final String testDocument = fetchTestDocument("namespace-changingprefixes.xml");
        this.client.insertDocument(TEST_COLLECTION_PATH, "namespaces.xml", testDocument);

        final StringWriter out = new StringWriter();
        final ContentHandler serializer = createSerializer(out);

        this.client.getDocumentAsSax(TEST_COLLECTION_PATH, "namespaces.xml", serializer);
        String doc = out.toString();

        assertNotNull(doc);
        assertXMLEqual(testDocument, doc);

        this.client.removeDocument(TEST_COLLECTION_PATH, "namespaces.xml");
        assertEquals(0, this.client.countDocument(TEST_COLLECTION_PATH));
    }

    private String fetchTestDocument(String name) throws IOException, SAXException, ParserConfigurationException
    {
        final StringWriter out = new StringWriter();
        final ContentHandler serializer = createSerializer(out);

        parseTestDocument(name, serializer);

        return out.toString();
    }

    private void parseTestDocument(String name, ContentHandler handler) throws IOException, SAXException, ParserConfigurationException
    {
        InputStream in = getClass().getResourceAsStream("testdocs/" + name);

        if (null == in) {
            throw new FileNotFoundException("The resource " + name + " could not be found");
        }

        final SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);

        final XMLReader saxReader = factory.newSAXParser().getXMLReader();

        saxReader.setContentHandler(handler);
        saxReader.parse(new InputSource(in));
    }

    private ContentHandler createSerializer(final Writer out)
    {
        final ContentHandler serializer = new StringSerializer()
        {
            public void endDocument() throws SAXException
            {
                super.endDocument();

                try {
                    out.write(this.toString());
                    out.flush();
                } catch (IOException e) {
                    throw new RuntimeException("Unexpected IO exception:" + e.getMessage());
                }
            }
        };

        return serializer;
    }

    protected void assertXMLEqual(String control, String result) throws Exception {
        try {
            XMLAssert.assertXMLEqual(control, result);
        } catch (SAXException e) {
            fail(e.getLocalizedMessage());
        }
    }
}
TOP

Related Classes of org.apache.xindice.integration.client.basic.DocumentTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.