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/jaxp/stream/8145974/SurrogatesTest.java
38861 views
1
/*
2
* Copyright (c) 2016, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
import java.io.ByteArrayInputStream;
27
import java.io.ByteArrayOutputStream;
28
import java.io.InputStream;
29
import java.io.OutputStreamWriter;
30
31
import javax.xml.stream.XMLInputFactory;
32
import javax.xml.stream.XMLOutputFactory;
33
import javax.xml.stream.XMLStreamConstants;
34
import javax.xml.stream.XMLStreamException;
35
import javax.xml.stream.XMLStreamReader;
36
import javax.xml.stream.XMLStreamWriter;
37
38
import org.testng.Assert;
39
import org.testng.annotations.Test;
40
import org.testng.annotations.DataProvider;
41
42
/*
43
* @test
44
* @bug 8145974
45
* @run testng/othervm SurrogatesTest
46
* @summary Check that XMLStreamWriter generates valid xml with surrogate pair
47
* used within element text
48
*/
49
50
public class SurrogatesTest {
51
52
// Test that valid surrogate characters can be written/readen by xml stream
53
// reader/writer
54
@Test(dataProvider = "validData")
55
public void xmlWithValidSurrogatesTest(String content)
56
throws Exception {
57
generateAndReadXml(content);
58
}
59
60
// Test that unbalanced surrogate character will
61
@Test(dataProvider = "invalidData",
62
expectedExceptions = XMLStreamException.class)
63
public void xmlWithUnbalancedSurrogatesTest(String content)
64
throws Exception {
65
generateAndReadXml(content);
66
}
67
68
// Generates xml content with XMLStreamWriter and read it to check
69
// for correctness of xml and generated data
70
void generateAndReadXml(String content) throws Exception {
71
ByteArrayOutputStream stream = new ByteArrayOutputStream();
72
XMLOutputFactory factory = XMLOutputFactory.newInstance();
73
OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
74
XMLStreamWriter writer = factory.createXMLStreamWriter(streamWriter);
75
76
// Generate xml with selected stream writer type
77
generateXML(writer, content);
78
String output = stream.toString();
79
System.out.println("Generated xml: " + output);
80
// Read generated xml with StAX parser
81
readXML(output.getBytes(), content);
82
}
83
84
// Generates XML with provided xml stream writer. Provided string
85
// is inserted into xml twice: with usage of writeCharacters( String )
86
// and writeCharacters( char [], int , int )
87
private void generateXML(XMLStreamWriter writer, String sequence)
88
throws XMLStreamException {
89
char[] seqArr = sequence.toCharArray();
90
writer.writeStartDocument();
91
writer.writeStartElement("root");
92
93
// Use writeCharacters( String ) to write characters
94
writer.writeStartElement("writeCharactersWithString");
95
writer.writeCharacters(sequence);
96
writer.writeEndElement();
97
98
// Use writeCharacters( char [], int , int ) to write characters
99
writer.writeStartElement("writeCharactersWithArray");
100
writer.writeCharacters(seqArr, 0, seqArr.length);
101
writer.writeEndElement();
102
103
// Close root element and document
104
writer.writeEndElement();
105
writer.writeEndDocument();
106
writer.flush();
107
writer.close();
108
}
109
110
// Reads generated XML data and check if it contains expected
111
// text in writeCharactersWithString and writeCharactersWithArray
112
// elements
113
private void readXML(byte[] xmlData, String expectedContent)
114
throws Exception {
115
InputStream stream = new ByteArrayInputStream(xmlData);
116
XMLInputFactory factory = XMLInputFactory.newInstance();
117
XMLStreamReader xmlReader
118
= factory.createXMLStreamReader(stream);
119
boolean inTestElement = false;
120
StringBuilder sb = new StringBuilder();
121
while (xmlReader.hasNext()) {
122
String ename;
123
switch (xmlReader.getEventType()) {
124
case XMLStreamConstants.START_ELEMENT:
125
ename = xmlReader.getLocalName();
126
if (ename.equals("writeCharactersWithString")
127
|| ename.equals("writeCharactersWithArray")) {
128
inTestElement = true;
129
}
130
break;
131
case XMLStreamConstants.END_ELEMENT:
132
ename = xmlReader.getLocalName();
133
if (ename.equals("writeCharactersWithString")
134
|| ename.equals("writeCharactersWithArray")) {
135
inTestElement = false;
136
String content = sb.toString();
137
System.out.println(ename + " text:'" + content + "' expected:'" + expectedContent+"'");
138
Assert.assertEquals(content, expectedContent);
139
sb.setLength(0);
140
}
141
break;
142
case XMLStreamConstants.CHARACTERS:
143
if (inTestElement) {
144
sb.append(xmlReader.getText());
145
}
146
break;
147
}
148
xmlReader.next();
149
}
150
}
151
152
@DataProvider(name = "validData")
153
Object[][] getValidData() {
154
return new Object[][] {
155
{"Don't Worry Be \uD83D\uDE0A"},
156
{"BMP characters \uE000\uFFFD"},
157
{"Simple text"},
158
};
159
}
160
161
@DataProvider(name = "invalidData")
162
Object[][] getInvalidData() {
163
return new Object[][] {
164
{"Unbalanced surrogate \uD83D"},
165
{"Unbalanced surrogate \uD83Dis here"},
166
{"Surrogate with followup BMP\uD83D\uFFF9"},
167
};
168
}
169
}
170
171