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

new File(parent, "/") breaks normalization – creates File with slash at the end

XMLWordPrintable

    • b25
    • generic
    • linux
    • Verified

      A DESCRIPTION OF THE PROBLEM :

      new File(parent, "/") breaks normalization – creates File with slash at the end
      and subsequently it may lead to File with doubled slash in path

      new File(parent, "/") will produce the File with slash as an trailing char of the path
      If in turn we will try to create yet another child from that File e.g. new File(new File(parent, "/"), "some-file") - it will contain double slash in path. Please see unit-test at Source code section.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See Source code section.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      File path should never contain double slashes, because it always stores normalized path.
      ACTUAL -
      Double slash in path.

      ---------- BEGIN SOURCE ----------
      public class FileTest {

          @Test
          public void doubleSlash() {
              File parent = new File("//some-dir//");
              Assert.assertEquals("/some-dir", parent.getAbsolutePath()); // passed
              Assert.assertEquals("/some-dir/some-file", new File(parent, "//some-file//").getAbsolutePath()); // passed
              Assert.assertEquals("/some-dir/some-file", new File(parent, "some-file").getAbsolutePath()); // passed

              // This assert fails, actual value contains doubled slash: /some-dir//some-file"
              Assert.assertEquals("/some-dir/some-file", new File(new File(parent, "/"), "some-file").getAbsolutePath());
          }

      }

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

      FREQUENCY : always


            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: