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
24package validation;
25
26import java.io.IOException;
27import java.io.StringReader;
28
29import javax.xml.parsers.ParserConfigurationException;
30import javax.xml.parsers.SAXParserFactory;
31import javax.xml.transform.stream.StreamSource;
32import javax.xml.validation.Schema;
33import javax.xml.validation.SchemaFactory;
34import javax.xml.validation.TypeInfoProvider;
35import javax.xml.validation.ValidatorHandler;
36
37import org.testng.Assert;
38import org.testng.annotations.Listeners;
39import org.testng.annotations.Test;
40import org.xml.sax.Attributes;
41import org.xml.sax.InputSource;
42import org.xml.sax.SAXException;
43import org.xml.sax.XMLReader;
44import org.xml.sax.helpers.DefaultHandler;
45
46/*
47 * @test
48 * @bug 4970951
49 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
50 * @run testng/othervm -DrunSecMngr=true validation.Bug4970951
51 * @run testng/othervm validation.Bug4970951
52 * @summary Test TypeInfoProvider.isSpecified(...) return true if the attribute was originally present.
53 */
54@Listeners({jaxp.library.BasePolicy.class})
55public class Bug4970951 {
56
57    public static final String XSD = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + "        xmlns:test='jaxp13_test'\n"
58            + "        targetNamespace='jaxp13_test'\n" + "        elementFormDefault='qualified'>\n" + "    <element name='test'>\n"
59            + "        <complexType>\n" + "            <sequence>\n" + "                <element name='child' type='string'/>\n" + "            </sequence>\n"
60            + "            <attribute name='id' />\n" + "            <attribute name='date' default='2003-12-04'/>\n" + "        </complexType>\n"
61            + "    </element>\n" + "</schema>\n";
62
63    public static final String XML = "<?xml version='1.0'?>\n" + "<ns:test xmlns:ns='jaxp13_test' id='i001'>\n" + "  <ns:child>123abc</ns:child>\n"
64            + "</ns:test>\n";
65
66    private ValidatorHandler createValidatorHandler(String xsd) throws SAXException {
67        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
68
69        StringReader reader = new StringReader(xsd);
70        StreamSource xsdSource = new StreamSource(reader);
71
72        Schema schema = schemaFactory.newSchema(xsdSource);
73        return schema.newValidatorHandler();
74    }
75
76    private XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
77        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
78        parserFactory.setNamespaceAware(true);
79
80        return parserFactory.newSAXParser().getXMLReader();
81    }
82
83    private void parse(XMLReader xmlReader, String xml) throws SAXException, IOException {
84        StringReader reader = new StringReader(xml);
85        InputSource inSource = new InputSource(reader);
86
87        xmlReader.parse(inSource);
88    }
89
90    @Test
91    public void test() throws Exception {
92        XMLReader xmlReader = createXMLReader();
93        final ValidatorHandler validatorHandler = createValidatorHandler(XSD);
94        xmlReader.setContentHandler(validatorHandler);
95
96        DefaultHandler handler = new DefaultHandler() {
97            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
98                if (!"ns:test".equals(qName)) {
99                    return;
100                }
101
102                TypeInfoProvider infoProvider = validatorHandler.getTypeInfoProvider();
103                if (infoProvider == null) {
104                    throw new SAXException("Can't obtain TypeInfoProvider object.");
105                }
106
107                int index = attributes.getIndex("id");
108                if (index == -1) {
109                    throw new SAXException("The attribute 'id' is not in the list.");
110                }
111
112                Assert.assertTrue(infoProvider.isSpecified(index));
113
114                index = attributes.getIndex("date");
115                if (index == -1) {
116                    throw new SAXException("The attribute 'date' is not in the list.");
117                }
118
119                Assert.assertFalse(infoProvider.isSpecified(index));
120
121                System.out.println("OK");
122            }
123        };
124        validatorHandler.setContentHandler(handler);
125
126        parse(xmlReader, XML);
127    }
128}
129