-
Notifications
You must be signed in to change notification settings - Fork 15
/
WriteXmlDeclarationTest.java
88 lines (77 loc) · 3.85 KB
/
WriteXmlDeclarationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package soap;
import jakarta.xml.soap.*;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import junit.framework.TestCase;
/**
* Tests for Bug Id: 4952738
*/
public class WriteXmlDeclarationTest extends TestCase {
private static final String xmlDecl =
"<?xml version=\"1.0\" encoding=\"utf-8\"";
public WriteXmlDeclarationTest(String name) {
super(name);
}
public void testXmlDeclWithInputStream1() throws Exception {
String testString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body/></soap:Envelope>";
byte[] utf8bytes = testString.getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream bais = new ByteArrayInputStream(utf8bytes);
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = msg.getSOAPPart();
soapPart.setContent(new StreamSource(bais));
soapPart.getEnvelope();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos);
String msgString = baos.toString(StandardCharsets.UTF_8);
assertTrue(msgString.startsWith(xmlDecl));
}
public void testXmlDeclWithInputStream2() throws Exception {
String testString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body/></soap:Envelope>";
byte[] utf8bytes = testString.getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream bais = new ByteArrayInputStream(utf8bytes);
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = msg.getSOAPPart();
soapPart.setContent(new StreamSource(bais));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos);
String msgString = baos.toString(StandardCharsets.UTF_8);
assertTrue(msgString.startsWith(xmlDecl));
}
public void testXmlDeclWithReader1() throws Exception {
String testString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body/></soap:Envelope>";
StringReader reader = new StringReader(testString);
StreamSource content = new StreamSource(reader);
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = msg.getSOAPPart();
soapPart.setContent(content);
soapPart.getEnvelope();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos);
String msgString = baos.toString(StandardCharsets.UTF_8);
assertTrue(msgString.startsWith(xmlDecl));
}
public void testXmlDeclWithReader2() throws Exception {
String testString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body/></soap:Envelope>";
StringReader reader = new StringReader(testString);
StreamSource content = new StreamSource(reader);
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = msg.getSOAPPart();
soapPart.setContent(content);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos);
String msgString = baos.toString(StandardCharsets.UTF_8);
assertTrue(msgString.startsWith(xmlDecl));
}
}