test/langtools/jdk/jshell/StartOptionTest.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 52938 5ff7480c9e28
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 48272
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    24
 /*
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    25
 * @test 8151754 8080883 8160089 8170162 8166581 8172102 8171343 8178023 8186708 8179856 8185840 8190383
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    26
 * @summary Testing startExCe-up options.
35359
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 33362
diff changeset
    27
 * @modules jdk.compiler/com.sun.tools.javac.api
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 33362
diff changeset
    28
 *          jdk.compiler/com.sun.tools.javac.main
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 35359
diff changeset
    29
 *          jdk.jdeps/com.sun.tools.javap
35359
f04501964016 8148147: Sync up @modules from jigsaw/jake
alanb
parents: 33362
diff changeset
    30
 *          jdk.jshell/jdk.internal.jshell.tool
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
 * @library /tools/lib
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36715
diff changeset
    32
 * @build Compiler toolbox.ToolBox
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
 * @run testng StartOptionTest
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
 */
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
    35
import java.io.ByteArrayInputStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import java.io.ByteArrayOutputStream;
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
    37
import java.io.InputStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import java.io.PrintStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import java.nio.charset.StandardCharsets;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import java.nio.file.Path;
41641
a628785b9dd9 8167637: jshell tool: /edit should use EDITOR setting
rfield
parents: 40767
diff changeset
    41
import java.util.HashMap;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36778
diff changeset
    42
import java.util.Locale;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import java.util.function.Consumer;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
44683
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 43367
diff changeset
    45
import java.util.logging.Level;
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 43367
diff changeset
    46
import java.util.logging.Logger;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
    47
import java.util.regex.Pattern;
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
    48
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import org.testng.annotations.AfterMethod;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import org.testng.annotations.BeforeMethod;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import org.testng.annotations.Test;
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    52
import jdk.jshell.tool.JavaShellToolBuilder;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import static org.testng.Assert.assertEquals;
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
    54
import static org.testng.Assert.assertFalse;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
import static org.testng.Assert.assertTrue;
42272
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41641
diff changeset
    56
import static org.testng.Assert.fail;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
@Test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
public class StartOptionTest {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    61
    protected ByteArrayOutputStream cmdout;
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    62
    protected ByteArrayOutputStream cmderr;
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    63
    protected ByteArrayOutputStream console;
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    64
    protected ByteArrayOutputStream userout;
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    65
    protected ByteArrayOutputStream usererr;
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    66
    protected InputStream cmdInStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    68
    private JavaShellToolBuilder builder() {
44683
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 43367
diff changeset
    69
        // turn on logging of launch failures
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 43367
diff changeset
    70
        Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL);
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    71
        return JavaShellToolBuilder
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    72
                .builder()
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    73
                .out(new PrintStream(cmdout), new PrintStream(console), new PrintStream(userout))
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    74
                .err(new PrintStream(cmderr), new PrintStream(usererr))
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    75
                .in(cmdInStream, null)
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    76
                .persistence(new HashMap<>())
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    77
                .env(new HashMap<>())
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    78
                .locale(Locale.ROOT);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    81
    protected int runShell(String... args) {
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    82
        try {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    83
            return builder()
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    84
                    .start(args);
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    85
        } catch (Exception ex) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    86
            fail("Repl tool died with exception", ex);
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    87
        }
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    88
        return -1; // for compiler
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    89
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    90
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
    91
    protected void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) {
38608
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
    92
        byte[] bytes = str.toByteArray();
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
    93
        str.reset();
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
    94
        String out = new String(bytes, StandardCharsets.UTF_8);
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
    95
        out = stripAnsi(out);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
    96
        out = out.replaceAll("[\r\n]+", "\n");
38608
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
    97
        if (checkOut != null) {
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
    98
            checkOut.accept(out);
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
    99
        } else {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   100
            assertEquals(out, "", label + ": Expected empty -- ");
38608
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   101
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   104
    protected void checkExit(int ec, Consumer<Integer> checkCode) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   105
        if (checkCode != null) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   106
            checkCode.accept(ec);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   107
        } else {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   108
            assertEquals(ec, 0, "Expected standard exit code (0), but found: " + ec);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   109
        }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   110
    }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   111
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   112
    // Start and check the resultant: exit code (Ex), command output (Co),
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   113
    // user output (Uo), command error (Ce), and console output (Cn)
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   114
    protected void startExCoUoCeCn(Consumer<Integer> checkExitCode,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   115
            Consumer<String> checkCmdOutput,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   116
            Consumer<String> checkUserOutput,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   117
            Consumer<String> checkError,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   118
            Consumer<String> checkConsole,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   119
            String... args) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   120
        int ec = runShell(args);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   121
        checkExit(ec, checkExitCode);
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   122
        check(cmdout, checkCmdOutput, "cmdout");
