1 /* 2 * Copyright (c) 2008, 2011, 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 package test.java.nio.file.Path; 29 30 import java.nio.file.*; 31 import java.net.URI; 32 import java.net.URISyntaxException; 33 import org.testng.annotations.Test; 34 import org.testng.Assert; 35 36 public class UriImportExport { 37 38 static int failures = 0; 39 40 /** 41 * Test Path -> URI -> Path 42 */ testPath(String s)43 static void testPath(String s) { 44 Path path = Paths.get(s); 45 URI uri = path.toUri(); 46 Path result = Paths.get(uri); 47 Assert.assertEquals(result, path.toAbsolutePath()); 48 } 49 50 /** 51 * Test Path -> (expected) URI -> Path 52 */ testPath(String s, String expectedUri)53 static void testPath(String s, String expectedUri) { 54 Path path = Paths.get(s); 55 URI uri = path.toUri(); 56 Assert.assertEquals(uri.toString(), expectedUri); 57 Path result = Paths.get(uri); 58 Assert.assertEquals(result, path.toAbsolutePath()); 59 } 60 61 /** 62 * Test URI -> Path -> URI 63 */ testUri(String s)64 static void testUri(String s) throws Exception { 65 URI uri = URI.create(s); 66 Path path = Paths.get(uri); 67 URI result = path.toUri(); 68 Assert.assertEquals(result, uri); 69 } 70 71 /** 72 * Test URI -> Path fails with IllegalArgumentException 73 */ testBadUri(String s)74 static void testBadUri(String s) { 75 URI uri = URI.create(s); 76 try { 77 Path path = Paths.get(uri); 78 Assert.fail("Expected IllegalArgumentException"); 79 } catch (IllegalArgumentException expected) { 80 } 81 } 82 83 @Test main()84 public void main() throws Exception { 85 testBadUri("file:foo"); 86 testBadUri("file:/foo?q"); 87 testBadUri("file:/foo#f"); 88 89 String osname = System.getProperty("os.name"); 90 if (osname.startsWith("Windows")) { 91 testPath("C:\\doesnotexist"); 92 testPath("C:doesnotexist"); 93 testPath("\\\\server.nowhere.oracle.com\\share\\"); 94 testPath("\\\\fe80--203-baff-fe5a-749ds1.ipv6-literal.net\\share\\missing", 95 "file://[fe80::203:baff:fe5a:749d%1]/share/missing"); 96 } else { 97 testPath("doesnotexist"); 98 testPath("/doesnotexist"); 99 testPath("/does not exist"); 100 testUri("file:///"); 101 testUri("file:///foo/bar/doesnotexist"); 102 testUri("file:/foo/bar/doesnotexist"); 103 104 // file:///foo/bar/\u0440\u0443\u0441\u0441\u043A\u0438\u0439 (Russian) 105 testUri("file:///foo/bar/%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9"); 106 107 // invalid 108 testBadUri("file:foo"); 109 testBadUri("file://server/foo"); 110 testBadUri("file:///foo%00"); 111 } 112 } 113 }