Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/stream/8145974/SurrogatesTest.java
38861 views
/*1* Copyright (c) 2016, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.ByteArrayInputStream;26import java.io.ByteArrayOutputStream;27import java.io.InputStream;28import java.io.OutputStreamWriter;2930import javax.xml.stream.XMLInputFactory;31import javax.xml.stream.XMLOutputFactory;32import javax.xml.stream.XMLStreamConstants;33import javax.xml.stream.XMLStreamException;34import javax.xml.stream.XMLStreamReader;35import javax.xml.stream.XMLStreamWriter;3637import org.testng.Assert;38import org.testng.annotations.Test;39import org.testng.annotations.DataProvider;4041/*42* @test43* @bug 814597444* @run testng/othervm SurrogatesTest45* @summary Check that XMLStreamWriter generates valid xml with surrogate pair46* used within element text47*/4849public class SurrogatesTest {5051// Test that valid surrogate characters can be written/readen by xml stream52// reader/writer53@Test(dataProvider = "validData")54public void xmlWithValidSurrogatesTest(String content)55throws Exception {56generateAndReadXml(content);57}5859// Test that unbalanced surrogate character will60@Test(dataProvider = "invalidData",61expectedExceptions = XMLStreamException.class)62public void xmlWithUnbalancedSurrogatesTest(String content)63throws Exception {64generateAndReadXml(content);65}6667// Generates xml content with XMLStreamWriter and read it to check68// for correctness of xml and generated data69void generateAndReadXml(String content) throws Exception {70ByteArrayOutputStream stream = new ByteArrayOutputStream();71XMLOutputFactory factory = XMLOutputFactory.newInstance();72OutputStreamWriter streamWriter = new OutputStreamWriter(stream);73XMLStreamWriter writer = factory.createXMLStreamWriter(streamWriter);7475// Generate xml with selected stream writer type76generateXML(writer, content);77String output = stream.toString();78System.out.println("Generated xml: " + output);79// Read generated xml with StAX parser80readXML(output.getBytes(), content);81}8283// Generates XML with provided xml stream writer. Provided string84// is inserted into xml twice: with usage of writeCharacters( String )85// and writeCharacters( char [], int , int )86private void generateXML(XMLStreamWriter writer, String sequence)87throws XMLStreamException {88char[] seqArr = sequence.toCharArray();89writer.writeStartDocument();90writer.writeStartElement("root");9192// Use writeCharacters( String ) to write characters93writer.writeStartElement("writeCharactersWithString");94writer.writeCharacters(sequence);95writer.writeEndElement();9697// Use writeCharacters( char [], int , int ) to write characters98writer.writeStartElement("writeCharactersWithArray");99writer.writeCharacters(seqArr, 0, seqArr.length);100writer.writeEndElement();101102// Close root element and document103writer.writeEndElement();104writer.writeEndDocument();105writer.flush();106writer.close();107}108109// Reads generated XML data and check if it contains expected110// text in writeCharactersWithString and writeCharactersWithArray111// elements112private void readXML(byte[] xmlData, String expectedContent)113throws Exception {114InputStream stream = new ByteArrayInputStream(xmlData);115XMLInputFactory factory = XMLInputFactory.newInstance();116XMLStreamReader xmlReader117= factory.createXMLStreamReader(stream);118boolean inTestElement = false;119StringBuilder sb = new StringBuilder();120while (xmlReader.hasNext()) {121String ename;122switch (xmlReader.getEventType()) {123case XMLStreamConstants.START_ELEMENT:124ename = xmlReader.getLocalName();125if (ename.equals("writeCharactersWithString")126|| ename.equals("writeCharactersWithArray")) {127inTestElement = true;128}129break;130case XMLStreamConstants.END_ELEMENT:131ename = xmlReader.getLocalName();132if (ename.equals("writeCharactersWithString")133|| ename.equals("writeCharactersWithArray")) {134inTestElement = false;135String content = sb.toString();136System.out.println(ename + " text:'" + content + "' expected:'" + expectedContent+"'");137Assert.assertEquals(content, expectedContent);138sb.setLength(0);139}140break;141case XMLStreamConstants.CHARACTERS:142if (inTestElement) {143sb.append(xmlReader.getText());144}145break;146}147xmlReader.next();148}149}150151@DataProvider(name = "validData")152Object[][] getValidData() {153return new Object[][] {154{"Don't Worry Be \uD83D\uDE0A"},155{"BMP characters \uE000\uFFFD"},156{"Simple text"},157};158}159160@DataProvider(name = "invalidData")161Object[][] getInvalidData() {162return new Object[][] {163{"Unbalanced surrogate \uD83D"},164{"Unbalanced surrogate \uD83Dis here"},165{"Surrogate with followup BMP\uD83D\uFFF9"},166};167}168}169170171