vgritsenko 2004/03/30 07:16:22
Modified: java/tests/src/org/apache/xindice/integration/client/basic DocumentTest.java Log: zap tabs Revision Changes Path 1.11 +288 -293 xml-xindice/java/tests/src/org/apache/xindice/integration/client/basic/DocumentTest.java Index: DocumentTest.java =================================================================== RCS file: /home/cvs/xml-xindice/java/tests/src/org/apache/xindice/integration/client/basic/DocumentTest.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- DocumentTest.java 24 Feb 2004 03:22:46 -0000 1.10 +++ DocumentTest.java 30 Mar 2004 15:16:22 -0000 1.11 @@ -43,295 +43,290 @@ */ public class DocumentTest extends AbstractXmlDbClientTest { - public void testInsertDocument() throws Exception { - this.client.insertDocument(TEST_COLLECTION_PATH, "testdoc", "<test>test data</test>"); - 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 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()); - } - } + public void testInsertDocument() throws Exception { + this.client.insertDocument(TEST_COLLECTION_PATH, "testdoc", "<test>test data</test>"); + 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 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()); + } + } }