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