AttributeEscapeTest.java revision 968:874082a9b565
1/* 2 * Copyright (c) 2014, 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 24package stream.XMLStreamWriterTest; 25 26import java.io.IOException; 27import java.io.StringReader; 28import java.io.StringWriter; 29 30import javax.xml.parsers.DocumentBuilderFactory; 31import javax.xml.parsers.ParserConfigurationException; 32import javax.xml.stream.XMLOutputFactory; 33import javax.xml.stream.XMLStreamException; 34import javax.xml.stream.XMLStreamWriter; 35 36import org.testng.Assert; 37import org.testng.annotations.Listeners; 38import org.testng.annotations.Test; 39import org.xml.sax.InputSource; 40import org.xml.sax.SAXException; 41 42/* 43 * @test 44 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 45 * @run testng/othervm -DrunSecMngr=true stream.XMLStreamWriterTest.AttributeEscapeTest 46 * @run testng/othervm stream.XMLStreamWriterTest.AttributeEscapeTest 47 * @summary Test XMLStreamWriter shall escape the illegal characters. 48 */ 49@Listeners({jaxp.library.BasePolicy.class}) 50public class AttributeEscapeTest { 51 52 /** 53 * XML content for testing the escaping of <, >, &, ', ". 54 */ 55 private static final String XML_CONTENT = "Testing escaping: lt=<, gt=>, amp=&, apos=', dquote=\""; 56 57 @Test 58 public void testCR6420953() { 59 60 try { 61 XMLOutputFactory xof = XMLOutputFactory.newInstance(); 62 StringWriter sw = new StringWriter(); 63 XMLStreamWriter w = xof.createXMLStreamWriter(sw); 64 65 w.writeStartDocument(); 66 w.writeStartElement("element"); 67 68 w.writeDefaultNamespace(XML_CONTENT); 69 w.writeNamespace("prefix", XML_CONTENT); 70 71 w.writeAttribute("attribute", XML_CONTENT); 72 w.writeAttribute(XML_CONTENT, "attribute2", XML_CONTENT); 73 w.writeAttribute("prefix", XML_CONTENT, "attribute3", XML_CONTENT); 74 75 w.writeCharacters("\n"); 76 w.writeCharacters(XML_CONTENT); 77 w.writeCharacters("\n"); 78 w.writeCharacters(XML_CONTENT.toCharArray(), 0, XML_CONTENT.length()); 79 w.writeCharacters("\n"); 80 81 w.writeEndElement(); 82 w.writeEndDocument(); 83 w.flush(); 84 85 System.out.println(sw); 86 87 // make sure that the generated XML parses 88 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 89 dbf.setNamespaceAware(true); 90 dbf.newDocumentBuilder().parse(new InputSource(new StringReader(sw.toString()))); 91 } catch (XMLStreamException xmlStreamException) { 92 xmlStreamException.printStackTrace(); 93 Assert.fail(xmlStreamException.toString()); 94 } catch (SAXException saxException) { 95 saxException.printStackTrace(); 96 Assert.fail(saxException.toString()); 97 } catch (ParserConfigurationException parserConfigurationException) { 98 parserConfigurationException.printStackTrace(); 99 Assert.fail(parserConfigurationException.toString()); 100 } catch (IOException ioException) { 101 ioException.printStackTrace(); 102 Assert.fail(ioException.toString()); 103 } 104 } 105} 106 107