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

URL equals is not transitive for files

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      URL.equals for "file" URLs has special handling for URLs with "localhost" as the host name (to match RFC 1738). This means that for three URLs

          var u1 = new URL("file", null, "path");
          var u2 = new URL("file", "", "path");
          var u3 = new URL("file", "localhost", "path");

          u1.equals(u3); // returns true
          u2.equals(u3); // returns true
          u1.equals(u2); // returns false

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create three URLs, all with the same file and protocol ("file") but different hosts, one null, one an empty string and one "localhost". Both the null and emptyString URLs are equal to the localhost URL but they are not equal to each other as required by the equals contract.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      They should all be equal or all be not equal
      ACTUAL -
      Both the null and emptyString URLs are equal to the localhost URL but they are not equal to each other as required by the equals contract

      ---------- BEGIN SOURCE ----------
      import java.net.URL;

      public class UrlDemo {
      public static void main(String[] argv) throws Exception {
      var u1 = new URL("file", null, "path");
      var u2 = new URL("file", "", "path");
      var u3 = new URL("file", "localhost", "path");

      System.out.println("u1 equals u3: " + (u1.equals(u3)));
      System.out.println("u2 equals u3: " + (u2.equals(u3)));
      System.out.println("u1 equals u2: " + (u1.equals(u2)));
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Always pick the same host. URI::toURL uses an empty string so is probably a sensible default

      FREQUENCY : always


            dfuchs Daniel Fuchs
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: