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.StringReader;
27
28import javax.xml.transform.Source;
29import javax.xml.transform.stream.StreamSource;
30import javax.xml.validation.SchemaFactory;
31
32import org.testng.Assert;
33import org.testng.annotations.Listeners;
34import org.testng.annotations.Test;
35import org.w3c.dom.ls.LSInput;
36import org.w3c.dom.ls.LSResourceResolver;
37
38/*
39 * @test
40 * @bug 4997818
41 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
42 * @run testng/othervm -DrunSecMngr=true validation.Bug4997818
43 * @run testng/othervm validation.Bug4997818
44 * @summary Test SchemaFactory.newSchema(...) throws an exception, which is thrown from LSResourceResolver.
45 */
46
47@Listeners({jaxp.library.BasePolicy.class})
48public class Bug4997818 {
49
50    @Test
51    public void test1() throws Exception {
52        String xsd1 = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + "        xmlns:test='jaxp13_test1'\n"
53                + "        targetNamespace='jaxp13_test1'\n" + "        elementFormDefault='qualified'>\n" + "    <import namespace='jaxp13_test2'/>\n"
54                + "    <element name='test'/>\n" + "    <element name='child1'/>\n" + "</schema>\n";
55
56        final NullPointerException EUREKA = new NullPointerException("NewSchema015");
57
58        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
59        StringReader reader = new StringReader(xsd1);
60        StreamSource source = new StreamSource(reader);
61        LSResourceResolver resolver = new LSResourceResolver() {
62            public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
63                LSInput input;
64                if (namespaceURI != null && namespaceURI.endsWith("jaxp13_test2")) {
65                    throw EUREKA;
66                } else {
67                    input = null;
68                }
69
70                return input;
71            }
72        };
73        schemaFactory.setResourceResolver(resolver);
74
75        try {
76            schemaFactory.newSchema(new Source[] { source });
77            Assert.fail("NullPointerException was not thrown.");
78        } catch (RuntimeException e) {
79            if (e != EUREKA)
80                throw e;
81        }
82    }
83}
84