Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/jar/JarBackSlash.java
38833 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* @summary jar tool fails to convert file separation characters for list and extract31* @author Sean Chou32*/3334import java.io.File;35import java.io.FileOutputStream;36import java.io.IOException;37import java.io.PipedInputStream;38import java.io.PipedOutputStream;39import java.io.PrintStream;40import java.util.ArrayList;41import java.util.List;42import java.util.jar.JarEntry;43import java.util.jar.JarOutputStream;4445import sun.tools.jar.Main;4647public class JarBackSlash {4849// used construct an entry JarBackSlash/dir/file.txt50private static String JARBACKSLASH = "JarBackSlash";51private static String DIR = "dir";52private static String FILENAME = "file.txt";5354private static File createJarFile() throws IOException {55File jarFile = File.createTempFile("JarBackSlashTest", ".jar");56jarFile.deleteOnExit();5758try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) {59JarEntry entry = new JarEntry(JARBACKSLASH + "/" + DIR + "/" + FILENAME);60output.putNextEntry(entry);61}6263return jarFile;64}6566private static void testJarList(String jarFile) throws IOException {67List<String> argList = new ArrayList<String>();68argList.add("-tvf");69argList.add(jarFile);70argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);7172String jarArgs[] = new String[argList.size()];73jarArgs = argList.toArray(jarArgs);7475PipedOutputStream pipedOutput = new PipedOutputStream();76PipedInputStream pipedInput = new PipedInputStream(pipedOutput);77PrintStream out = new PrintStream(pipedOutput);7879Main jarTool = new Main(out, System.err, "jar");80if (!jarTool.run(jarArgs)) {81fail("Could not list jar file.");82}8384out.flush();85check(pipedInput.available() > 0);86}878889private static void testJarExtract(String jarFile) throws IOException {90List<String> argList = new ArrayList<String>();91argList.add("-xvf");92argList.add(jarFile);93argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);9495String jarArgs[] = new String[argList.size()];96jarArgs = argList.toArray(jarArgs);9798PipedOutputStream pipedOutput = new PipedOutputStream();99PipedInputStream pipedInput = new PipedInputStream(pipedOutput);100PrintStream out = new PrintStream(pipedOutput);101102Main jarTool = new Main(out, System.err, "jar");103if (!jarTool.run(jarArgs)) {104fail("Could not list jar file.");105}106107out.flush();108check(pipedInput.available() > 0);109}110111public static void realMain(String[] args) throws Throwable {112File tmpJarFile = createJarFile();113String tmpJarFilePath = tmpJarFile.getAbsolutePath();114115testJarList(tmpJarFilePath);116testJarExtract(tmpJarFilePath);117}118119120//--------------------- Infrastructure ---------------------------121static volatile int passed = 0, failed = 0;122static void pass() {passed++;}123static void fail() {failed++; Thread.dumpStack();}124static void fail(String msg) {System.out.println(msg); fail();}125static void unexpected(Throwable t) {failed++; t.printStackTrace();}126static void check(boolean cond) {if (cond) pass(); else fail();}127static void equal(Object x, Object y) {128if (x == null ? y == null : x.equals(y)) pass();129else fail(x + " not equal to " + y);}130public static void main(String[] args) throws Throwable {131try {realMain(args);} catch (Throwable t) {unexpected(t);}132System.out.println("\nPassed = " + passed + " failed = " + failed);133if (failed > 0) throw new AssertionError("Some tests failed");}134}135136137