Path: blob/master/test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamReaderTest/Bug6472982Test.java
43197 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.XMLStreamReaderTest;2425import java.io.InputStream;2627import javax.xml.namespace.NamespaceContext;28import javax.xml.stream.XMLInputFactory;29import javax.xml.stream.XMLStreamReader;3031import org.testng.Assert;32import org.testng.annotations.Listeners;33import org.testng.annotations.Test;3435/*36* @test37* @bug 647298238* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest39* @run testng/othervm -DrunSecMngr=true -Djava.security.manager=allow stream.XMLStreamReaderTest.Bug6472982Test40* @run testng/othervm stream.XMLStreamReaderTest.Bug6472982Test41* @summary Test XMLStreamReader.getNamespaceContext().getPrefix("") won't throw IllegalArgumentException.42*/43@Listeners({jaxp.library.BasePolicy.class})44public class Bug6472982Test {45String namespaceURI = "foobar.com";46String rootElement = "foo";47String childElement = "foochild";48String prefix = "a";4950@Test51public void testNamespaceContext() {52try {53XMLInputFactory xif = XMLInputFactory.newInstance();54xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);55InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes());56XMLStreamReader sr = xif.createXMLStreamReader(is);57NamespaceContext context = sr.getNamespaceContext();58Assert.assertTrue(context.getPrefix("") == null);5960} catch (IllegalArgumentException iae) {61Assert.fail("NamespacePrefix#getPrefix() should not throw an IllegalArgumentException for empty uri. ");62} catch (Exception ex) {63ex.printStackTrace();64}65}6667String getXML() {68StringBuffer sbuffer = new StringBuffer();69sbuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");70sbuffer.append("<" + rootElement + " xmlns:");71sbuffer.append(prefix);72sbuffer.append("=\"" + namespaceURI + "\">");73sbuffer.append("<" + prefix + ":" + childElement + ">");74sbuffer.append("blahblah");75sbuffer.append("</" + prefix + ":" + childElement + ">");76sbuffer.append("</" + rootElement + ">");77// System.out.println("XML = " + sbuffer.toString()) ;78return sbuffer.toString();79}80}818283