< prev index next >

test/java/nio/file/Path/PathOps.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -20,27 +20,28 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /* @test
- * @bug 4313887 6838333 6925932 7006126 8037945 8072495
+ * @bug 4313887 6838333 6925932 7006126 8037945 8072495 8140449
  * @summary Unit test for java.nio.file.Path path operations
  */
 
-import java.nio.file.*;
+import java.nio.file.FileSystems;
+import java.nio.file.InvalidPathException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 public class PathOps {
 
     static final java.io.PrintStream out = System.out;
 
-    private String input;
     private Path path;
     private Exception exc;
 
     private PathOps(String first, String... more) {
         out.println();
-        input = first;
         try {
             path = FileSystems.getDefault().getPath(first, more);
             out.format("%s -> %s", first, path);
         } catch (Exception x) {
             exc = x;

@@ -85,10 +86,18 @@
         checkPath();
         check(path.getRoot(), expected);
         return this;
     }
 
+    PathOps hasRoot() {
+        out.println("check has root");
+        checkPath();
+        if (path.getRoot() == null)
+            fail();
+        return this;
+    }
+
     PathOps parent(String expected) {
         out.println("check parent");
         checkPath();
         check(path.getParent(), expected);
         return this;

@@ -145,10 +154,15 @@
         Path s = FileSystems.getDefault().getPath(suffix);
         check(path.endsWith(s), false);
         return this;
     }
 
+    PathOps makeAbsolute() {
+        this.path = path.toAbsolutePath();
+        return this;
+    }
+
     PathOps absolute() {
         out.println("check path is absolute");
         checkPath();
         check(path.isAbsolute(), true);
         return this;

@@ -207,20 +221,26 @@
 
     static PathOps test(String first, String... more) {
         return new PathOps(first, more);
     }
 
+    static PathOps test(Path path) {
+        return new PathOps(path.toString());
+    }
+
+
     // -- PathOpss --
 
     static void header(String s) {
         out.println();
         out.println();
         out.println("-- " + s + " --");
     }
 
     static void doWindowsTests() {
         header("Windows specific tests");
+        Path cwd = Paths.get("").toAbsolutePath();
 
         // construction
         test("C:\\")
             .string("C:\\");
         test("C:\\", "")

@@ -415,10 +435,56 @@
         test("C:").notAbsolute();
         test("C:\\").absolute();
         test("C:\\abc").absolute();
         test("\\\\server\\share\\").absolute();
         test("").notAbsolute();
+        test(cwd).absolute();
+
+        // toAbsolutePath
+        test("")
+            .makeAbsolute()
+            .absolute()
+            .hasRoot()
+            .string(cwd.toString());
+        test(".")
+            .makeAbsolute()
+            .absolute()
+            .hasRoot()
+            .string(cwd.toString() + "\\.");
+        test("foo")
+            .makeAbsolute()
+            .absolute()
+            .hasRoot()
+            .string(cwd.toString() + "\\foo");
+
+        String rootAsString = cwd.getRoot().toString();
+        if (rootAsString.length() == 3
+                && rootAsString.charAt(1) == ':'
+                && rootAsString.charAt(2) == '\\') {
+            Path root = Paths.get(rootAsString.substring(0, 2));
+
+            // C:
+            test(root)
+                .makeAbsolute()
+                .absolute()
+                .hasRoot()
+                .string(cwd.toString());
+
+            // C:.
+            test(root + ".")
+                .makeAbsolute()
+                .absolute()
+                .hasRoot()
+                .string(cwd.toString() + "\\.");
+
+            // C:foo
+            test(root + "foo")
+                .makeAbsolute()
+                .absolute()
+                .hasRoot()
+                .string(cwd.toString() + "\\foo");
+        }
 
         // resolve
         test("C:\\")
             .resolve("foo", "C:\\foo")
             .resolve("D:\\bar", "D:\\bar")

@@ -504,23 +570,33 @@
             .resolveSibling("", "")
             .resolveSibling("foo", "foo")
             .resolveSibling("C:\\", "C:\\");
 
         // relativize
+        test("foo")
+            .relativize("foo", "")
+            .relativize("bar", "..\\bar")
+            .relativize("..", "..\\..")
+            .relativize("", "..");
         test("foo\\bar")
             .relativize("foo\\bar", "")
-            .relativize("foo", "..");
+            .relativize("foo", "..")
+            .relativize("gus", "..\\..\\gus")
+            .relativize("..", "..\\..\\..")
+            .relativize("", "..\\..");
         test("C:\\a\\b\\c")
             .relativize("C:\\a", "..\\..")
-            .relativize("C:\\a\\b\\c", "");
+            .relativize("C:\\a\\b\\c", "")
+            .relativize("C:\\x", "..\\..\\..\\x");
         test("\\\\server\\share\\foo")
             .relativize("\\\\server\\share\\bar", "..\\bar")
             .relativize("\\\\server\\share\\foo", "");
         test("")
-            .relativize("", "")
             .relativize("a", "a")
-            .relativize("a\\b\\c", "a\\b\\c");
+            .relativize("a\\b\\c", "a\\b\\c")
+            .relativize("..", "..")
+            .relativize("", "");
 
         // normalize
         test("C:\\")
             .normalize("C:\\");
         test("C:\\.")

@@ -670,10 +746,11 @@
             throw new RuntimeException("PathOps failed");
     }
 
     static void doUnixTests() {
         header("Unix specific tests");
+        Path cwd = Paths.get("").toAbsolutePath();
 
         // construction
         test("/")
             .string("/");
         test("/", "")

@@ -838,11 +915,26 @@
             .absolute();
         test("tmp")
             .notAbsolute();
         test("")
             .notAbsolute();
+        test(cwd)
+            .absolute();
 
+        // toAbsolutePath
+        test("/")
+            .makeAbsolute()
+            .absolute();
+        test("/tmp")
+            .makeAbsolute()
+            .absolute();
+        test("tmp")
+            .makeAbsolute()
+            .absolute();
+        test("")
+            .makeAbsolute()
+            .absolute();
 
         // resolve
         test("/tmp")
             .resolve("foo", "/tmp/foo")
             .resolve("/foo", "/foo")
< prev index next >