1 /*
2  * Copyright (c) 2015, 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 catalog;
25 
26 import static catalog.CatalogTestUtils.CATALOG_PUBLIC;
27 import static catalog.CatalogTestUtils.CATALOG_SYSTEM;
28 import static catalog.CatalogTestUtils.CATALOG_URI;
29 import static catalog.CatalogTestUtils.catalogResolver;
30 import static catalog.CatalogTestUtils.catalogUriResolver;
31 import static catalog.ResolutionChecker.checkSysIdResolution;
32 import static catalog.ResolutionChecker.checkUriResolution;
33 
34 import javax.xml.catalog.CatalogException;
35 import javax.xml.catalog.CatalogResolver;
36 
37 import org.testng.annotations.DataProvider;
38 import org.testng.annotations.Listeners;
39 import org.testng.annotations.Test;
40 
41 /*
42  * @test
43  * @bug 8077931
44  * @library /javax/xml/jaxp/libs
45  * @run testng/othervm -DrunSecMngr=true catalog.LoadCatalogTest
46  * @run testng/othervm catalog.LoadCatalogTest
47  * @summary When catalog resolver loads catalog files, the current catalog file
48  *          and the catalog files specified by the nextCatalog entries may not
49  *          accessible. This case tests how does the resolver handle this issue.
50  */
51 @Listeners({jaxp.library.FilePolicy.class})
52 public class LoadCatalogTest {
53 
54     private static final String CATALOG_LOADCATALOGFILES = "loadCatalogFiles.xml";
55     private static final String CATALOG_DUMMY = "dummy.xml";
56 
57     private static final String ID_ALICE = "http://remote/dtd/alice/docAlice.dtd";
58     private static final String ID_ALICE_URI = "http://remote/dtd/uri/alice/docAlice.dtd";
59     private static final String ID_DUMMY = "http://remote/dtd/doc.dtd";
60 
61     @Test(dataProvider = "entityResolver")
testMatchOnEntityResolver(CatalogResolver resolver)62     public void testMatchOnEntityResolver(CatalogResolver resolver) {
63         checkSysIdResolution(resolver, ID_ALICE,
64                 "http://local/dtd/docAliceSys.dtd");
65     }
66 
67     @DataProvider(name = "entityResolver")
entityResolver()68     public Object[][] entityResolver() {
69         return new Object[][] {
70                 // This EntityResolver loads multiple catalog files one by one.
71                 // All of the files are available.
72                 { catalogResolver(CATALOG_PUBLIC, CATALOG_URI,
73                         CATALOG_SYSTEM) },
74 
75                 // This EntityResolver loads multiple catalog files one by one,
76                 // but the middle one isn't existing.
77                 { catalogResolver(CATALOG_PUBLIC, CATALOG_DUMMY,
78                         CATALOG_SYSTEM) } };
79     }
80 
81     @Test(dataProvider = "uriResolver")
testMatchOnUriResolver(CatalogResolver resolver)82     public void testMatchOnUriResolver(CatalogResolver resolver) {
83         checkUriResolution(resolver, ID_ALICE_URI,
84                 "http://local/dtd/docAliceURI.dtd");
85     }
86 
87     @DataProvider(name = "uriResolver")
uriResolver()88     public Object[][] uriResolver() {
89         return new Object[][] {
90                 // This URIResolver loads multiple catalog files one by one.
91                 // All of the files are available.
92                 { catalogUriResolver(CATALOG_PUBLIC, CATALOG_SYSTEM,
93                         CATALOG_URI) },
94 
95                 // This URIResolver loads multiple catalog files one by one,
96                 // but the middle one isn't existing.
97                 { catalogUriResolver(CATALOG_PUBLIC, CATALOG_DUMMY,
98                         CATALOG_URI) } };
99     }
100 
101     @Test(dataProvider = "catalogName",
102             expectedExceptions = CatalogException.class)
testExceptionOnEntityResolver(String[] catalogName)103     public void testExceptionOnEntityResolver(String[] catalogName) {
104         catalogResolver(catalogName).resolveEntity(null, ID_DUMMY);
105     }
106 
107     @Test(dataProvider = "catalogName",
108             expectedExceptions = CatalogException.class)
testExceptionOnUriResolver(String[] catalogName)109     public void testExceptionOnUriResolver(String[] catalogName) {
110         catalogUriResolver(catalogName).resolve(ID_DUMMY, null);
111     }
112 
113     @DataProvider(name = "catalogName")
catalogName()114     public Object[][] catalogName() {
115         return new Object[][] {
116                 // This catalog file set includes null catalog files.
117                 { (String[]) null },
118 
119                 // This catalog file set includes one catalog file, but this
120                 // catalog defines a non-existing next catalog.
121                 { new String[] { CATALOG_LOADCATALOGFILES } } };
122     }
123 }
124