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