Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8253569

javax.xml.catalog.Catalog.matchURI() implementation should reset state variables

XMLWordPrintable

    • b17
    • generic
    • generic
    • Verified

      A DESCRIPTION OF THE PROBLEM :
      See the reproducible bug report here: https://github.com/NicolasRouquette/xml-catalog-reset-bug

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See the instructions here: https://github.com/NicolasRouquette/xml-catalog-reset-bug



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      If the implementation was correct, the output should be (the difference is in the output of `Test1`):

      ```
      Catalog: /catalog.xml
      # Test1
      http://entailments/example.org/A/B/derived.ttl -> /derived/A/B/derived.ttl As expected? true
      http://example.org/A/B.owl -> /sources/A/B.owl As expected? true
      # Test2
      http://example.org/A/B.owl -> /sources/A/B.owl As expected? true
      http://entailments/example.org/A/B/derived.ttl -> /derived/A/B/derived.ttl As expected? true
      # Test3
      http://example.org/A/B.owl -> /sources/A/B.owl As expected? true
      http://entailments/example.org/A/B/derived.ttl -> /derived/A/B/derived.ttl As expected? true
      ```
      ACTUAL -
      ```
      Catalog: /catalog.xml
      # Test1
      http://entailments/example.org/A/B/derived.ttl -> /derived/A/B/derived.ttl As expected? true
      http://example.org/A/B.owl -> /derived/A/B/derived.ttl As expected? false
      # Test2
      http://example.org/A/B.owl -> /sources/A/B.owl As expected? true
      http://entailments/example.org/A/B/derived.ttl -> /derived/A/B/derived.ttl As expected? true
      # Test3
      http://example.org/A/B.owl -> /sources/A/B.owl As expected? true
      http://entailments/example.org/A/B/derived.ttl -> /derived/A/B/derived.ttl As expected? true
      ```

      ---------- BEGIN SOURCE ----------
      // See: https://github.com/NicolasRouquette/xml-catalog-reset-bug
      // This requires resources (Catalog & files)

      package org.example;

      import javax.xml.catalog.Catalog;
      import javax.xml.catalog.CatalogFeatures;
      import javax.xml.catalog.CatalogManager;
      import java.net.URI;
      import java.net.URISyntaxException;
      import java.net.URL;

      public class Main {

          public static void main(String[] args) {

              URL catalogURL = Main.class.getResource("/catalog.xml");
              if (null == catalogURL)
                  throw new IllegalArgumentException("Missing 'catalog.xml' in the classpath.");
              URI catalogURI = null;
              try {
                  catalogURI = catalogURL.toURI();
              } catch (URISyntaxException e) {
                  System.err.println(e.getMessage());
                  e.printStackTrace(System.err);
              }

              System.out.println("Catalog: " + relativize(catalogURI.toString()));

              test1(catalogURI);
              test2(catalogURI);
              test3(catalogURI);
          }

          static void test1(URI catalogURI) {
              System.out.println("# Test1");
              Catalog c = makeStrictCatalog(catalogURI);
              resolveDerived(c);
              resolveSource(c);
          }

          static void test2(URI catalogURI) {
              System.out.println("# Test2");
              Catalog c = makeStrictCatalog(catalogURI);
              resolveSource(c);
              resolveDerived(c);
          }

          static void test3(URI catalogURI) {
              System.out.println("# Test3");
              Catalog c1 = makeStrictCatalog(catalogURI);
              resolveSource(c1);

              Catalog c2 = makeStrictCatalog(catalogURI);
              resolveDerived(c2);
          }

          static Catalog makeStrictCatalog(URI catalogURI) {
              return CatalogManager.catalog(CatalogFeatures.builder().with(CatalogFeatures.Feature.RESOLVE, "strict").build(), catalogURI);
          }

          static void showResult(String uri, String match, String expected) {
              boolean ok = match.endsWith(expected);
              System.out.println(uri + " -> " + relativize(match) + " As expected? " + ok);
          }

          static void resolveDerived(Catalog c) {
              String uri = "http://entailments/example.org/A/B/derived.ttl";
              String m = c.matchURI(uri);
              showResult(uri, m, "derived/A/B/derived.ttl");
          }

          static void resolveSource(Catalog c) {
              String uri = "http://example.org/A/B.owl";
              String m = c.matchURI(uri);
              showResult(uri, m, "sources/A/B.owl");
          }

          static URL here = Main.class.getResource("/");

          static String relativize(String path) {
              if (null == here) {
                  // This case happens when the resources are compiled in the jar.
                  int bang = path.indexOf("!");
                  if (bang > 0)
                      return path.substring(bang + 1);
                  else
                      return path;
              } else
                  return path.replace(here.toString(), "/");
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Create a fresh Catalog for each call to a Catalog match operation.

      FREQUENCY : always


            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: