langtools/test/tools/javac/javazip/JavaZipTest.java
author jjg
Thu, 31 Mar 2016 15:20:50 -0700
changeset 36778 e04318f39f92
parent 36526 3b41f1c69604
permissions -rw-r--r--
8152897: refactor ToolBox to allow reduced documented dependencies Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     1
/*
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     4
 *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     8
 *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    13
 * accompanied this code).
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    14
 *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    18
 *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    21
 * questions.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    22
 */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    23
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    24
/*
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    25
 * @test
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    26
 * @bug 4098712 6304984 6388453
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    27
 * @summary check that source files inside zip files on the class path are ignored
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    28
 * @library /tools/lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.main
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    31
 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    32
 * @run main JavaZipTest
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    33
 */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    34
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    35
import java.nio.file.Files;
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    36
import java.nio.file.Paths;
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    37
import java.util.Arrays;
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    38
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    39
import toolbox.JarTask;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    40
import toolbox.JavacTask;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    41
import toolbox.Task;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    42
import toolbox.ToolBox;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    43
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    44
// Original test: test/tools/javac/javazip/Test.sh
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    45
public class JavaZipTest {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    46
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    47
    private static final String ASrc =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    48
        "class A {\n" +
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    49
        "    B b;\n" +
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    50
        "}";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    51
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    52
    private static final String BGoodSrc =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    53
        "public class B {}";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    54
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    55
    private static final String BBadSrc =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    56
        "class B";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    57
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    58
    private static final String[][] jarArgs = {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    59
        {"cf", "good.jar", "-C", "good", "B.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    60
        {"cf", "good.zip", "-C", "good", "B.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    61
        {"cf", "bad.jar", "-C", "bad", "B.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    62
        {"cf", "bad.zip", "-C", "bad", "B.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    63
    };
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    64
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    65
    private static final String[][] successfulCompilationArgs = {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    66
        {"-d", "output", "A.java", "good/B.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    67
        {"-d", "output", "-cp", "good", "A.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    68
        {"-d", "output", "-sourcepath", "good", "A.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    69
        {"-d", "output", "-cp", "good.zip", "A.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    70
        {"-d", "output", "-cp", "good.jar", "A.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    71
    };
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    72
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    73
    private static final String[][] unsuccessfulCompilationArgs = {
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    74
        {"-d", "output", "A.java", "bad/B.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    75
        {"-d", "output", "-cp", "bad", "A.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    76
        {"-d", "output", "-sourcepath", "bad", "A.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    77
        {"-d", "output", "-sourcepath", "bad.zip", "A.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    78
        {"-d", "output", "-sourcepath", "bad.jar", "A.java"},
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    79
    };
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    80
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    81
    public static void main(String[] args) throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    82
        new JavaZipTest().test();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    83
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    84
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    85
    private final ToolBox tb = new ToolBox();
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    86
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    87
    public void test() throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    88
        createOutputDirAndSourceFiles();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    89
        createZipsAndJars();
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    90
        check(Task.Expect.SUCCESS, successfulCompilationArgs);
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    91
        check(Task.Expect.FAIL, unsuccessfulCompilationArgs);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    92
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    93
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    94
    void createOutputDirAndSourceFiles() throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    95
        //create output dir
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    96
        Files.createDirectory(Paths.get("output"));
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    97
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    98
        //source file creation
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
    99
        tb.writeJavaFiles(Paths.get("good"), BGoodSrc);
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   100
        tb.writeJavaFiles(Paths.get("bad"), BBadSrc);
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   101
        tb.writeJavaFiles(ToolBox.currDir, ASrc);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   102
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   103
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   104
    void createZipsAndJars() throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   105
        //jar and zip creation
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   106
        for (String[] args: jarArgs) {
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   107
            new JarTask(tb).run(args).writeAll();
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   108
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   109
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   110
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   111
    void check(Task.Expect expectedStatus, String[][] theArgs) throws Exception {
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   112
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   113
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   114
        for (String[] allArgs: theArgs) {
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   115
            new JavacTask(tb)
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   116
                    .options(opts(allArgs))
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   117
                    .files(files(allArgs))
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   118
                    .run(expectedStatus)
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   119
                    .writeAll();
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   120
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   121
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   122
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   123
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   124
    private String[] opts(String... allArgs) {
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   125
        int i = allArgs.length;
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   126
        while (allArgs[i - 1].endsWith(".java"))
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   127
            i--;
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   128
        return Arrays.copyOfRange(allArgs, 0, i);
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   129
    }
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   130
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   131
    private String[] files(String... allArgs) {
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   132
        int i = allArgs.length;
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   133
        while (allArgs[i - 1].endsWith(".java"))
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   134
            i--;
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   135
        return Arrays.copyOfRange(allArgs, i, allArgs.length);
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   136
    }
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 16304
diff changeset
   137
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   138
}