1 /* 2 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4313887 7003155 26 * @summary Unit test for java.nio.file.Path 27 */ 28 29 import java.nio.file.*; 30 import java.net.URI; 31 import java.net.URISyntaxException; 32 import java.io.PrintStream; 33 34 public class UriImportExport { 35 36 static final PrintStream log = System.out; 37 static int failures = 0; 38 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 } 53 log.println(); 54 } 55 56 /** 57 * Test Path -> (expected) URI -> Path 58 */ 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); 66 failures++; 67 return; 68 } 69 Path result = Paths.get(uri); 70 log.println(" --> " + result); 71 if (!result.equals(path.toAbsolutePath())) { 72 log.println("FAIL: Expected " + path + ", got " + result); 73 failures++; 74 } 75 log.println(); 76 } 77 78 /** 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(); 109 } 110 111 public static void main(String[] args) throws Exception { 112 testBadUri("file:foo"); 113 testBadUri("file:/foo?q"); 114 testBadUri("file:/foo#f"); 115 116 String osname = System.getProperty("os.name"); 117 if (osname.startsWith("Windows")) { 118 testPath("C:\\doesnotexist"); 119 testPath("C:doesnotexist"); 120 testPath("\\\\server.nowhere.oracle.com\\share\\"); 121 testPath("\\\\fe80--203-baff-fe5a-749ds1.ipv6-literal.net\\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"); 138 } 139 140 if (failures > 0) 141 throw new RuntimeException(failures + " test(s) failed"); 142 } 143 }