jdk/test/tools/jar/compat/CLICompatibility.java
author alanb
Fri, 16 Dec 2016 06:19:16 +0000
changeset 42703 20c39ea4a507
parent 42463 39c0bbf9d03c
child 45286 cd809e28c082
permissions -rw-r--r--
8170987: Module system implementation refresh (12/2016) 8170859: Run time and tool support for ModuleResolution Reviewed-by: redestad, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, chris.hegarty@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import java.io.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.lang.reflect.Method;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.function.Consumer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.jar.JarEntry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.jar.JarInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.jar.JarOutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import jdk.testlibrary.FileUtils;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import jdk.testlibrary.JDKToolFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import org.testng.annotations.BeforeTest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import static java.lang.String.format;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import static java.lang.System.out;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import static java.nio.charset.StandardCharsets.UTF_8;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
    46
import static org.testng.Assert.assertFalse;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import static org.testng.Assert.assertTrue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * @test
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
    51
 * @bug 8170952
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 * @library /lib/testlibrary
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * @build jdk.testlibrary.FileUtils jdk.testlibrary.JDKToolFinder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * @run testng CLICompatibility
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * @summary Basic test for compatibility of CLI options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
public class CLICompatibility {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", "."));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
38762
2956ccc7cb77 8157850: Jar tests should pass through VM options
anazarov
parents: 36511
diff changeset
    62
    static final String TOOL_VM_OPTIONS = System.getProperty("test.tool.vm.opts", "");
2956ccc7cb77 8157850: Jar tests should pass through VM options
anazarov
parents: 36511
diff changeset
    63
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    final boolean legacyOnly;  // for running on older JDK's ( test validation )
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    // Resources we know to exist, that can be used for creating jar files.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    static final String RES1 = "CLICompatibility.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    static final String RES2 = "CLICompatibility$Result.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    @BeforeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    public void setupResourcesForJar() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        // Copy the files that we are going to use for creating/updating test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        // jar files, so that they can be referred to without '-C dir'
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        Files.copy(TEST_CLASSES.resolve(RES1), USER_DIR.resolve(RES1));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        Files.copy(TEST_CLASSES.resolve(RES2), USER_DIR.resolve(RES2));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    static final IOConsumer<InputStream> ASSERT_CONTAINS_RES1 = in -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        try (JarInputStream jin = new JarInputStream(in)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            assertTrue(jarContains(jin, RES1), "Failed to find " + RES1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    static final IOConsumer<InputStream> ASSERT_CONTAINS_RES2 = in -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        try (JarInputStream jin = new JarInputStream(in)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            assertTrue(jarContains(jin, RES2), "Failed to find " + RES2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    static final IOConsumer<InputStream> ASSERT_CONTAINS_MAINFEST = in -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        try (JarInputStream jin = new JarInputStream(in)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            assertTrue(jin.getManifest() != null, "No META-INF/MANIFEST.MF");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    static final IOConsumer<InputStream> ASSERT_DOES_NOT_CONTAIN_MAINFEST = in -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        try (JarInputStream jin = new JarInputStream(in)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            assertTrue(jin.getManifest() == null, "Found unexpected META-INF/MANIFEST.MF");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    static final FailCheckerWithMessage FAIL_TOO_MANY_MAIN_OPS =
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   100
        new FailCheckerWithMessage("You may not specify more than one '-cuxtid' options",
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        /* legacy */ "{ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    // Create
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    public void createBadArgs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        final FailCheckerWithMessage FAIL_CREATE_NO_ARGS = new FailCheckerWithMessage(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                "'c' flag requires manifest or input files to be specified!");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        jar("c")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            .resultChecker(FAIL_CREATE_NO_ARGS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        jar("-c")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            .resultChecker(FAIL_CREATE_NO_ARGS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        if (!legacyOnly)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            jar("--create")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                .resultChecker(FAIL_CREATE_NO_ARGS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        jar("ct")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        jar("-ct")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        if (!legacyOnly)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            jar("--create --list")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    public void createWriteToFile() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        Path path = Paths.get("createJarFile.jar");  // for creating
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        String jn = path.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        for (String opts : new String[]{"cf " + jn, "-cf " + jn, "--create --file=" + jn}) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            jar(opts, RES1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                    ASSERT_CONTAINS_RES1.accept(Files.newInputStream(path));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                    ASSERT_CONTAINS_MAINFEST.accept(Files.newInputStream(path));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        FileUtils.deleteFileIfExistsWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    public void createWriteToStdout() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        for (String opts : new String[]{"c", "-c", "--create"}) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            jar(opts, RES1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                    ASSERT_CONTAINS_RES1.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
                    ASSERT_CONTAINS_MAINFEST.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
    public void createWriteToStdoutNoManifest() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        for (String opts : new String[]{"cM", "-cM", "--create --no-manifest"} ){
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            jar(opts, RES1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                    ASSERT_CONTAINS_RES1.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                    ASSERT_DOES_NOT_CONTAIN_MAINFEST.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
    // Update
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
    public void updateBadArgs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        final FailCheckerWithMessage FAIL_UPDATE_NO_ARGS = new FailCheckerWithMessage(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                "'u' flag requires manifest, 'e' flag or input files to be specified!");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        jar("u")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
            .resultChecker(FAIL_UPDATE_NO_ARGS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
        jar("-u")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
            .resultChecker(FAIL_UPDATE_NO_ARGS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        if (!legacyOnly)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            jar("--update")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                .resultChecker(FAIL_UPDATE_NO_ARGS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
        jar("ut")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        jar("-ut")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
            .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        if (!legacyOnly)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
            jar("--update --list")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
    public void updateReadFileWriteFile() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        Path path = Paths.get("updateReadWriteStdout.jar");  // for updating
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        String jn = path.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
        for (String opts : new String[]{"uf " + jn, "-uf " + jn, "--update --file=" + jn}) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            createJar(path, RES1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            jar(opts, RES2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                    ASSERT_CONTAINS_RES1.accept(Files.newInputStream(path));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                    ASSERT_CONTAINS_RES2.accept(Files.newInputStream(path));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                    ASSERT_CONTAINS_MAINFEST.accept(Files.newInputStream(path));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
        FileUtils.deleteFileIfExistsWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
    public void updateReadStdinWriteStdout() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        Path path = Paths.get("updateReadStdinWriteStdout.jar");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
        for (String opts : new String[]{"u", "-u", "--update"}) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
            createJar(path, RES1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            jarWithStdin(path.toFile(), opts, RES2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
                    ASSERT_CONTAINS_RES1.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                    ASSERT_CONTAINS_RES2.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                    ASSERT_CONTAINS_MAINFEST.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
        FileUtils.deleteFileIfExistsWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
    public void updateReadStdinWriteStdoutNoManifest() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        Path path = Paths.get("updateReadStdinWriteStdoutNoManifest.jar");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
        for (String opts : new String[]{"uM", "-uM", "--update --no-manifest"} ){
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
            createJar(path, RES1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            jarWithStdin(path.toFile(), opts, RES2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
                .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                    ASSERT_CONTAINS_RES1.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
                    ASSERT_CONTAINS_RES2.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                    ASSERT_DOES_NOT_CONTAIN_MAINFEST.accept(r.stdoutAsStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
        FileUtils.deleteFileIfExistsWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
    // List
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
    public void listBadArgs() {
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   284
        jar("tx")
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   288
        jar("-tx")
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
            .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        if (!legacyOnly)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
            jar("--list --extract")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
                .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
    public void listReadFromFileWriteToStdout() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        Path path = Paths.get("listReadFromFileWriteToStdout.jar");  // for listing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        createJar(path, RES1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
        String jn = path.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
        for (String opts : new String[]{"tf " + jn, "-tf " + jn, "--list --file " + jn}) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
            jar(opts)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                .resultChecker(r ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                    assertTrue(r.output.contains("META-INF/MANIFEST.MF") && r.output.contains(RES1),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
                               "Failed, got [" + r.output + "]")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
                );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        FileUtils.deleteFileIfExistsWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
    public void listReadFromStdinWriteToStdout() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
        Path path = Paths.get("listReadFromStdinWriteToStdout.jar");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        createJar(path, RES1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
        for (String opts : new String[]{"t", "-t", "--list"} ){
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
            jarWithStdin(path.toFile(), opts)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
                .resultChecker(r ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                    assertTrue(r.output.contains("META-INF/MANIFEST.MF") && r.output.contains(RES1),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                               "Failed, got [" + r.output + "]")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        FileUtils.deleteFileIfExistsWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
    // Extract
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
    public void extractBadArgs() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
        jar("xi")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
            .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
        jar("-xi")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
            .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
            .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   349
        if (!legacyOnly) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
            jar("--extract --generate-index")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
                .assertFailure()
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   352
                .resultChecker(new FailCheckerWithMessage(
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   353
                                   "option --generate-index requires an argument"));
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   354
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   355
            jar("--extract --generate-index=foo")
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   356
                .assertFailure()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
                .resultChecker(FAIL_TOO_MANY_MAIN_OPS);
42463
39c0bbf9d03c 8170952: jar's usage message output need some cleanup
sherman
parents: 39313
diff changeset
   358
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
    public void extractReadFromStdin() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
        Path path = Paths.get("extract");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
        Path jarPath = path.resolve("extractReadFromStdin.jar"); // for extracting
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
        createJar(jarPath, RES1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
        for (String opts : new String[]{"x" ,"-x", "--extract"}) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
            jarWithStdinAndWorkingDir(jarPath.toFile(), path.toFile(), opts)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                .resultChecker(r ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
                    assertTrue(Files.exists(path.resolve(RES1)),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
                               "Expected to find:" + path.resolve(RES1))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
                );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
            FileUtils.deleteFileIfExistsWithRetry(path.resolve(RES1));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
        FileUtils.deleteFileTreeWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
    public void extractReadFromFile() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
        Path path = Paths.get("extract");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
        String jn = "extractReadFromFile.jar";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
        Path jarPath = path.resolve(jn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
        createJar(jarPath, RES1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
        for (String opts : new String[]{"xf "+jn ,"-xf "+jn, "--extract --file "+jn}) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
            if (legacyOnly && opts.startsWith("--"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
            jarWithStdinAndWorkingDir(null, path.toFile(), opts)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                .resultChecker(r ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                    assertTrue(Files.exists(path.resolve(RES1)),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
                               "Expected to find:" + path.resolve(RES1))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
            FileUtils.deleteFileIfExistsWithRetry(path.resolve(RES1));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
        FileUtils.deleteFileTreeWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
    // Basic help
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
    public void helpBadOptionalArg() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
        if (legacyOnly)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
        jar("--help:")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
            .assertFailure();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
        jar("--help:blah")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
            .assertFailure();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
    public void help() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
        if (legacyOnly)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
        jar("-h")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
            .resultChecker(r ->
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38762
diff changeset
   426
                assertTrue(r.output.startsWith("Usage: jar [OPTION...] [ [--release VERSION] [-C dir] files]"),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
                           "Failed, got [" + r.output + "]")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
            );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
        jar("--help")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
            .assertSuccess()
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   432
            .resultChecker(r -> {
39313
0970a9a1de13 8114827: JDK 9 multi-release enabled jar tool
sdrach
parents: 38762
diff changeset
   433
                assertTrue(r.output.startsWith("Usage: jar [OPTION...] [ [--release VERSION] [-C dir] files]"),
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   434
                           "Failed, got [" + r.output + "]");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   435
                assertFalse(r.output.contains("--do-not-resolve-by-default"));
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   436
                assertFalse(r.output.contains("--warn-if-resolved"));
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   437
            });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        jar("--help:compat")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            .resultChecker(r ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
                assertTrue(r.output.startsWith("Compatibility Interface:"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
                           "Failed, got [" + r.output + "]")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
            );
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   445
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   446
        jar("--help-extra")
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   447
            .assertSuccess()
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   448
            .resultChecker(r -> {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   449
                assertTrue(r.output.startsWith("Usage: jar [OPTION...] [ [--release VERSION] [-C dir] files]"),
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   450
                           "Failed, got [" + r.output + "]");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   451
                assertTrue(r.output.contains("--do-not-resolve-by-default"));
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   452
                assertTrue(r.output.contains("--warn-if-resolved"));
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42463
diff changeset
   453
            });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
    // -- Infrastructure
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
    static boolean jarContains(JarInputStream jis, String entryName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
        JarEntry e;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
        boolean found = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
        while((e = jis.getNextJarEntry()) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
            if (e.getName().equals(entryName))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
                return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
        return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
    /* Creates a simple jar with entries of size 0, good enough for testing */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
    static void createJar(Path path, String... entries) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
        FileUtils.deleteFileIfExistsWithRetry(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        Path parent = path.getParent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
        if (parent != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
            Files.createDirectories(parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
        try (OutputStream out = Files.newOutputStream(path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
             JarOutputStream jos = new JarOutputStream(out)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
            JarEntry je = new JarEntry("META-INF/MANIFEST.MF");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
            jos.putNextEntry(je);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
            jos.closeEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
            for (String entry : entries) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
                je = new JarEntry(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
                jos.putNextEntry(je);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
                jos.closeEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
    static class FailCheckerWithMessage implements Consumer<Result> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
        final String[] messages;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
        FailCheckerWithMessage(String... m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
            messages = m;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
        public void accept(Result r) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
            //out.printf("%s%n", r.output);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
            boolean found = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
            for (String m : messages) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
                if (r.output.contains(m)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
                    found = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
            assertTrue(found,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
                       "Excepted out to contain one of: " + Arrays.asList(messages)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
                           + " but got: " + r.output);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
    static Result jar(String... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
        return jarWithStdinAndWorkingDir(null, null, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
    static Result jarWithStdin(File stdinSource, String... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
        return jarWithStdinAndWorkingDir(stdinSource, null, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
    static Result jarWithStdinAndWorkingDir(File stdinFrom,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
                                            File workingDir,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
                                            String... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        String jar = getJDKTool("jar");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
        List<String> commands = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
        commands.add(jar);
38762
2956ccc7cb77 8157850: Jar tests should pass through VM options
anazarov
parents: 36511
diff changeset
   525
        if (!TOOL_VM_OPTIONS.isEmpty()) {
2956ccc7cb77 8157850: Jar tests should pass through VM options
anazarov
parents: 36511
diff changeset
   526
            commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1)));
2956ccc7cb77 8157850: Jar tests should pass through VM options
anazarov
parents: 36511
diff changeset
   527
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
        Stream.of(args).map(s -> s.split(" "))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
                       .flatMap(Arrays::stream)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
                       .forEach(x -> commands.add(x));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
        ProcessBuilder p = new ProcessBuilder(commands);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
        if (stdinFrom != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
            p.redirectInput(stdinFrom);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
        if (workingDir != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
            p.directory(workingDir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
        return run(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
    static Result run(ProcessBuilder pb) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        Process p;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
        byte[] stdout, stderr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
        out.printf("Running: %s%n", pb.command());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
            p = pb.start();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
        } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            throw new RuntimeException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
                    format("Couldn't start process '%s'", pb.command()), e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
        String output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
            stdout = readAllBytes(p.getInputStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
            stderr = readAllBytes(p.getErrorStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
            output = toString(stdout, stderr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
        } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
            throw new RuntimeException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
                    format("Couldn't read process output '%s'", pb.command()), e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
            p.waitFor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
        } catch (InterruptedException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
            throw new RuntimeException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
                    format("Process hasn't finished '%s'", pb.command()), e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
        return new Result(p.exitValue(), stdout, stderr, output);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
    static final Path JAVA_HOME = Paths.get(System.getProperty("java.home"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
    static String getJDKTool(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
            return JDKToolFinder.getJDKTool(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
        } catch (Exception x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
            Path j = JAVA_HOME.resolve("bin").resolve(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
            if (Files.exists(j))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
                return j.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
            j = JAVA_HOME.resolve("..").resolve("bin").resolve(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
            if (Files.exists(j))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
                return j.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
            throw new RuntimeException(x);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
    static String toString(byte[] ba1, byte[] ba2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
        return (new String(ba1, UTF_8)).concat(new String(ba2, UTF_8));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
    static class Result {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
        final int exitValue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
        final byte[] stdout;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
        final byte[] stderr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
        final String output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
        private Result(int exitValue, byte[] stdout, byte[] stderr, String output) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
            this.exitValue = exitValue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
            this.stdout = stdout;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
            this.stderr = stderr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
            this.output = output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
        InputStream stdoutAsStream() { return new ByteArrayInputStream(stdout); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
        Result assertSuccess() { assertTrue(exitValue == 0, output); return this; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
        Result assertFailure() { assertTrue(exitValue != 0, output); return this; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
        Result resultChecker(IOConsumer<Result> r) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
            try {  r.accept(this); return this; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
            catch (IOException x) { throw new UncheckedIOException(x); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
        Result resultChecker(FailCheckerWithMessage c) { c.accept(this); return this; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
    interface IOConsumer<T> { void accept(T t) throws IOException ;  }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
    // readAllBytes implementation so the test can be run pre 1.9 ( legacyOnly )
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
    static byte[] readAllBytes(InputStream is) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
        byte[] buf = new byte[8192];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
        int capacity = buf.length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
        int nread = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        int n;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
        for (;;) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
            // read to EOF which may read more or less than initial buffer size
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
            while ((n = is.read(buf, nread, capacity - nread)) > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
                nread += n;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
            // if the last call to read returned -1, then we're done
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
            if (n < 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
            // need to allocate a larger buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
            capacity = capacity << 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
            buf = Arrays.copyOf(buf, capacity);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
        return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
    // Standalone entry point for running with, possibly older, JDKs.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
    public static void main(String[] args) throws Throwable {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
        boolean legacyOnly = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
        if (args.length != 0 && args[0].equals("legacyOnly"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
            legacyOnly = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
        CLICompatibility test = new CLICompatibility(legacyOnly);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
        for (Method m : CLICompatibility.class.getDeclaredMethods()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
            if (m.getAnnotation(Test.class) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
                System.out.println("Invoking " + m.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
                m.invoke(test);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
    CLICompatibility(boolean legacyOnly) { this.legacyOnly = legacyOnly; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
    CLICompatibility() { this.legacyOnly = false; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
}