1 /*
   2  * Copyright (c) 2014, 2016, 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 sax;
  25 
  26 import java.io.ByteArrayInputStream;
  27 
  28 import javax.xml.parsers.SAXParser;
  29 import javax.xml.parsers.SAXParserFactory;
  30 
  31 import org.testng.Assert;
  32 import org.testng.annotations.Listeners;
  33 import org.testng.annotations.Test;
  34 import org.xml.sax.Attributes;
  35 import org.xml.sax.SAXException;
  36 import org.xml.sax.helpers.DefaultHandler;
  37 
  38 /*
  39  * @bug 6949607
  40  * @summary Test Attributes.getValue returns null when parameter uri is empty.
  41  */
  42 @Listeners({jaxp.library.BasePolicy.class})
  43 public class Bug6949607Test {
  44 
  45     final String MSG = "Failed to parse XML";
  46     String textXML = "<prefix:rootElem xmlns:prefix=\"something\" prefix:attr=\"attrValue\" />";
  47 
  48     @Test
  49     public void testException() {
  50         try {
  51             SAXParserFactory factory = SAXParserFactory.newInstance();
  52             factory.setNamespaceAware(true);
  53             factory.setValidating(true);
  54             SAXParser saxParser = factory.newSAXParser();
  55 
  56             saxParser.parse(new ByteArrayInputStream(textXML.getBytes()), new TestFilter());
  57 
  58         } catch (Throwable t) {
  59             t.printStackTrace();
  60         }
  61     }
  62 
  63     class TestFilter extends DefaultHandler {
  64         @Override
  65         public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
  66             super.startElement(uri, localName, qName, atts);
  67 
  68             String attr_WithNs = atts.getValue("something", "attr");
  69             String attr_NoNs = atts.getValue("", "attr");
  70 
  71             System.out.println("withNs: " + attr_WithNs);
  72             System.out.println("NoNs: " + attr_NoNs);
  73 
  74             Assert.assertTrue(attr_NoNs == null, "Should return null when uri is empty.");
  75 
  76         }
  77     }
  78 
  79 }