Path: blob/master/test/jdk/tools/jar/JarBackSlash.java
41144 views
/*1* Copyright (c) 2012, 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* Portions Copyright (c) 2012 IBM Corporation25*/2627/*28* @test29* @bug 720115630* @modules jdk.jartool31* @summary jar tool fails to convert file separation characters for list and extract32* @author Sean Chou33*/3435import java.io.File;36import java.io.FileOutputStream;37import java.io.IOException;38import java.io.PipedInputStream;39import java.io.PipedOutputStream;40import java.io.PrintStream;41import java.util.ArrayList;42import java.util.List;43import java.util.jar.JarEntry;44import java.util.jar.JarOutputStream;45import java.util.spi.ToolProvider;4647public class JarBackSlash {48private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")49.orElseThrow(() ->50new RuntimeException("jar tool not found")51);5253// used construct an entry JarBackSlash/dir/file.txt54private static String JARBACKSLASH = "JarBackSlash";55private static String DIR = "dir";56private static String FILENAME = "file.txt";5758private static File createJarFile() throws IOException {59File jarFile = File.createTempFile("JarBackSlashTest", ".jar");60jarFile.deleteOnExit();6162try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) {63JarEntry entry = new JarEntry(JARBACKSLASH + "/" + DIR + "/" + FILENAME);64output.putNextEntry(entry);65}6667return jarFile;68}6970private static void testJarList(String jarFile) throws IOException {71List<String> argList = new ArrayList<String>();72argList.add("-tvf");73argList.add(jarFile);74argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);7576String jarArgs[] = new String[argList.size()];77jarArgs = argList.toArray(jarArgs);7879PipedOutputStream pipedOutput = new PipedOutputStream();80PipedInputStream pipedInput = new PipedInputStream(pipedOutput);81PrintStream out = new PrintStream(pipedOutput);8283int rc = JAR_TOOL.run(out, System.err, jarArgs);84if (rc != 0) {85fail("Could not list jar file.");86}8788out.flush();89check(pipedInput.available() > 0);90}919293private static void testJarExtract(String jarFile) throws IOException {94List<String> argList = new ArrayList<String>();95argList.add("-xvf");96argList.add(jarFile);97argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);9899String jarArgs[] = new String[argList.size()];100jarArgs = argList.toArray(jarArgs);101102PipedOutputStream pipedOutput = new PipedOutputStream();103PipedInputStream pipedInput = new PipedInputStream(pipedOutput);104PrintStream out = new PrintStream(pipedOutput);105106int rc = JAR_TOOL.run(out, System.err, jarArgs);107if (rc != 0) {108fail("Could not list jar file.");109}110111out.flush();112check(pipedInput.available() > 0);113}114115public static void realMain(String[] args) throws Throwable {116File tmpJarFile = createJarFile();117String tmpJarFilePath = tmpJarFile.getAbsolutePath();118119testJarList(tmpJarFilePath);120testJarExtract(tmpJarFilePath);121}122123124//--------------------- Infrastructure ---------------------------125static volatile int passed = 0, failed = 0;126static void pass() {passed++;}127static void fail() {failed++; Thread.dumpStack();}128static void fail(String msg) {System.out.println(msg); fail();}129static void unexpected(Throwable t) {failed++; t.printStackTrace();}130static void check(boolean cond) {if (cond) pass(); else fail();}131static void equal(Object x, Object y) {132if (x == null ? y == null : x.equals(y)) pass();133else fail(x + " not equal to " + y);}134public static void main(String[] args) throws Throwable {135try {realMain(args);} catch (Throwable t) {unexpected(t);}136System.out.println("\nPassed = " + passed + " failed = " + failed);137if (failed > 0) throw new AssertionError("Some tests failed");}138}139140141