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