5548N/A/*
5548N/A * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
5548N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5548N/A *
5548N/A * This code is free software; you can redistribute it and/or modify it
5548N/A * under the terms of the GNU General Public License version 2 only, as
5548N/A * published by the Free Software Foundation.
5548N/A *
5548N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5548N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5548N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5548N/A * version 2 for more details (a copy is included in the LICENSE file that
5548N/A * accompanied this code).
5548N/A *
5548N/A * You should have received a copy of the GNU General Public License version
5548N/A * 2 along with this work; if not, write to the Free Software Foundation,
5548N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5548N/A *
5548N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5548N/A * or visit www.oracle.com if you need additional information or have any
5548N/A * questions.
5548N/A */
5548N/A
5548N/A/*
5548N/A * Portions Copyright (c) 2012 IBM Corporation
5548N/A */
5548N/A
5548N/A/*
5548N/A * @test
5548N/A * @bug 7201156
5548N/A * @summary jar tool fails to convert file separation characters for list and extract
5548N/A * @author Sean Chou
5548N/A */
5548N/A
5548N/Aimport java.io.File;
5548N/Aimport java.io.FileOutputStream;
5548N/Aimport java.io.IOException;
5548N/Aimport java.io.PipedInputStream;
5548N/Aimport java.io.PipedOutputStream;
5548N/Aimport java.io.PrintStream;
5548N/Aimport java.util.ArrayList;
5548N/Aimport java.util.List;
5548N/Aimport java.util.jar.JarEntry;
5548N/Aimport java.util.jar.JarOutputStream;
5548N/A
5548N/Aimport sun.tools.jar.Main;
5548N/A
5548N/Apublic class JarBackSlash {
5548N/A
5548N/A // used construct an entry JarBackSlash/dir/file.txt
5548N/A private static String JARBACKSLASH = "JarBackSlash";
5548N/A private static String DIR = "dir";
5548N/A private static String FILENAME = "file.txt";
5548N/A
5548N/A private static File createJarFile() throws IOException {
5548N/A File jarFile = File.createTempFile("JarBackSlashTest", ".jar");
5548N/A jarFile.deleteOnExit();
5548N/A
5548N/A try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) {
5548N/A JarEntry entry = new JarEntry(JARBACKSLASH + "/" + DIR + "/" + FILENAME);
5548N/A output.putNextEntry(entry);
5548N/A }
5548N/A
5548N/A return jarFile;
5548N/A }
5548N/A
5548N/A private static void testJarList(String jarFile) throws IOException {
5548N/A List<String> argList = new ArrayList<String>();
5548N/A argList.add("-tvf");
5548N/A argList.add(jarFile);
5548N/A argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
5548N/A
5548N/A String jarArgs[] = new String[argList.size()];
5548N/A jarArgs = argList.toArray(jarArgs);
5548N/A
5548N/A PipedOutputStream pipedOutput = new PipedOutputStream();
5548N/A PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
5548N/A PrintStream out = new PrintStream(pipedOutput);
5548N/A
5548N/A Main jarTool = new Main(out, System.err, "jar");
5548N/A if (!jarTool.run(jarArgs)) {
5548N/A fail("Could not list jar file.");
5548N/A }
5548N/A
5548N/A out.flush();
5548N/A check(pipedInput.available() > 0);
5548N/A }
5548N/A
5548N/A
5548N/A private static void testJarExtract(String jarFile) throws IOException {
5548N/A List<String> argList = new ArrayList<String>();
5548N/A argList.add("-xvf");
5548N/A argList.add(jarFile);
5548N/A argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
5548N/A
5548N/A String jarArgs[] = new String[argList.size()];
5548N/A jarArgs = argList.toArray(jarArgs);
5548N/A
5548N/A PipedOutputStream pipedOutput = new PipedOutputStream();
5548N/A PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
5548N/A PrintStream out = new PrintStream(pipedOutput);
5548N/A
5548N/A Main jarTool = new Main(out, System.err, "jar");
5548N/A if (!jarTool.run(jarArgs)) {
5548N/A fail("Could not list jar file.");
5548N/A }
5548N/A
5548N/A out.flush();
5548N/A check(pipedInput.available() > 0);
5548N/A }
5548N/A
5548N/A public static void realMain(String[] args) throws Throwable {
5548N/A File tmpJarFile = createJarFile();
5548N/A String tmpJarFilePath = tmpJarFile.getAbsolutePath();
5548N/A
5548N/A testJarList(tmpJarFilePath);
5548N/A testJarExtract(tmpJarFilePath);
5548N/A }
5548N/A
5548N/A
5548N/A //--------------------- Infrastructure ---------------------------
5548N/A static volatile int passed = 0, failed = 0;
5548N/A static void pass() {passed++;}
5548N/A static void fail() {failed++; Thread.dumpStack();}
5548N/A static void fail(String msg) {System.out.println(msg); fail();}
5548N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
5548N/A static void check(boolean cond) {if (cond) pass(); else fail();}
5548N/A static void equal(Object x, Object y) {
5548N/A if (x == null ? y == null : x.equals(y)) pass();
5548N/A else fail(x + " not equal to " + y);}
5548N/A public static void main(String[] args) throws Throwable {
5548N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
5548N/A System.out.println("\nPassed = " + passed + " failed = " + failed);
5548N/A if (failed > 0) throw new AssertionError("Some tests failed");}
5548N/A}