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.DEFER_FALSE;
  27 import static catalog.CatalogTestUtils.DEFER_TRUE;
  28 import static catalog.CatalogTestUtils.getCatalogPath;
  29 import static javax.xml.catalog.CatalogFeatures.Feature.DEFER;
  30 import static javax.xml.catalog.CatalogManager.catalog;
  31 import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
  32 import static jaxp.library.JAXPTestUtilities.tryRunWithAllPerm;
  33 
  34 import java.lang.reflect.Method;
  35 
  36 import javax.xml.catalog.Catalog;
  37 import javax.xml.catalog.CatalogFeatures;
  38 
  39 import org.testng.Assert;
  40 import org.testng.annotations.DataProvider;
  41 import org.testng.annotations.Listeners;
  42 import org.testng.annotations.Test;
  43 
  44 /*
  45  * @test
  46  * @bug 8077931
  47  * @library /javax/xml/jaxp/libs
  48  * @modules java.xml/javax.xml.catalog:open
  49  * @run testng/othervm -DrunSecMngr=true catalog.DeferFeatureTest
  50  * @run testng/othervm catalog.DeferFeatureTest
  51  * @summary This case tests whether the catalogs specified in delegateSystem,
  52  *          delegatePublic, delegateURI and nextCatalog entries are used lazily
  53  *          in resolution via defer feature.
  54  */
  55 @Listeners({jaxp.library.FilePolicy.class})
  56 public class DeferFeatureTest {
  57 
  58     @Test(dataProvider = "catalog-countOfLoadedCatalogFile")
  59     public void testDeferFeature(Catalog catalog, int catalogCount)
  60             throws Exception {
  61         Assert.assertEquals(loadedCatalogCount(catalog), catalogCount);
  62     }
  63 
  64     @DataProvider(name = "catalog-countOfLoadedCatalogFile")
  65     public Object[][] data() {
  66         return new Object[][]{
  67             // By default, alternative catalogs are not loaded.
  68             {createCatalog(CatalogFeatures.defaults()), 1},
  69             // Alternative catalogs are not loaded when DEFER is set to true.
  70             {createCatalog(createDeferFeature(DEFER_TRUE)), 1},
  71             // The 3 alternative catalogs are pre-loaded along with the parent
  72             //when DEFER is set to false.
  73             {createCatalog(createDeferFeature(DEFER_FALSE)), 4}};
  74     }
  75 
  76     private CatalogFeatures createDeferFeature(String defer) {
  77         return CatalogFeatures.builder().with(DEFER, defer).build();
  78     }
  79 
  80     private Catalog createCatalog(CatalogFeatures feature) {
  81         return catalog(feature, getCatalogPath("deferFeature.xml"));
  82     }
  83 
  84     private int loadedCatalogCount(Catalog catalog) throws Exception {
  85         Method method = tryRunWithAllPerm(() -> catalog.getClass().getDeclaredMethod("loadedCatalogCount"));
  86         runWithAllPerm(() -> method.setAccessible(true));
  87         return (int) method.invoke(catalog);
  88     }
  89 }