jdk/test/java/nio/file/Path/UriImportExport.java
changeset 8184 526b9179a2bb
parent 5506 202f599c92aa
child 9035 1255eb81cc2f
equal deleted inserted replaced
8183:06852303ca4e 8184:526b9179a2bb
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4313887
    25  * @bug 4313887 7003155
    26  * @summary Unit test for java.nio.file.Path
    26  * @summary Unit test for java.nio.file.Path
    27  */
    27  */
    28 
    28 
    29 import java.nio.file.*;
    29 import java.nio.file.*;
    30 import java.net.URI;
    30 import java.net.URI;
    34 public class UriImportExport {
    34 public class UriImportExport {
    35 
    35 
    36     static final PrintStream log = System.out;
    36     static final PrintStream log = System.out;
    37     static int failures = 0;
    37     static int failures = 0;
    38 
    38 
    39     static void test(String fn, String expected) {
    39     /**
       
    40      * Test Path -> URI -> Path
       
    41      */
       
    42     static void testPath(String s) {
       
    43         Path path = Paths.get(s);
       
    44         log.println(path);
       
    45         URI uri = path.toUri();
       
    46         log.println("  --> " + uri);
       
    47         Path result = Paths.get(uri);
       
    48         log.println("  --> " + result);
       
    49         if (!result.equals(path.toAbsolutePath())) {
       
    50             log.println("FAIL: Expected " + path + ", got " + result);
       
    51             failures++;
       
    52         }
    40         log.println();
    53         log.println();
    41         Path p = Paths.get(fn);
    54     }
    42         log.println(p);
    55 
    43         URI u = p.toUri();
    56     /**
    44         log.println("  --> " + u);
    57      * Test Path -> (expected) URI -> Path
    45         if (expected != null && !(u.toString().equals(expected))) {
    58      */
    46             log.println("FAIL: Expected " + expected);
    59     static void testPath(String s, String expectedUri) {
       
    60         Path path = Paths.get(s);
       
    61         log.println(path);
       
    62         URI uri = path.toUri();
       
    63         log.println("  --> " + uri);
       
    64         if (!uri.toString().equals(expectedUri)) {
       
    65             log.println("FAILED: Expected " + expectedUri + ", got " + uri);
    47             failures++;
    66             failures++;
    48             return;
    67             return;
    49         }
    68         }
    50         Path q = Paths.get(u);
    69         Path result = Paths.get(uri);
    51         log.println("  --> " + q);
    70         log.println("  --> " + result);
    52         if (!p.toAbsolutePath().equals(q)) {
    71         if (!result.equals(path.toAbsolutePath())) {
    53             log.println("FAIL: Expected " + p + ", got " + q);
    72             log.println("FAIL: Expected " + path + ", got " + result);
    54             failures++;
    73             failures++;
    55             return;
       
    56         }
    74         }
       
    75         log.println();
    57     }
    76     }
    58 
    77 
    59     static void test(String fn) {
    78     /**
    60         test(fn, null);
    79      * Test URI -> Path -> URI
       
    80      */
       
    81     static void testUri(String s) throws Exception {
       
    82         URI uri = URI.create(s);
       
    83         log.println(uri);
       
    84         Path path = Paths.get(uri);
       
    85         log.println("  --> " + path);
       
    86         URI result = path.toUri();
       
    87         log.println("  --> " + result);
       
    88         if (!result.equals(uri)) {
       
    89             log.println("FAIL: Expected " + uri + ", got " + result);
       
    90             failures++;
       
    91         }
       
    92         log.println();
       
    93     }
       
    94 
       
    95     /**
       
    96      * Test URI -> Path fails with IllegalArgumentException
       
    97      */
       
    98     static void testBadUri(String s) throws Exception {
       
    99         URI uri = URI.create(s);
       
   100         log.println(uri);
       
   101         try {
       
   102             Path path = Paths.get(uri);
       
   103             log.format(" --> %s  FAIL: Expected IllegalArgumentException\n", path);
       
   104             failures++;
       
   105         } catch (IllegalArgumentException expected) {
       
   106             log.println("  --> IllegalArgumentException (expected)");
       
   107         }
       
   108         log.println();
    61     }
   109     }
    62 
   110 
    63     public static void main(String[] args) throws Exception {
   111     public static void main(String[] args) throws Exception {
    64         test("foo");
   112         testBadUri("file:foo");
    65         test("/foo");
   113         testBadUri("file:/foo?q");
    66         test("/foo bar");
   114         testBadUri("file:/foo#f");
    67 
   115 
    68         String osname = System.getProperty("os.name");
   116         String osname = System.getProperty("os.name");
    69         if (osname.startsWith("Windows")) {
   117         if (osname.startsWith("Windows")) {
    70             test("C:\\foo");
   118             testPath("C:\\doesnotexist");
    71             test("C:foo");
   119             testPath("C:doesnotexist");
    72             test("\\\\rialto.dublin.com\\share\\");
   120             testPath("\\\\server.nowhere.oracle.com\\share\\");
    73             test("\\\\fe80--203-baff-fe5a-749ds1.ipv6-literal.net\\share\\missing",
   121             testPath("\\\\fe80--203-baff-fe5a-749ds1.ipv6-literal.net\\share\\missing",
    74                 "file://[fe80::203:baff:fe5a:749d%1]/share/missing");
   122                 "file://[fe80::203:baff:fe5a:749d%1]/share/missing");
       
   123         } else {
       
   124             testPath("doesnotexist");
       
   125             testPath("/doesnotexist");
       
   126             testPath("/does not exist");
       
   127             testUri("file:///");
       
   128             testUri("file:///foo/bar/doesnotexist");
       
   129             testUri("file:/foo/bar/doesnotexist");
       
   130 
       
   131             // file:///foo/bar/\u0440\u0443\u0441\u0441\u043A\u0438\u0439 (Russian)
       
   132             testUri("file:///foo/bar/%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9");
       
   133 
       
   134             // invalid
       
   135             testBadUri("file:foo");
       
   136             testBadUri("file://server/foo");
       
   137             testBadUri("file:///foo%00");
    75         }
   138         }
    76 
   139 
    77         if (failures > 0)
   140         if (failures > 0)
    78             throw new RuntimeException(failures + " test(s) failed");
   141             throw new RuntimeException(failures + " test(s) failed");
    79     }
   142     }