synapse-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asan...@apache.org
Subject svn commit: r608568 - in /webservices/synapse/trunk/java/modules/core/src: main/java/org/apache/synapse/util/ test/java/org/apache/synapse/util/
Date Thu, 03 Jan 2008 17:21:10 GMT
Author: asankha
Date: Thu Jan  3 09:21:10 2008
New Revision: 608568

URL: http://svn.apache.org/viewvc?rev=608568&view=rev
Log:
fix SYNAPSE-216 by applying patch submitted by Andreas Veithen

Added:
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/WrappedTextNodeStreamReader.java
    webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/
    webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/TextFileDataSourceTest.java
    webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/WrappedTextNodeStreamReaderTest.java
Modified:
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/TextFileDataSource.java

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/TextFileDataSource.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/TextFileDataSource.java?rev=608568&r1=608567&r2=608568&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/TextFileDataSource.java
(original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/TextFileDataSource.java
Thu Jan  3 09:21:10 2008
@@ -25,7 +25,6 @@
 import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
 import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
-import org.apache.axiom.om.util.StAXUtils;
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.transport.base.BaseConstants;
 
@@ -38,13 +37,9 @@
 
 public class TextFileDataSource implements OMDataSource {
 
-    private static final byte[] prefix =
-        "<text xmlns=\"http://ws.apache.org/commons/ns/payload\">".getBytes();
-    private static final byte[] suffix = "</text>".getBytes();
     private static final byte[] empty =
         "<text xmlns=\"http://ws.apache.org/commons/ns/payload\"/>".getBytes();
     private InputStream is = null;
-    private int i = 0, j = 0;
 
     public TextFileDataSource(DataSource ds) {
         try {
@@ -57,14 +52,11 @@
 
     public void serialize(OutputStream out, OMOutputFormat format) throws XMLStreamException
{
         try {
-            //out.write(prefix);
-            // Transfer bytes from is to out
             byte[] buf = new byte[4096];
             int len;
             while ((len = is.read(buf)) > 0) {
                 out.write(buf, 0, len);
             }
-            //out.write(suffix);
         } catch (IOException e) {
             throw new SynapseException("Error serializing TextFileDataSource to an OutputStream",
e);
         }
@@ -84,67 +76,6 @@
     }
 
     public XMLStreamReader getReader() throws XMLStreamException {
-        return StAXUtils.createXMLStreamReader(getInputStream());
-    }
-
-    private InputStream getInputStream() {
-
-        return new InputStream() {
-
-            public int read(byte b[]) throws IOException {
-                return read(b, 0, b.length);
-            }
-
-            public int read(byte b[], int off, int len) throws IOException {
-                int pos = off;
-                if (i < prefix.length) {
-                    while (i < prefix.length && pos-off < len) {
-                        b[pos++] = prefix[i++];
-                    }
-                    return pos - off;
-                }
-
-                int ret = is.read(b, pos, len-pos);
-
-                if (ret == -1 && j < suffix.length) {
-                    while (j < suffix.length && pos-off < len) {
-                        b[pos++] = suffix[j++];
-                    }
-                    return pos - off;
-                }
-
-                return ret;
-            }
-
-            public int read() throws IOException {
-                if (i < prefix.length) {
-                    while (i < prefix.length) {
-                        return prefix[i++];
-                    }
-                }
-                int ret = is.read();
-
-                if (ret == -1 && j < suffix.length) {
-                    while (j < suffix.length) {
-                        return suffix[j++];
-                    }
-                }
-                return ret;
-            }
-        };
-    }
-
-    public static void main(String[] args) throws Exception {
-        TextFileDataSource textFileDataSource = new TextFileDataSource(
-            //    new File("/tmp/test.txt"));
-            new FileDataSource("/home/asankha/code/synapse/repository/conf/sample/resources/transform/message.xml"));
-
-        OMFactory fac = OMAbstractFactory.getOMFactory();
-        OMSourcedElementImpl element =
-            new OMSourcedElementImpl(
-                BaseConstants.DEFAULT_TEXT_WRAPPER, fac, textFileDataSource);
-        element.serializeAndConsume(new FileOutputStream("/tmp/out.txt"));
-        element.serialize(System.out);
-        //element.serializeAndConsume(System.out);
+        return new WrappedTextNodeStreamReader(BaseConstants.DEFAULT_TEXT_WRAPPER, new InputStreamReader(is));
     }
 }

Added: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/WrappedTextNodeStreamReader.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/WrappedTextNodeStreamReader.java?rev=608568&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/WrappedTextNodeStreamReader.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/WrappedTextNodeStreamReader.java
Thu Jan  3 09:21:10 2008
@@ -0,0 +1,439 @@
+/*
+ *  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.synapse.util;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.util.Collections;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.om.impl.EmptyOMLocation;
+import org.apache.axiom.om.impl.llom.util.NamespaceContextImpl;
+import org.apache.commons.io.IOUtils;
+
+/**
+ * {@link javax.xml.stream.XMLStreamException XMLInputStreamReader} implementation that
+ * represents a text node wrapped inside an element. The text data is provided by a
+ * {@link java.io.Reader Reader}.
+ * <p>
+ * It will produce the following sequence of XML events:
+ * <ul>
+ *   <li>START_DOCUMENT</li>
+ *   <li>START_ELEMENT</li>
+ *   <li>(CHARACTER)*</li>
+ *   <li>END_ELEMENT</li>
+ *   <li>END_DOCMENT</li>
+ * </ul>
+ * The class is implemented as a simple state machine, where the state is identified
+ * by the current event type and with the following transitions triggered by
+ * {@link #next()}:
+ * <ul>
+ *   <li>-1 &rarr; START_DOCUMENT</li>
+ *   <li>START_DOCUMENT &rarr; START_ELEMENT</li>
+ *   <li>START_ELEMENT &rarr; END_ELEMENT (if character stream is empty)</li>
+ *   <li>START_ELEMENT &rarr; CHARACTERS (if character stream is not empty)</li>
+ *   <li>CHARACTERS &rarr; CHARACTERS (if data available in stream)</li>
+ *   <li>CHARACTERS &rarr; END_ELEMENT (if end of stream reached)</li>
+ *   <li>END_ELEMENT &rarr; END_DOCUMENT</li>
+ * </ul>
+ * Additionally, {@link #getElementText()} triggers the following transition:
+ * <ul>
+ *   <li>START_ELEMENT &rarr; END_ELEMENT</li>
+ * </ul>
+ * Note that since multiple consecutive CHARACTERS events may be returned, this
+ * "parser" is not coalescing.
+ * 
+ */
+public class WrappedTextNodeStreamReader implements XMLStreamReader {
+    /**
+     * Location object returned by {@link #getLocation()}.
+     * It always returns -1 for the location and null for the publicId and systemId. 
+     */
+    private final static Location EMPTY_LOCATION = new EmptyOMLocation();
+    
+    /**
+     * The qualified name of the wrapper element.
+     */
+    private final QName wrapperElementName;
+    
+    /**
+     * The Reader object that represents the text data.
+     */
+    private final Reader reader;
+    
+    /**
+     * The maximum number of characters to return for each CHARACTER event.
+     */
+    private final int chunkSize;
+    
+    /**
+     * The type of the current XML event.
+     */
+    private int eventType = -1;
+    
+    /**
+     * The character data for the current event. This is only set if the current
+     * event is a CHARACTER event. The size of the array is determined by
+     * {@link #chunkSize}
+     */
+    private char[] charData;
+    
+    /**
+     * The length of the character data in {@link #charData}.
+     */
+    private int charDataLength;
+    
+    /**
+     * The namespace context applicable in the scope of the wrapper element.
+     * Beside the default mappings for xml and xmlns, it only contains the
+     * mapping for the namespace of the wrapper element.
+     * This attribute is initialized lazily by {@link #getNamespaceContext()}.
+     */
+    private NamespaceContext namespaceContext;
+    
+    /**
+     * Create a new instance.
+     * 
+     * @param wrapperElementName the qualified name of the wrapper element
+     * @param reader the Reader object holding the character data to be wrapped
+     * @param chunkSize the maximum number of characters that are returned for each CHARACTER
event
+     */
+    public WrappedTextNodeStreamReader(QName wrapperElementName, Reader reader, int chunkSize)
{
+        this.wrapperElementName = wrapperElementName;
+        this.reader = reader;
+        this.chunkSize = chunkSize;
+    }
+    
+    /**
+     * Create a new instance with chunk size 4096.
+     * 
+     * @param wrapperElementName the qualified name of the wrapper element
+     * @param reader the Reader object holding the character data to be wrapped
+     */
+    public WrappedTextNodeStreamReader(QName wrapperElementName, Reader reader) {
+        this(wrapperElementName, reader, 4096);
+    }
+
+    public Object getProperty(String name) throws IllegalArgumentException {
+        // We don't define any properties
+        return null;
+    }
+    
+    //
+    // Methods to manipulate the parser state
+    //
+    
+    public boolean hasNext() throws XMLStreamException {
+        return eventType != END_DOCUMENT;
+    }
+    
+    public int next() throws XMLStreamException {
+        // Determine next event type based on current event type. If current event type
+        // is START_ELEMENT or CHARACTERS, pull new data from the reader.
+        switch (eventType) {
+            case -1:
+                eventType = START_DOCUMENT;
+                break;
+            case START_DOCUMENT:
+                eventType = START_ELEMENT;
+                break;
+            case START_ELEMENT:
+                charData = new char[chunkSize];
+                // No break here!
+            case CHARACTERS:
+                try {
+                    charDataLength = reader.read(charData);
+                }
+                catch (IOException ex) {
+                    throw new XMLStreamException(ex);
+                }
+                if (charDataLength == -1) {
+                    charData = null;
+                    eventType = END_ELEMENT;
+                } else {
+                    eventType = CHARACTERS;
+                }
+                break;
+            case END_ELEMENT:
+                eventType = END_DOCUMENT;
+                break;
+            default:
+                throw new IllegalStateException();
+        }
+        return eventType;
+    }
+    
+    public int nextTag() throws XMLStreamException {
+        // We don't have white space, comments or processing instructions
+        throw new XMLStreamException("Current event is not white space");
+    }
+
+    public int getEventType() {
+        return eventType;
+    }
+
+    public boolean isStartElement() { return eventType == START_ELEMENT; }
+    public boolean isEndElement() { return eventType == END_ELEMENT; }
+    public boolean isCharacters() { return eventType == CHARACTERS; }
+    public boolean isWhiteSpace() { return false; }
+    public boolean hasText() { return eventType == CHARACTERS; }
+    public boolean hasName() { return eventType == START_ELEMENT || eventType == END_ELEMENT;
}
+    
+    public void require(int type, String namespaceURI, String localName) throws XMLStreamException
{
+        if (type != eventType
+             || (namespaceURI != null && !namespaceURI.equals(getNamespaceURI()))
+             || (localName != null && !namespaceURI.equals(getLocalName()))) {
+            throw new XMLStreamException("Unexpected event type");
+        }
+    }
+    
+    public Location getLocation() {
+        // We do not support location information
+        return EMPTY_LOCATION;
+    }
+    
+    public void close() throws XMLStreamException {
+        // Javadoc says that this method should not close the underlying input source,
+        // but we need to close the reader somewhere.
+        try {
+            reader.close();
+        }
+        catch (IOException ex) {
+            throw new XMLStreamException(ex);
+        }
+    }
+
+    //
+    // Methods related to the xml declaration.
+    //
+    
+    public String getEncoding() {
+        // Encoding is not known (not relevant?)
+        return null;
+    }
+
+    public String getCharacterEncodingScheme() {
+        // Encoding is not known (not relevant?)
+        return null;
+    }
+
+    public String getVersion() {
+        // Version is not relevant
+        return null;
+    }
+
+    public boolean standaloneSet() {
+        return false;
+    }
+
+    public boolean isStandalone() {
+        return true;
+    }
+    
+    //
+    // Methods related to the namespace context
+    //
+    
+    public NamespaceContext getNamespaceContext() {
+        if (namespaceContext == null) {
+            namespaceContext = new NamespaceContextImpl(Collections.singletonMap(wrapperElementName.getPrefix(),
wrapperElementName.getNamespaceURI()));
+        }
+        return namespaceContext;
+    }
+    
+    public String getNamespaceURI(String prefix) {
+        String namespaceURI = getNamespaceContext().getNamespaceURI(prefix);
+        // NamespaceContext#getNamespaceURI and XMLStreamReader#getNamespaceURI have slightly
+        // different semantics for unbound prefixes.
+        return namespaceURI.equals(XMLConstants.NULL_NS_URI) ? null : prefix;
+    }
+    
+    //
+    // Methods related to elements
+    //
+    
+    private void checkStartElement() {
+        if (eventType != START_ELEMENT) {
+            throw new IllegalStateException();
+        }
+    }
+    
+    public String getAttributeValue(String namespaceURI, String localName) {
+        checkStartElement();
+        return null;
+    }
+
+    public int getAttributeCount() {
+        checkStartElement();
+        return 0;
+    }
+    
+    public QName getAttributeName(int index) {
+        checkStartElement();
+        throw new ArrayIndexOutOfBoundsException();
+    }
+
+    public String getAttributeLocalName(int index) {
+        checkStartElement();
+        throw new ArrayIndexOutOfBoundsException();
+    }
+
+    public String getAttributePrefix(int index) {
+        checkStartElement();
+        throw new ArrayIndexOutOfBoundsException();
+    }
+
+    public String getAttributeNamespace(int index) {
+        checkStartElement();
+        throw new ArrayIndexOutOfBoundsException();
+    }
+
+    public String getAttributeType(int index) {
+        checkStartElement();
+        throw new ArrayIndexOutOfBoundsException();
+    }
+
+    public String getAttributeValue(int index) {
+        checkStartElement();
+        throw new ArrayIndexOutOfBoundsException();
+    }
+
+    public boolean isAttributeSpecified(int index) {
+        checkStartElement();
+        throw new ArrayIndexOutOfBoundsException();
+    }
+    
+    private void checkElement() {
+        if (eventType != START_ELEMENT && eventType != END_ELEMENT) {
+            throw new IllegalStateException();
+        }
+    }
+    
+    public QName getName() {
+        return null;
+    }
+
+    public String getLocalName() {
+        checkElement();
+        return wrapperElementName.getLocalPart();
+    }
+
+    public String getPrefix() {
+        return wrapperElementName.getPrefix();
+    }
+
+    public String getNamespaceURI() {
+        checkElement();
+        return wrapperElementName.getNamespaceURI();
+    }
+    
+    public int getNamespaceCount() {
+        checkElement();
+        // There is one namespace declared on the wrapper element
+        return 1;
+    }
+
+    public String getNamespacePrefix(int index) {
+        checkElement();
+        if (index == 0) {
+            return wrapperElementName.getPrefix();
+        } else {
+            throw new IndexOutOfBoundsException();
+        }
+    }
+
+    public String getNamespaceURI(int index) {
+        checkElement();
+        if (index == 0) {
+            return wrapperElementName.getNamespaceURI();
+        } else {
+            throw new IndexOutOfBoundsException();
+        }
+    }
+    
+    public String getElementText() throws XMLStreamException {
+        if (eventType == START_ELEMENT) {
+            // Actually the purpose of this class is to avoid storing
+            // the character data entirely in memory, but if the caller
+            // wants a String, we don't have the choice...
+            try {
+                String result = IOUtils.toString(reader);
+                eventType = END_ELEMENT;
+                return result;
+            }
+            catch (IOException ex) {
+                throw new XMLStreamException(ex);
+            }
+        } else {
+            throw new XMLStreamException("Current event is not a START_ELEMENT");
+        }
+    }
+
+    private void checkCharacters() {
+        if (eventType != CHARACTERS) {
+            throw new IllegalStateException();
+        }
+    }
+    
+    public String getText() {
+        checkCharacters();
+        return new String(charData, 0, charDataLength);
+    }
+
+    public char[] getTextCharacters() {
+        checkCharacters();
+        return charData;
+    }
+
+    public int getTextStart() {
+        checkCharacters();
+        return 0;
+    }
+
+    public int getTextLength() {
+        checkCharacters();
+        return charDataLength;
+    }
+
+    public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
throws XMLStreamException {
+        checkCharacters();
+        int c = Math.min(charDataLength-sourceStart, length);
+        System.arraycopy(charData, sourceStart, target, targetStart, c);
+        return c;
+    }
+    
+    //
+    // Methods related to processing instructions
+    //
+    
+    public String getPIData() {
+        throw new IllegalStateException();
+    }
+
+    public String getPITarget() {
+        throw new IllegalStateException();
+    }
+}

Added: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/TextFileDataSourceTest.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/TextFileDataSourceTest.java?rev=608568&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/TextFileDataSourceTest.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/TextFileDataSourceTest.java
Thu Jan  3 09:21:10 2008
@@ -0,0 +1,43 @@
+/*
+ *  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.synapse.util;
+
+import org.apache.axiom.attachments.ByteArrayDataSource;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
+import org.apache.synapse.transport.base.BaseConstants;
+import org.apache.synapse.util.TextFileDataSource;
+
+import junit.framework.TestCase;
+
+public class TextFileDataSourceTest extends TestCase {
+
+    public void testWithXMLChars() throws Exception {
+        String testString = "Test string with ampersand (&)";
+        OMDataSource dataSource
+            = new TextFileDataSource(new ByteArrayDataSource(testString.getBytes("UTF-8")));
+        OMFactory omFactory = OMAbstractFactory.getOMFactory();
+        OMSourcedElementImpl element
+            = new OMSourcedElementImpl(BaseConstants.DEFAULT_TEXT_WRAPPER, omFactory, dataSource);
+        assertEquals(testString, element.getText());
+    }
+}

Added: webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/WrappedTextNodeStreamReaderTest.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/WrappedTextNodeStreamReaderTest.java?rev=608568&view=auto
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/WrappedTextNodeStreamReaderTest.java
(added)
+++ webservices/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/WrappedTextNodeStreamReaderTest.java
Thu Jan  3 09:21:10 2008
@@ -0,0 +1,154 @@
+/*
+ *  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.synapse.util;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
+import org.apache.synapse.util.WrappedTextNodeStreamReader;
+import org.custommonkey.xmlunit.XMLTestCase;
+
+public class WrappedTextNodeStreamReaderTest extends XMLTestCase {
+    //
+    // Tests that construct the Axiom tree and check the result
+    //
+    
+    private void testUsingBuilder(QName wrapperElementName,
+                                  String testString,
+                                  int chunkSize) {
+        StringReader reader = new StringReader(testString);
+        XMLStreamReader xmlStreamReader
+            = new WrappedTextNodeStreamReader(wrapperElementName, reader, chunkSize);
+        OMElement element = new StAXOMBuilder(xmlStreamReader).getDocumentElement();
+        assertEquals(wrapperElementName, element.getQName());
+        assertEquals(wrapperElementName.getPrefix(), element.getQName().getPrefix());
+        assertEquals(testString, element.getText());
+    }
+    
+    public void testShortStringUsingBuilder() {
+        testUsingBuilder(
+                new QName("urn:test", "test"),
+                "This is a test string for WrappedTextNodeStreamReader",
+                4096);
+    }
+    
+    public void testLongStringUsingBuilder() {
+        // "Long" is relative to the chunk size
+        testUsingBuilder(
+                new QName("urn:test", "test"),
+                "This is a test string for WrappedTextNodeStreamReader",
+                10);
+    }
+    
+    public void testWrapperElementWithoutNamespaceUsingBuilder() {
+        testUsingBuilder(
+                new QName("test"),
+                "This is a test string for WrappedTextNodeStreamReader",
+                4096);
+    }
+    
+    public void testWrapperElementWithPrefixUsingBuilder() {
+        testUsingBuilder(
+                new QName("urn:test", "bar", "foo"),
+                "This is a test string for WrappedTextNodeStreamReader",
+                4096);
+    }
+    
+    //
+    // Test that serialize the stream of XML events to plain XML and compare
+    // with the expected result.
+    //
+    
+    private void testUsingSerializer(QName wrapperElementName,
+                                     String testString,
+                                     int chunkSize,
+                                     String expectedXML) throws Exception {
+        StringReader reader = new StringReader(testString);
+        XMLStreamReader xmlStreamReader
+            = new WrappedTextNodeStreamReader(wrapperElementName, reader, chunkSize);
+        StringWriter writer = new StringWriter();
+        XMLStreamWriter xmlStreamWriter
+            = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
+        new StreamingOMSerializer().serialize(xmlStreamReader, xmlStreamWriter);
+        xmlStreamWriter.flush();
+        assertXMLEqual(expectedXML, writer.toString());
+    }
+
+    public void testShortStringUsingSerializer() throws Exception {
+        String testString = "This is a test string for WrappedTextNodeStreamReader";
+        testUsingSerializer(
+                new QName("urn:test", "test"),
+                testString,
+                4096,
+                "<test xmlns=\"urn:test\">" + testString + "</test>");
+    }
+
+    public void testLongStringUsingSerializer() throws Exception {
+        String testString = "This is a test string for WrappedTextNodeStreamReader";
+        testUsingSerializer(
+                new QName("urn:test", "test"),
+                testString,
+                10,
+                "<test xmlns=\"urn:test\">" + testString + "</test>");
+    }
+
+    public void testStringWithAmpersandUsingSerializer() throws Exception {
+        testUsingSerializer(
+                new QName("urn:test", "test"),
+                "String containing ampersand (&)",
+                4096,
+                "<test xmlns=\"urn:test\">String containing ampersand (&amp;)</test>");
+    }
+    
+    //
+    // Tests that construct the Axiom tree, serialize it using serializeAndConsume and
+    // compare with the expected result.
+    //
+    
+    private void testUsingSerializeAndConsume(QName wrapperElementName,
+                                              String testString,
+                                              int chunkSize,
+                                              String expectedXML) throws Exception {
+        StringReader reader = new StringReader(testString);
+        XMLStreamReader xmlStreamReader
+            = new WrappedTextNodeStreamReader(wrapperElementName, reader, chunkSize);
+        OMElement element = new StAXOMBuilder(xmlStreamReader).getDocumentElement();
+        StringWriter writer = new StringWriter();
+        element.serializeAndConsume(writer);
+        assertXMLEqual(expectedXML, writer.toString());
+    }
+    
+    public void testShortStringUsingSerializeAndConsume() throws Exception {
+        String testString = "This is a test string for WrappedTextNodeStreamReader";
+        testUsingSerializeAndConsume(
+                new QName("urn:test", "test"),
+                testString,
+                4096,
+                "<test xmlns=\"urn:test\">" + testString + "</test>");
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org


Mime
View raw message