Bug4970402.java revision 997:540334ae53fe
167754Smsmith/*
267754Smsmith * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3193267Sjkim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
467754Smsmith *
567754Smsmith * This code is free software; you can redistribute it and/or modify it
667754Smsmith * under the terms of the GNU General Public License version 2 only, as
767754Smsmith * published by the Free Software Foundation.
8316303Sjkim *
9316303Sjkim * This code is distributed in the hope that it will be useful, but WITHOUT
10316303Sjkim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11316303Sjkim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12316303Sjkim * version 2 for more details (a copy is included in the LICENSE file that
1370243Smsmith * accompanied this code).
1467754Smsmith *
15316303Sjkim * You should have received a copy of the GNU General Public License version
16316303Sjkim * 2 along with this work; if not, write to the Free Software Foundation,
17316303Sjkim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18316303Sjkim *
19316303Sjkim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20316303Sjkim * or visit www.oracle.com if you need additional information or have any
21316303Sjkim * questions.
22316303Sjkim */
23316303Sjkim
24316303Sjkimpackage validation;
25316303Sjkim
26316303Sjkimimport java.io.IOException;
27316303Sjkimimport java.io.StringReader;
28316303Sjkim
29316303Sjkimimport javax.xml.parsers.ParserConfigurationException;
30316303Sjkimimport javax.xml.parsers.SAXParserFactory;
31316303Sjkimimport javax.xml.transform.stream.StreamSource;
32316303Sjkimimport javax.xml.validation.Schema;
33316303Sjkimimport javax.xml.validation.SchemaFactory;
34316303Sjkimimport javax.xml.validation.TypeInfoProvider;
35316303Sjkimimport javax.xml.validation.ValidatorHandler;
36316303Sjkim
37316303Sjkimimport org.testng.Assert;
38316303Sjkimimport org.testng.annotations.Listeners;
39316303Sjkimimport org.testng.annotations.Test;
40316303Sjkimimport org.xml.sax.Attributes;
41316303Sjkimimport org.xml.sax.InputSource;
42316303Sjkimimport org.xml.sax.SAXException;
43316303Sjkimimport org.xml.sax.XMLReader;
44316303Sjkimimport org.xml.sax.helpers.DefaultHandler;
45316303Sjkim
46316303Sjkim/*
47316303Sjkim * @test
48316303Sjkim * @bug 4970402
49316303Sjkim * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
50316303Sjkim * @run testng/othervm -DrunSecMngr=true validation.Bug4970402
51316303Sjkim * @run testng/othervm validation.Bug4970402
52316303Sjkim * @summary Test TypeInfoProvider's attribute accessing methods throw IndexOutOfBoundsException when index parameter is invalid.
53316303Sjkim */
54316303Sjkim@Listeners({jaxp.library.BasePolicy.class})
55316303Sjkimpublic class Bug4970402 {
56316303Sjkim
57316303Sjkim    public static final String XSD = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + "        xmlns:test='jaxp13_test'\n"
58316303Sjkim            + "        targetNamespace='jaxp13_test'\n" + "        elementFormDefault='qualified'>\n" + "    <element name='test'>\n"
59316303Sjkim            + "        <complexType>\n" + "            <sequence>\n" + "                <element name='child' type='string'/>\n" + "            </sequence>\n"
60316303Sjkim            + "            <attribute name='id' />\n" + "        </complexType>\n" + "    </element>\n" + "</schema>\n";
61316303Sjkim
62316303Sjkim    public static final String XML = "<?xml version='1.0'?>\n" + "<ns:test xmlns:ns='jaxp13_test' id='2003-12-02'>\n" + "  <ns:child>123abc</ns:child>\n"
63316303Sjkim            + "</ns:test>\n";
64316303Sjkim
65316303Sjkim    private ValidatorHandler createValidatorHandler(String xsd) throws SAXException {
66316303Sjkim        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
67316303Sjkim
68316303Sjkim        StringReader reader = new StringReader(xsd);
69316303Sjkim        StreamSource xsdSource = new StreamSource(reader);
70316303Sjkim
71316303Sjkim        Schema schema = schemaFactory.newSchema(xsdSource);
72316303Sjkim        return schema.newValidatorHandler();
73316303Sjkim    }
74316303Sjkim
75316303Sjkim    private XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
76316303Sjkim        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
77316303Sjkim        parserFactory.setNamespaceAware(true);
78316303Sjkim        return parserFactory.newSAXParser().getXMLReader();
79316303Sjkim    }
80316303Sjkim
81316303Sjkim    private void parse(XMLReader xmlReader, String xml) throws SAXException, IOException {
82316303Sjkim        StringReader reader = new StringReader(xml);
83316303Sjkim        InputSource inSource = new InputSource(reader);
84316303Sjkim
85316303Sjkim        xmlReader.parse(inSource);
86316303Sjkim    }
87316303Sjkim
88316303Sjkim    @Test
89316303Sjkim    public void test() throws Exception {
90316303Sjkim        XMLReader xmlReader = createXMLReader();
91316303Sjkim        final ValidatorHandler validatorHandler = createValidatorHandler(XSD);
92316303Sjkim        xmlReader.setContentHandler(validatorHandler);
93316303Sjkim
94316303Sjkim        DefaultHandler handler = new DefaultHandler() {
95316303Sjkim            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
96316303Sjkim                if (!"ns:test".equals(qName)) {
97316303Sjkim                    return;
98316303Sjkim                }
99316303Sjkim
100316303Sjkim                TypeInfoProvider infoProvider = null;
101316303Sjkim                synchronized (validatorHandler) {
102316303Sjkim                    infoProvider = validatorHandler.getTypeInfoProvider();
103316303Sjkim                }
104316303Sjkim                Assert.assertTrue(infoProvider != null, "Can't obtain TypeInfoProvider object.");
105316303Sjkim
106316303Sjkim                try {
107316303Sjkim                    infoProvider.getAttributeTypeInfo(-1);
108316303Sjkim                    Assert.fail("IndexOutOfBoundsException was not thrown.");
109316303Sjkim                } catch (IndexOutOfBoundsException e) {
110316303Sjkim                    ; // as expected
111316303Sjkim                }
112316303Sjkim
113316303Sjkim                try {
114316303Sjkim                    infoProvider.isIdAttribute(-1);
115316303Sjkim                    Assert.fail("IndexOutOfBoundsException was not thrown.");
116316303Sjkim                } catch (IndexOutOfBoundsException e) {
117316303Sjkim                    ; // as expected
118316303Sjkim                }
119316303Sjkim            }
120217365Sjkim        };
121217365Sjkim        validatorHandler.setContentHandler(handler);
122217365Sjkim
123217365Sjkim        parse(xmlReader, XML);
124217365Sjkim    }
125217365Sjkim}
126217365Sjkim