Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/ws/8159058/SaajEmptyNamespaceTest.java
38853 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8159058 818644126* @summary Test that empty default namespace declaration clears the27* default namespace value28* @compile -XDignore.symbol.file SaajEmptyNamespaceTest.java29* @run testng/othervm SaajEmptyNamespaceTest30*/3132import com.sun.xml.internal.ws.api.SOAPVersion;33import com.sun.xml.internal.ws.api.message.saaj.SAAJFactory;34import com.sun.xml.internal.ws.message.stream.StreamMessage;35import java.io.ByteArrayInputStream;36import java.io.StringReader;37import java.io.StringWriter;38import java.io.UnsupportedEncodingException;39import javax.xml.namespace.QName;40import javax.xml.soap.MessageFactory;41import javax.xml.soap.SOAPBody;42import javax.xml.soap.SOAPElement;43import javax.xml.soap.SOAPException;44import javax.xml.soap.SOAPMessage;45import javax.xml.stream.XMLInputFactory;46import javax.xml.stream.XMLStreamReader;47import javax.xml.transform.OutputKeys;48import javax.xml.transform.Transformer;49import javax.xml.transform.TransformerException;50import javax.xml.transform.TransformerFactory;51import javax.xml.transform.dom.DOMSource;52import javax.xml.transform.stream.StreamResult;53import javax.xml.transform.stream.StreamSource;54import org.testng.Assert;55import org.testng.annotations.Test;56import org.w3c.dom.Node;5758public class SaajEmptyNamespaceTest {5960/*61* Test that SOAP reader doesn't move namespaces declarations to SOAP body element62* as reported in JDK-818644163*/64@Test65public void testPreserveNamespacesPosition() throws Exception {66// Create SOAP message from XML string and process it with SAAJ reader67XMLStreamReader envelope = XMLInputFactory.newFactory().createXMLStreamReader(68new StringReader(INPUT_SOAP_MESSAGE_2));69StreamMessage streamMessage = new StreamMessage(SOAPVersion.SOAP_11,70envelope, null);71SAAJFactory saajFact = new SAAJFactory();72SOAPMessage soapMessage = saajFact.readAsSOAPMessage(SOAPVersion.SOAP_11, streamMessage);7374//Get SOAP body and convert it to string representation75SOAPBody body = soapMessage.getSOAPBody();76String bodyAsString = nodeToText(body);77Assert.assertEquals(bodyAsString, PRESERVE_NAMESPACES_EXPECTED_RESULT);78}7980/*81* Test that SOAP message with default namespace declaration that contains empty82* string is properly processed by SAAJ reader.83*/84@Test85public void testResetDefaultNamespaceSAAJ() throws Exception {86// Create SOAP message from XML string and process it with SAAJ reader87XMLStreamReader envelope = XMLInputFactory.newFactory().createXMLStreamReader(88new StringReader(INPUT_SOAP_MESSAGE));89StreamMessage streamMessage = new StreamMessage(SOAPVersion.SOAP_11,90envelope, null);91SAAJFactory saajFact = new SAAJFactory();92SOAPMessage soapMessage = saajFact.readAsSOAPMessage(SOAPVersion.SOAP_11, streamMessage);9394// Check if constructed object model meets local names and namespace expectations95SOAPElement request = (SOAPElement) soapMessage.getSOAPBody().getFirstChild();96// Check top body element name97Assert.assertEquals(request.getLocalName(), "SampleServiceRequest");98// Check top body element namespace99Assert.assertEquals(request.getNamespaceURI(), TEST_NS);100SOAPElement params = (SOAPElement) request.getFirstChild();101// Check first child name102Assert.assertEquals(params.getLocalName(), "RequestParams");103// Check if first child namespace is null104Assert.assertNull(params.getNamespaceURI());105106// Check inner elements of the first child107SOAPElement param1 = (SOAPElement) params.getFirstChild();108Assert.assertEquals(param1.getLocalName(), "Param1");109Assert.assertNull(param1.getNamespaceURI());110SOAPElement param2 = (SOAPElement) params.getChildNodes().item(1);111Assert.assertEquals(param2.getLocalName(), "Param2");112Assert.assertNull(param2.getNamespaceURI());113// Check full content of SOAP body114Assert.assertEquals(nodeToText(request), EXPECTED_RESULT);115}116117/*118* Test that adding element with explicitly null namespace URI shall put the119* element into global namespace. Namespace declarations are not added explicitly.120*/121@Test122public void testAddElementToNullNsNoDeclarations() throws Exception {123// Create empty SOAP message124SOAPMessage msg = createSoapMessage();125SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();126127// Add elements128SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);129SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", null);130SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");131132// Check namespace URIs133Assert.assertNull(childGlobalNS.getNamespaceURI());134Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);135}136137/*138* Test that adding element with explicitly empty namespace URI shall put139* the element into global namespace. Namespace declarations are not added140* explicitly.141*/142@Test143public void testAddElementToGlobalNsNoDeclarations() throws Exception {144// Create empty SOAP message145SOAPMessage msg = createSoapMessage();146SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();147148// Add elements149SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);150SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", "");151SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");152153// Check namespace URIs154Assert.assertNull(childGlobalNS.getNamespaceURI());155Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);156}157158/*159* Test that adding element with explicitly empty namespace URI set via QName160* shall put the element into global namespace.161*/162@Test163public void testAddElementToNullNsQName() throws Exception {164// Create empty SOAP message165SOAPMessage msg = createSoapMessage();166SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();167168// Add elements169SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);170parentExplicitNS.addNamespaceDeclaration("", TEST_NS);171SOAPElement childGlobalNS = parentExplicitNS.addChildElement(new QName(null, "global-child"));172childGlobalNS.addNamespaceDeclaration("", "");173SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");174SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");175176// Check namespace URIs177Assert.assertNull(childGlobalNS.getNamespaceURI());178Assert.assertNull(grandChildGlobalNS.getNamespaceURI());179Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);180}181182/*183* Test that adding element with explicitly empty namespace URI shall put184* the element into global namespace.185*/186@Test187public void testAddElementToGlobalNs() throws Exception {188// Create empty SOAP message189SOAPMessage msg = createSoapMessage();190SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();191192// Add elements193SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);194parentExplicitNS.addNamespaceDeclaration("", TEST_NS);195SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", "");196childGlobalNS.addNamespaceDeclaration("", "");197SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");198SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");199200// Check namespace URIs201Assert.assertNull(childGlobalNS.getNamespaceURI());202Assert.assertNull(grandChildGlobalNS.getNamespaceURI());203Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);204}205206/*207* Test that adding element with explicitly null namespace URI shall put208* the element into global namespace.209*/210@Test211public void testAddElementToNullNs() throws Exception {212// Create empty SOAP message213SOAPMessage msg = createSoapMessage();214SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();215216// Add elements217SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);218parentExplicitNS.addNamespaceDeclaration("", TEST_NS);219SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", null);220childGlobalNS.addNamespaceDeclaration("", null);221SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");222SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");223224// Check namespace URIs225Assert.assertNull(childGlobalNS.getNamespaceURI());226Assert.assertNull(grandChildGlobalNS.getNamespaceURI());227Assert.assertEquals(TEST_NS, childDefaultNS.getNamespaceURI());228}229230/*231* Test that adding element with explicitly empty namespace URI via QName232* shall put the element in global namespace.233*/234@Test235public void testAddElementToGlobalNsQName() throws Exception {236// Create empty SOAP message237SOAPMessage msg = createSoapMessage();238SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();239240// Add elements241SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);242parentExplicitNS.addNamespaceDeclaration("", TEST_NS);243SOAPElement childGlobalNS = parentExplicitNS.addChildElement(new QName("", "global-child"));244childGlobalNS.addNamespaceDeclaration("", "");245SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");246SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");247248// Check namespace URIs249Assert.assertNull(childGlobalNS.getNamespaceURI());250Assert.assertNull(grandChildGlobalNS.getNamespaceURI());251Assert.assertEquals(childDefaultNS.getNamespaceURI(),TEST_NS);252}253254// Convert DOM node to text representation255private String nodeToText(Node node) throws TransformerException {256Transformer trans = TransformerFactory.newInstance().newTransformer();257trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");258StringWriter writer = new StringWriter();259StreamResult result = new StreamResult(writer);260trans.transform(new DOMSource(node), result);261String bodyContent = writer.toString();262System.out.println("SOAP body content read by SAAJ:"+bodyContent);263return bodyContent;264}265266// Create SOAP message with empty body267private static SOAPMessage createSoapMessage() throws SOAPException, UnsupportedEncodingException {268String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"269+"<SOAP-ENV:Body/></SOAP-ENV:Envelope>";270MessageFactory mFactory = MessageFactory.newInstance();271SOAPMessage msg = mFactory.createMessage();272msg.getSOAPPart().setContent(new StreamSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));273return msg;274}275276// Namespace value used in tests277private static String TEST_NS = "http://example.org/test";278279// Content of SOAP message passed to SAAJ factory280private static String INPUT_SOAP_MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"281+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"282+ "<s:Body>"283+ "<SampleServiceRequest xmlns=\"http://example.org/test\""284+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"285+ "<RequestParams xmlns=\"\">"286+ "<Param1>hogehoge</Param1>"287+ "<Param2>fugafuga</Param2>"288+ "</RequestParams>"289+ "</SampleServiceRequest>"290+ "</s:Body>"291+ "</s:Envelope>";292293// Expected body content after SAAJ processing294private static String EXPECTED_RESULT = "<SampleServiceRequest"295+ " xmlns=\"http://example.org/test\""296+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"297+ "<RequestParams xmlns=\"\">"298+ "<Param1>hogehoge</Param1>"299+ "<Param2>fugafuga</Param2>"300+ "</RequestParams>"301+ "</SampleServiceRequest>";302303private static String PRESERVE_NAMESPACES_EXPECTED_RESULT =304"<s:Body xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"305+"<Request xmlns=\"http://example.org/NS_1\">"306+"<Item><Contact xmlns=\"http://example.org/NS_2\">Test_Contact</Contact>"307+"</Item></Request></s:Body>";308309private static String INPUT_SOAP_MESSAGE_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"310+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"311+ "<s:Body>"312+ "<Request xmlns=\"http://example.org/NS_1\">"313+ "<Item>"314+ "<Contact xmlns=\"http://example.org/NS_2\">Test_Contact</Contact>"315+ "</Item>"316+ "</Request>"317+ "</s:Body>"318+ "</s:Envelope>";319}320321322