Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B29596DFD for ; Mon, 25 Jul 2011 15:06:22 +0000 (UTC) Received: (qmail 34130 invoked by uid 500); 25 Jul 2011 15:06:22 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 33935 invoked by uid 500); 25 Jul 2011 15:06:22 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 33922 invoked by uid 99); 25 Jul 2011 15:06:21 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jul 2011 15:06:21 +0000 X-ASF-Spam-Status: No, hits=-1998.0 required=5.0 tests=ALL_TRUSTED,FB_GET_MEDS X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jul 2011 15:06:19 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B02992388A6C for ; Mon, 25 Jul 2011 15:05:59 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1150739 - in /cxf/trunk/common/common/src: main/java/org/apache/cxf/staxutils/transform/ test/java/org/apache/cxf/staxutils/ test/java/org/apache/cxf/staxutils/transform/ Date: Mon, 25 Jul 2011 15:05:59 -0000 To: commits@cxf.apache.org From: sergeyb@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110725150559.B02992388A6C@eris.apache.org> Author: sergeyb Date: Mon Jul 25 15:05:57 2011 New Revision: 1150739 URL: http://svn.apache.org/viewvc?rev=1150739&view=rev Log: [CXF-3681] More tests from Andi and a fix for PartialXMLStreamReader issue Added: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java (with props) Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java?rev=1150739&r1=1150738&r2=1150739&view=diff ============================================================================== --- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java (original) +++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/DelegatingNamespaceContext.java Mon Jul 25 15:05:57 2011 @@ -87,7 +87,6 @@ class DelegatingNamespaceContext impleme return prefix; } - @SuppressWarnings("unchecked") public Iterator getPrefixes(String ns) { return nc.getPrefixes(ns); } Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java?rev=1150739&r1=1150738&r2=1150739&view=diff ============================================================================== --- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java (original) +++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java Mon Jul 25 15:05:57 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 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; Added: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java?rev=1150739&view=auto ============================================================================== --- cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java (added) +++ cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java Mon Jul 25 15:05:57 2011 @@ -0,0 +1,90 @@ +/** + * 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.cxf.staxutils; + +import java.io.ByteArrayOutputStream; +import java.io.StringReader; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.w3c.dom.Document; + +import org.junit.Assert; +import org.junit.Test; + +public class PartialXMLStreamReaderTest extends Assert { + + @Test + public void testReader() throws Exception { + String test = + "" + + "" + + "" + + "SIMULATION1" + + "" + + "SomeComplexValue" + + "" + + "" + + "" + + "" + + "" + + ""; + + XMLStreamReader reader = + StaxUtils.createXMLStreamReader(new StringReader(test)); + 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(); + + System.out.println("value: " + value); + + assertTrue(("" + + "" + + "" + + "SIMULATION1" + + "" + + "SomeComplexValue" + + "" + + "" + + ""). + equals(value.trim())); + + } +} + Propchange: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/PartialXMLStreamReaderTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java?rev=1150739&r1=1150738&r2=1150739&view=diff ============================================================================== --- cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java (original) +++ cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java Mon Jul 25 15:05:57 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("".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("".equals(value)); + } + + + @Test public void testReadWithSameNamespace() throws Exception { InputStream is = new ByteArrayInputStream( ("" @@ -241,4 +270,253 @@ public class InTransformReaderTest exten + "xmlns:ns4=\"http://cxf.apache.org/transform/fault\"/>"). equals(value.trim())); } + + @Test + public void testReadPartialWithComplexRequestSameNamespace() throws Exception { + InputStream is = new ByteArrayInputStream( + ("" + + "" + + "" + + "SIMULATION1" + + "" + + "SomeComplexValue" + + "" + + "" + + "" + + "" + + "" + + "").getBytes()); + XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); + + Map inMap = new HashMap(); + 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( + ("" + + "" + + "" + + "SIMULATION1" + + "" + + "SomeComplexValue" + + "" + + "" + + "" + + "" + + "" + + "").getBytes()); + XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); + + Map inMap = new HashMap(); + 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( + ("" + + "" + + "" + + "SIMULATION1" + + "" + + "SomeComplexValue" + + "" + + "" + + "" + + "" + + "" + + "").getBytes()); + XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); + + Map inMap = new HashMap(); + 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); + } }