langtools/test/jdk/jshell/ToolBasicTest.java
author rfield
Fri, 06 Jan 2017 10:31:25 -0800
changeset 43038 7b8b8750a78e
parent 42972 47ca49eee534
child 43856 fcdebb803c62
permissions -rw-r--r--
8165405: jshell tool: /classpath is inconsistent 8172103: JShell: crash in TaskFactory$WrapSourceHandler.diag Reviewed-by: jlahoda
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
/*
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
     2
 * Copyright (c) 2015, 2016, 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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
/*
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
 * @test
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
    26
 * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36718
diff changeset
    27
 * @summary Tests for Basic tests for REPL tool
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.main
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
    30
 *          jdk.jdeps/com.sun.tools.javap
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
    31
 *          jdk.jshell/jdk.internal.jshell.tool
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
    32
 * @library /tools/lib
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36718
diff changeset
    33
 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36718
diff changeset
    34
 * @build KullaTesting TestingInputStream Compiler
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34570
diff changeset
    35
 * @run testng/timeout=600 ToolBasicTest
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import java.io.PrintWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import java.io.StringWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import java.nio.file.Files;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import java.nio.file.Path;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import java.nio.file.Paths;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import java.util.ArrayList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import java.util.Arrays;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import java.util.List;
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36778
diff changeset
    47
import java.util.Locale;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import java.util.Scanner;
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
    49
import java.util.function.BiFunction;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import java.util.function.Consumer;
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
    51
import java.util.function.Function;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import java.util.stream.Collectors;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import java.util.stream.Stream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
import org.testng.annotations.Test;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import static org.testng.Assert.assertEquals;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import static org.testng.Assert.assertTrue;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
import static org.testng.Assert.fail;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
@Test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
public class ToolBasicTest extends ReplToolTesting {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
    public void elideStartUpFromList() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
        test(
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
    66
                (a) -> assertCommandOutputContains(a, "123", "==> 123"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
                (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
                    int cnt;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
                    try (Scanner scanner = new Scanner(s)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
                        cnt = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
                        while (scanner.hasNextLine()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
                            String line = scanner.nextLine();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
                            if (!line.trim().isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
                                ++cnt;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
                            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
                    assertEquals(cnt, 1, "Expected only one listed line");
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
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
    public void elideStartUpFromSave() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
        Path path = compiler.getPath("myfile");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
        test(
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
    87
                (a) -> assertCommandOutputContains(a, "123", "==> 123"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
                (a) -> assertCommand(a, "/save " + path.toString(), "")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
        try (Stream<String> lines = Files.lines(path)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
            assertEquals(lines.count(), 1, "Expected only one saved line");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
    public void testInterrupt() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
        ReplTest interrupt = (a) -> assertCommand(a, "\u0003", "");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
        for (String s : new String[] { "", "\u0003" }) {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
    98
            test(false, new String[]{"--no-startup"},
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
                    (a) -> assertCommand(a, "int a = 2 +" + s, ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   100
                    interrupt,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
                    (a) -> assertCommand(a, "int a\u0003", ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
                    (a) -> assertCommand(a, "int a = 2 + 2\u0003", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   103
                    (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
                    (a) -> evaluateExpression(a, "int", "2", "2"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   105
                    (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
                    (a) -> assertCommand(a, "void f() {", ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   107
                    (a) -> assertCommand(a, "int q = 10;" + s, ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   108
                    interrupt,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   109
                    (a) -> assertCommand(a, "void f() {}\u0003", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   110
                    (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
                    (a) -> assertMethod(a, "int f() { return 0; }", "()int", "f"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   112
                    (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
                    (a) -> assertCommand(a, "class A {" + s, ""),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   114
                    interrupt,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   115
                    (a) -> assertCommand(a, "class A {}\u0003", ""),
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 37389
diff changeset
   116
                    (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
                    (a) -> assertClass(a, "interface A {}", "interface", "A"),
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 37389
diff changeset
   118
                    (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   119
                    (a) -> assertCommand(a, "import java.util.stream." + s, ""),
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   120
                    interrupt,
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   121
                    (a) -> assertCommand(a, "import java.util.stream.\u0003", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   122
                    (a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   123
                    (a) -> assertImport(a, "import java.util.stream.Stream", "", "java.util.stream.Stream"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   124
                    (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   126
        }
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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   129
    private final Object lock = new Object();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   130
    private PrintWriter out;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
    private boolean isStopped;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   132
    private Thread t;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
    private void assertStop(boolean after, String cmd, String output) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
        if (!after) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   135
            isStopped = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
            StringWriter writer = new StringWriter();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
            out = new PrintWriter(writer);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
            setCommandInput(cmd + "\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
            t = new Thread(() -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
                try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
                    // no chance to know whether cmd is being evaluated
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
                    Thread.sleep(5000);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   143
                } catch (InterruptedException ignored) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
                int i = 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
                int n = 30;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
                synchronized (lock) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   148
                    do {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
                        setCommandInput("\u0003");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
                        if (!isStopped) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
                            out.println("Not stopped. Try again: " + i);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
                            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   153
                                lock.wait(1000);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
                            } catch (InterruptedException ignored) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
                            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   156
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   157
                    } while (i++ < n && !isStopped);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   158
                    if (!isStopped) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
                        System.err.println(writer.toString());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   160
                        fail("Evaluation was not stopped: '" + cmd + "'");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
            });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
            t.start();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
            synchronized (lock)  {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
                out.println("Evaluation was stopped successfully: '" + cmd + "'");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
                isStopped = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
                lock.notify();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   170
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   171
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   172
                t.join();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
                t = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
            } catch (InterruptedException ignored) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
            assertOutput(getCommandOutput(), "", "command");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
            assertOutput(getCommandErrorOutput(), "", "command error");
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   178
            assertOutput(getUserOutput().trim(), output, "user");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
            assertOutput(getUserErrorOutput(), "", "user error");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
    public void testStop() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        test(
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   185
                (a) -> assertStop(a, "while (true) {}", ""),
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   186
                (a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
    public void testRerun() {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   191
        test(false, new String[] {"--no-startup"},
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   192
                (a) -> assertCommand(a, "/0", "|  No such command or snippet id: /0\n|  Type /help for help."),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   193
                (a) -> assertCommand(a, "/5", "|  No such command or snippet id: /5\n|  Type /help for help.")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
        String[] codes = new String[] {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
                "int a = 0;", // var
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
                "class A {}", // class
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
                "void f() {}", // method
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
                "bool b;", // active failed
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   200
                "void g() { h(); }", // active corralled
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
        List<ReplTest> tests = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
        for (String s : codes) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
            tests.add((a) -> assertCommand(a, s, null));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
        }
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   206
        // Test /1 through /5 -- assure references are correct
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
        for (int i = 0; i < codes.length; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
            final int finalI = i;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
            Consumer<String> check = (s) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
                String[] ss = s.split("\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
                assertEquals(ss[0], codes[finalI]);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
                assertTrue(ss.length > 1, s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   213
            };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
            tests.add((a) -> assertCommandCheckOutput(a, "/" + (finalI + 1), check));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   215
        }
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   216
        // Test /-1 ... note that the snippets added by history must be stepped over
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   217
        for (int i = 0; i < codes.length; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   218
            final int finalI = i;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   219
            Consumer<String> check = (s) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   220
                String[] ss = s.split("\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   221
                assertEquals(ss[0], codes[codes.length - finalI - 1]);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   222
                assertTrue(ss.length > 1, s);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   223
            };
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   224
            tests.add((a) -> assertCommandCheckOutput(a, "/-" + (2 * finalI + 1), check));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   225
        }
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   226
        tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("int a = 0;")));
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   227
        test(false, new String[]{"--no-startup"},
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   228
                tests.toArray(new ReplTest[tests.size()]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   231
    public void test8142447() {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   232
        Function<String, BiFunction<String, Integer, ReplTest>> assertRerun = cmd -> (code, assertionCount) ->
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   233
                (a) -> assertCommandCheckOutput(a, cmd, s -> {
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   234
                            String[] ss = s.split("\n");
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   235
                            assertEquals(ss[0], code);
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   236
                            loadVariable(a, "int", "assertionCount", Integer.toString(assertionCount), Integer.toString(assertionCount));
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   237
                        });
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   238
        ReplTest assertVariables = (a) -> assertCommandCheckOutput(a, "/v", assertVariables());
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   239
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   240
        Compiler compiler = new Compiler();
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   241
        Path startup = compiler.getPath("StartupFileOption/startup.txt");
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   242
        compiler.writeToFile(startup, "int assertionCount = 0;\n" + // id: s1
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   243
                "void add(int n) { assertionCount += n; }");
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   244
        test(new String[]{"--startup", startup.toString()},
34477
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   245
                (a) -> assertCommand(a, "add(1)", ""), // id: 1
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   246
                (a) -> assertCommandCheckOutput(a, "add(ONE)", s -> assertEquals(s.split("\n")[0], "|  Error:")), // id: e1
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   247
                (a) -> assertVariable(a, "int", "ONE", "1", "1"),
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   248
                assertRerun.apply("/1").apply("add(1)", 2), assertVariables,
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   249
                assertRerun.apply("/e1").apply("add(ONE)", 3), assertVariables,
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   250
                assertRerun.apply("/s1").apply("int assertionCount = 0;", 0), assertVariables
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   251
        );
64001b0533a2 8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents: 34475
diff changeset
   252
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   253
        test(false, new String[] {"--no-startup"},
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   254
                (a) -> assertCommand(a, "/s1", "|  No such command or snippet id: /s1\n|  Type /help for help."),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   255
                (a) -> assertCommand(a, "/1", "|  No such command or snippet id: /1\n|  Type /help for help."),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   256
                (a) -> assertCommand(a, "/e1", "|  No such command or snippet id: /e1\n|  Type /help for help.")
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   257
        );
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   258
    }
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   259
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
    public void testClasspathDirectory() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
        Path outDir = Paths.get("testClasspathDirectory");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
        compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
        Path classpath = compiler.getPath(outDir);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
        test(
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   266
                (a) -> assertCommand(a, "/env --class-path " + classpath,
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   267
                        "|  Setting new options and restoring state."),
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   268
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
        );
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   270
        test(new String[] { "--class-path", classpath.toString() },
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   271
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   273
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   274
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
    public void testClasspathJar() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   276
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   277
        Path outDir = Paths.get("testClasspathJar");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   278
        compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   279
        String jarName = "test.jar";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   280
        compiler.jar(outDir, jarName, "pkg/A.class");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   281
        Path jarPath = compiler.getPath(outDir).resolve(jarName);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   282
        test(
43038
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   283
                (a) -> assertCommand(a, "/env --class-path " + jarPath,
7b8b8750a78e 8165405: jshell tool: /classpath is inconsistent
rfield
parents: 42972
diff changeset
   284
                        "|  Setting new options and restoring state."),
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   285
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
        );
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   287
        test(new String[] { "--class-path", jarPath.toString() },
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   288
                (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   290
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   291
41852
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   292
    public void testModulePath() {
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   293
        Compiler compiler = new Compiler();
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   294
        Path modsDir = Paths.get("mods");
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   295
        Path outDir = Paths.get("mods", "org.astro");
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   296
        compiler.compile(outDir, "package org.astro; public class World { public static String name() { return \"world\"; } }");
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   297
        compiler.compile(outDir, "module org.astro { exports org.astro; }");
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   298
        Path modsPath = compiler.getPath(modsDir);
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   299
        test(new String[] { "--module-path", modsPath.toString(), "--add-modules", "org.astro" },
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   300
                (a) -> assertCommand(a, "import org.astro.World;", ""),
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   301
                (a) -> evaluateExpression(a, "String",
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   302
                        "String.format(\"Greetings %s!\", World.name());",
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   303
                        "\"Greetings world!\"")
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   304
        );
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   305
    }
448273b190ad 8166649: jshell tool: missing --add-modules and --module-path
rfield
parents: 41635
diff changeset
   306
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   307
    public void testStartupFileOption() {
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   308
        Compiler compiler = new Compiler();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   309
        Path startup = compiler.getPath("StartupFileOption/startup.txt");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   310
        compiler.writeToFile(startup, "class A { public String toString() { return \"A\"; } }");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   311
        test(new String[]{"--startup", startup.toString()},
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   312
                (a) -> evaluateExpression(a, "A", "new A()", "A")
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   313
        );
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   314
        test(new String[]{"--no-startup"},
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   315
                (a) -> assertCommandCheckOutput(a, "Pattern.compile(\"x+\")", assertStartsWith("|  Error:\n|  cannot find symbol"))
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   316
        );
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   317
        test(
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   318
                (a) -> assertCommand(a, "Pattern.compile(\"x+\")", "$1 ==> x+", "", null, "", "")
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   319
        );
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   320
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   321
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   322
    public void testLoadingFromArgs() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   323
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   324
        Path path = compiler.getPath("loading.repl");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   325
        compiler.writeToFile(path, "int a = 10; double x = 20; double a = 10;");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   326
        test(new String[] { path.toString() },
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   327
                (a) -> assertCommand(a, "x", "x ==> 20.0"),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   328
                (a) -> assertCommand(a, "a", "a ==> 10.0")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   329
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   330
        Path unknown = compiler.getPath("UNKNOWN.jar");
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36778
diff changeset
   331
        test(Locale.ROOT, true, new String[]{unknown.toString()},
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   332
                "|  File '" + unknown
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   333
                + "' for 'jshell' is not found.");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   334
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   335
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   336
    public void testReset() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   337
        test(
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   338
                (a) -> assertReset(a, "/res"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   339
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   340
                (a) -> assertVariable(a, "int", "x"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   341
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   342
                (a) -> assertMethod(a, "void f() { }", "()void", "f"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   343
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   344
                (a) -> assertClass(a, "class A { }", "class", "A"),
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 37389
diff changeset
   345
                (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   346
                (a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   347
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports()),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   348
                (a) -> assertReset(a, "/reset"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   349
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   350
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 37389
diff changeset
   351
                (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   352
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   353
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   354
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   355
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   356
    public void testOpen() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   357
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   358
        Path path = compiler.getPath("testOpen.repl");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   359
        compiler.writeToFile(path,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   360
                "int a = 10;\ndouble x = 20;\ndouble a = 10;\n" +
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   361
                        "class A { public String toString() { return \"A\"; } }\nimport java.util.stream.*;");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   362
        for (String s : new String[]{"/o", "/open"}) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   363
            test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   364
                    (a) -> assertCommand(a, s + " " + path.toString(), ""),
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   365
                    (a) -> assertCommand(a, "a", "a ==> 10.0"),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   366
                    (a) -> evaluateExpression(a, "A", "new A();", "A"),
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   367
                    (a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   368
                    (a) -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   369
                        loadVariable(a, "double", "x", "20.0", "20.0");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   370
                        loadVariable(a, "double", "a", "10.0", "10.0");
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   371
                        loadVariable(a, "A", "$7", "new A();", "A");
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   372
                        loadVariable(a, "long", "$8", "Stream.of(\"A\").count();", "1");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   373
                        loadClass(a, "class A { public String toString() { return \"A\"; } }",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   374
                                "class", "A");
33714
8064f484590e 8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents: 33362
diff changeset
   375
                        loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*");
38514
f7df9ab653b0 8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents: 37389
diff changeset
   376
                        assertCommandCheckOutput(a, "/types", assertClasses());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   377
                    },
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   378
                    (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   379
                    (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   380
                    (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   381
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   382
            Path unknown = compiler.getPath("UNKNOWN.repl");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   383
            test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   384
                    (a) -> assertCommand(a, s + " " + unknown,
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   385
                            "|  File '" + unknown + "' for '/open' is not found.")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   386
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   387
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   388
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   389
42972
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   390
    public void testOpenResource() {
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   391
        test(
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   392
                (a) -> assertCommand(a, "/open PRINTING", ""),
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   393
                (a) -> assertCommandOutputContains(a, "/list",
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   394
                        "void println", "System.out.printf"),
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   395
                (a) -> assertCommand(a, "printf(\"%4.2f\", Math.PI)",
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   396
                        "", "", null, "3.14", "")
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   397
        );
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   398
    }
47ca49eee534 8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents: 42843
diff changeset
   399
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   400
    public void testSave() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   401
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   402
        Path path = compiler.getPath("testSave.repl");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   403
        List<String> list = Arrays.asList(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   404
                "int a;",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   405
                "class A { public String toString() { return \"A\"; } }"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   406
        );
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   407
        test(
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   408
                (a) -> assertVariable(a, "int", "a"),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   409
                (a) -> assertCommand(a, "()", null, null, null, "", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   410
                (a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   411
                (a) -> assertCommand(a, "/save " + path.toString(), "")
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   412
        );
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   413
        assertEquals(Files.readAllLines(path), list);
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   414
        {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   415
            List<String> output = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   416
            test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   417
                    (a) -> assertCommand(a, "int a;", null),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   418
                    (a) -> assertCommand(a, "()", null, null, null, "", ""),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   419
                    (a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
38520
17e72b872ffd 8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents: 38514
diff changeset
   420
                    (a) -> assertCommandCheckOutput(a, "/list -all", (out) ->
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   421
                            output.addAll(Stream.of(out.split("\n"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   422
                                    .filter(str -> !str.isEmpty())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   423
                                    .map(str -> str.substring(str.indexOf(':') + 2))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   424
                                    .filter(str -> !str.startsWith("/"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   425
                                    .collect(Collectors.toList()))),
38520
17e72b872ffd 8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents: 38514
diff changeset
   426
                    (a) -> assertCommand(a, "/save -all " + path.toString(), "")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   427
            );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   428
            assertEquals(Files.readAllLines(path), output);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   429
        }
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   430
        List<String> output = new ArrayList<>();
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   431
        test(
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   432
                (a) -> assertVariable(a, "int", "a"),
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   433
                (a) -> assertCommand(a, "()", null, null, null, "", ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   434
                (a) -> assertClass(a, "class A { public String toString() { return \"A\"; } }", "class", "A"),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   435
                (a) -> assertCommandCheckOutput(a, "/history", (out) ->
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   436
                        output.addAll(Stream.of(out.split("\n"))
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   437
                                .filter(str -> !str.isEmpty())
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   438
                                .collect(Collectors.toList()))),
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   439
                (a) -> assertCommand(a, "/save -history " + path.toString(), "")
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   440
        );
38613
2a8e50869b08 8157953: JShell tests: reenable ToolBasicTest
rfield
parents: 38531
diff changeset
   441
        output.add("/save -history " + path.toString());
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   442
        assertEquals(Files.readAllLines(path), output);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   443
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   444
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   445
    public void testStartRetain() {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   446
        Compiler compiler = new Compiler();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   447
        Path startUpFile = compiler.getPath("startUp.txt");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   448
        test(
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   449
                (a) -> assertVariable(a, "int", "a"),
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   450
                (a) -> assertVariable(a, "double", "b", "10", "10.0"),
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   451
                (a) -> assertMethod(a, "void f() {}", "()V", "f"),
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   452
                (a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   453
                (a) -> assertCommand(a, "/save " + startUpFile.toString(), null),
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   454
                (a) -> assertCommand(a, "/set start -retain " + startUpFile.toString(), null)
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   455
        );
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   456
        Path unknown = compiler.getPath("UNKNOWN");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   457
        test(
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   458
                (a) -> assertCommandOutputStartsWith(a, "/set start -retain " + unknown.toString(),
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   459
                        "|  File '" + unknown + "' for '/set start' is not found.")
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   460
        );
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   461
        test(false, new String[0],
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   462
                (a) -> {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   463
                    loadVariable(a, "int", "a");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   464
                    loadVariable(a, "double", "b", "10.0", "10.0");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   465
                    loadMethod(a, "void f() {}", "()void", "f");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   466
                    loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   467
                    assertCommandCheckOutput(a, "/types", assertClasses());
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   468
                },
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   469
                (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   470
                (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   471
                (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 41858
diff changeset
   472
        );
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   473
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   474
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   475
    public void testStartSave() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   476
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   477
        Path startSave = compiler.getPath("startSave.txt");
38520
17e72b872ffd 8157185: jshell tool: ambiguous format -- distinguished arguments should be options
rfield
parents: 38514
diff changeset
   478
        test(a -> assertCommand(a, "/save -start " + startSave.toString(), null));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   479
        List<String> lines = Files.lines(startSave)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   480
                .filter(s -> !s.isEmpty())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   481
                .collect(Collectors.toList());
34570
8a8f52a733dd 8144095: jshell tool: normalize command parameter handling
rfield
parents: 34481
diff changeset
   482
        assertEquals(lines, START_UP);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   483
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   484
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   485
    public void testConstrainedUpdates() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   486
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   487
                a -> assertClass(a, "class XYZZY { }", "class", "XYZZY"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   488
                a -> assertVariable(a, "XYZZY", "xyzzy"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   489
                a -> assertCommandCheckOutput(a, "import java.util.stream.*",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   490
                        (out) -> assertTrue(out.trim().isEmpty(), "Expected no output, got: " + out))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   491
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   492
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   493
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   494
    public void testRemoteExit() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   495
        test(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   496
                a -> assertVariable(a, "int", "x"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   497
                a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   498
                a -> assertCommandOutputContains(a, "System.exit(5);", "terminated"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   499
                a -> assertCommandCheckOutput(a, "/vars", s ->
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   500
                        assertTrue(s.trim().isEmpty(), s)),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   501
                a -> assertMethod(a, "void f() { }", "()void", "f"),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   502
                a -> assertCommandCheckOutput(a, "/methods", assertMethods())
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   503
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   504
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   505
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   506
    public void testFeedbackNegative() {
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   507
        test(a -> assertCommandCheckOutput(a, "/set feedback aaaa",
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   508
                assertStartsWith("|  Does not match any current feedback mode")));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   509
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   510
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   511
    public void testFeedbackSilent() {
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   512
        for (String off : new String[]{"s", "silent"}) {
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   513
            test(
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   514
                    a -> assertCommand(a, "/set feedback " + off, ""),
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   515
                    a -> assertCommand(a, "int a", ""),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   516
                    a -> assertCommand(a, "void f() {}", ""),
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   517
                    a -> assertCommandCheckOutput(a, "aaaa", assertStartsWith("|  Error:")),
41858
5843b57ce3a6 8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents: 41852
diff changeset
   518
                    a -> assertCommandCheckOutput(a, "static void f() {}", assertStartsWith("|  Warning:"))
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   519
            );
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   520
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   521
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   522
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   523
    public void testFeedbackNormal() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   524
        Compiler compiler = new Compiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   525
        Path testNormalFile = compiler.getPath("testConciseNormal");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   526
        String[] sources = new String[] {"int a", "void f() {}", "class A {}", "a = 10"};
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   527
        String[] sources2 = new String[] {"int a //again", "void f() {int y = 4;}", "class A {} //again", "a = 10"};
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   528
        String[] output = new String[] {
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   529
                "a ==> 0",
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   530
                "|  created method f()",
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   531
                "|  created class A",
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   532
                "a ==> 10"
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   533
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   534
        compiler.writeToFile(testNormalFile, sources2);
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   535
        for (String feedback : new String[]{"/set fe", "/set feedback"}) {
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   536
            for (String feedbackState : new String[]{"n", "normal"}) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   537
                test(
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   538
                        a -> assertCommand(a, feedback + " " + feedbackState, "|  Feedback mode: normal"),
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   539
                        a -> assertCommand(a, sources[0], output[0]),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   540
                        a -> assertCommand(a, sources[1], output[1]),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   541
                        a -> assertCommand(a, sources[2], output[2]),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   542
                        a -> assertCommand(a, sources[3], output[3]),
36494
4175f47b2a50 8148316: jshell tool: Configurable output format
rfield
parents: 35812
diff changeset
   543
                        a -> assertCommand(a, "/o " + testNormalFile.toString(), "")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   544
                );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   545
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   546
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   547
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   548
40516
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 38836
diff changeset
   549
    public void testVarsWithNotActive() {
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 38836
diff changeset
   550
        test(
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 38836
diff changeset
   551
                a -> assertVariable(a, "Blath", "x"),
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 38836
diff changeset
   552
                a -> assertCommandOutputContains(a, "/var -all", "(not-active)")
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 38836
diff changeset
   553
        );
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 38836
diff changeset
   554
    }
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 38836
diff changeset
   555
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   556
    public void testHistoryReference() {
40588
b5c32bfa9710 8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents: 40516
diff changeset
   557
        test(false, new String[]{"--no-startup"},
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   558
                a -> assertCommand(a, "System.err.println(1)", "", "", null, "", "1\n"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   559
                a -> assertCommand(a, "System.err.println(2)", "", "", null, "", "2\n"),
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   560
                a -> assertCommand(a, "/-2", "System.err.println(1)", "", null, "", "1\n"),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   561
                a -> assertCommand(a, "/history",
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   562
                                                    "/debug 0\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   563
                                                    "System.err.println(1)\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   564
                                                    "System.err.println(2)\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   565
                                                    "System.err.println(1)\n" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   566
                                                    "/history\n"),
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   567
                a -> assertCommand(a, "/-2", "System.err.println(2)", "", null, "", "2\n"),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   568
                a -> assertCommand(a, "/!", "System.err.println(2)", "", null, "", "2\n"),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   569
                a -> assertCommand(a, "/2", "System.err.println(2)", "", null, "", "2\n"),
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37007
diff changeset
   570
                a -> assertCommand(a, "/1", "System.err.println(1)", "", null, "", "1\n")
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   571
        );
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   572
    }
34475
7af94fd75ede 8143037: JShell should determine commands by prefix
jlahoda
parents: 33918
diff changeset
   573
38836
b09d1cfbf28c 8131029: JShell: recover from VMConnection launch failure
rfield
parents: 38613
diff changeset
   574
    @Test(enabled = false) // TODO 8158197
38531
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   575
    public void testHeadlessEditPad() {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   576
        String prevHeadless = System.getProperty("java.awt.headless");
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   577
        try {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   578
            System.setProperty("java.awt.headless", "true");
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   579
            test(
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   580
                (a) -> assertCommandOutputStartsWith(a, "/edit printf", "|  Cannot launch editor -- unexpected exception:")
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   581
            );
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   582
        } finally {
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   583
            System.setProperty("java.awt.headless", prevHeadless==null? "false" : prevHeadless);
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   584
        }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   585
    }
c449daa25b45 8157200: jshell tool: Add /retain command to persist settings
rfield
parents: 38520
diff changeset
   586
41248
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   587
    public void testAddExports() {
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   588
        test(false, new String[]{"--no-startup"},
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   589
                a -> assertCommandOutputStartsWith(a, "import jdk.internal.misc.VM;", "|  Error:")
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   590
        );
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   591
        test(false, new String[]{"--no-startup",
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   592
            "-R--add-exports", "-Rjava.base/jdk.internal.misc=ALL-UNNAMED",
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   593
            "-C--add-exports", "-Cjava.base/jdk.internal.misc=ALL-UNNAMED"},
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   594
                a -> assertImport(a, "import jdk.internal.misc.VM;", "", "jdk.internal.misc.VM"),
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   595
                a -> assertCommand(a, "System.err.println(VM.isBooted())", "", "", null, "", "true\n")
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   596
        );
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   597
        test(false, new String[]{"--no-startup", "--add-exports", "java.base/jdk.internal.misc"},
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   598
                a -> assertImport(a, "import jdk.internal.misc.VM;", "", "jdk.internal.misc.VM"),
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   599
                a -> assertCommand(a, "System.err.println(VM.isBooted())", "", "", null, "", "true\n")
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   600
        );
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   601
    }
2a3e74c5ad8a 8154714: jshell tool: add exports support
shinyafox
parents: 40588
diff changeset
   602
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   603
}