38608
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   123
        check(cmderr, checkError, "cmderr");
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   124
        check(console, checkConsole, "console");
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   125
        check(userout, checkUserOutput, "userout");
38608
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   126
        check(usererr, null, "usererr");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   127
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   128
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   129
    // Start with an exit code and command error check
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   130
    protected void startExCe(int eec, Consumer<String> checkError, String... args) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   131
        StartOptionTest.this.startExCoUoCeCn(
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   132
                (Integer ec) -> assertEquals((int) ec, eec,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   133
                        "Expected error exit code (" + eec + "), but found: " + ec),
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   134
                null, null, checkError, null, args);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   135
    }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   136
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   137
    // Start with a command output check
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   138
    protected void startCo(Consumer<String> checkCmdOutput, String... args) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   139
        StartOptionTest.this.startExCoUoCeCn(null, checkCmdOutput, null, null, null, args);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   140
    }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   141
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   142
    private Consumer<String> assertOrNull(String expected, String label) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   143
        return expected == null
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   144
                ? null
48272
128de6a38f8e 8191455: jdk/jshell/StartOptionTest.java and jdk/jshell/ToolProviderTest.java failed after changeset e0f08a
rfield
parents: 47840
diff changeset
   145
                : s -> assertEquals(s.replaceAll("\\r\\n?", "\n").trim(), expected.trim(), label);
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   146
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   147
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   148
    // Start and check the resultant: exit code (Ex), command output (Co),
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   149
    // user output (Uo), command error (Ce), and console output (Cn)
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   150
    protected void startExCoUoCeCn(int expectedExitCode,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   151
            String expectedCmdOutput,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   152
            String expectedUserOutput,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   153
            String expectedError,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   154
            String expectedConsole,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   155
            String... args) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   156
        startExCoUoCeCn(
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   157
                expectedExitCode == 0
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   158
                        ? null
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   159
                        : (Integer i) -> assertEquals((int) i, expectedExitCode,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   160
                        "Expected exit code (" + expectedExitCode + "), but found: " + i),
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   161
                assertOrNull(expectedCmdOutput, "cmdout: "),
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   162
                assertOrNull(expectedUserOutput, "userout: "),
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   163
                assertOrNull(expectedError, "cmderr: "),
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   164
                assertOrNull(expectedConsole, "console: "),
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   165
                args);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   168
    // Start with an expected exit code and command error
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   169
    protected void startExCe(int ec, String expectedError, String... args) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   170
        startExCoUoCeCn(ec, null, null, expectedError, null, args);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   171
    }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   172
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   173
    // Start with an expected command output
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   174
    protected void startCo(String expectedCmdOutput, String... args) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   175
        startExCoUoCeCn(0, expectedCmdOutput, null, null, null, args);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   176
    }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   177
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   178
    // Start with an expected user output
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   179
    protected void startUo(String expectedUserOutput, String... args) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   180
        startExCoUoCeCn(0, null, expectedUserOutput, null, null, args);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   181
    }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   182
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
    @BeforeMethod
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
    public void setUp() {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   185
        cmdout = new ByteArrayOutputStream();
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   186
        cmderr = new ByteArrayOutputStream();
38608
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   187
        console = new ByteArrayOutputStream();
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   188
        userout = new ByteArrayOutputStream();
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   189
        usererr = new ByteArrayOutputStream();
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   190
        setIn("/exit\n");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   193
    protected String writeToFile(String stuff) {
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   194
        Compiler compiler = new Compiler();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   195
        Path p = compiler.getPath("doit.repl");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   196
        compiler.writeToFile(p, stuff);
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   197
        return p.toString();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   198
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   199
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   200
    // Set the input from a String
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   201
    protected void setIn(String s) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   202
        cmdInStream = new ByteArrayInputStream(s.getBytes());
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   203
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42272
diff changeset
   204
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   205
    // Test load files
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   206
    public void testCommandFile() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   207
        String fn = writeToFile("String str = \"Hello \"\n" +
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   208
                "/list\n" +
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   209
                "System.out.println(str + str)\n" +
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   210
                "/exit\n");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   211
        startExCoUoCeCn(0,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   212
                "1 : String str = \"Hello \";\n",
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   213
                "Hello Hello",
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   214
                null,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   215
                null,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   216
                "--no-startup", fn, "-s");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   217
    }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   218
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   219
    // Test that the usage message is printed
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   220
    public void testUsage() {
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 48272
diff changeset
   221
        for (String opt : new String[]{"-?", "-h", "--help"}) {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   222
            startCo(s -> {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 38613
diff changeset
   223
                assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
47504
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   224
                assertTrue(s.startsWith("Usage:   jshell <option>..."), "Unexpect usage start: " + s);
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   225
                assertTrue(s.contains("--show-version"), "Expected help: " + s);
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   226
                assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   227
            }, opt);
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   228
        }
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   229
    }
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   230
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   231
    // Test the --help-extra message
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   232
    public void testHelpExtra() {
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   233
        for (String opt : new String[]{"-X", "--help-extra"}) {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   234
            startCo(s -> {
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   235
                assertTrue(s.split("\n").length >= 5, "Not enough help-extra lines: " + s);
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   236
                assertTrue(s.contains("--add-exports"), "Expected --add-exports: " + s);
48940
ca22f8cb0c9b 8198573: JShell: class replace loses instances
rfield
parents: 48543
diff changeset
   237
                assertTrue(s.contains("--execution"), "Expected --execution: " + s);
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   238
                assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   239
            }, opt);
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 38613
diff changeset
   240
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   243
    // Test handling of bogus options
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   244
    public void testUnknown() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   245
        startExCe(1, "Unknown option: u", "-unknown");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   246
        startExCe(1, "Unknown option: unknown", "--unknown");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   249
    // Test that input is read with "-" and there is no extra output.
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   250
    public void testHypenFile() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   251
        setIn("System.out.print(\"Hello\");\n");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   252
        startUo("Hello", "-");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   253
        setIn("System.out.print(\"Hello\");\n");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   254
        startUo("Hello", "-", "-");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   255
        String fn = writeToFile("System.out.print(\"===\");");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   256
        setIn("System.out.print(\"Hello\");\n");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   257
        startUo("===Hello===", fn, "-", fn);
47504
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   258
        // check that errors go to standard error
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   259
        setIn(") Foobar");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   260
        startExCe(0, s -> assertTrue(s.contains("illegal start of expression"),
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   261
                "cmderr: illegal start of expression"),
47504
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   262
                "-");
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   263
    }
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   264
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   265
    // Test that user specified exit codes are propagated
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   266
    public void testExitCode() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   267
        setIn("/exit 57\n");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   268
        startExCoUoCeCn(57, null, null, null, "-> /exit 57", "-s");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   269
        setIn("int eight = 8\n" +
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   270
                "/exit eight + \n" +
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   271
                " eight\n");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   272
        startExCoUoCeCn(16, null, null, null,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   273
                "-> int eight = 8\n" +
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   274
                "-> /exit eight + \n" +
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   275
                ">>  eight",
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   276
                "-s");
47504
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   277
    }
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   278
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   279
    // Test that non-existent load file sends output to stderr and does not startExCe (no welcome).
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   280
    public void testUnknownLoadFile() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   281
        startExCe(1, "File 'UNKNOWN' for 'jshell' is not found.", "UNKNOWN");
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38608
diff changeset
   282
    }
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38608
diff changeset
   283
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   284
    // Test bad usage of the --startup option
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   285
    public void testStartup() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   286
        String fn = writeToFile("");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   287
        startExCe(1, "Argument to startup missing.", "--startup");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   288
        startExCe(1, "Conflicting options: both --startup and --no-startup were used.", "--no-startup", "--startup", fn);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   289
        startExCe(1, "Conflicting options: both --startup and --no-startup were used.", "--startup", fn, "--no-startup");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   290
        startExCe(1, "Argument to startup missing.", "--no-startup", "--startup");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   291
    }
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   292
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   293
    // Test an option that causes the back-end to fail is propagated
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   294
    public void testStartupFailedOption() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   295
        startExCe(1, s -> assertTrue(s.contains("Unrecognized option: -hoge-foo-bar"), "cmderr: " + s),
44683
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 43367
diff changeset
   296
                "-R-hoge-foo-bar");
