Path: blob/master/test/jdk/tools/jar/ChangeDir.java
41144 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* @modules jdk.jartool27* @summary jar -C doesn't ignore multiple // in path28*/2930import java.io.*;31import java.nio.file.*;32import java.util.*;33import java.util.jar.*;34import java.util.spi.ToolProvider;35import java.util.stream.Stream;3637public class ChangeDir {38private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")39.orElseThrow(() ->40new RuntimeException("jar tool not found")41);4243private final static String jarName = "test.jar";44private final static String fileName = "hello.txt";4546/** Remove dirs & files needed for test. */47private static void cleanup(Path dir) {48try {49if (Files.isDirectory(dir)) {50try (Stream<Path> s = Files.list(dir)) {51s.forEach( p -> cleanup(p));52}53}54Files.delete(dir);55} catch (IOException x) {56fail(x.toString());57}58}5960public static void realMain(String[] args) throws Throwable {61doTest("/");62doTest("//");63doTest("///");64doTest("////");65if (System.getProperty("os.name").startsWith("Windows")) {66doTest("\\");67doTest("\\\\");68doTest("\\\\\\");69doTest("\\\\\\\\");70doTest("\\/");71}72}7374static void doTest(String sep) throws Throwable {75Path topDir = Files.createTempDirectory("delete");76try {77Files.deleteIfExists(Paths.get(jarName));7879// Create a subdirectory "a/b"80Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b"));8182// Create file in that subdirectory83Path testFile = testDir.resolve(fileName);84Files.createFile(testFile);8586// Create a jar file from that subdirectory, but with a // in the87// path name.88List<String> argList = new ArrayList<String>();89argList.add("cf");90argList.add(jarName);91argList.add("-C");92argList.add(topDir.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional93argList.add(fileName);9495int rc = JAR_TOOL.run(System.out, System.err,96argList.toArray(new String[argList.size()]));97if (rc != 0) {98fail("Could not create jar file.");99}100101// Check that the entry for hello.txt does *not* have a pathname.102try (JarFile jf = new JarFile(jarName)) {103for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {104JarEntry je = i.nextElement();105String name = je.getName();106if (name.indexOf(fileName) != -1) {107if (name.indexOf(fileName) != 0) {108fail(String.format(109"Expected '%s' but got '%s'%n", fileName, name));110} else {111pass();112}113}114}115}116} finally {117cleanup(topDir);118Files.deleteIfExists(Paths.get(jarName));119}120}121122//--------------------- Infrastructure ---------------------------123static volatile int passed = 0, failed = 0;124static void pass() {passed++;}125static void fail() {failed++; Thread.dumpStack();}126static void fail(String msg) {System.out.println(msg); fail();}127static void unexpected(Throwable t) {failed++; t.printStackTrace();}128static void check(boolean cond) {if (cond) pass(); else fail();}129static void equal(Object x, Object y) {130if (x == null ? y == null : x.equals(y)) pass();131else fail(x + " not equal to " + y);}132public static void main(String[] args) throws Throwable {133try {realMain(args);} catch (Throwable t) {unexpected(t);}134System.out.println("\nPassed = " + passed + " failed = " + failed);135if (failed > 0) throw new AssertionError("Some tests failed");}136}137138139