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