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 
  24 package stream.XMLStreamReaderTest;
  25 
  26 import java.io.StringReader;
  27 
  28 import javax.xml.stream.XMLInputFactory;
  29 import javax.xml.stream.XMLStreamException;
  30 import javax.xml.stream.XMLStreamReader;
  31 
  32 import org.testng.Assert;
  33 import org.testng.annotations.Listeners;
  34 import org.testng.annotations.Test;
  35 
  36 /*
  37  * @test
  38  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  39  * @run testng/othervm -DrunSecMngr=true stream.XMLStreamReaderTest.DoubleXmlnsTest
  40  * @run testng/othervm stream.XMLStreamReaderTest.DoubleXmlnsTest
  41  * @summary Test double namespaces and nested namespaces.
  42  */
  43 @Listeners({jaxp.library.BasePolicy.class})
  44 public class DoubleXmlnsTest {
  45 
  46     @Test
  47     public void testDoubleNS() throws Exception {
  48 
  49         final String INVALID_XML = "<foo xmlns:xmli='http://www.w3.org/XML/1998/namespacei' xmlns:xmli='http://www.w3.org/XML/1998/namespacei' />";
  50 
  51         try {
  52             XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(INVALID_XML));
  53 
  54             while (xsr.hasNext()) {
  55                 xsr.next();
  56             }
  57 
  58             Assert.fail("Wellformedness error expected: " + INVALID_XML);
  59         } catch (XMLStreamException e) {
  60             ; // this is expected
  61         }
  62     }
  63 
  64     @Test
  65     public void testNestedNS() throws Exception {
  66 
  67         final String VALID_XML = "<foo xmlns:xmli='http://www.w3.org/XML/1998/namespacei'><bar xmlns:xmli='http://www.w3.org/XML/1998/namespaceii'></bar></foo>";
  68 
  69         try {
  70             XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(VALID_XML));
  71 
  72             while (xsr.hasNext()) {
  73                 xsr.next();
  74             }
  75 
  76             // expected success
  77         } catch (XMLStreamException e) {
  78             e.printStackTrace();
  79 
  80             Assert.fail("Wellformedness error is not expected: " + VALID_XML + ", " + e.getMessage());
  81         }
  82     }
  83 
  84     @Test
  85     public void testDoubleXmlns() throws Exception {
  86 
  87         final String INVALID_XML = "<foo xmlns:xml='http://www.w3.org/XML/1998/namespace' xmlns:xml='http://www.w3.org/XML/1998/namespace' ></foo>";
  88 
  89         try {
  90             XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(INVALID_XML));
  91 
  92             while (xsr.hasNext()) {
  93                 xsr.next();
  94             }
  95 
  96             Assert.fail("Wellformedness error expected :" + INVALID_XML);
  97         } catch (XMLStreamException e) {
  98             ; // this is expected
  99         }
 100     }
 101 
 102     @Test
 103     public void testNestedXmlns() throws Exception {
 104 
 105         final String VALID_XML = "<foo xmlns:xml='http://www.w3.org/XML/1998/namespace'><bar xmlns:xml='http://www.w3.org/XML/1998/namespace'></bar></foo>";
 106 
 107         try {
 108             XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(VALID_XML));
 109 
 110             while (xsr.hasNext()) {
 111                 xsr.next();
 112             }
 113 
 114             // expected success
 115         } catch (XMLStreamException e) {
 116             e.printStackTrace();
 117             Assert.fail("Wellformedness error is not expected: " + VALID_XML + ", " + e.getMessage());
 118         }
 119     }
 120 }
 121