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 parsers;
25 
26 import java.io.StringReader;
27 
28 import javax.xml.XMLConstants;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.stream.StreamSource;
32 import javax.xml.validation.Schema;
33 import javax.xml.validation.SchemaFactory;
34 
35 import org.testng.Assert;
36 import org.testng.annotations.Listeners;
37 import org.testng.annotations.Test;
38 import org.xml.sax.InputSource;
39 
40 /*
41  * @test
42  * @bug 4967002
43  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
44  * @run testng/othervm -DrunSecMngr=true parsers.Bug4967002
45  * @run testng/othervm parsers.Bug4967002
46  * @summary Test DocumentBuilderFactory.newDocumentBuilder() throws ParserConfigurationException
47  * when it uses the "http://java.sun.com/xml/jaxp/properties/schemaSource" property
48  * and/or the "http://java.sun.com/xml/jaxp/properties/schemaLanguage" property
49  * in conjunction with setting a Schema object.
50  */
51 @Listeners({jaxp.library.BasePolicy.class})
52 public class Bug4967002 {
53     String schemaSource = "<?xml version='1.0'?>\n" + "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\n" + "  <xsd:element name='test101'>\n"
54             + "    <xsd:complexType>\n" + "      <xsd:attribute name='attr'/>\n" + "      <xsd:attribute name='attr2' default='DEF'/>\n"
55             + "    </xsd:complexType>\n" + "  </xsd:element>\n" + "</xsd:schema>\n";
56 
createSchema()57     Schema createSchema() {
58         SchemaFactory schFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
59         try {
60             Schema sch = schFactory.newSchema(new StreamSource(new StringReader(schemaSource)));
61             return sch;
62         } catch (Exception se) {
63             throw new IllegalStateException("No Schema : " + se);
64         }
65     }
66 
67     @Test
test1()68     public void test1() {
69         setAttr(true);
70     }
71 
72     @Test
test2()73     public void test2() {
74         setAttr(false);
75     }
76 
setAttr(boolean setSrc)77     void setAttr(boolean setSrc) {
78         DocumentBuilderFactory docBFactory = DocumentBuilderFactory.newInstance();
79         Schema sch = createSchema();
80         docBFactory.setSchema(sch);
81         docBFactory.setNamespaceAware(true);
82         docBFactory.setValidating(true);
83 
84         final String aSchemaLanguage = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
85         final String aSchemaSource = "http://java.sun.com/xml/jaxp/properties/schemaSource";
86 
87         docBFactory.setAttribute(aSchemaLanguage, "http://www.w3.org/2001/XMLSchema");
88         // System.out.println("---- Set schemaLanguage: " +
89         // docBFactory.getAttribute(aSchemaLanguage));
90         if (setSrc) {
91             docBFactory.setAttribute(aSchemaSource, new InputSource(new StringReader(schemaSource)));
92             // System.out.println("---- Set schemaSource: " +
93             // docBFactory.getAttribute(aSchemaSource));
94         }
95 
96         try {
97             docBFactory.newDocumentBuilder();
98             Assert.fail("ParserConfigurationException expected");
99         } catch (ParserConfigurationException pce) {
100             return; // success
101         }
102     }
103 }
104