1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4313887 7003155
* @summary Unit test for java.nio.file.Path
*/
import java.nio.file.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.PrintStream;
public class UriImportExport {
static final PrintStream log = System.out;
static int failures = 0;
/**
* Test Path -> URI -> Path
*/
static void testPath(String s) {
Path path = Paths.get(s);
log.println(path);
URI uri = path.toUri();
log.println(" --> " + uri);
Path result = Paths.get(uri);
log.println(" --> " + result);
if (!result.equals(path.toAbsolutePath())) {
log.println("FAIL: Expected " + path + ", got " + result);
failures++;
}
log.println();
}
/**
* Test Path -> (expected) URI -> Path
*/
static void testPath(String s, String expectedUri) {
Path path = Paths.get(s);
log.println(path);
URI uri = path.toUri();
log.println(" --> " + uri);
if (!uri.toString().equals(expectedUri)) {
log.println("FAILED: Expected " + expectedUri + ", got " + uri);
failures++;
return;
}
Path result = Paths.get(uri);
log.println(" --> " + result);
if (!result.equals(path.toAbsolutePath())) {
log.println("FAIL: Expected " + path + ", got " + result);
failures++;
}
log.println();
}
/**
* Test URI -> Path -> URI
*/
static void testUri(String s) throws Exception {
URI uri = URI.create(s);
log.println(uri);
Path path = Paths.get(uri);
log.println(" --> " + path);
URI result = path.toUri();
log.println(" --> " + result);
if (!result.equals(uri)) {
log.println("FAIL: Expected " + uri + ", got " + result);
failures++;
}
log.println();
}
/**
* Test URI -> Path fails with IllegalArgumentException
*/
static void testBadUri(String s) throws Exception {
URI uri = URI.create(s);
log.println(uri);
try {
Path path = Paths.get(uri);
log.format(" --> %s FAIL: Expected IllegalArgumentException\n", path);
failures++;
} catch (IllegalArgumentException expected) {
log.println(" --> IllegalArgumentException (expected)");
}
log.println();
}
public static void main(String[] args) throws Exception {
testBadUri("file:foo");
testBadUri("file:/foo?q");
testBadUri("file:/foo#f");
String osname = System.getProperty("os.name");
if (osname.startsWith("Windows")) {
testPath("C:\\doesnotexist");
testPath("C:doesnotexist");
testPath("\\\\server.nowhere.oracle.com\\share\\");
testPath("\\\\fe80--203-baff-fe5a-749ds1.ipv6-literal.net\\share\\missing",
"file://[fe80::203:baff:fe5a:749d%1]/share/missing");
} else {
testPath("doesnotexist");
testPath("/doesnotexist");
testPath("/does not exist");
testUri("file:///");
testUri("file:///foo/bar/doesnotexist");
testUri("file:/foo/bar/doesnotexist");
// file:///foo/bar/\u0440\u0443\u0441\u0441\u043A\u0438\u0439 (Russian)
testUri("file:///foo/bar/%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9");
// invalid
testBadUri("file:foo");
testBadUri("file://server/foo");
testBadUri("file:///foo%00");
}
if (failures > 0)
throw new RuntimeException(failures + " test(s) failed");
}
}
|