jdk/test/tools/jar/JarBackSlash.java
author mchung
Wed, 12 Oct 2016 15:41:00 -0700
changeset 41484 834b7539ada3
parent 30820 0d4717a011d3
permissions -rw-r--r--
8164689: Retrofit jar, jlink, jmod as a ToolProvider Reviewed-by: alanb, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14504
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
     1
/*
14856
92a1bcf46888 8004982: JDK8 source with GPL header errors
katleman
parents: 14504
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
14504
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
     4
 *
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
     7
 * published by the Free Software Foundation.
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
     8
 *
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    13
 * accompanied this code).
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    14
 *
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    18
 *
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    21
 * questions.
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    22
 */
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    23
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    24
/*
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    25
 * Portions Copyright (c) 2012 IBM Corporation
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    26
 */
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    27
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    28
/*
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    29
 * @test
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    30
 * @bug 7201156
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    31
 * @modules jdk.jartool
14504
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    32
 * @summary jar tool fails to convert file separation characters for list and extract
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    33
 * @author Sean Chou
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    34
 */
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    35
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    36
import java.io.File;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    37
import java.io.FileOutputStream;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    38
import java.io.IOException;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    39
import java.io.PipedInputStream;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    40
import java.io.PipedOutputStream;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    41
import java.io.PrintStream;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    42
import java.util.ArrayList;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    43
import java.util.List;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    44
import java.util.jar.JarEntry;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    45
import java.util.jar.JarOutputStream;
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    46
import java.util.spi.ToolProvider;
14504
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    47
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    48
public class JarBackSlash {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    49
    private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    50
        .orElseThrow(() ->
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    51
            new RuntimeException("jar tool not found")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    52
        );
14504
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    53
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    54
    // used construct an entry JarBackSlash/dir/file.txt
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    55
    private static String JARBACKSLASH = "JarBackSlash";
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    56
    private static String DIR = "dir";
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    57
    private static String FILENAME = "file.txt";
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    58
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    59
    private static File createJarFile() throws IOException {
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    60
        File jarFile = File.createTempFile("JarBackSlashTest", ".jar");
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    61
        jarFile.deleteOnExit();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    62
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    63
        try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) {
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    64
            JarEntry entry = new JarEntry(JARBACKSLASH + "/" + DIR + "/" + FILENAME);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    65
            output.putNextEntry(entry);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    66
        }
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    67
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    68
        return jarFile;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    69
    }
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    70
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    71
    private static void testJarList(String jarFile) throws IOException {
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    72
        List<String> argList = new ArrayList<String>();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    73
        argList.add("-tvf");
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    74
        argList.add(jarFile);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    75
        argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    76
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    77
        String jarArgs[] = new String[argList.size()];
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    78
        jarArgs = argList.toArray(jarArgs);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    79
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    80
        PipedOutputStream pipedOutput = new PipedOutputStream();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    81
        PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    82
        PrintStream out = new PrintStream(pipedOutput);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    83
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    84
        int rc = JAR_TOOL.run(out, System.err, jarArgs);
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    85
        if (rc != 0) {
14504
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    86
            fail("Could not list jar file.");
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    87
        }
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    88
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    89
        out.flush();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    90
        check(pipedInput.available() > 0);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    91
    }
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    92
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    93
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    94
    private static void testJarExtract(String jarFile) throws IOException {
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    95
        List<String> argList = new ArrayList<String>();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    96
        argList.add("-xvf");
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    97
        argList.add(jarFile);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    98
        argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
    99
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   100
        String jarArgs[] = new String[argList.size()];
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   101
        jarArgs = argList.toArray(jarArgs);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   102
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   103
        PipedOutputStream pipedOutput = new PipedOutputStream();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   104
        PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   105
        PrintStream out = new PrintStream(pipedOutput);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   106
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
   107
        int rc = JAR_TOOL.run(out, System.err, jarArgs);
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
   108
        if (rc != 0) {
14504
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   109
            fail("Could not list jar file.");
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   110
        }
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   111
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   112
        out.flush();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   113
        check(pipedInput.available() > 0);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   114
    }
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   115
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   116
    public static void realMain(String[] args) throws Throwable {
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   117
        File tmpJarFile = createJarFile();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   118
        String tmpJarFilePath = tmpJarFile.getAbsolutePath();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   119
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   120
        testJarList(tmpJarFilePath);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   121
        testJarExtract(tmpJarFilePath);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   122
    }
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   123
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   124
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   125
    //--------------------- Infrastructure ---------------------------
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   126
    static volatile int passed = 0, failed = 0;
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   127
    static void pass() {passed++;}
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   128
    static void fail() {failed++; Thread.dumpStack();}
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   129
    static void fail(String msg) {System.out.println(msg); fail();}
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   130
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   131
    static void check(boolean cond) {if (cond) pass(); else fail();}
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   132
    static void equal(Object x, Object y) {
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   133
        if (x == null ? y == null : x.equals(y)) pass();
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   134
        else fail(x + " not equal to " + y);}
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   135
    public static void main(String[] args) throws Throwable {
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   136
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   137
        System.out.println("\nPassed = " + passed + " failed = " + failed);
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   138
        if (failed > 0) throw new AssertionError("Some tests failed");}
aee973f6b53a 7201156: jar tool fails to convert file separation characters for list and extract
zhouyx
parents:
diff changeset
   139
}