vgritsenko 2003/12/21 18:38:46
Modified: java/tests/src/org/apache/xindice/xml/sax SAXEventGeneratorTest.java Log: Fix line endings Revision Changes Path 1.3 +224 -224 xml-xindice/java/tests/src/org/apache/xindice/xml/sax/SAXEventGeneratorTest.java Index: SAXEventGeneratorTest.java =================================================================== RCS file: /home/cvs/xml-xindice/java/tests/src/org/apache/xindice/xml/sax/SAXEventGeneratorTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SAXEventGeneratorTest.java 22 Dec 2003 02:14:08 -0000 1.2 +++ SAXEventGeneratorTest.java 22 Dec 2003 02:38:46 -0000 1.3 @@ -1,224 +1,224 @@ -package org.apache.xindice.xml.sax; - -import java.io.StringReader; -import java.util.ArrayList; - -import javax.xml.parsers.SAXParserFactory; -import junit.framework.TestCase; - -import org.apache.xindice.xml.dom.DOMParser; - -import org.w3c.dom.Document; -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.ext.LexicalHandler; - -/** - * This is a simple test to verify that the output of XIndice internal SAX - * parser and a generic Jaxp parser is the same. - * - * @author <a href="mailto:[EMAIL PROTECTED]">Gianugo Rabellino</a> - */ -public class SAXEventGeneratorTest extends TestCase { - - private static final String XML = - "<?xml version=\"1.0\"?>" - + "<n1:n1 " - + "xmlns:n1=\"http://apache.org/xindice/mockns/n1\" " - + "xmlns:n2=\"http://apache.org/xindice/mockns/n1/n2\" " - + "xmlns:n3=\"http://apache.org/xindice/mockns/n1/n3\" " - + "xmlns:n4=\"http://apache.org/xindice/mockns/n1/n4\" " - + "xmlns:n5=\"http://apache.org/xindice/mockns/n1/n5\" " - + "xmlns:n6=\"http://apache.org/xindice/mockns/n1/n6\" " - + "xmlns:n7=\"http://apache.org/xindice/mockns/n7\" " - + "n7:aaa=\"n1\" " - + "n7:bbb=\"something\">" - + "" - + "<n2:aaa />" - + "<n2:bbb />" - + "<n2:ccc with=\"an attribute\">Whatever</n2:ccc>" - + "<n2:ddd>Hello World!</n2:ddd>" - + "<n2:eee />" - + "<n1:aaa>0</n1:aaa>" - + "<n1:bbb>More text</n1:bbb>" - + "<n3:aaa />" - + "<n3:bbb />" - + "<n4:aaa />" - + "<n4:bbb />" - + "<n5:aaa>Even more text, with newlines\n and stuff</n5:aaa>" - + "<n6:aaa />" - + "</n1:n1>"; - - - Document doc; - SAXEventGenerator generator; - LoggingContentHandler xindiceHandler; - LoggingContentHandler jaxpHandler; - - /** - * Setup the test, build the parsers and let them parse - * the document. - * - * @see junit.framework.TestCase#setUp() - */ - protected void setUp() throws Exception { - // Xindice parsing - doc = DOMParser.toDocument(XML); - generator = new SAXEventGenerator(doc); - xindiceHandler = new LoggingContentHandler(); - generator.setContentHandler(xindiceHandler); - generator.start(); - - // Jaxp Parsing - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - XMLReader reader = factory.newSAXParser().getXMLReader(); - jaxpHandler = new LoggingContentHandler(); - reader.setContentHandler(jaxpHandler); - reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); - reader.parse(new InputSource(new StringReader(XML))); - - } - - public void testStartElement() { - assertEquals(jaxpHandler.startElementCount, xindiceHandler.startElementCount); - } - - public void testEndElement() { - assertEquals(jaxpHandler.endElementCount, xindiceHandler.endElementCount); - } - - public void testNameSpaces() { - assertEquals(jaxpHandler.nameSpacesList.size(), xindiceHandler.nameSpacesList.size()); - } - - public void testAttributesCount() { - assertEquals(jaxpHandler.attributesCount, xindiceHandler.attributesCount); - } - - public void testCharacters() { - assertEquals(jaxpHandler.charactersCount, xindiceHandler.charactersCount); - } - - public void testStartPrefixMapping() { - assertEquals(jaxpHandler.startPrefixMappingCount, xindiceHandler.startPrefixMappingCount); - } - - public void testEndPrefixMapping() { - assertEquals(jaxpHandler.endPrefixMappingCount, xindiceHandler.endPrefixMappingCount); - } - - public void testStartCDATA() { - assertEquals(jaxpHandler.startCDATACount, xindiceHandler.startCDATACount); - } - - public void testEndCDATA() { - assertEquals(jaxpHandler.endCDATACount, xindiceHandler.endCDATACount); - } - - public void testComment() { - assertEquals(jaxpHandler.commentCount, xindiceHandler.commentCount); - } -} - -/** - * Horrible hack to quickly check if two parsers have (roughly) - * the same output. - * - * @author <a href="mailto:[EMAIL PROTECTED]">Gianugo Rabellino</a> - */ -class LoggingContentHandler implements ContentHandler, LexicalHandler { - - int startElementCount = 0; - int endElementCount = 0; - int attributesCount = 0; - int startPrefixMappingCount = 0; - int endPrefixMappingCount = 0; - int charactersCount = 0; - int ignorableWhitespaceCount = 0; - int startEntityCount = 0; - int endEntityCount = 0; - int startCDATACount = 0; - int endCDATACount = 0; - int commentCount = 0; - ArrayList nameSpacesList = new ArrayList(); - - public void setDocumentLocator(Locator arg0) { - } - - public void startDocument() throws SAXException { - } - - public void endDocument() throws SAXException { - } - - public void startPrefixMapping(String arg0, String arg1) - throws SAXException { - startPrefixMappingCount++; - } - - public void endPrefixMapping(String arg0) throws SAXException { - endPrefixMappingCount++; - } - - public void startElement(String nsUri, String localName, String name, Attributes attrs) - throws SAXException { - startElementCount++; - if (!nameSpacesList.contains(nsUri)) { - nameSpacesList.add(nsUri); - } - // Do not count namespace declaration attributes - for (int i=0; i <attrs.getLength(); i++) { - if (!attrs.getQName(i).startsWith("xmlns:")) - attributesCount ++; - } - } - - public void endElement(String nsUri, String localName, String name) - throws SAXException { - endElementCount++; - } - - public void characters(char[] ch, int start, int end) - throws SAXException { - charactersCount += new String(ch, start, end).length(); - } - - public void ignorableWhitespace(char[] arg0, int arg1, int arg2) - throws SAXException { - } - - public void processingInstruction(String arg0, String arg1) - throws SAXException { - } - - public void skippedEntity(String arg0) throws SAXException { - } - - public void startDTD(String arg0, String arg1, String arg2) - throws SAXException { - } - - public void endDTD() throws SAXException { - } - - public void startEntity(String arg0) throws SAXException { - } - - public void endEntity(String arg0) throws SAXException { - } - - public void startCDATA() throws SAXException { - } - - public void endCDATA() throws SAXException { - } - - public void comment(char[] ch, int start, int end) throws SAXException { - commentCount += new String(ch, start, end).length(); - } -} +package org.apache.xindice.xml.sax; + +import java.io.StringReader; +import java.util.ArrayList; + +import javax.xml.parsers.SAXParserFactory; +import junit.framework.TestCase; + +import org.apache.xindice.xml.dom.DOMParser; + +import org.w3c.dom.Document; +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.ext.LexicalHandler; + +/** + * This is a simple test to verify that the output of XIndice internal SAX + * parser and a generic Jaxp parser is the same. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Gianugo Rabellino</a> + */ +public class SAXEventGeneratorTest extends TestCase { + + private static final String XML = + "<?xml version=\"1.0\"?>" + + "<n1:n1 " + + "xmlns:n1=\"http://apache.org/xindice/mockns/n1\" " + + "xmlns:n2=\"http://apache.org/xindice/mockns/n1/n2\" " + + "xmlns:n3=\"http://apache.org/xindice/mockns/n1/n3\" " + + "xmlns:n4=\"http://apache.org/xindice/mockns/n1/n4\" " + + "xmlns:n5=\"http://apache.org/xindice/mockns/n1/n5\" " + + "xmlns:n6=\"http://apache.org/xindice/mockns/n1/n6\" " + + "xmlns:n7=\"http://apache.org/xindice/mockns/n7\" " + + "n7:aaa=\"n1\" " + + "n7:bbb=\"something\">" + + "" + + "<n2:aaa />" + + "<n2:bbb />" + + "<n2:ccc with=\"an attribute\">Whatever</n2:ccc>" + + "<n2:ddd>Hello World!</n2:ddd>" + + "<n2:eee />" + + "<n1:aaa>0</n1:aaa>" + + "<n1:bbb>More text</n1:bbb>" + + "<n3:aaa />" + + "<n3:bbb />" + + "<n4:aaa />" + + "<n4:bbb />" + + "<n5:aaa>Even more text, with newlines\n and stuff</n5:aaa>" + + "<n6:aaa />" + + "</n1:n1>"; + + + Document doc; + SAXEventGenerator generator; + LoggingContentHandler xindiceHandler; + LoggingContentHandler jaxpHandler; + + /** + * Setup the test, build the parsers and let them parse + * the document. + * + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + // Xindice parsing + doc = DOMParser.toDocument(XML); + generator = new SAXEventGenerator(doc); + xindiceHandler = new LoggingContentHandler(); + generator.setContentHandler(xindiceHandler); + generator.start(); + + // Jaxp Parsing + SAXParserFactory factory = SAXParserFactory.newInstance(); + factory.setNamespaceAware(true); + XMLReader reader = factory.newSAXParser().getXMLReader(); + jaxpHandler = new LoggingContentHandler(); + reader.setContentHandler(jaxpHandler); + reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); + reader.parse(new InputSource(new StringReader(XML))); + + } + + public void testStartElement() { + assertEquals(jaxpHandler.startElementCount, xindiceHandler.startElementCount); + } + + public void testEndElement() { + assertEquals(jaxpHandler.endElementCount, xindiceHandler.endElementCount); + } + + public void testNameSpaces() { + assertEquals(jaxpHandler.nameSpacesList.size(), xindiceHandler.nameSpacesList.size()); + } + + public void testAttributesCount() { + assertEquals(jaxpHandler.attributesCount, xindiceHandler.attributesCount); + } + + public void testCharacters() { + assertEquals(jaxpHandler.charactersCount, xindiceHandler.charactersCount); + } + + public void testStartPrefixMapping() { + assertEquals(jaxpHandler.startPrefixMappingCount, xindiceHandler.startPrefixMappingCount); + } + + public void testEndPrefixMapping() { + assertEquals(jaxpHandler.endPrefixMappingCount, xindiceHandler.endPrefixMappingCount); + } + + public void testStartCDATA() { + assertEquals(jaxpHandler.startCDATACount, xindiceHandler.startCDATACount); + } + + public void testEndCDATA() { + assertEquals(jaxpHandler.endCDATACount, xindiceHandler.endCDATACount); + } + + public void testComment() { + assertEquals(jaxpHandler.commentCount, xindiceHandler.commentCount); + } +} + +/** + * Horrible hack to quickly check if two parsers have (roughly) + * the same output. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Gianugo Rabellino</a> + */ +class LoggingContentHandler implements ContentHandler, LexicalHandler { + + int startElementCount = 0; + int endElementCount = 0; + int attributesCount = 0; + int startPrefixMappingCount = 0; + int endPrefixMappingCount = 0; + int charactersCount = 0; + int ignorableWhitespaceCount = 0; + int startEntityCount = 0; + int endEntityCount = 0; + int startCDATACount = 0; + int endCDATACount = 0; + int commentCount = 0; + ArrayList nameSpacesList = new ArrayList(); + + public void setDocumentLocator(Locator arg0) { + } + + public void startDocument() throws SAXException { + } + + public void endDocument() throws SAXException { + } + + public void startPrefixMapping(String arg0, String arg1) + throws SAXException { + startPrefixMappingCount++; + } + + public void endPrefixMapping(String arg0) throws SAXException { + endPrefixMappingCount++; + } + + public void startElement(String nsUri, String localName, String name, Attributes attrs) + throws SAXException { + startElementCount++; + if (!nameSpacesList.contains(nsUri)) { + nameSpacesList.add(nsUri); + } + // Do not count namespace declaration attributes + for (int i=0; i <attrs.getLength(); i++) { + if (!attrs.getQName(i).startsWith("xmlns:")) + attributesCount ++; + } + } + + public void endElement(String nsUri, String localName, String name) + throws SAXException { + endElementCount++; + } + + public void characters(char[] ch, int start, int end) + throws SAXException { + charactersCount += new String(ch, start, end).length(); + } + + public void ignorableWhitespace(char[] arg0, int arg1, int arg2) + throws SAXException { + } + + public void processingInstruction(String arg0, String arg1) + throws SAXException { + } + + public void skippedEntity(String arg0) throws SAXException { + } + + public void startDTD(String arg0, String arg1, String arg2) + throws SAXException { + } + + public void endDTD() throws SAXException { + } + + public void startEntity(String arg0) throws SAXException { + } + + public void endEntity(String arg0) throws SAXException { + } + + public void startCDATA() throws SAXException { + } + + public void endCDATA() throws SAXException { + } + + public void comment(char[] ch, int start, int end) throws SAXException { + commentCount += new String(ch, start, end).length(); + } +}