Path: blob/master/test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamWriterTest/AttributeEscapeTest.java
42103 views
/*1* Copyright (c) 2014, 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*/2223package stream.XMLStreamWriterTest;2425import java.io.IOException;26import java.io.StringReader;27import java.io.StringWriter;2829import javax.xml.parsers.DocumentBuilderFactory;30import javax.xml.parsers.ParserConfigurationException;31import javax.xml.stream.XMLOutputFactory;32import javax.xml.stream.XMLStreamException;33import javax.xml.stream.XMLStreamWriter;3435import org.testng.Assert;36import org.testng.annotations.Listeners;37import org.testng.annotations.Test;38import org.xml.sax.InputSource;39import org.xml.sax.SAXException;4041/*42* @test43* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest44* @run testng/othervm -DrunSecMngr=true -Djava.security.manager=allow stream.XMLStreamWriterTest.AttributeEscapeTest45* @run testng/othervm stream.XMLStreamWriterTest.AttributeEscapeTest46* @summary Test XMLStreamWriter shall escape the illegal characters.47*/48@Listeners({jaxp.library.BasePolicy.class})49public class AttributeEscapeTest {5051/**52* XML content for testing the escaping of <, >, &, ', ".53*/54private static final String XML_CONTENT = "Testing escaping: lt=<, gt=>, amp=&, apos=', dquote=\"";5556@Test57public void testCR6420953() {5859try {60XMLOutputFactory xof = XMLOutputFactory.newInstance();61StringWriter sw = new StringWriter();62XMLStreamWriter w = xof.createXMLStreamWriter(sw);6364w.writeStartDocument();65w.writeStartElement("element");6667w.writeDefaultNamespace(XML_CONTENT);68w.writeNamespace("prefix", XML_CONTENT);6970w.writeAttribute("attribute", XML_CONTENT);71w.writeAttribute(XML_CONTENT, "attribute2", XML_CONTENT);72w.writeAttribute("prefix", XML_CONTENT, "attribute3", XML_CONTENT);7374w.writeCharacters("\n");75w.writeCharacters(XML_CONTENT);76w.writeCharacters("\n");77w.writeCharacters(XML_CONTENT.toCharArray(), 0, XML_CONTENT.length());78w.writeCharacters("\n");7980w.writeEndElement();81w.writeEndDocument();82w.flush();8384System.out.println(sw);8586// make sure that the generated XML parses87DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();88dbf.setNamespaceAware(true);89dbf.newDocumentBuilder().parse(new InputSource(new StringReader(sw.toString())));90} catch (XMLStreamException xmlStreamException) {91xmlStreamException.printStackTrace();92Assert.fail(xmlStreamException.toString());93} catch (SAXException saxException) {94saxException.printStackTrace();95Assert.fail(saxException.toString());96} catch (ParserConfigurationException parserConfigurationException) {97parserConfigurationException.printStackTrace();98Assert.fail(parserConfigurationException.toString());99} catch (IOException ioException) {100ioException.printStackTrace();101Assert.fail(ioException.toString());102}103}104}105106107