You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2011/07/25 17:09:28 UTC
svn commit: r1150741 - in /cxf/branches/2.4.x-fixes: ./
common/common/src/main/java/org/apache/cxf/staxutils/transform/
common/common/src/test/java/org/apache/cxf/staxutils/
common/common/src/test/java/org/apache/cxf/staxutils/transform/
Author: sergeyb
Date: Mon Jul 25 15:09:27 2011
New Revision: 1150741
URL: http://svn.apache.org/viewvc?rev=1150741&view=rev
Log:
Merged revisions 1150739 via svnmerge from
https://svn.apache.org/repos/asf/cxf/trunk
........
r1150739 | sergeyb | 2011-07-25 16:05:57 +0100 (Mon, 25 Jul 2011) | 1 line
[CXF-3681] More tests from Andi and a fix for PartialXMLStreamReader issue
........
Added:
cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java
- copied unchanged from r1150739, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java
Modified:
cxf/branches/2.4.x-fixes/ (props changed)
cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java
cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jul 25 15:09:27 2011
@@ -1 +1 @@
-/cxf/trunk:1149655,1150498
+/cxf/trunk:1149655,1150498,1150739
Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java?rev=1150741&r1=1150740&r2=1150741&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java (original)
+++ cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java Mon Jul 25 15:09:27 2011
@@ -87,7 +87,6 @@ class DelegatingNamespaceContext impleme
return prefix;
}
- @SuppressWarnings("unchecked")
public Iterator getPrefixes(String ns) {
return nc.getPrefixes(ns);
}
Modified: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java?rev=1150741&r1=1150740&r2=1150741&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java (original)
+++ cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java Mon Jul 25 15:09:27 2011
@@ -42,6 +42,7 @@ public class InTransformReader extends D
private int previousDepth = -1;
private boolean blockOriginalReader = true;
private DelegatingNamespaceContext namespaceContext;
+ private boolean appendInProgress;
public InTransformReader(XMLStreamReader reader,
Map<String, String> inMap,
@@ -64,7 +65,8 @@ public class InTransformReader extends D
}
public int next() throws XMLStreamException {
- if (currentQName != null) {
+ if (currentQName != null && appendInProgress) {
+ appendInProgress = false;
return XMLStreamConstants.START_ELEMENT;
} else if (previousDepth != -1 && previousDepth == getDepth() + 1) {
previousDepth = -1;
@@ -140,6 +142,7 @@ public class InTransformReader extends D
QName theName = readCurrentElement();
QName appendQName = inAppendMap.remove(theName);
if (appendQName != null) {
+ appendInProgress = true;
previousDepth = getDepth();
previousQName = theName;
currentQName = appendQName;
Modified: cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java?rev=1150741&r1=1150740&r2=1150741&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java (original)
+++ cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java Mon Jul 25 15:09:27 2011
@@ -21,13 +21,25 @@ package org.apache.cxf.staxutils.transfo
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.io.StringReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.staxutils.PartialXMLStreamReader;
import org.apache.cxf.staxutils.StaxUtils;
+
import org.junit.Assert;
import org.junit.Test;
@@ -63,6 +75,23 @@ public class InTransformReaderTest exten
}
@Test
+ public void testTransformAndAppend() throws Exception {
+ InputStream is = new ByteArrayInputStream("<test/>".getBytes());
+ XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+ reader = new InTransformReader(reader,
+ Collections.singletonMap("test", "test2"),
+ Collections.singletonMap("test", "wrapper"),
+ false);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ StaxUtils.copy(reader, bos);
+ String value = bos.toString();
+ System.out.println(value);
+ assertTrue("<wrapper><test2/></wrapper>".equals(value));
+ }
+
+
+ @Test
public void testReadWithSameNamespace() throws Exception {
InputStream is = new ByteArrayInputStream(
("<test xmlns=\"http://bar\" xmlns:ns1=\"http://foo\">"
@@ -241,4 +270,253 @@ public class InTransformReaderTest exten
+ "xmlns:ns4=\"http://cxf.apache.org/transform/fault\"/></soap:Body></ps1:TheEnvelope>").
equals(value.trim()));
}
+
+ @Test
+ public void testReadPartialWithComplexRequestSameNamespace() throws Exception {
+ InputStream is = new ByteArrayInputStream(
+ ("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ + "<soap:Header>"
+ + "<ns2:SoapHeaderIn xmlns:ns4=\"http://cxf.apache.org/transform/fault\" "
+ + "xmlns:ns3=\"http://cxf.apache.org/transform/header/element\" "
+ + "xmlns:ns2=\"http://cxf.apache.org/transform/header\" "
+ + "xmlns=\"http://cxf.apache.org/transform/test\">"
+ + "<ns2:OperationalMode>SIMULATION1</ns2:OperationalMode>"
+ + "<ns2:SomeComplexHeaderType>"
+ + "<ns3:CallerCorrelationId>SomeComplexValue</ns3:CallerCorrelationId>"
+ + "</ns2:SomeComplexHeaderType>"
+ + "</ns2:SoapHeaderIn>"
+ + "</soap:Header>"
+ + "<soap:Body>"
+ + "<TransformTestRequest xmlns=\"http://cxf.apache.org/transform/test\" "
+ + "xmlns:ns2=\"http://cxf.apache.org/transform/header\" "
+ + "xmlns:ns3=\"http://cxf.apache.org/transform/header/element\" "
+ + "xmlns:ns4=\"http://cxf.apache.org/transform/fault\" />"
+ + "</soap:Body></soap:Envelope>").getBytes());
+ XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+
+ Map<String, String> inMap = new HashMap<String, String>();
+ inMap.put("{http://cxf.apache.org/transform/header/element}*",
+ "{http://cxf.apache.org/transform/header/element}*");
+
+ reader = new InTransformReader(reader,
+ inMap,
+ null, false);
+
+ QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body");
+ PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag);
+
+ Document doc = StaxUtils.read(filteredReader);
+
+ assertNotNull(doc);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(bos);
+ StaxUtils.copy(doc, writer);
+ writer.flush();
+
+ String value = bos.toString();
+
+ Document docTransformed = DOMUtils.readXml(new StringReader(value));
+ Element env = docTransformed.getDocumentElement();
+ assertEquals("http://schemas.xmlsoap.org/soap/envelope/", env.getNamespaceURI());
+ assertEquals("soap", env.getPrefix());
+
+ Element header = getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Header", "soap");
+
+ Element customHeader = getElement(header, "http://cxf.apache.org/transform/header",
+ "SoapHeaderIn", "ns2");
+ NamedNodeMap map = customHeader.getAttributes();
+ assertEquals(4, map.getLength());
+ validateAttribute(map, "xmlns", "http://cxf.apache.org/transform/test");
+ validateAttribute(map, "xmlns:ns2", "http://cxf.apache.org/transform/header");
+ validateAttribute(map, "xmlns:ns4", "http://cxf.apache.org/transform/fault");
+ validateAttribute(map, "xmlns:ps1", "http://cxf.apache.org/transform/header/element");
+
+ Element mode =
+ getElement(customHeader, "http://cxf.apache.org/transform/header", "OperationalMode", "ns2");
+ assertEquals("SIMULATION1", mode.getTextContent());
+ Element headerType =
+ getElement(customHeader, "http://cxf.apache.org/transform/header",
+ "SomeComplexHeaderType", "ns2");
+
+ Element correlationId =
+ getElement(headerType, "http://cxf.apache.org/transform/header/element",
+ "CallerCorrelationId", "ps1");
+ assertEquals("SomeComplexValue", correlationId.getTextContent());
+ getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Body", "soap");
+ }
+
+ @Test
+ public void testPartialReadWithComplexRequestMultipleNamespace() throws Exception {
+ InputStream is = new ByteArrayInputStream(
+ ("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ + "<soap:Header>"
+ + "<ns2:SoapHeaderIn xmlns:ns4=\"http://cxf.apache.org/transform/fault\" "
+ + "xmlns:ns3=\"http://cxf.apache.org/transform/header/element\" "
+ + "xmlns:ns2=\"http://cxf.apache.org/transform/header\" "
+ + "xmlns=\"http://cxf.apache.org/transform/test\">"
+ + "<ns2:OperationalMode>SIMULATION1</ns2:OperationalMode>"
+ + "<ns2:SomeComplexHeaderType>"
+ + "<ns3:CallerCorrelationId>SomeComplexValue</ns3:CallerCorrelationId>"
+ + "</ns2:SomeComplexHeaderType>"
+ + "</ns2:SoapHeaderIn>"
+ + "</soap:Header>"
+ + "<soap:Body>"
+ + "<TransformTestRequest xmlns=\"http://cxf.apache.org/transform/test\" "
+ + "xmlns:ns2=\"http://cxf.apache.org/transform/header\" "
+ + "xmlns:ns3=\"http://cxf.apache.org/transform/header/element\" "
+ + "xmlns:ns4=\"http://cxf.apache.org/transform/fault\" />"
+ + "</soap:Body></soap:Envelope>").getBytes());
+ XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+
+ Map<String, String> inMap = new HashMap<String, String>();
+ inMap.put("{http://cxf.apache.org/transform/header/element}*",
+ "{http://cxf.apache.org/transform/header/otherelement}*");
+ inMap.put("{http://cxf.apache.org/transform/test}*",
+ "{http://cxf.apache.org/transform/othertest}*");
+
+ reader = new InTransformReader(reader,
+ inMap,
+ null, false);
+
+ QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body");
+ PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag);
+
+ Document doc = StaxUtils.read(filteredReader);
+
+ assertNotNull(doc);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(bos);
+ StaxUtils.copy(doc, writer);
+ writer.flush();
+
+ String value = bos.toString();
+
+ Document docTransformed = DOMUtils.readXml(new StringReader(value));
+ Element env = docTransformed.getDocumentElement();
+ assertEquals("http://schemas.xmlsoap.org/soap/envelope/", env.getNamespaceURI());
+ assertEquals("soap", env.getPrefix());
+
+ Element header = getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Header", "soap");
+
+ Element customHeader = getElement(header, "http://cxf.apache.org/transform/header",
+ "SoapHeaderIn", "ns2");
+ NamedNodeMap map = customHeader.getAttributes();
+ assertEquals(4, map.getLength());
+ validateAttribute(map, "xmlns:ps2", "http://cxf.apache.org/transform/othertest");
+ validateAttribute(map, "xmlns:ns2", "http://cxf.apache.org/transform/header");
+ validateAttribute(map, "xmlns:ns4", "http://cxf.apache.org/transform/fault");
+ validateAttribute(map, "xmlns:ps1", "http://cxf.apache.org/transform/header/otherelement");
+
+ Element mode =
+ getElement(customHeader, "http://cxf.apache.org/transform/header", "OperationalMode", "ns2");
+ assertEquals("SIMULATION1", mode.getTextContent());
+ Element headerType =
+ getElement(customHeader, "http://cxf.apache.org/transform/header",
+ "SomeComplexHeaderType", "ns2");
+
+ Element correlationId =
+ getElement(headerType, "http://cxf.apache.org/transform/header/otherelement",
+ "CallerCorrelationId", "ps1");
+ assertEquals("SomeComplexValue", correlationId.getTextContent());
+ getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Body", "soap");
+
+ }
+
+ @Test
+ public void testPartialReadWithComplexTransformationNamespace() throws Exception {
+ InputStream is = new ByteArrayInputStream(
+ ("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ + "<soap:Header>"
+ + "<ns2:SoapHeaderIn xmlns:ns4=\"http://cxf.apache.org/transform/fault\" "
+ + "xmlns:ns3=\"http://cxf.apache.org/transform/header/element\" "
+ + "xmlns:ns2=\"http://cxf.apache.org/transform/header\" "
+ + "xmlns=\"http://cxf.apache.org/transform/test\">"
+ + "<ns2:OperationalMode>SIMULATION1</ns2:OperationalMode>"
+ + "<ns2:SomeComplexHeaderType>"
+ + "<ns3:CallerCorrelationId>SomeComplexValue</ns3:CallerCorrelationId>"
+ + "</ns2:SomeComplexHeaderType>"
+ + "</ns2:SoapHeaderIn>"
+ + "</soap:Header>"
+ + "<soap:Body>"
+ + "<TransformTestRequest xmlns=\"http://cxf.apache.org/transform/test\" "
+ + "xmlns:ns2=\"http://cxf.apache.org/transform/header\" "
+ + "xmlns:ns3=\"http://cxf.apache.org/transform/header/element\" "
+ + "xmlns:ns4=\"http://cxf.apache.org/transform/fault\" />"
+ + "</soap:Body></soap:Envelope>").getBytes());
+ XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+
+ Map<String, String> inMap = new HashMap<String, String>();
+ inMap.put("{http://cxf.apache.org/transform/header/element}*",
+ "{http://cxf.apache.org/transform/header/otherelement}*");
+ inMap.put("{http://cxf.apache.org/transform/test}*",
+ "{http://cxf.apache.org/transform/othertest}*");
+ inMap.put("{http://schemas.xmlsoap.org/soap/envelope/}Envelope",
+ "{http://schemas.xmlsoap.org/soap/envelope/}TheEnvelope");
+
+ // set the block original reader flag to true
+ reader = new InTransformReader(reader,
+ inMap,
+ null, true);
+
+ QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body");
+ PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag);
+
+ Document doc = StaxUtils.read(filteredReader);
+
+ assertNotNull(doc);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(bos);
+ StaxUtils.copy(doc, writer);
+ writer.flush();
+
+ String value = bos.toString();
+
+ Document docTransformed = DOMUtils.readXml(new StringReader(value));
+ Element env = docTransformed.getDocumentElement();
+ assertEquals("http://schemas.xmlsoap.org/soap/envelope/", env.getNamespaceURI());
+ assertEquals("ps1", env.getPrefix());
+ assertEquals("TheEnvelope", env.getLocalName());
+
+ Element header = getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Header", "soap");
+
+ Element customHeader = getElement(header, "http://cxf.apache.org/transform/header",
+ "SoapHeaderIn", "ns2");
+ NamedNodeMap map = customHeader.getAttributes();
+ assertEquals(4, map.getLength());
+ validateAttribute(map, "xmlns:ps3", "http://cxf.apache.org/transform/othertest");
+ validateAttribute(map, "xmlns:ns2", "http://cxf.apache.org/transform/header");
+ validateAttribute(map, "xmlns:ns4", "http://cxf.apache.org/transform/fault");
+ validateAttribute(map, "xmlns:ps2", "http://cxf.apache.org/transform/header/otherelement");
+
+ Element mode =
+ getElement(customHeader, "http://cxf.apache.org/transform/header", "OperationalMode", "ns2");
+ assertEquals("SIMULATION1", mode.getTextContent());
+ Element headerType =
+ getElement(customHeader, "http://cxf.apache.org/transform/header",
+ "SomeComplexHeaderType", "ns2");
+
+ Element correlationId =
+ getElement(headerType, "http://cxf.apache.org/transform/header/otherelement",
+ "CallerCorrelationId", "ps2");
+ assertEquals("SomeComplexValue", correlationId.getTextContent());
+ getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Body", "soap");
+
+ }
+
+
+ private Element getElement(Element parent, String namespace, String name, String prefix) {
+ NodeList list = parent.getElementsByTagNameNS(namespace, name);
+ assertEquals(1, list.getLength());
+ Element el = (Element)list.item(0);
+ assertEquals(prefix, el.getPrefix());
+ return el;
+ }
+
+ private void validateAttribute(NamedNodeMap map, String name, String value) {
+ Node attr = map.getNamedItem(name);
+ assertEquals(attr.getTextContent(), value);
+ }
}