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.FEATURE_FILES;
  27 import static catalog.CatalogTestUtils.catalogResolver;
  28 import static catalog.CatalogTestUtils.catalogUriResolver;
  29 import static catalog.CatalogTestUtils.getCatalogPath;
  30 import static catalog.ResolutionChecker.checkSysIdResolution;
  31 import static catalog.ResolutionChecker.checkUriResolution;
  32 import static javax.xml.catalog.CatalogFeatures.builder;
  33 import static javax.xml.catalog.CatalogFeatures.Feature.FILES;
  34 
  35 import javax.xml.catalog.CatalogFeatures;
  36 import javax.xml.catalog.CatalogResolver;
  37 import javax.xml.catalog.CatalogUriResolver;
  38 
  39 import org.testng.annotations.Listeners;
  40 import org.testng.annotations.Test;
  41 
  42 /*
  43  * @test
  44  * @bug 8077931
  45  * @summary This case tests how to specify the catalog files.
  46  * @compile ../../libs/catalog/CatalogTestUtils.java
  47  * @compile ../../libs/catalog/ResolutionChecker.java
  48  */
  49 @Listeners({jaxp.library.FilePolicy.class})
  50 public class SpecifyCatalogTest {
  51 
  52     private static final String ID_URI = "http://remote/dtd/uri/doc.dtd";
  53     private static final String ID_SYS = "http://remote/dtd/sys/doc.dtd";
  54 
  55     private static final CatalogFeatures FILES_FEATURE = createFeature(
  56             "specifyCatalog-feature.xml");
  57 
  58     /*
  59      * CatalogResolver specifies catalog via feature javax.xml.catalog.files.
  60      */
  61     @Test
  62     public void specifyCatalogOnEntityResolver() {
  63         checkSysIdResolution(catalogResolver(FILES_FEATURE, (String[]) null),
  64                 ID_SYS, "http://local/base/dtd/docFeatureSys.dtd");
  65     }
  66 
  67     /*
  68      * CatalogUriResolver specifies catalog via feature javax.xml.catalog.files.
  69      */
  70     @Test
  71     public void specifyCatalogOnUriResolver() {
  72         checkUriResolution(catalogUriResolver(FILES_FEATURE, (String[]) null),
  73                 ID_URI, "http://local/base/dtd/docFeatureURI.dtd");
  74     }
  75 
  76     /*
  77      * Resolver specifies catalog via system property javax.xml.catalog.files.
  78      */
  79     @Test
  80     public void specifyCatalogViaSysProps() {
  81         System.setProperty(FEATURE_FILES,
  82                 getCatalogPath("specifyCatalog-sysProps.xml"));
  83 
  84         checkResolutionOnEntityResolver(catalogResolver((String[]) null),
  85                 "http://local/base/dtd/docSysPropsSys.dtd");
  86         checkResolutionOnEntityResolver(
  87                 catalogResolver(FILES_FEATURE, "specifyCatalog-api.xml"),
  88                 "http://local/base/dtd/docAPISys.dtd");
  89 
  90         checkResolutionOnUriResolver(catalogUriResolver((String[]) null),
  91                 "http://local/base/dtd/docSysPropsURI.dtd");
  92         checkResolutionOnUriResolver(
  93                 catalogUriResolver(FILES_FEATURE, "specifyCatalog-api.xml"),
  94                 "http://local/base/dtd/docAPIURI.dtd");
  95     }
  96 
  97     private void checkResolutionOnEntityResolver(CatalogResolver resolver,
  98             String matchedUri) {
  99         checkSysIdResolution(resolver, ID_SYS, matchedUri);
 100     }
 101 
 102     private void checkResolutionOnUriResolver(CatalogUriResolver resolver,
 103             String matchedUri) {
 104         checkUriResolution(resolver, ID_URI, matchedUri);
 105     }
 106 
 107     private static CatalogFeatures createFeature(String catalogName) {
 108         return builder().with(FILES, getCatalogPath(catalogName)).build();
 109     }
 110 }