42272
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41641
diff changeset
   297
    }
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41641
diff changeset
   298
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   299
    // Test the use of non-existant files with the --startup option
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   300
    public void testStartupUnknown() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   301
        startExCe(1, "File 'UNKNOWN' for '--startup' is not found.", "--startup", "UNKNOWN");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   302
        startExCe(1, "File 'UNKNOWN' for '--startup' is not found.", "--startup", "DEFAULT", "--startup", "UNKNOWN");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   303
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   304
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   305
    // Test bad usage of --class-path option
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   306
    public void testClasspath() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   307
        for (String cp : new String[]{"--class-path"}) {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   308
            startExCe(1, "Only one --class-path option may be used.", cp, ".", "--class-path", ".");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   309
            startExCe(1, "Argument to class-path missing.", cp);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   311
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   312
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   313
    // Test bogus module on --add-modules option
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   314
    public void testUnknownModule() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   315
        startExCe(1, s -> assertTrue(s.contains("rror") && s.contains("unKnown"), "cmderr: " + s),
44683
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 43367
diff changeset
   316
                "--add-modules", "unKnown");
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 43367
diff changeset
   317
    }
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 43367
diff changeset
   318
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   319
    // Test that muliple feedback options fail
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   320
    public void testFeedbackOptionConflict() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   321
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.",
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 38613
diff changeset
   322
                "--feedback", "concise", "--feedback", "verbose");
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   323
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-s");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   324
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "verbose", "-q");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   325
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-v");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   326
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "--feedback", "concise");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   327
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-v");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   328
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-s", "-v");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   329
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "-q");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   330
        startExCe(1, "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-s");
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 38613
diff changeset
   331
    }
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 38613
diff changeset
   332
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   333
    // Test bogus arguments to the --feedback option
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   334
    public void testNegFeedbackOption() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   335
        startExCe(1, "Argument to feedback missing.", "--feedback");
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   336
        startExCe(1, "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp");
