893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A/* @test
3497N/A * @bug 4313887 7003155
893N/A * @summary Unit test for java.nio.file.Path
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.net.URI;
893N/Aimport java.net.URISyntaxException;
893N/Aimport java.io.PrintStream;
893N/A
893N/Apublic class UriImportExport {
893N/A
893N/A static final PrintStream log = System.out;
893N/A static int failures = 0;
893N/A
3497N/A /**
3497N/A * Test Path -> URI -> Path
3497N/A */
3497N/A static void testPath(String s) {
3497N/A Path path = Paths.get(s);
3497N/A log.println(path);
3497N/A URI uri = path.toUri();
3497N/A log.println(" --> " + uri);
3497N/A Path result = Paths.get(uri);
3497N/A log.println(" --> " + result);
3497N/A if (!result.equals(path.toAbsolutePath())) {
3497N/A log.println("FAIL: Expected " + path + ", got " + result);
3497N/A failures++;
3497N/A }
893N/A log.println();
3497N/A }
3497N/A
3497N/A /**
3497N/A * Test Path -> (expected) URI -> Path
3497N/A */
3497N/A static void testPath(String s, String expectedUri) {
3497N/A Path path = Paths.get(s);
3497N/A log.println(path);
3497N/A URI uri = path.toUri();
3497N/A log.println(" --> " + uri);
3497N/A if (!uri.toString().equals(expectedUri)) {
3497N/A log.println("FAILED: Expected " + expectedUri + ", got " + uri);
893N/A failures++;
893N/A return;
893N/A }
3497N/A Path result = Paths.get(uri);
3497N/A log.println(" --> " + result);
3497N/A if (!result.equals(path.toAbsolutePath())) {
3497N/A log.println("FAIL: Expected " + path + ", got " + result);
3497N/A failures++;
3497N/A }
3497N/A log.println();
893N/A }
893N/A
3497N/A /**
3497N/A * Test URI -> Path -> URI
3497N/A */
3497N/A static void testUri(String s) throws Exception {
3497N/A URI uri = URI.create(s);
3497N/A log.println(uri);
3497N/A Path path = Paths.get(uri);
3497N/A log.println(" --> " + path);
3497N/A URI result = path.toUri();
3497N/A log.println(" --> " + result);
3497N/A if (!result.equals(uri)) {
3497N/A log.println("FAIL: Expected " + uri + ", got " + result);
3497N/A failures++;
3497N/A }
3497N/A log.println();
3497N/A }
3497N/A
3497N/A /**
3497N/A * Test URI -> Path fails with IllegalArgumentException
3497N/A */
3497N/A static void testBadUri(String s) throws Exception {
3497N/A URI uri = URI.create(s);
3497N/A log.println(uri);
3497N/A try {
3497N/A Path path = Paths.get(uri);
3497N/A log.format(" --> %s FAIL: Expected IllegalArgumentException\n", path);
3497N/A failures++;
3497N/A } catch (IllegalArgumentException expected) {
3497N/A log.println(" --> IllegalArgumentException (expected)");
3497N/A }
3497N/A log.println();
893N/A }
893N/A
893N/A public static void main(String[] args) throws Exception {
3497N/A testBadUri("file:foo");
3497N/A testBadUri("file:/foo?q");
3497N/A testBadUri("file:/foo#f");
893N/A
893N/A String osname = System.getProperty("os.name");
893N/A if (osname.startsWith("Windows")) {
3497N/A testPath("C:\\doesnotexist");
3497N/A testPath("C:doesnotexist");
3497N/A testPath("\\\\server.nowhere.oracle.com\\share\\");
3497N/A testPath("\\\\fe80--203-baff-fe5a-749ds1.ipv6-literal.net\\share\\missing",
893N/A "file://[fe80::203:baff:fe5a:749d%1]/share/missing");
3497N/A } else {
3497N/A testPath("doesnotexist");
3497N/A testPath("/doesnotexist");
3497N/A testPath("/does not exist");
3497N/A testUri("file:///");
3497N/A testUri("file:///foo/bar/doesnotexist");
3497N/A testUri("file:/foo/bar/doesnotexist");
3497N/A
3497N/A // file:///foo/bar/\u0440\u0443\u0441\u0441\u043A\u0438\u0439 (Russian)
3497N/A testUri("file:///foo/bar/%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9");
3497N/A
3497N/A // invalid
3497N/A testBadUri("file:foo");
3497N/A testBadUri("file://server/foo");
3497N/A testBadUri("file:///foo%00");
893N/A }
893N/A
893N/A if (failures > 0)
893N/A throw new RuntimeException(failures + " test(s) failed");
893N/A }
893N/A}