You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2008/12/24 10:44:57 UTC
svn commit: r729287 - in /cocoon/whiteboard/xml-utils: ./
src/main/java/org/apache/cocoon/xml/sax/ src/test/ src/test/java/
src/test/java/org/ src/test/java/org/apache/
src/test/java/org/apache/cocoon/ src/test/java/org/apache/cocoon/xml/
src/test/java...
Author: cziegeler
Date: Wed Dec 24 01:44:56 2008
New Revision: 729287
URL: http://svn.apache.org/viewvc?rev=729287&view=rev
Log:
Adding unit tests
Added:
cocoon/whiteboard/xml-utils/src/test/
cocoon/whiteboard/xml-utils/src/test/java/
cocoon/whiteboard/xml-utils/src/test/java/org/
cocoon/whiteboard/xml-utils/src/test/java/org/apache/
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderStreamerTestCase.java (with props)
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderTestCase.java (with props)
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/AbstractXMLTestCase.java (with props)
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/NamespacesTableTestCase.java (with props)
cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/SAXBufferTestCase.java (with props)
Modified:
cocoon/whiteboard/xml-utils/pom.xml
cocoon/whiteboard/xml-utils/src/main/java/org/apache/cocoon/xml/sax/SAXBuffer.java
Modified: cocoon/whiteboard/xml-utils/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/xml-utils/pom.xml?rev=729287&r1=729286&r2=729287&view=diff
==============================================================================
--- cocoon/whiteboard/xml-utils/pom.xml (original)
+++ cocoon/whiteboard/xml-utils/pom.xml Wed Dec 24 01:44:56 2008
@@ -58,6 +58,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>xmlunit</groupId>
+ <artifactId>xmlunit</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
Modified: cocoon/whiteboard/xml-utils/src/main/java/org/apache/cocoon/xml/sax/SAXBuffer.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/xml-utils/src/main/java/org/apache/cocoon/xml/sax/SAXBuffer.java?rev=729287&r1=729286&r2=729287&view=diff
==============================================================================
--- cocoon/whiteboard/xml-utils/src/main/java/org/apache/cocoon/xml/sax/SAXBuffer.java (original)
+++ cocoon/whiteboard/xml-utils/src/main/java/org/apache/cocoon/xml/sax/SAXBuffer.java Wed Dec 24 01:44:56 2008
@@ -47,7 +47,7 @@
*
* @version $Id$
*/
-public class SAXBuffer implements Serializable {
+public class SAXBuffer implements ContentHandler, LexicalHandler, Serializable {
/**
* Stores list of {@link SaxBit} objects.
Added: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderStreamerTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderStreamerTestCase.java?rev=729287&view=auto
==============================================================================
--- cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderStreamerTestCase.java (added)
+++ cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderStreamerTestCase.java Wed Dec 24 01:44:56 2008
@@ -0,0 +1,248 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.cocoon.xml.dom;
+
+import org.custommonkey.xmlunit.XMLTestCase;
+import org.custommonkey.xmlunit.XMLUnit;
+
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.xml.sax.helpers.AttributesImpl;
+
+/**
+ * Testcase for DOMStreamer and DOMBuilder.
+ *
+ * @version $Id$
+ */
+public class DOMBuilderStreamerTestCase extends XMLTestCase {
+
+ public DOMBuilderStreamerTestCase(String name) {
+ super(name);
+ }
+
+ public void testBuilderWithOneElement() throws Exception {
+ AttributesImpl atts = new AttributesImpl();
+
+ DOMBuilder builder = new DOMBuilder();
+ builder.startDocument();
+ builder.startElement("", "root", "root", atts);
+ builder.endElement("", "root", "root");
+ builder.endDocument();
+
+ Document document = XMLUnit.buildControlDocument("<root/>");
+ assertXMLEqual(document, builder.getDocument());
+ }
+
+ public void testBuilderWithMoreElements() throws Exception {
+ AttributesImpl atts = new AttributesImpl();
+
+ DOMBuilder builder = new DOMBuilder();
+ builder.startDocument();
+ builder.startElement("", "root", "root", atts);
+ builder.startElement("", "node", "node", atts);
+ builder.endElement("", "node", "node");
+ builder.startElement("", "node", "node", atts);
+ builder.endElement("", "node", "node");
+ builder.endElement("", "root", "root");
+ builder.endDocument();
+
+ Document document = XMLUnit.buildControlDocument("<root><node/><node/></root>");
+ assertXMLEqual(document, builder.getDocument());
+ }
+
+ public void testBuilderWithText() throws Exception {
+ AttributesImpl atts = new AttributesImpl();
+
+ DOMBuilder builder = new DOMBuilder();
+ builder.startDocument();
+ builder.startElement("", "root", "root", atts);
+ builder.characters("abcd".toCharArray(), 0, 4);
+ builder.endElement("", "root", "node");
+ builder.endDocument();
+
+ Document document = XMLUnit.buildControlDocument("<root>abcd</root>");
+ assertXMLEqual(document, builder.getDocument());
+ }
+
+ /*public void testBuilderWithNS() throws Exception {
+ AttributesImpl atts = new AttributesImpl();
+
+ DOMBuilder builder = new DOMBuilder();
+ builder.startDocument();
+ builder.startPrefixMapping("", "http://xml.apache.org");
+ builder.startElement("", "root", "root", atts);
+ builder.endElement("", "node", "node");
+ builder.endPrefixMapping("");
+ builder.endDocument();
+
+ Document document = XMLUnit.buildControlDocument("<root xmlns=\"http://xml.apache.org\"/>");
+ assertXMLEqual(document, builder.getDocument());
+ }*/
+
+ /*public void testBuilderWithPrefix() throws Exception {
+ AttributesImpl atts = new AttributesImpl();
+
+ DOMBuilder builder = new DOMBuilder();
+ builder.startDocument();
+ builder.startPrefixMapping("bla", "http://xml.apache.org");
+ builder.startElement("http://xml.apache.org", "root", "bla:root", atts);
+ builder.endElement("http://xml.apache.org", "root", "bla:root");
+ builder.endPrefixMapping("bla");
+ builder.endDocument();
+
+ Document document = XMLUnit.buildControlDocument("<bla:root xmlns:bla=\"http://xml.apache.org\"/>");
+ assertXMLEqual(document, builder.getDocument());
+ }*/
+
+ /*public void testBuilderWithNSError() throws Exception {
+ AttributesImpl atts = new AttributesImpl();
+
+ DOMBuilder builder = new DOMBuilder();
+
+ try {
+ builder.startDocument();
+ builder.startPrefixMapping("bla", "http://xml.apache.org");
+ atts.addAttribute( "", "bla", "xmlns:bla", "CDATA", "http://xml.apache.org");
+ builder.startElement("http://xml.apache.org", "root", "bla:root", atts);
+ builder.endElement("http://xml.apache.org", "root", "bla:root");
+ builder.endPrefixMapping("bla");
+ builder.endDocument();
+
+ fail("DOMBuilder should throw exception because of permitted attribute");
+ } catch (Exception e) {
+ // nothing
+ }
+ }*/
+
+ public void testBuilderWithComments() throws Exception {
+ AttributesImpl atts = new AttributesImpl();
+
+ DOMBuilder builder = new DOMBuilder();
+ builder.startDocument();
+ builder.startElement("", "root", "root", atts);
+ builder.comment("abcd".toCharArray(), 0, 4);
+ builder.endElement("", "root", "node");
+ builder.endDocument();
+
+ Document document = XMLUnit.buildControlDocument("<root><!--abcd--></root>");
+
+ assertXMLEqual(document, builder.getDocument());
+ }
+
+ public void testBuilderWithCommentWithinDocType() throws Exception {
+ AttributesImpl atts = new AttributesImpl();
+
+ DOMBuilder builder = new DOMBuilder();
+ builder.startDocument();
+ builder.startDTD("skinconfig", null, null);
+ builder.comment("abcd".toCharArray(), 0, 4);
+ builder.endDTD();
+ builder.startElement("", "root", "root", atts);
+ builder.endElement("", "root", "node");
+ builder.endDocument();
+
+ Document document = XMLUnit.buildControlDocument("<!DOCTYPE skinconfig [<!--abcd-->]><root></root>");
+
+ print(document);
+ print(builder.getDocument());
+
+ assertXMLEqual(document, builder.getDocument());
+ }
+
+ public final void print(Document document) {
+ TransformerFactory factory = TransformerFactory.newInstance();
+ try
+ {
+ javax.xml.transform.Transformer serializer = factory.newTransformer();
+ serializer.transform(new DOMSource(document), new StreamResult(System.out));
+ System.out.println();
+ }
+ catch (TransformerException te)
+ {
+ te.printStackTrace();
+ }
+ }
+
+
+ public void testTestFacility() throws Exception {
+ Document document = XMLUnit.getControlParser().newDocument();
+ Element elemA = document.createElement("root");
+ document.appendChild(elemA);
+
+ Document oneElementDocument = XMLUnit.buildControlDocument("<root/>");
+ assertXMLEqual(oneElementDocument, document);
+
+ document = XMLUnit.getControlParser().newDocument();
+ elemA = document.createElement("node");
+ document.appendChild(elemA);
+
+ oneElementDocument = XMLUnit.buildControlDocument("<root/>");
+ assertXMLNotEqual(oneElementDocument, document);
+ }
+
+ public void testStreamer() throws Exception {
+
+ Document document = XMLUnit.getControlParser().newDocument();
+ Element elemA = document.createElement("root");
+ document.appendChild(elemA);
+
+ Element elemB = document.createElement("node");
+ elemA.appendChild(elemB);
+
+ elemB = document.createElement("node");
+ elemA.appendChild(elemB);
+
+ DOMBuilder builder = new DOMBuilder();
+ DOMStreamer streamer = new DOMStreamer(builder);
+
+ streamer.stream(document);
+
+ document = builder.getDocument();
+
+ Document moreElementDocument = XMLUnit.buildControlDocument("<root><node/><node/></root>");
+ assertXMLEqual(moreElementDocument, document);
+ }
+
+ /*public void testStreamerWithNS() throws Exception {
+
+ Document document = XMLUnit.getControlParser().newDocument();
+ Element elemA = document.createElementNS("http://xml.apache.org", "root");
+ document.appendChild(elemA);
+
+ Element elemB = document.createElementNS("http://xml.apache.org", "node");
+ elemA.appendChild(elemB);
+
+ elemB = document.createElementNS("http://xml.apache.org", "node");
+ elemA.appendChild(elemB);
+
+ DOMBuilder builder = new DOMBuilder();
+ DOMStreamer streamer = new DOMStreamer(builder);
+
+ streamer.stream(document);
+
+ document = builder.getDocument();
+
+ Document moreElementDocument = XMLUnit.buildControlDocument("<root xmlns=\"http://xml.apache.org\"><node/><node/></root>");
+ assertXMLEqual(moreElementDocument, document);
+ }*/
+}
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderStreamerTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderStreamerTestCase.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderStreamerTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderTestCase.java?rev=729287&view=auto
==============================================================================
--- cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderTestCase.java (added)
+++ cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderTestCase.java Wed Dec 24 01:44:56 2008
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.cocoon.xml.dom;
+
+import junit.framework.TestCase;
+
+import org.w3c.dom.Document;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+
+/**
+ * JUnit Testcase for {@link DOMBuilder}.
+ *
+ * @version $Id$
+ */
+public class DOMBuilderTestCase extends TestCase {
+
+ /**
+ * Constructor.
+ * @param name
+ */
+ public DOMBuilderTestCase(String name) {
+ super(name);
+ }
+
+ /**
+ * Test if two consecutive "characters" events result in two text nodes
+ * whose concatenation is equal to the concatenation
+ * of the two strings (cfr. bug #26219).
+ *
+ * @throws SAXException
+ */
+ public void testMultipleCharactersEvents() throws SAXException {
+ DOMBuilder builder = new DOMBuilder();
+ Attributes attrs = new AttributesImpl();
+ char c1[] = "ABC".toCharArray();
+ char c2[] = "DEF".toCharArray();
+ builder.startDocument();
+ builder.startElement("", "test", "test", attrs);
+ builder.characters(c1, 0, 3);
+ builder.characters(c2, 0, 3);
+ builder.endElement("", "test", "test");
+ builder.endDocument();
+ Document dom = builder.getDocument();
+ StringBuffer value = new StringBuffer();
+ for (int i = 0 ; i < dom.getDocumentElement().getChildNodes().getLength() ; ++i) {
+ value.append(dom.getDocumentElement().getChildNodes().item(i).getNodeValue());
+ }
+ assertEquals("Content of root element not what expected",
+ "ABCDEF", value.toString());
+ }
+}
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderTestCase.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/dom/DOMBuilderTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/AbstractXMLTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/AbstractXMLTestCase.java?rev=729287&view=auto
==============================================================================
--- cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/AbstractXMLTestCase.java (added)
+++ cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/AbstractXMLTestCase.java Wed Dec 24 01:44:56 2008
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.cocoon.xml.sax;
+
+import org.custommonkey.xmlunit.XMLTestCase;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+import org.apache.cocoon.xml.dom.DOMBuilder;
+
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.Source;
+import javax.xml.transform.Result;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.dom.DOMSource;
+import java.io.ByteArrayOutputStream;
+
+
+/**
+ * general functions for XML related Testcases
+ *
+ */
+public abstract class AbstractXMLTestCase extends XMLTestCase {
+
+ public AbstractXMLTestCase(String s) {
+ super(s);
+ }
+
+ protected void generateLargeSAX( ContentHandler consumer ) throws SAXException {
+ AttributesImpl atts = new AttributesImpl();
+
+ final int size = 66000;
+ char[] large = new char[size];
+ for(int i=0;i<size;i++) {
+ large[i] = '\uffff';
+ }
+
+ consumer.startDocument();
+ consumer.startElement("", "root", "root", atts);
+ for (int i=0; i<0x10000; ++i) {
+ final String uniqueString = "f_" + i;
+ consumer.startElement("", uniqueString, uniqueString, atts);
+ consumer.endElement("", uniqueString, uniqueString);
+ }
+ consumer.characters(large,0,size);
+ consumer.endElement("", "root", "root");
+ consumer.endDocument();
+ }
+
+ protected void generateSmallSAX( ContentHandler consumer ) throws SAXException {
+ AttributesImpl atts = new AttributesImpl();
+
+ consumer.startDocument();
+ consumer.startElement("", "root", "root", atts);
+ consumer.characters("test".toCharArray(),0,4);
+ consumer.endElement("", "root", "root");
+ consumer.endDocument();
+ }
+
+ protected byte[] generateByteArray() throws Exception {
+ DOMBuilder in = new DOMBuilder();
+ generateSmallSAX(in);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ TransformerFactory tFactory = TransformerFactory.newInstance();
+ Transformer t = tFactory.newTransformer();
+ Source input = new DOMSource(in.getDocument());
+ Result output = new StreamResult(bos);
+ t.transform(input, output);
+ bos.close();
+
+ return bos.toByteArray();
+ }
+}
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/AbstractXMLTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/AbstractXMLTestCase.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/AbstractXMLTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/NamespacesTableTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/NamespacesTableTestCase.java?rev=729287&view=auto
==============================================================================
--- cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/NamespacesTableTestCase.java (added)
+++ cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/NamespacesTableTestCase.java Wed Dec 24 01:44:56 2008
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.cocoon.xml.sax;
+
+import junit.framework.TestCase;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * Test case for NamespacesTable
+ *
+ * @version $Id$
+ */
+public class NamespacesTableTestCase extends TestCase {
+ public NamespacesTableTestCase(String name) {
+ super(name);
+ }
+
+ public void testSimple() {
+ NamespacesTable ns = new NamespacesTable();
+
+ ns.addDeclaration("ns1", "http://ns1");
+ ns.addDeclaration("ns2", "http://ns2");
+
+ ns.enterScope();
+
+ assertEquals("http://ns1", ns.getUri("ns1"));
+ assertEquals("ns1", ns.getPrefix("http://ns1"));
+
+ assertEquals("http://ns2", ns.getUri("ns2"));
+ assertEquals("ns2", ns.getPrefix("http://ns2"));
+
+ ns.enterScope();
+
+ ns.addDeclaration("ns3", "http://ns3");
+ ns.enterScope();
+
+ assertEquals("ns1", ns.getPrefix("http://ns1"));
+ assertEquals("ns3", ns.getPrefix("http://ns3"));
+ assertEquals(0, ns.getCurrentScopeDeclarations().length);
+ ns.leaveScope();
+
+ // Declarations in this scope are no more visible...
+ assertNull(ns.getUri("ns3"));
+ // ... but still listed in the declared mappings
+ assertEquals(1, ns.getCurrentScopeDeclarations().length);
+ assertEquals("ns3", ns.getCurrentScopeDeclarations()[0].getPrefix());
+
+ ns.leaveScope();
+
+ assertNull(ns.getPrefix(ns.getPrefix("http://ns3")));
+ assertNull(ns.getUri("ns3"));
+
+ ns.leaveScope();
+ // Declarations that occured before this scope are no more visible...
+ assertNull(ns.getUri("ns1"));
+ assertNull(ns.getPrefix("http://ns1"));
+
+ assertNull(ns.getUri("ns2"));
+ assertNull(ns.getPrefix("http://ns2"));
+
+ //... but are still available in getDeclaredPrefixes
+ NamespacesTable.Declaration[] prefixes = ns.getCurrentScopeDeclarations();
+ assertEquals(2, prefixes.length);
+
+ assertEquals("ns2", prefixes[0].getPrefix());
+ assertEquals("http://ns2", prefixes[0].getUri());
+ assertEquals("ns1", prefixes[1].getPrefix());
+ assertEquals("http://ns1", prefixes[1].getUri());
+
+ }
+
+ public void testOverride() {
+ NamespacesTable ns = new NamespacesTable();
+
+ ns.addDeclaration("ns1", "http://ns1");
+ ns.enterScope();
+ ns.addDeclaration("ns1", "http://otherns1");
+ ns.enterScope();
+ ns.addDeclaration("ns1", "http://yetanotherns1");
+ ns.enterScope();
+
+ assertEquals("http://yetanotherns1", ns.getUri("ns1"));
+ assertEquals(0, ns.getPrefixes("http://ns1").length);
+
+ ns.leaveScope();
+ ns.leaveScope();
+
+ assertEquals("http://ns1", ns.getUri("ns1"));
+ assertEquals(1, ns.getPrefixes("http://ns1").length);
+
+ ns.leaveScope();
+ assertNull(ns.getUri("ns1"));
+ }
+
+ public void testMultiDeclaration() {
+ NamespacesTable ns = new NamespacesTable();
+ ns.addDeclaration("ns1", "http://ns1");
+ ns.enterScope();
+ // two in the same scope
+ ns.addDeclaration("ns2", "http://ns1");
+ ns.addDeclaration("ns3", "http://ns1");
+ ns.enterScope();
+
+ String[] prefixes = ns.getPrefixes("http://ns1");
+ assertEquals(3, prefixes.length);
+ assertEquals("ns3", prefixes[0]);
+ assertEquals("ns2", prefixes[1]);
+ assertEquals("ns1", prefixes[2]);
+ }
+
+ public void testStreamDeclarations() throws Exception {
+ NamespacesTable ns = new NamespacesTable();
+ ns.addDeclaration("ns1", "http://ns1");
+ ns.enterScope();
+ ns.addDeclaration("ns2", "http://ns2");
+ ns.enterScope(new DefaultHandler() {
+ public void startPrefixMapping(String prefix, String uri) throws org.xml.sax.SAXException {
+ assertEquals("ns2", prefix);
+ assertEquals("http://ns2", uri);
+ }
+ });
+
+ // Enter and leave a nested scope
+ ns.addDeclaration("ns3", "http://ns3");
+ ns.enterScope();
+ ns.leaveScope();
+
+ ns.leaveScope(new DefaultHandler() {
+ public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException {
+ assertEquals("ns2", prefix);
+ }
+ });
+ }
+
+ /**
+ * A scenario that occurs in with jx:import where some prefixes are started but not used.
+ * @throws Exception
+ */
+ public void testJXImport() throws Exception {
+ NamespacesTable ns = new NamespacesTable();
+ ContentHandler handler = new DefaultHandler();
+
+ ns.addDeclaration("ft", "http://apache.org/cocoon/forms/1.0#template");
+ ns.addDeclaration("fi", "http://apache.org/cocoon/forms/1.0#instance");
+ ns.addDeclaration("jx", "http://apache.org/cocoon/templates/jx/1.0");
+ ns.enterScope(handler);
+ assertEquals("ft", ns.getPrefix("http://apache.org/cocoon/forms/1.0#template"));
+ assertEquals("fi", ns.getPrefix("http://apache.org/cocoon/forms/1.0#instance"));
+ assertEquals("jx", ns.getPrefix("http://apache.org/cocoon/templates/jx/1.0"));
+
+ // Add declarations that won't be used
+ ns.addDeclaration("jx", "http://apache.org/cocoon/templates/jx/1.0");
+ ns.addDeclaration("fi", "http://apache.org/cocoon/forms/1.0#instance");
+ ns.addDeclaration("bu", "http://apache.org/cocoon/browser-update/1.0");
+
+ ns.leaveScope(handler);
+ assertNull(ns.getPrefix("http://apache.org/cocoon/forms/1.0#template"));
+ assertNull(ns.getPrefix("http://apache.org/cocoon/forms/1.0#instance"));
+ assertNull(ns.getPrefix("http://apache.org/cocoon/templates/jx/1.0"));
+ assertEquals(3, ns.getCurrentScopeDeclarations().length);
+
+ }
+
+ public void testDuplicate() throws Exception {
+ NamespacesTable ns = new NamespacesTable();
+
+ ns.addDeclaration("ns1", "http://ns1");
+ ns.enterScope();
+ ns.addDeclaration("ns1", "http://ns1");
+ ns.enterScope();
+ ns.leaveScope();
+ ns.removeDeclaration("ns1");
+ assertEquals("http://ns1", ns.getUri("ns1"));
+ ns.leaveScope();
+ ns.removeDeclaration("ns1");
+ assertNull(ns.getUri("ns1"));
+ }
+
+}
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/NamespacesTableTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/NamespacesTableTestCase.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/NamespacesTableTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/SAXBufferTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/SAXBufferTestCase.java?rev=729287&view=auto
==============================================================================
--- cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/SAXBufferTestCase.java (added)
+++ cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/SAXBufferTestCase.java Wed Dec 24 01:44:56 2008
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.cocoon.xml.sax;
+
+import java.io.ByteArrayInputStream;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.cocoon.xml.dom.DOMBuilder;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * Testcase for SaxBuffer
+ *
+ */
+public final class SAXBufferTestCase extends AbstractXMLTestCase {
+ public SAXBufferTestCase(String s) {
+ super(s);
+ }
+
+ public void testCompareDOM() throws Exception {
+ DOMBuilder in = new DOMBuilder();
+ generateLargeSAX(in);
+
+ SAXBuffer sb = new SAXBuffer();
+ generateLargeSAX(sb);
+
+ DOMBuilder out = new DOMBuilder();
+ sb.toSAX(out);
+
+ assertXMLEqual(in.getDocument(), out.getDocument());
+ }
+
+ public void testStressLoop() throws Exception {
+ SAXBuffer sb = new SAXBuffer();
+
+ long loop = 10000;
+
+ // simply consume documents
+ long start = System.currentTimeMillis();
+ for(int i=0;i<loop;i++) {
+ generateSmallSAX(sb);
+ sb.recycle();
+ }
+ long stop = System.currentTimeMillis() + 1;
+
+ double r = 1000*loop/(stop-start);
+ System.out.println("consuming: "+ r + " documents per second");
+ }
+
+ public void testCompareToParsing() throws Exception {
+ DOMBuilder in = new DOMBuilder();
+ generateSmallSAX(in);
+
+ SAXParserFactory pfactory = SAXParserFactory.newInstance();
+ SAXParser p = pfactory.newSAXParser();
+
+
+ DefaultHandler ch = new DefaultHandler();
+
+ SAXBuffer b = new SAXBuffer();
+ ByteArrayInputStream bis = new ByteArrayInputStream(generateByteArray());
+
+ long loop = 10000;
+
+ long start = System.currentTimeMillis();
+ for(int i=0;i<loop;i++) {
+ b.recycle();
+ bis.reset();
+ p.parse(bis, ch);
+ }
+ long stop = System.currentTimeMillis() + 1;
+
+ double r = 1000*loop/(stop-start);
+ System.out.println("parsed:" + r + " documents per second");
+
+
+
+ start = System.currentTimeMillis();
+ for(int i=0;i<loop;i++) {
+ b.toSAX(ch);
+ }
+ stop = System.currentTimeMillis() + 1;
+
+ r = 1000*loop/(stop-start);
+ System.out.println("recalling: " + r + " documents per second");
+ }
+
+
+}
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/SAXBufferTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/SAXBufferTestCase.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange: cocoon/whiteboard/xml-utils/src/test/java/org/apache/cocoon/xml/sax/SAXBufferTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain