Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/zipfs/PathOps.java
38833 views
/*1* Copyright (c) 2009, 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*/2223import java.nio.file.*;24import java.net.*;25import java.util.*;26import java.io.IOException;2728/**29* Tests path operations for zip provider.30*/3132public class PathOps {3334static final java.io.PrintStream out = System.out;35static FileSystem fs;3637private String input;38private Path path;39private Exception exc;4041private PathOps(String s) {42out.println();43input = s;44try {45path = fs.getPath(s);46out.format("%s -> %s", s, path);47} catch (Exception x) {48exc = x;49out.format("%s -> %s", s, x);50}51out.println();52}5354Path path() {55return path;56}5758void fail() {59throw new RuntimeException("PathOps failed");60}6162void checkPath() {63if (path == null) {64throw new InternalError("path is null");65}66}6768void check(Object result, String expected) {69out.format("\tExpected: %s\n", expected);70out.format("\tActual: %s\n", result);71if (result == null) {72if (expected == null) return;73} else {74// compare string representations75if (expected != null && result.toString().equals(expected.toString()))76return;77}78fail();79}8081void check(Object result, boolean expected) {82check(result, Boolean.toString(expected));83}8485PathOps root(String expected) {86out.println("check root");87checkPath();88check(path.getRoot(), expected);89return this;90}9192PathOps parent(String expected) {93out.println("check parent");94checkPath();95check(path.getParent(), expected);96return this;97}9899PathOps name(String expected) {100out.println("check name");101checkPath();102check(path.getFileName(), expected);103return this;104}105106PathOps element(int index, String expected) {107out.format("check element %d\n", index);108checkPath();109check(path.getName(index), expected);110return this;111}112113PathOps subpath(int startIndex, int endIndex, String expected) {114out.format("test subpath(%d,%d)\n", startIndex, endIndex);115checkPath();116check(path.subpath(startIndex, endIndex), expected);117return this;118}119120PathOps starts(String prefix) {121out.format("test startsWith with %s\n", prefix);122checkPath();123Path s = fs.getPath(prefix);124check(path.startsWith(s), true);125return this;126}127128PathOps notStarts(String prefix) {129out.format("test not startsWith with %s\n", prefix);130checkPath();131Path s = fs.getPath(prefix);132check(path.startsWith(s), false);133return this;134}135136PathOps ends(String suffix) {137out.format("test endsWith %s\n", suffix);138checkPath();139Path s = fs.getPath(suffix);140check(path.endsWith(s), true);141return this;142}143144PathOps notEnds(String suffix) {145out.format("test not endsWith %s\n", suffix);146checkPath();147Path s = fs.getPath(suffix);148check(path.endsWith(s), false);149return this;150}151152PathOps absolute() {153out.println("check path is absolute");154checkPath();155check(path.isAbsolute(), true);156return this;157}158159PathOps notAbsolute() {160out.println("check path is not absolute");161checkPath();162check(path.isAbsolute(), false);163return this;164}165166PathOps resolve(String other, String expected) {167out.format("test resolve %s\n", other);168checkPath();169check(path.resolve(other), expected);170return this;171}172173PathOps relativize(String other, String expected) {174out.format("test relativize %s\n", other);175checkPath();176Path that = fs.getPath(other);177check(path.relativize(that), expected);178return this;179}180181PathOps normalize(String expected) {182out.println("check normalized path");183checkPath();184check(path.normalize(), expected);185return this;186}187188PathOps string(String expected) {189out.println("check string representation");190checkPath();191check(path, expected);192return this;193}194195PathOps isSameFile(String target) {196try {197out.println("check two paths are same");198checkPath();199check(Files.isSameFile(path, test(target).path()), true);200} catch (IOException ioe) {201fail();202}203return this;204}205206PathOps invalid() {207if (!(exc instanceof InvalidPathException)) {208out.println("InvalidPathException not thrown as expected");209fail();210}211return this;212}213214static PathOps test(String s) {215return new PathOps(s);216}217218// -- PathOpss --219220static void header(String s) {221out.println();222out.println();223out.println("-- " + s + " --");224}225226static void doPathOpTests() {227header("Path operations");228229// all components230test("/a/b/c")231.root("/")232.parent("/a/b")233.name("c");234235// root component only236test("/")237.root("/")238.parent(null)239.name(null);240241// no root component242test("a/b")243.root(null)244.parent("a")245.name("b");246247// name component only248test("foo")249.root(null)250.parent(null)251.name("foo");252253// startsWith254test("")255.starts("")256.notStarts("/");257test("/")258.starts("/")259.notStarts("/foo");260test("/foo")261.starts("/")262.starts("/foo")263.notStarts("/f")264.notStarts("");265test("/foo/bar")266.starts("/")267.starts("/foo")268.starts("/foo/")269.starts("/foo/bar")270.notStarts("/f")271.notStarts("foo")272.notStarts("foo/bar")273.notStarts("");274test("foo")275.starts("foo")276.notStarts("f");277test("foo/bar")278.starts("foo")279.starts("foo/")280.starts("foo/bar")281.notStarts("f")282.notStarts("/foo")283.notStarts("/foo/bar");284285// endsWith286test("")287.ends("")288.notEnds("/");289test("/")290.ends("/")291.notEnds("foo")292.notEnds("/foo");293test("/foo")294.ends("foo")295.ends("/foo")296.notEnds("/");297test("/foo/bar")298.ends("bar")299.ends("foo/bar")300.ends("foo/bar/")301.ends("/foo/bar")302.notEnds("/bar");303test("/foo/bar/")304.ends("bar")305.ends("foo/bar")306.ends("foo/bar/")307.ends("/foo/bar")308.notEnds("/bar");309test("foo")310.ends("foo");311test("foo/bar")312.ends("bar")313.ends("bar/")314.ends("foo/bar/")315.ends("foo/bar");316317318// elements319test("a/b/c")320.element(0,"a")321.element(1,"b")322.element(2,"c");323324// isAbsolute325test("/")326.absolute();327test("/tmp")328.absolute();329test("tmp")330.notAbsolute();331test("")332.notAbsolute();333334// resolve335test("/tmp")336.resolve("foo", "/tmp/foo")337.resolve("/foo", "/foo");338test("tmp")339.resolve("foo", "tmp/foo")340.resolve("/foo", "/foo");341342// relativize343test("/a/b/c")344.relativize("/a/b/c", "")345.relativize("/a/b/c/d/e", "d/e")346.relativize("/a/x", "../../x");347348// normalize349test("/")350.normalize("/");351test("foo")352.normalize("foo");353test("/foo")354.normalize("/foo");355test(".")356.normalize("");357test("..")358.normalize("..");359test("/..")360.normalize("/");361test("/../..")362.normalize("/");363test("foo/.")364.normalize("foo");365test("./foo")366.normalize("foo");367test("foo/..")368.normalize("");369test("../foo")370.normalize("../foo");371test("../../foo")372.normalize("../../foo");373test("foo/bar/..")374.normalize("foo");375test("foo/bar/gus/../..")376.normalize("foo");377test("/foo/bar/gus/../..")378.normalize("/foo");379test("/./.")380.normalize("/");381test("/.")382.normalize("/");383test("/./abc")384.normalize("/abc");385// invalid386test("foo\u0000bar")387.invalid();388test("\u0000foo")389.invalid();390test("bar\u0000")391.invalid();392test("//foo\u0000bar")393.invalid();394test("//\u0000foo")395.invalid();396test("//bar\u0000")397.invalid();398399// normalization400test("//foo//bar")401.string("/foo/bar")402.root("/")403.parent("/foo")404.name("bar");405406// isSameFile407test("/fileDoesNotExist")408.isSameFile("/fileDoesNotExist");409}410411static void npes() {412header("NullPointerException");413414Path path = fs.getPath("foo");415416try {417path.resolve((String)null);418throw new RuntimeException("NullPointerException not thrown");419} catch (NullPointerException npe) {420}421422try {423path.relativize(null);424throw new RuntimeException("NullPointerException not thrown");425} catch (NullPointerException npe) {426}427428try {429path.compareTo(null);430throw new RuntimeException("NullPointerException not thrown");431} catch (NullPointerException npe) {432}433434try {435path.startsWith((Path)null);436throw new RuntimeException("NullPointerException not thrown");437} catch (NullPointerException npe) {438}439440try {441path.endsWith((Path)null);442throw new RuntimeException("NullPointerException not thrown");443} catch (NullPointerException npe) {444}445446}447448public static void main(String[] args) throws Throwable {449450Path zipfile = Paths.get(args[0]);451fs = FileSystems.newFileSystem(zipfile, null);452npes();453doPathOpTests();454fs.close();455}456}457458459