Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/jar/ChangeDir.java
38833 views
/*1* Copyright (c) 2007, 2013, 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*/2223/**24* @test25* @bug 4806786 802311326* @summary jar -C doesn't ignore multiple // in path27*/2829import java.io.*;30import java.nio.file.*;31import java.util.*;32import java.util.jar.*;33import java.util.stream.Stream;34import sun.tools.jar.Main;3536public class ChangeDir {37private final static String jarName = "test.jar";38private final static String fileName = "hello.txt";3940/** Remove dirs & files needed for test. */41private static void cleanup(Path dir) {42try {43if (Files.isDirectory(dir)) {44try (Stream<Path> s = Files.list(dir)) {45s.forEach( p -> cleanup(p));46}47}48Files.delete(dir);49} catch (IOException x) {50fail(x.toString());51}52}5354public static void realMain(String[] args) throws Throwable {55doTest("/");56doTest("//");57doTest("///");58doTest("////");59if (System.getProperty("os.name").startsWith("Windows")) {60doTest("\\");61doTest("\\\\");62doTest("\\\\\\");63doTest("\\\\\\\\");64doTest("\\/");65}66}6768static void doTest(String sep) throws Throwable {69Path topDir = Files.createTempDirectory("delete");70try {71Files.deleteIfExists(Paths.get(jarName));7273// Create a subdirectory "a/b"74Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b"));7576// Create file in that subdirectory77Path testFile = testDir.resolve(fileName);78Files.createFile(testFile);7980// Create a jar file from that subdirectory, but with a // in the81// path name.82List<String> argList = new ArrayList<String>();83argList.add("cf");84argList.add(jarName);85argList.add("-C");86argList.add(topDir.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional87argList.add(fileName);8889Main jarTool = new Main(System.out, System.err, "jar");90if (!jarTool.run(argList.toArray(new String[argList.size()]))) {91fail("Could not create jar file.");92}9394// Check that the entry for hello.txt does *not* have a pathname.95try (JarFile jf = new JarFile(jarName)) {96for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {97JarEntry je = i.nextElement();98String name = je.getName();99if (name.indexOf(fileName) != -1) {100if (name.indexOf(fileName) != 0) {101fail(String.format(102"Expected '%s' but got '%s'%n", fileName, name));103} else {104pass();105}106}107}108}109} finally {110cleanup(topDir);111Files.deleteIfExists(Paths.get(jarName));112}113}114115//--------------------- Infrastructure ---------------------------116static volatile int passed = 0, failed = 0;117static void pass() {passed++;}118static void fail() {failed++; Thread.dumpStack();}119static void fail(String msg) {System.out.println(msg); fail();}120static void unexpected(Throwable t) {failed++; t.printStackTrace();}121static void check(boolean cond) {if (cond) pass(); else fail();}122static void equal(Object x, Object y) {123if (x == null ? y == null : x.equals(y)) pass();124else fail(x + " not equal to " + y);}125public static void main(String[] args) throws Throwable {126try {realMain(args);} catch (Throwable t) {unexpected(t);}127System.out.println("\nPassed = " + passed + " failed = " + failed);128if (failed > 0) throw new AssertionError("Some tests failed");}129}130131132