Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/ws/8159058/SaajEmptyNamespaceTest.java
38853 views
1
/*
2
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8159058 8186441
27
* @summary Test that empty default namespace declaration clears the
28
* default namespace value
29
* @compile -XDignore.symbol.file SaajEmptyNamespaceTest.java
30
* @run testng/othervm SaajEmptyNamespaceTest
31
*/
32
33
import com.sun.xml.internal.ws.api.SOAPVersion;
34
import com.sun.xml.internal.ws.api.message.saaj.SAAJFactory;
35
import com.sun.xml.internal.ws.message.stream.StreamMessage;
36
import java.io.ByteArrayInputStream;
37
import java.io.StringReader;
38
import java.io.StringWriter;
39
import java.io.UnsupportedEncodingException;
40
import javax.xml.namespace.QName;
41
import javax.xml.soap.MessageFactory;
42
import javax.xml.soap.SOAPBody;
43
import javax.xml.soap.SOAPElement;
44
import javax.xml.soap.SOAPException;
45
import javax.xml.soap.SOAPMessage;
46
import javax.xml.stream.XMLInputFactory;
47
import javax.xml.stream.XMLStreamReader;
48
import javax.xml.transform.OutputKeys;
49
import javax.xml.transform.Transformer;
50
import javax.xml.transform.TransformerException;
51
import javax.xml.transform.TransformerFactory;
52
import javax.xml.transform.dom.DOMSource;
53
import javax.xml.transform.stream.StreamResult;
54
import javax.xml.transform.stream.StreamSource;
55
import org.testng.Assert;
56
import org.testng.annotations.Test;
57
import org.w3c.dom.Node;
58
59
public class SaajEmptyNamespaceTest {
60
61
/*
62
* Test that SOAP reader doesn't move namespaces declarations to SOAP body element
63
* as reported in JDK-8186441
64
*/
65
@Test
66
public void testPreserveNamespacesPosition() throws Exception {
67
// Create SOAP message from XML string and process it with SAAJ reader
68
XMLStreamReader envelope = XMLInputFactory.newFactory().createXMLStreamReader(
69
new StringReader(INPUT_SOAP_MESSAGE_2));
70
StreamMessage streamMessage = new StreamMessage(SOAPVersion.SOAP_11,
71
envelope, null);
72
SAAJFactory saajFact = new SAAJFactory();
73
SOAPMessage soapMessage = saajFact.readAsSOAPMessage(SOAPVersion.SOAP_11, streamMessage);
74
75
//Get SOAP body and convert it to string representation
76
SOAPBody body = soapMessage.getSOAPBody();
77
String bodyAsString = nodeToText(body);
78
Assert.assertEquals(bodyAsString, PRESERVE_NAMESPACES_EXPECTED_RESULT);
79
}
80
81
/*
82
* Test that SOAP message with default namespace declaration that contains empty
83
* string is properly processed by SAAJ reader.
84
*/
85
@Test
86
public void testResetDefaultNamespaceSAAJ() throws Exception {
87
// Create SOAP message from XML string and process it with SAAJ reader
88
XMLStreamReader envelope = XMLInputFactory.newFactory().createXMLStreamReader(
89
new StringReader(INPUT_SOAP_MESSAGE));
90
StreamMessage streamMessage = new StreamMessage(SOAPVersion.SOAP_11,
91
envelope, null);
92
SAAJFactory saajFact = new SAAJFactory();
93
SOAPMessage soapMessage = saajFact.readAsSOAPMessage(SOAPVersion.SOAP_11, streamMessage);
94
95
// Check if constructed object model meets local names and namespace expectations
96
SOAPElement request = (SOAPElement) soapMessage.getSOAPBody().getFirstChild();
97
// Check top body element name
98
Assert.assertEquals(request.getLocalName(), "SampleServiceRequest");
99
// Check top body element namespace
100
Assert.assertEquals(request.getNamespaceURI(), TEST_NS);
101
SOAPElement params = (SOAPElement) request.getFirstChild();
102
// Check first child name
103
Assert.assertEquals(params.getLocalName(), "RequestParams");
104
// Check if first child namespace is null
105
Assert.assertNull(params.getNamespaceURI());
106
107
// Check inner elements of the first child
108
SOAPElement param1 = (SOAPElement) params.getFirstChild();
109
Assert.assertEquals(param1.getLocalName(), "Param1");
110
Assert.assertNull(param1.getNamespaceURI());
111
SOAPElement param2 = (SOAPElement) params.getChildNodes().item(1);
112
Assert.assertEquals(param2.getLocalName(), "Param2");
113
Assert.assertNull(param2.getNamespaceURI());
114
// Check full content of SOAP body
115
Assert.assertEquals(nodeToText(request), EXPECTED_RESULT);
116
}
117
118
/*
119
* Test that adding element with explicitly null namespace URI shall put the
120
* element into global namespace. Namespace declarations are not added explicitly.
121
*/
122
@Test
123
public void testAddElementToNullNsNoDeclarations() throws Exception {
124
// Create empty SOAP message
125
SOAPMessage msg = createSoapMessage();
126
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
127
128
// Add elements
129
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
130
SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", null);
131
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
132
133
// Check namespace URIs
134
Assert.assertNull(childGlobalNS.getNamespaceURI());
135
Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);
136
}
137
138
/*
139
* Test that adding element with explicitly empty namespace URI shall put
140
* the element into global namespace. Namespace declarations are not added
141
* explicitly.
142
*/
143
@Test
144
public void testAddElementToGlobalNsNoDeclarations() throws Exception {
145
// Create empty SOAP message
146
SOAPMessage msg = createSoapMessage();
147
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
148
149
// Add elements
150
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
151
SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", "");
152
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
153
154
// Check namespace URIs
155
Assert.assertNull(childGlobalNS.getNamespaceURI());
156
Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);
157
}
158
159
/*
160
* Test that adding element with explicitly empty namespace URI set via QName
161
* shall put the element into global namespace.
162
*/
163
@Test
164
public void testAddElementToNullNsQName() throws Exception {
165
// Create empty SOAP message
166
SOAPMessage msg = createSoapMessage();
167
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
168
169
// Add elements
170
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
171
parentExplicitNS.addNamespaceDeclaration("", TEST_NS);
172
SOAPElement childGlobalNS = parentExplicitNS.addChildElement(new QName(null, "global-child"));
173
childGlobalNS.addNamespaceDeclaration("", "");
174
SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");
175
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
176
177
// Check namespace URIs
178
Assert.assertNull(childGlobalNS.getNamespaceURI());
179
Assert.assertNull(grandChildGlobalNS.getNamespaceURI());
180
Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);
181
}
182
183
/*
184
* Test that adding element with explicitly empty namespace URI shall put
185
* the element into global namespace.
186
*/
187
@Test
188
public void testAddElementToGlobalNs() throws Exception {
189
// Create empty SOAP message
190
SOAPMessage msg = createSoapMessage();
191
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
192
193
// Add elements
194
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
195
parentExplicitNS.addNamespaceDeclaration("", TEST_NS);
196
SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", "");
197
childGlobalNS.addNamespaceDeclaration("", "");
198
SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");
199
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
200
201
// Check namespace URIs
202
Assert.assertNull(childGlobalNS.getNamespaceURI());
203
Assert.assertNull(grandChildGlobalNS.getNamespaceURI());
204
Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);
205
}
206
207
/*
208
* Test that adding element with explicitly null namespace URI shall put
209
* the element into global namespace.
210
*/
211
@Test
212
public void testAddElementToNullNs() throws Exception {
213
// Create empty SOAP message
214
SOAPMessage msg = createSoapMessage();
215
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
216
217
// Add elements
218
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
219
parentExplicitNS.addNamespaceDeclaration("", TEST_NS);
220
SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", null);
221
childGlobalNS.addNamespaceDeclaration("", null);
222
SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");
223
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
224
225
// Check namespace URIs
226
Assert.assertNull(childGlobalNS.getNamespaceURI());
227
Assert.assertNull(grandChildGlobalNS.getNamespaceURI());
228
Assert.assertEquals(TEST_NS, childDefaultNS.getNamespaceURI());
229
}
230
231
/*
232
* Test that adding element with explicitly empty namespace URI via QName
233
* shall put the element in global namespace.
234
*/
235
@Test
236
public void testAddElementToGlobalNsQName() throws Exception {
237
// Create empty SOAP message
238
SOAPMessage msg = createSoapMessage();
239
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
240
241
// Add elements
242
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
243
parentExplicitNS.addNamespaceDeclaration("", TEST_NS);
244
SOAPElement childGlobalNS = parentExplicitNS.addChildElement(new QName("", "global-child"));
245
childGlobalNS.addNamespaceDeclaration("", "");
246
SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");
247
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
248
249
// Check namespace URIs
250
Assert.assertNull(childGlobalNS.getNamespaceURI());
251
Assert.assertNull(grandChildGlobalNS.getNamespaceURI());
252
Assert.assertEquals(childDefaultNS.getNamespaceURI(),TEST_NS);
253
}
254
255
// Convert DOM node to text representation
256
private String nodeToText(Node node) throws TransformerException {
257
Transformer trans = TransformerFactory.newInstance().newTransformer();
258
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
259
StringWriter writer = new StringWriter();
260
StreamResult result = new StreamResult(writer);
261
trans.transform(new DOMSource(node), result);
262
String bodyContent = writer.toString();
263
System.out.println("SOAP body content read by SAAJ:"+bodyContent);
264
return bodyContent;
265
}
266
267
// Create SOAP message with empty body
268
private static SOAPMessage createSoapMessage() throws SOAPException, UnsupportedEncodingException {
269
String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
270
+"<SOAP-ENV:Body/></SOAP-ENV:Envelope>";
271
MessageFactory mFactory = MessageFactory.newInstance();
272
SOAPMessage msg = mFactory.createMessage();
273
msg.getSOAPPart().setContent(new StreamSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
274
return msg;
275
}
276
277
// Namespace value used in tests
278
private static String TEST_NS = "http://example.org/test";
279
280
// Content of SOAP message passed to SAAJ factory
281
private static String INPUT_SOAP_MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
282
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
283
+ "<s:Body>"
284
+ "<SampleServiceRequest xmlns=\"http://example.org/test\""
285
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
286
+ "<RequestParams xmlns=\"\">"
287
+ "<Param1>hogehoge</Param1>"
288
+ "<Param2>fugafuga</Param2>"
289
+ "</RequestParams>"
290
+ "</SampleServiceRequest>"
291
+ "</s:Body>"
292
+ "</s:Envelope>";
293
294
// Expected body content after SAAJ processing
295
private static String EXPECTED_RESULT = "<SampleServiceRequest"
296
+ " xmlns=\"http://example.org/test\""
297
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
298
+ "<RequestParams xmlns=\"\">"
299
+ "<Param1>hogehoge</Param1>"
300
+ "<Param2>fugafuga</Param2>"
301
+ "</RequestParams>"
302
+ "</SampleServiceRequest>";
303
304
private static String PRESERVE_NAMESPACES_EXPECTED_RESULT =
305
"<s:Body xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
306
+"<Request xmlns=\"http://example.org/NS_1\">"
307
+"<Item><Contact xmlns=\"http://example.org/NS_2\">Test_Contact</Contact>"
308
+"</Item></Request></s:Body>";
309
310
private static String INPUT_SOAP_MESSAGE_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
311
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
312
+ "<s:Body>"
313
+ "<Request xmlns=\"http://example.org/NS_1\">"
314
+ "<Item>"
315
+ "<Contact xmlns=\"http://example.org/NS_2\">Test_Contact</Contact>"
316
+ "</Item>"
317
+ "</Request>"
318
+ "</s:Body>"
319
+ "</s:Envelope>";
320
}
321
322