< prev index next >

test/javax/xml/jaxp/unittest/catalog/CatalogTest.java

Print this page




  71  * @bug 8081248 8144966 8146606 8146237 8150969 8151162 8152527 8154220 8163232
  72  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  73  * @run testng/othervm -DrunSecMngr=true catalog.CatalogTest
  74  * @run testng/othervm catalog.CatalogTest
  75  * @summary Tests basic Catalog functions.
  76  */
  77 @Listeners({jaxp.library.FilePolicy.class})
  78 public class CatalogTest extends CatalogSupportBase {
  79     static final String KEY_FILES = "javax.xml.catalog.files";
  80 
  81 
  82     /*
  83      * Initializing fields
  84      */
  85     @BeforeClass
  86     public void setUpClass() throws Exception {
  87         super.setUp();
  88     }
  89 
  90     /*
  91      * @bug 8162431
  92      * Verifies that circular references are caught and
  93      * CatalogException is thrown.
  94      */
  95     @Test(dataProvider = "getFeatures", expectedExceptions = CatalogException.class)
  96     public void testCircularRef(CatalogFeatures cf, String xml) throws Exception {
  97         CatalogResolver catalogResolver = CatalogManager.catalogResolver(
  98                 cf,
  99                 getClass().getResource(xml).toURI());
 100         catalogResolver.resolve("anyuri", "");
 101     }
 102 
 103     /*
 104        DataProvider: used to verify circular reference
 105         Data columns: CatalogFeatures, catalog
 106      */
 107     @DataProvider(name = "getFeatures")
 108     public Object[][] getFeatures() {
 109         String self = "catalogReferCircle-itself.xml";
 110         String left = "catalogReferCircle-left.xml";
 111         return new Object[][]{
 112             {CatalogFeatures.builder().with(CatalogFeatures.Feature.DEFER, "false").build(), self},
 113             {CatalogFeatures.defaults(), self},
 114             {CatalogFeatures.builder().with(CatalogFeatures.Feature.DEFER, "false").build(), left},
 115             {CatalogFeatures.defaults(), left}
 116         };
 117     }
 118 
 119     /*
 120      * @bug 8163232
 121      * Verifies that the CatalogResolver supports the following XML Resolvers:
 122           javax.xml.stream.XMLResolver
 123           javax.xml.transform.URIResolver
 124           org.w3c.dom.ls.LSResourceResolver
 125           org.xml.sax.EntityResolver
 126      *
 127      * Plus, system and uri entries can equally be used.
 128      */
 129 
 130     /*
 131      * Verifies the support for org.xml.sax.EntityResolver.
 132      * Expected: the parser returns the expected string.
 133     */
 134     @Test(dataProvider = "supportXMLResolver")
 135     public void supportEntityResolver(URI catalogFile, String xml, String expected) throws Exception {
 136         String xmlSource = getClass().getResource(xml).getFile();
 137 
 138         CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);
 139         MyCatalogHandler handler = new MyCatalogHandler(cr, elementInSystem);


 420             result = c.matchSystem(systemId);
 421         }
 422         Assert.assertEquals(expected, result);
 423     }
 424 
 425     /*
 426      * @bug 8151162
 427      * Verifies that the CatalogResolver resolves specified publicId or systemId
 428      * in accordance with the prefer setting.
 429      * prefer "system": resolves with a system entry.
 430      *                  Exception: use the public entry when the catalog contains
 431      *                  only public entry and only publicId is specified.
 432      * prefer "public": attempts to resolve with a system entry;
 433      *                  attempts to resolve with a public entry if no matching
 434      *                  system entry is found.
 435      */
 436     @Test(dataProvider = "resolveWithPrefer")
 437     public void resolveWithPrefer(String prefer, String cfile, String publicId,
 438             String systemId, String expected) throws Exception {
 439         URI catalogFile = getClass().getResource(cfile).toURI();
 440         CatalogFeatures f = CatalogFeatures.builder().with(CatalogFeatures.Feature.PREFER, prefer).with(CatalogFeatures.Feature.RESOLVE, "ignore").build();



 441         CatalogResolver catalogResolver = CatalogManager.catalogResolver(f, catalogFile);
 442         String result = catalogResolver.resolveEntity(publicId, systemId).getSystemId();
 443         Assert.assertEquals(expected, result);
 444     }
 445 
 446     /**
 447      * @bug 8150969
 448      * Verifies that the defer attribute set in the catalog file takes precedence
 449      * over other settings, in which case, whether next and delegate Catalogs will
 450      * be loaded is determined by the defer attribute.
 451      */
 452     @Test(dataProvider = "invalidAltCatalogs", expectedExceptions = CatalogException.class)
 453     public void testDeferAltCatalogs(String file) throws Exception {
 454         URI catalogFile = getClass().getResource(file).toURI();
 455         CatalogFeatures features = CatalogFeatures.builder().with(CatalogFeatures.Feature.DEFER, "true").build();


 456         /*
 457           Since the defer attribute is set to false in the specified catalog file,
 458           the parent catalog will try to load the alt catalog, which will fail
 459           since it points to an invalid catalog.
 460         */
 461         Catalog catalog = CatalogManager.catalog(features, catalogFile);
 462     }
 463 
 464 
 465     /**
 466      * @bug 8146237
 467      * PREFER from Features API taking precedence over catalog file
 468      */
 469     @Test
 470     public void testJDK8146237() throws Exception {
 471         URI catalogFile = getClass().getResource("JDK8146237_catalog.xml").toURI();
 472 
 473         try {
 474             CatalogFeatures features = CatalogFeatures.builder().with(CatalogFeatures.Feature.PREFER, "system").build();


 475             Catalog catalog = CatalogManager.catalog(features, catalogFile);
 476             CatalogResolver catalogResolver = CatalogManager.catalogResolver(catalog);
 477             String actualSystemId = catalogResolver.resolveEntity("-//FOO//DTD XML Dummy V0.0//EN", "http://www.oracle.com/alt1sys.dtd").getSystemId();
 478             Assert.assertTrue(actualSystemId.contains("dummy.dtd"), "Resulting id should contain dummy.dtd, indicating a match by publicId");




 479 
 480         } catch (Exception e) {
 481             Assert.fail(e.getMessage());
 482         }
 483     }
 484 
 485     /*
 486        @bug 8146606
 487        Verifies that the resulting systemId does not contain duplicate slashes
 488     */
 489     @Test
 490     public void testRewriteSystem() throws Exception {
 491         URI catalog = getClass().getResource("rewriteCatalog.xml").toURI();
 492 
 493         try {
 494             CatalogResolver resolver = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalog);
 495             String actualSystemId = resolver.resolveEntity(null, "http://remote.com/dtd/book.dtd").getSystemId();
 496             Assert.assertTrue(!actualSystemId.contains("//"), "result contains duplicate slashes");
 497         } catch (Exception e) {
 498             Assert.fail(e.getMessage());


 555         try {
 556             CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalog);
 557             XMLReader reader = saxParser.getXMLReader();
 558             reader.setEntityResolver(cr);
 559             MyHandler handler = new MyHandler(saxParser);
 560             reader.setContentHandler(handler);
 561             reader.parse(url);
 562             System.out.println(test + ": expected [" + expected + "] <> actual [" + handler.getResult() + "]");
 563             Assert.assertEquals(handler.getResult(), expected);
 564         } catch (SAXException | IOException e) {
 565             Assert.fail(e.getMessage());
 566         }
 567     }
 568 
 569     /*
 570        Verifies that when there's no match, in this case only an invalid
 571     catalog is provided, the resolver will throw an exception by default.
 572     */
 573     @Test
 574     public void testInvalidCatalog() throws Exception {

 575         URI catalog = getClass().getResource("catalog_invalid.xml").toURI();
 576 
 577         String test = "testInvalidCatalog";
 578         try {
 579             CatalogResolver resolver = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalog);
 580             String actualSystemId = resolver.resolveEntity(null, "http://remote/xml/dtd/sys/alice/docAlice.dtd").getSystemId();




 581         } catch (Exception e) {
 582             String msg = e.getMessage();
 583             if (msg != null) {
 584                 if (msg.contains("No match found for publicId")) {
 585                     Assert.assertEquals(msg, "No match found for publicId 'null' and systemId 'http://remote/xml/dtd/sys/alice/docAlice.dtd'.");
 586                     System.out.println(test + ": expected [No match found for publicId 'null' and systemId 'http://remote/xml/dtd/sys/alice/docAlice.dtd'.]");
 587                     System.out.println("actual [" + msg + "]");
 588                 }
 589             }
 590         }
 591     }
 592 
 593     /*
 594        Verifies that if resolve is "ignore", an empty InputSource will be returned
 595     when there's no match. The systemId is then null.
 596     */
 597     @Test
 598     public void testIgnoreInvalidCatalog() {
 599         String catalog = getClass().getResource("catalog_invalid.xml").toExternalForm();
 600         CatalogFeatures f = CatalogFeatures.builder()
 601                 .with(Feature.FILES, catalog)
 602                 .with(Feature.PREFER, "public")
 603                 .with(Feature.DEFER, "true")
 604                 .with(Feature.RESOLVE, "ignore")
 605                 .build();
 606 
 607         String test = "testInvalidCatalog";
 608         try {
 609             CatalogResolver resolver = CatalogManager.catalogResolver(f);
 610             String actualSystemId = resolver.resolveEntity(null, "http://remote/xml/dtd/sys/alice/docAlice.dtd").getSystemId();



 611             System.out.println("testIgnoreInvalidCatalog: expected [null]");
 612             System.out.println("testIgnoreInvalidCatalog: expected [null]");
 613             System.out.println("actual [" + actualSystemId + "]");
 614             Assert.assertEquals(actualSystemId, null);
 615         } catch (Exception e) {
 616             Assert.fail(e.getMessage());
 617         }
 618     }
 619 
 620 
 621     /*
 622         DataProvider: used to verify CatalogResolver's resolve function.
 623         Data columns:
 624         catalog, uri or publicId, expectedFile, expectedUri, msg
 625 
 626         This DataProvider is copied from JCK ResolveTests' dataMatch1
 627      */
 628     @DataProvider(name = "resolveUri")
 629     public Object[][] getDataForUriResolver() {
 630         return new Object[][]{
 631             {"uri.xml", "urn:publicid:-:Acme,+Inc.:DTD+Book+Version+1.0", null, "http://local/base/dtd/book.dtd", "Uri in publicId namespace is incorrectly unwrapped"},




 632         };
 633     }
 634 
 635     /*
 636         DataProvider: used to verify hierarchical catalogs. Refer to JCK test
 637     hierarchyOfCatFiles2.
 638      */
 639     @DataProvider(name = "hierarchyOfCatFilesData")
 640     public Object[][] getHierarchyOfCatFilesData() {
 641         return new Object[][]{
 642             {"http://www.oracle.com/sequence.dtd", "first.dtd"},
 643             {"http://www.oracle.com/sequence_next.dtd", "next.dtd"},
 644             {"http://www.oracle.com/sequence_second.dtd", "second.dtd"}
 645         };
 646     }
 647 
 648     /*
 649         DataProvider: used to verify CatalogResolver's resolveEntity function.
 650         Data columns:
 651         catalog, prefer, systemId, publicId, expectedUri, expectedFile, msg
 652      */
 653     @DataProvider(name = "resolveEntity")
 654     public Object[][] getDataForMatchingBothIds() {
 655         String expected = "http://www.groupxmlbase.com/dtds/rewrite.dtd";
 656         return new Object[][]{
 657             {"rewriteSystem_id.xml", "system", "http://www.sys00test.com/rewrite.dtd", "PUB-404", expected, expected, "Relative rewriteSystem with xml:base at group level failed"},






 658         };
 659     }
 660 
 661     static String id = "http://openjdk.java.net/xml/catalog/dtd/system.dtd";
 662     /*
 663        DataProvider: used to verify how prefer settings affect the result of the
 664         Catalog's matching operation.
 665         Data columns:
 666         prefer, catalog, publicId, systemId, expected result
 667      */
 668     @DataProvider(name = "matchWithPrefer")
 669     public Object[][] getDataForMatch() {
 670         return new Object[][]{
 671             {"public", "pubOnly.xml", id, "", "http://local/base/dtd/public.dtd"},
 672             {"public", "sysOnly.xml", id, "", null},
 673             {"public", "sysAndPub.xml", id, "", "http://local/base/dtd/public.dtd"},
 674             {"system", "pubOnly.xml", id, "", "http://local/base/dtd/public.dtd"},
 675             {"system", "sysOnly.xml", id, "", null},
 676             {"system", "sysAndPub.xml", id, "", "http://local/base/dtd/public.dtd"},
 677             {"public", "pubOnly.xml", "", id, null},




  71  * @bug 8081248 8144966 8146606 8146237 8150969 8151162 8152527 8154220 8163232
  72  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  73  * @run testng/othervm -DrunSecMngr=true catalog.CatalogTest
  74  * @run testng/othervm catalog.CatalogTest
  75  * @summary Tests basic Catalog functions.
  76  */
  77 @Listeners({jaxp.library.FilePolicy.class})
  78 public class CatalogTest extends CatalogSupportBase {
  79     static final String KEY_FILES = "javax.xml.catalog.files";
  80 
  81 
  82     /*
  83      * Initializing fields
  84      */
  85     @BeforeClass
  86     public void setUpClass() throws Exception {
  87         super.setUp();
  88     }
  89 
  90     /*





























  91      * @bug 8163232
  92      * Verifies that the CatalogResolver supports the following XML Resolvers:
  93           javax.xml.stream.XMLResolver
  94           javax.xml.transform.URIResolver
  95           org.w3c.dom.ls.LSResourceResolver
  96           org.xml.sax.EntityResolver
  97      *
  98      * Plus, system and uri entries can equally be used.
  99      */
 100 
 101     /*
 102      * Verifies the support for org.xml.sax.EntityResolver.
 103      * Expected: the parser returns the expected string.
 104     */
 105     @Test(dataProvider = "supportXMLResolver")
 106     public void supportEntityResolver(URI catalogFile, String xml, String expected) throws Exception {
 107         String xmlSource = getClass().getResource(xml).getFile();
 108 
 109         CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);
 110         MyCatalogHandler handler = new MyCatalogHandler(cr, elementInSystem);


 391             result = c.matchSystem(systemId);
 392         }
 393         Assert.assertEquals(expected, result);
 394     }
 395 
 396     /*
 397      * @bug 8151162
 398      * Verifies that the CatalogResolver resolves specified publicId or systemId
 399      * in accordance with the prefer setting.
 400      * prefer "system": resolves with a system entry.
 401      *                  Exception: use the public entry when the catalog contains
 402      *                  only public entry and only publicId is specified.
 403      * prefer "public": attempts to resolve with a system entry;
 404      *                  attempts to resolve with a public entry if no matching
 405      *                  system entry is found.
 406      */
 407     @Test(dataProvider = "resolveWithPrefer")
 408     public void resolveWithPrefer(String prefer, String cfile, String publicId,
 409             String systemId, String expected) throws Exception {
 410         URI catalogFile = getClass().getResource(cfile).toURI();
 411         CatalogFeatures f = CatalogFeatures.builder()
 412                 .with(CatalogFeatures.Feature.PREFER, prefer)
 413                 .with(CatalogFeatures.Feature.RESOLVE, "ignore")
 414                 .build();
 415         CatalogResolver catalogResolver = CatalogManager.catalogResolver(f, catalogFile);
 416         String result = catalogResolver.resolveEntity(publicId, systemId).getSystemId();
 417         Assert.assertEquals(expected, result);
 418     }
 419 
 420     /**
 421      * @bug 8150969
 422      * Verifies that the defer attribute set in the catalog file takes precedence
 423      * over other settings, in which case, whether next and delegate Catalogs will
 424      * be loaded is determined by the defer attribute.
 425      */
 426     @Test(dataProvider = "invalidAltCatalogs", expectedExceptions = CatalogException.class)
 427     public void testDeferAltCatalogs(String file) throws Exception {
 428         URI catalogFile = getClass().getResource(file).toURI();
 429         CatalogFeatures features = CatalogFeatures.builder().
 430                 with(CatalogFeatures.Feature.DEFER, "true")
 431                 .build();
 432         /*
 433           Since the defer attribute is set to false in the specified catalog file,
 434           the parent catalog will try to load the alt catalog, which will fail
 435           since it points to an invalid catalog.
 436         */
 437         Catalog catalog = CatalogManager.catalog(features, catalogFile);
 438     }
 439 
 440 
 441     /**
 442      * @bug 8146237
 443      * PREFER from Features API taking precedence over catalog file
 444      */
 445     @Test
 446     public void testJDK8146237() throws Exception {
 447         URI catalogFile = getClass().getResource("JDK8146237_catalog.xml").toURI();
 448 
 449         try {
 450             CatalogFeatures features = CatalogFeatures.builder()
 451                     .with(CatalogFeatures.Feature.PREFER, "system")
 452                     .build();
 453             Catalog catalog = CatalogManager.catalog(features, catalogFile);
 454             CatalogResolver catalogResolver = CatalogManager.catalogResolver(catalog);
 455             String actualSystemId = catalogResolver.resolveEntity(
 456                     "-//FOO//DTD XML Dummy V0.0//EN",
 457                     "http://www.oracle.com/alt1sys.dtd")
 458                     .getSystemId();
 459             Assert.assertTrue(actualSystemId.contains("dummy.dtd"),
 460                     "Resulting id should contain dummy.dtd, indicating a match by publicId");
 461 
 462         } catch (Exception e) {
 463             Assert.fail(e.getMessage());
 464         }
 465     }
 466 
 467     /*
 468        @bug 8146606
 469        Verifies that the resulting systemId does not contain duplicate slashes
 470     */
 471     @Test
 472     public void testRewriteSystem() throws Exception {
 473         URI catalog = getClass().getResource("rewriteCatalog.xml").toURI();
 474 
 475         try {
 476             CatalogResolver resolver = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalog);
 477             String actualSystemId = resolver.resolveEntity(null, "http://remote.com/dtd/book.dtd").getSystemId();
 478             Assert.assertTrue(!actualSystemId.contains("//"), "result contains duplicate slashes");
 479         } catch (Exception e) {
 480             Assert.fail(e.getMessage());


 537         try {
 538             CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalog);
 539             XMLReader reader = saxParser.getXMLReader();
 540             reader.setEntityResolver(cr);
 541             MyHandler handler = new MyHandler(saxParser);
 542             reader.setContentHandler(handler);
 543             reader.parse(url);
 544             System.out.println(test + ": expected [" + expected + "] <> actual [" + handler.getResult() + "]");
 545             Assert.assertEquals(handler.getResult(), expected);
 546         } catch (SAXException | IOException e) {
 547             Assert.fail(e.getMessage());
 548         }
 549     }
 550 
 551     /*
 552        Verifies that when there's no match, in this case only an invalid
 553     catalog is provided, the resolver will throw an exception by default.
 554     */
 555     @Test
 556     public void testInvalidCatalog() throws Exception {
 557         String expectedMsgId = "JAXP09040001";
 558         URI catalog = getClass().getResource("catalog_invalid.xml").toURI();
 559 

 560         try {
 561             CatalogResolver resolver = CatalogManager.catalogResolver(
 562                     CatalogFeatures.defaults(), catalog);
 563             String actualSystemId = resolver.resolveEntity(
 564                     null,
 565                     "http://remote/xml/dtd/sys/alice/docAlice.dtd")
 566                     .getSystemId();
 567         } catch (Exception e) {
 568             String msg = e.getMessage();
 569             if (msg != null) {
 570                 Assert.assertTrue(msg.contains(expectedMsgId),
 571                         "Message shall contain the corrent message ID " + expectedMsgId);



 572             }
 573         }
 574     }
 575 
 576     /*
 577        Verifies that if resolve is "ignore", an empty InputSource will be returned
 578     when there's no match. The systemId is then null.
 579     */
 580     @Test
 581     public void testIgnoreInvalidCatalog() {
 582         String catalog = getClass().getResource("catalog_invalid.xml").toExternalForm();
 583         CatalogFeatures f = CatalogFeatures.builder()
 584                 .with(Feature.FILES, catalog)
 585                 .with(Feature.PREFER, "public")
 586                 .with(Feature.DEFER, "true")
 587                 .with(Feature.RESOLVE, "ignore")
 588                 .build();
 589 
 590         String test = "testInvalidCatalog";
 591         try {
 592             CatalogResolver resolver = CatalogManager.catalogResolver(f);
 593             String actualSystemId = resolver.resolveEntity(
 594                     null,
 595                     "http://remote/xml/dtd/sys/alice/docAlice.dtd")
 596                     .getSystemId();
 597             System.out.println("testIgnoreInvalidCatalog: expected [null]");
 598             System.out.println("testIgnoreInvalidCatalog: expected [null]");
 599             System.out.println("actual [" + actualSystemId + "]");
 600             Assert.assertEquals(actualSystemId, null);
 601         } catch (Exception e) {
 602             Assert.fail(e.getMessage());
 603         }
 604     }
 605 
 606 
 607     /*
 608         DataProvider: used to verify CatalogResolver's resolve function.
 609         Data columns:
 610         catalog, uri or publicId, expectedFile, expectedUri, msg
 611 
 612         This DataProvider is copied from JCK ResolveTests' dataMatch1
 613      */
 614     @DataProvider(name = "resolveUri")
 615     public Object[][] getDataForUriResolver() {
 616         return new Object[][]{
 617             {"uri.xml",
 618                 "urn:publicid:-:Acme,+Inc.:DTD+Book+Version+1.0",
 619                 null,
 620                 "http://local/base/dtd/book.dtd",
 621                 "Uri in publicId namespace is incorrectly unwrapped"},
 622         };
 623     }
 624 
 625     /*
 626         DataProvider: used to verify hierarchical catalogs. Refer to JCK test
 627     hierarchyOfCatFiles2.
 628      */
 629     @DataProvider(name = "hierarchyOfCatFilesData")
 630     public Object[][] getHierarchyOfCatFilesData() {
 631         return new Object[][]{
 632             {"http://www.oracle.com/sequence.dtd", "first.dtd"},
 633             {"http://www.oracle.com/sequence_next.dtd", "next.dtd"},
 634             {"http://www.oracle.com/sequence_second.dtd", "second.dtd"}
 635         };
 636     }
 637 
 638     /*
 639         DataProvider: used to verify CatalogResolver's resolveEntity function.
 640         Data columns:
 641         catalog, prefer, systemId, publicId, expectedUri, expectedFile, msg
 642      */
 643     @DataProvider(name = "resolveEntity")
 644     public Object[][] getDataForMatchingBothIds() {
 645         String expected = "http://www.groupxmlbase.com/dtds/rewrite.dtd";
 646         return new Object[][]{
 647             {"rewriteSystem_id.xml",
 648                 "system",
 649                 "http://www.sys00test.com/rewrite.dtd",
 650                 "PUB-404",
 651                 expected,
 652                 expected,
 653                 "Relative rewriteSystem with xml:base at group level failed"},
 654         };
 655     }
 656 
 657     static String id = "http://openjdk.java.net/xml/catalog/dtd/system.dtd";
 658     /*
 659        DataProvider: used to verify how prefer settings affect the result of the
 660         Catalog's matching operation.
 661         Data columns:
 662         prefer, catalog, publicId, systemId, expected result
 663      */
 664     @DataProvider(name = "matchWithPrefer")
 665     public Object[][] getDataForMatch() {
 666         return new Object[][]{
 667             {"public", "pubOnly.xml", id, "", "http://local/base/dtd/public.dtd"},
 668             {"public", "sysOnly.xml", id, "", null},
 669             {"public", "sysAndPub.xml", id, "", "http://local/base/dtd/public.dtd"},
 670             {"system", "pubOnly.xml", id, "", "http://local/base/dtd/public.dtd"},
 671             {"system", "sysOnly.xml", id, "", null},
 672             {"system", "sysAndPub.xml", id, "", "http://local/base/dtd/public.dtd"},
 673             {"public", "pubOnly.xml", "", id, null},


< prev index next >