1/*
2 * Copyright (c) 2015, 2017, 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 catalog;
25
26import static catalog.CatalogTestUtils.DEFER_FALSE;
27import static catalog.CatalogTestUtils.DEFER_TRUE;
28import static catalog.CatalogTestUtils.getCatalogPath;
29import static javax.xml.catalog.CatalogFeatures.Feature.DEFER;
30import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
31import static jaxp.library.JAXPTestUtilities.tryRunWithAllPerm;
32
33import java.lang.reflect.Method;
34
35import javax.xml.catalog.Catalog;
36import javax.xml.catalog.CatalogException;
37import javax.xml.catalog.CatalogFeatures;
38import javax.xml.catalog.CatalogManager;
39import javax.xml.catalog.CatalogResolver;
40
41import org.testng.Assert;
42import org.testng.annotations.DataProvider;
43import org.testng.annotations.Listeners;
44import org.testng.annotations.Test;
45
46/*
47 * @test
48 * @bug 8077931 8176405
49 * @library /javax/xml/jaxp/libs
50 * @modules java.xml/javax.xml.catalog:open
51 * @run testng/othervm -DrunSecMngr=true catalog.DeferFeatureTest
52 * @run testng/othervm catalog.DeferFeatureTest
53 * @summary This case tests whether the catalogs specified in delegateSystem,
54 *          delegatePublic, delegateURI and nextCatalog entries are used lazily
55 *          in resolution via defer feature.
56 */
57@Listeners({jaxp.library.FilePolicy.class})
58public class DeferFeatureTest {
59
60    @Test(dataProvider = "catalog-countOfLoadedCatalogFile")
61    public void testDeferFeature(Catalog catalog, int catalogCount)
62            throws Exception {
63        Assert.assertEquals(loadedCatalogCount(catalog), catalogCount);
64    }
65
66    @Test(dataProvider = "testDeferFeatureByResolve")
67    public void testDeferFeatureByResolve(Catalog catalog, int catalogCount)
68            throws Exception {
69        CatalogResolver cr = createResolver(catalog);
70        // trigger loading alternative catalogs
71        try {
72            cr.resolveEntity("-//REMOTE//DTD ALICE DOCALICE", "http://remote/dtd/alice/");
73        } catch (CatalogException ce) {}
74
75        Assert.assertEquals(loadedCatalogCount(catalog), catalogCount);
76    }
77
78    @DataProvider(name = "catalog-countOfLoadedCatalogFile")
79    public Object[][] data() {
80        return new Object[][]{
81            // By default, alternative catalogs are not loaded.
82            {createCatalog(CatalogFeatures.defaults()), 1},
83            // Alternative catalogs are not loaded when DEFER is set to true.
84            {createCatalog(createDeferFeature(DEFER_TRUE)), 1},
85            // The 3 alternative catalogs are pre-loaded along with the parent
86            //when DEFER is set to false.
87            {createCatalog(createDeferFeature(DEFER_FALSE)), 4}};
88    }
89
90    @DataProvider(name = "testDeferFeatureByResolve")
91    public Object[][] getData() {
92        return new Object[][]{
93            {createCatalog(createDeferFeature(DEFER_TRUE)), 4}
94        };
95    }
96
97    private CatalogFeatures createDeferFeature(String defer) {
98        return CatalogFeatures.builder().with(DEFER, defer).build();
99    }
100
101    private Catalog createCatalog(CatalogFeatures feature) {
102        return CatalogManager.catalog(feature, getCatalogPath("deferFeature.xml"));
103    }
104
105    private CatalogResolver createResolver(Catalog catalog) {
106        return CatalogManager.catalogResolver(catalog);
107    }
108
109    private int loadedCatalogCount(Catalog catalog) throws Exception {
110        Method method = tryRunWithAllPerm(() -> catalog.getClass().getDeclaredMethod("loadedCatalogCount"));
111        runWithAllPerm(() -> method.setAccessible(true));
112        return (int) method.invoke(catalog);
113    }
114}
115