PathOps.java revision 3493
5429N/A/*
5429N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
5429N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5429N/A *
5429N/A * This code is free software; you can redistribute it and/or modify it
5429N/A * under the terms of the GNU General Public License version 2 only, as
5429N/A * published by the Free Software Foundation.
5429N/A *
5429N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6982N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6982N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5429N/A * version 2 for more details (a copy is included in the LICENSE file that
5429N/A * accompanied this code).
5429N/A *
6982N/A * You should have received a copy of the GNU General Public License version
5429N/A * 2 along with this work; if not, write to the Free Software Foundation,
6982N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6982N/A *
6982N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6982N/A * or visit www.oracle.com if you need additional information or have any
5429N/A * questions.
5429N/A */
5429N/A
5429N/Aimport java.nio.file.*;
6042N/Aimport java.net.*;
5429N/Aimport java.util.*;
5429N/Aimport java.io.IOException;
5429N/A
5429N/A/**
5429N/A * Tests path operations for zip provider.
5429N/A */
5429N/A
5429N/Apublic class PathOps {
5429N/A
5429N/A static final java.io.PrintStream out = System.out;
5429N/A static FileSystem fs;
5429N/A
5429N/A private String input;
5429N/A private Path path;
5429N/A private Exception exc;
5429N/A
5429N/A private PathOps(String s) {
5429N/A out.println();
5429N/A input = s;
5429N/A try {
5429N/A path = fs.getPath(s);
5429N/A out.format("%s -> %s", s, path);
5429N/A } catch (Exception x) {
5429N/A exc = x;
5429N/A out.format("%s -> %s", s, x);
5429N/A }
5429N/A out.println();
5429N/A }
5429N/A
5429N/A Path path() {
5429N/A return path;
5429N/A }
5429N/A
5462N/A void fail() {
5475N/A throw new RuntimeException("PathOps failed");
5475N/A }
5475N/A
5475N/A void checkPath() {
5475N/A if (path == null) {
5475N/A throw new InternalError("path is null");
5475N/A }
5475N/A }
5475N/A
5475N/A void check(Object result, String expected) {
5475N/A out.format("\tExpected: %s\n", expected);
5475N/A out.format("\tActual: %s\n", result);
5475N/A if (result == null) {
5429N/A if (expected == null) return;
5429N/A } else {
5429N/A // compare string representations
5429N/A if (expected != null && result.toString().equals(expected.toString()))
5429N/A return;
5429N/A }
5429N/A fail();
5429N/A }
5429N/A
5429N/A void check(Object result, boolean expected) {
5429N/A check(result, Boolean.toString(expected));
5429N/A }
5429N/A
5429N/A PathOps root(String expected) {
5429N/A out.println("check root");
5429N/A checkPath();
5429N/A check(path.getRoot(), expected);
5429N/A return this;
5429N/A }
5429N/A
5429N/A PathOps parent(String expected) {
5429N/A out.println("check parent");
5429N/A checkPath();
6984N/A check(path.getParent(), expected);
6984N/A return this;
6984N/A }
5429N/A
5429N/A PathOps name(String expected) {
5429N/A out.println("check name");
5429N/A checkPath();
5429N/A check(path.getFileName(), expected);
5429N/A return this;
5429N/A }
5429N/A
5429N/A PathOps element(int index, String expected) {
5429N/A out.format("check element %d\n", index);
5429N/A checkPath();
5429N/A check(path.getName(index), expected);
5429N/A return this;
5429N/A }
5429N/A
5429N/A PathOps subpath(int startIndex, int endIndex, String expected) {
5475N/A out.format("test subpath(%d,%d)\n", startIndex, endIndex);
5429N/A checkPath();
5429N/A check(path.subpath(startIndex, endIndex), expected);
5429N/A return this;
5429N/A }
5429N/A
5429N/A PathOps starts(String prefix) {
5429N/A out.format("test startsWith with %s\n", prefix);
5429N/A checkPath();
5429N/A Path s = fs.getPath(prefix);
5429N/A check(path.startsWith(s), true);
5475N/A return this;
5429N/A }
5429N/A
5429N/A PathOps notStarts(String prefix) {
5429N/A out.format("test not startsWith with %s\n", prefix);
5429N/A checkPath();
5429N/A Path s = fs.getPath(prefix);
5429N/A check(path.startsWith(s), false);
5429N/A return this;
5429N/A }
5429N/A
5429N/A PathOps ends(String suffix) {
5429N/A out.format("test endsWith %s\n", suffix);
5475N/A checkPath();
5475N/A Path s = fs.getPath(suffix);
5475N/A check(path.endsWith(s), true);
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps notEnds(String suffix) {
5475N/A out.format("test not endsWith %s\n", suffix);
5475N/A checkPath();
5475N/A Path s = fs.getPath(suffix);
5475N/A check(path.endsWith(s), false);
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps absolute() {
5475N/A out.println("check path is absolute");
5475N/A checkPath();
5475N/A check(path.isAbsolute(), true);
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps notAbsolute() {
5475N/A out.println("check path is not absolute");
5475N/A checkPath();
5475N/A check(path.isAbsolute(), false);
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps resolve(String other, String expected) {
5475N/A out.format("test resolve %s\n", other);
5475N/A checkPath();
5475N/A check(path.resolve(other), expected);
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps relativize(String other, String expected) {
5475N/A out.format("test relativize %s\n", other);
5475N/A checkPath();
5475N/A Path that = fs.getPath(other);
5475N/A check(path.relativize(that), expected);
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps normalize(String expected) {
5475N/A out.println("check normalized path");
5475N/A checkPath();
5475N/A check(path.normalize(), expected);
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps string(String expected) {
5475N/A out.println("check string representation");
6042N/A checkPath();
6042N/A check(path, expected);
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps isSameFile(String target) {
5475N/A try {
5475N/A out.println("check two paths are same");
5475N/A checkPath();
5475N/A check(Files.isSameFile(path, test(target).path()), true);
5475N/A } catch (IOException ioe) {
5475N/A fail();
5475N/A }
5475N/A return this;
5475N/A }
5475N/A
5475N/A PathOps invalid() {
5475N/A if (!(exc instanceof InvalidPathException)) {
5475N/A out.println("InvalidPathException not thrown as expected");
5475N/A fail();
5475N/A }
5475N/A return this;
5475N/A }
5475N/A
5475N/A static PathOps test(String s) {
5475N/A return new PathOps(s);
5475N/A }
5475N/A
5475N/A // -- PathOpss --
5475N/A
5475N/A static void header(String s) {
5475N/A out.println();
5475N/A out.println();
5475N/A out.println("-- " + s + " --");
5475N/A }
5475N/A
5475N/A static void doPathOpTests() {
5475N/A header("Path operations");
5475N/A
5475N/A // all components
5475N/A test("/a/b/c")
5475N/A .root("/")
5475N/A .parent("/a/b")
5475N/A .name("c");
5475N/A
5475N/A // root component only
5475N/A test("/")
5475N/A .root("/")
5475N/A .parent(null)
5475N/A .name(null);
5475N/A
5475N/A // no root component
5475N/A test("a/b")
5475N/A .root(null)
5475N/A .parent("a")
5475N/A .name("b");
5475N/A
5475N/A // name component only
5475N/A test("foo")
5475N/A .root(null)
5475N/A .parent(null)
5475N/A .name("foo");
5475N/A
5475N/A // startsWith
5475N/A test("")
5475N/A .starts("")
5475N/A .notStarts("/");
5475N/A test("/")
5475N/A .starts("/")
5475N/A .notStarts("/foo");
5475N/A test("/foo")
5475N/A .starts("/")
5475N/A .starts("/foo")
5475N/A .notStarts("/f")
5475N/A .notStarts("");
5475N/A test("/foo/bar")
5475N/A .starts("/")
5475N/A .starts("/foo")
5475N/A .starts("/foo/")
5475N/A .starts("/foo/bar")
5475N/A .notStarts("/f")
5475N/A .notStarts("foo")
5475N/A .notStarts("foo/bar")
5475N/A .notStarts("");
5475N/A test("foo")
5475N/A .starts("foo")
5475N/A .notStarts("f");
5475N/A test("foo/bar")
5475N/A .starts("foo")
5475N/A .starts("foo/")
5475N/A .starts("foo/bar")
5475N/A .notStarts("f")
5475N/A .notStarts("/foo")
5475N/A .notStarts("/foo/bar");
5475N/A
5475N/A // endsWith
5475N/A test("")
5475N/A .ends("")
5475N/A .notEnds("/");
5475N/A test("/")
5475N/A .ends("/")
5475N/A .notEnds("foo")
5475N/A .notEnds("/foo");
5475N/A test("/foo")
5475N/A .ends("foo")
5475N/A .ends("/foo")
5475N/A .notEnds("/");
5475N/A test("/foo/bar")
5475N/A .ends("bar")
5475N/A .ends("foo/bar")
5475N/A .ends("foo/bar/")
5475N/A .ends("/foo/bar")
5475N/A .notEnds("/bar");
5475N/A test("/foo/bar/")
5475N/A .ends("bar")
5475N/A .ends("foo/bar")
5475N/A .ends("foo/bar/")
5475N/A .ends("/foo/bar")
5475N/A .notEnds("/bar");
5475N/A test("foo")
5475N/A .ends("foo");
5475N/A test("foo/bar")
5475N/A .ends("bar")
5475N/A .ends("bar/")
5475N/A .ends("foo/bar/")
5475N/A .ends("foo/bar");
5475N/A
5475N/A
5475N/A // elements
5475N/A test("a/b/c")
5475N/A .element(0,"a")
5475N/A .element(1,"b")
5475N/A .element(2,"c");
5475N/A
5475N/A // isAbsolute
5475N/A test("/")
5475N/A .absolute();
5475N/A test("/tmp")
5475N/A .absolute();
5475N/A test("tmp")
5475N/A .notAbsolute();
5475N/A test("")
5475N/A .notAbsolute();
5475N/A
5475N/A // resolve
5475N/A test("/tmp")
5475N/A .resolve("foo", "/tmp/foo")
5475N/A .resolve("/foo", "/foo");
5475N/A test("tmp")
5475N/A .resolve("foo", "tmp/foo")
5475N/A .resolve("/foo", "/foo");
5475N/A
5475N/A // relativize
5475N/A test("/a/b/c")
5475N/A .relativize("/a/b/c", "")
5475N/A .relativize("/a/b/c/d/e", "d/e")
5475N/A .relativize("/a/x", "../../x");
5475N/A
5475N/A // normalize
5429N/A test("/")
5429N/A .normalize("/");
5429N/A test("foo")
5429N/A .normalize("foo");
5429N/A test("/foo")
5429N/A .normalize("/foo");
5429N/A test(".")
5429N/A .normalize("");
5429N/A test("..")
5429N/A .normalize("..");
5429N/A test("/..")
5429N/A .normalize("/");
5429N/A test("/../..")
5429N/A .normalize("/");
5429N/A test("foo/.")
5429N/A .normalize("foo");
5429N/A test("./foo")
5429N/A .normalize("foo");
5429N/A test("foo/..")
5429N/A .normalize("");
5429N/A test("../foo")
5429N/A .normalize("../foo");
5429N/A test("../../foo")
5429N/A .normalize("../../foo");
5429N/A test("foo/bar/..")
5429N/A .normalize("foo");
5429N/A test("foo/bar/gus/../..")
5429N/A .normalize("foo");
5429N/A test("/foo/bar/gus/../..")
5429N/A .normalize("/foo");
5429N/A test("/./.")
5429N/A .normalize("/");
5429N/A test("/.")
5429N/A .normalize("/");
5429N/A test("/./abc")
5429N/A .normalize("/abc");
5429N/A // invalid
5429N/A test("foo\u0000bar")
5429N/A .invalid();
5429N/A test("\u0000foo")
5429N/A .invalid();
5429N/A test("bar\u0000")
5429N/A .invalid();
5429N/A test("//foo\u0000bar")
5429N/A .invalid();
5429N/A test("//\u0000foo")
5429N/A .invalid();
5429N/A test("//bar\u0000")
5429N/A .invalid();
5429N/A
5429N/A // normalization
5429N/A test("//foo//bar")
5429N/A .string("/foo/bar")
5429N/A .root("/")
5429N/A .parent("/foo")
5429N/A .name("bar");
6627N/A
6627N/A // isSameFile
6627N/A test("/fileDoesNotExist")
6627N/A .isSameFile("/fileDoesNotExist");
5429N/A }
5429N/A
5429N/A static void npes() {
5429N/A header("NullPointerException");
5429N/A
5429N/A Path path = fs.getPath("foo");
5429N/A
5429N/A try {
5429N/A path.resolve((String)null);
5429N/A throw new RuntimeException("NullPointerException not thrown");
5429N/A } catch (NullPointerException npe) {
5429N/A }
5429N/A
5429N/A try {
5429N/A path.relativize(null);
5429N/A throw new RuntimeException("NullPointerException not thrown");
5429N/A } catch (NullPointerException npe) {
5429N/A }
5429N/A
5429N/A try {
6627N/A path.compareTo(null);
6627N/A throw new RuntimeException("NullPointerException not thrown");
6627N/A } catch (NullPointerException npe) {
6627N/A }
6627N/A
6627N/A try {
6627N/A path.startsWith((Path)null);
6627N/A throw new RuntimeException("NullPointerException not thrown");
6627N/A } catch (NullPointerException npe) {
6627N/A }
5429N/A
5429N/A try {
5429N/A path.endsWith((Path)null);
5429N/A throw new RuntimeException("NullPointerException not thrown");
5429N/A } catch (NullPointerException npe) {
5429N/A }
5429N/A
}
public static void main(String[] args) throws Throwable {
Path zipfile = Paths.get(args[0]);
fs = FileSystems.newFileSystem(zipfile, null);
npes();
doPathOpTests();
fs.close();
}
}