Path: blob/master/test/jdk/java/nio/file/Path/UriImportExport.java
41153 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 700315525* @summary Unit test for java.nio.file.Path26*/2728import java.nio.file.*;29import java.net.URI;30import java.net.URISyntaxException;31import java.io.PrintStream;3233public class UriImportExport {3435static final PrintStream log = System.out;36static int failures = 0;3738/**39* Test Path -> URI -> Path40*/41static void testPath(String s) {42Path path = Paths.get(s);43log.println(path);44URI uri = path.toUri();45log.println(" --> " + uri);46Path result = Paths.get(uri);47log.println(" --> " + result);48if (!result.equals(path.toAbsolutePath())) {49log.println("FAIL: Expected " + path + ", got " + result);50failures++;51}52log.println();53}5455/**56* Test Path -> (expected) URI -> Path57*/58static void testPath(String s, String expectedUri) {59Path path = Paths.get(s);60log.println(path);61URI uri = path.toUri();62log.println(" --> " + uri);63if (!uri.toString().equals(expectedUri)) {64log.println("FAILED: Expected " + expectedUri + ", got " + uri);65failures++;66return;67}68Path result = Paths.get(uri);69log.println(" --> " + result);70if (!result.equals(path.toAbsolutePath())) {71log.println("FAIL: Expected " + path + ", got " + result);72failures++;73}74log.println();75}7677/**78* Test URI -> Path -> URI79*/80static void testUri(String s) throws Exception {81URI uri = URI.create(s);82log.println(uri);83Path path = Paths.get(uri);84log.println(" --> " + path);85URI result = path.toUri();86log.println(" --> " + result);87if (!result.equals(uri)) {88log.println("FAIL: Expected " + uri + ", got " + result);89failures++;90}91log.println();92}9394/**95* Test URI -> Path fails with IllegalArgumentException96*/97static void testBadUri(String s) throws Exception {98URI uri = URI.create(s);99log.println(uri);100try {101Path path = Paths.get(uri);102log.format(" --> %s FAIL: Expected IllegalArgumentException\n", path);103failures++;104} catch (IllegalArgumentException expected) {105log.println(" --> IllegalArgumentException (expected)");106}107log.println();108}109110public static void main(String[] args) throws Exception {111testBadUri("file:foo");112testBadUri("file:/foo?q");113testBadUri("file:/foo#f");114115String osname = System.getProperty("os.name");116if (osname.startsWith("Windows")) {117testPath("C:\\doesnotexist");118testPath("C:doesnotexist");119testPath("\\\\server.nowhere.oracle.com\\share\\");120testPath("\\\\fe80--203-baff-fe5a-749ds1.ipv6-literal.net\\share\\missing",121"file://[fe80::203:baff:fe5a:749d%1]/share/missing");122} else {123testPath("doesnotexist");124testPath("/doesnotexist");125testPath("/does not exist");126testUri("file:///");127testUri("file:///foo/bar/doesnotexist");128testUri("file:/foo/bar/doesnotexist");129130// file:///foo/bar/\u0440\u0443\u0441\u0441\u043A\u0438\u0439 (Russian)131testUri("file:///foo/bar/%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9");132133// invalid134testBadUri("file:foo");135testBadUri("file://server/foo");136testBadUri("file:///foo%00");137}138139if (failures > 0)140throw new RuntimeException(failures + " test(s) failed");141}142}143144145