1/*
2 * Copyright (c) 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
24import static org.testng.Assert.assertTrue;
25
26import java.io.File;
27import java.nio.file.Path;
28import java.nio.file.Paths;
29
30import static jdk.test.lib.process.ProcessTools.executeTestJava;
31import jdk.test.lib.compiler.CompilerUtils;
32
33import org.testng.annotations.BeforeTest;
34import org.testng.annotations.Test;
35
36/*
37 * @test
38 * @library /test/lib
39 * @run testng BasicModularXMLParserTest
40 * @bug 8078820 8156119
41 * @summary Tests JAXP lib can instantiate the following interfaces
42 *          with customized provider module on boot layer
43 *
44 *          javax.xml.datatype.DatatypeFactory
45 *          javax.xml.parsers.DocumentBuilderFactory
46 *          javax.xml.parsers.SAXParserFactory
47 *          javax.xml.stream.XMLEventFactory
48 *          javax.xml.stream.XMLInputFactory
49 *          javax.xml.stream.XMLOutputFactory
50 *          javax.xml.transform.TransformerFactory
51 *          javax.xml.validation.SchemaFactory
52 *          javax.xml.xpath.XPathFactory
53 *          org.xml.sax.XMLReader
54 */
55
56@Test
57public class BasicModularXMLParserTest {
58
59    private static final String TEST_SRC = System.getProperty("test.src");
60
61    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
62    private static final Path MOD_DIR1 = Paths.get("mod1");
63    private static final Path MOD_DIR2 = Paths.get("mod2");
64    private static final Path CLASSES_DIR = Paths.get("classes");
65
66    /*
67     * Compiles all modules used by the test
68     */
69    @BeforeTest
70    public void compileAll() throws Exception {
71        assertTrue(CompilerUtils.compile(SRC_DIR.resolve("xmlprovider1"), MOD_DIR1.resolve("xmlprovider1")));
72        assertTrue(CompilerUtils.compile(SRC_DIR.resolve("xmlprovider2"), MOD_DIR2.resolve("xmlprovider2")));
73        assertTrue(CompilerUtils.compile(SRC_DIR.resolve("unnamed"), CLASSES_DIR));
74    }
75
76    /*
77     * test the default JAXP implementation
78     */
79    public void testDefault() throws Exception {
80        int exitValue
81            = executeTestJava("-cp", CLASSES_DIR.toString(),
82                              "Main")
83                .outputTo(System.out)
84                .errorTo(System.out)
85                .getExitValue();
86
87        assertTrue(exitValue == 0);
88    }
89
90    /*
91     * test loading one provider module
92     */
93    public void testWithOneProvider() throws Exception {
94        int exitValue
95            = executeTestJava("--module-path", MOD_DIR1.toString(),
96                              "-cp", CLASSES_DIR.toString(),
97                              "Main", "xmlprovider1")
98                .outputTo(System.out)
99                .errorTo(System.out)
100                .getExitValue();
101
102        assertTrue(exitValue == 0);
103    }
104
105    /*
106     * test loading both provider modules
107     */
108    public void testWithTwoProvider() throws Exception {
109        int exitValue
110            = executeTestJava("--module-path", MOD_DIR1.toString() + File.pathSeparator + MOD_DIR2.toString(),
111                              "-cp", CLASSES_DIR.toString(),
112                              "Main", "xmlprovider1", "xmlprovider2")
113                .outputTo(System.out)
114                .errorTo(System.out)
115                .getExitValue();
116
117        assertTrue(exitValue == 0);
118    }
119
120}
121