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