37745
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 36993
diff changeset
   337
    }
4b6b59f8e327 8150382: JShell API: Allow setting remote JVM arguments
rfield
parents: 36993
diff changeset
   338
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   339
    // Test --version
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   340
    public void testVersion() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   341
        startCo(s -> {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   342
            assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   343
            assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   344
        },
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   345
                "--version");
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   346
    }
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   347
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   348
    // Test --show-version
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   349
    public void testShowVersion() {
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   350
        startExCoUoCeCn(null,
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   351
                s -> {
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   352
                    assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   353
                    assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   354
                },
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   355
                null,
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   356
                null,
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   357
                s -> assertTrue(s.trim().startsWith("jshell>"), "Expected prompt, got: " + s),
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   358
                "--show-version");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   359
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   360
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   361
    @AfterMethod
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   362
    public void tearDown() {
47840
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   363
        cmdout = null;
e0f08a49f3e3 8177076: jshell tool: allow non-zero /exit
rfield
parents: 47504
diff changeset
   364
        cmderr = null;
38608
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   365
        console = null;
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   366
        userout = null;
691b607bbcd6 8157917: JShell: shutdown could cause remote JDWP errors to be visible to users
rfield
parents: 37745
diff changeset
   367
        usererr = null;
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42972
diff changeset
   368
        cmdInStream = null;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   369
    }
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
   370
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
   371
    private static String stripAnsi(String str) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
   372
        if (str == null) return "";
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
   373
        return ANSI_CODE_PATTERN.matcher(str).replaceAll("");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
   374
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
   375
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 48940
diff changeset
   376
    public static final Pattern ANSI_CODE_PATTERN = Pattern.compile("\033\\[[\060-\077]*[\040-\057]*[\100-\176]");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   377
}