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