test/langtools/jdk/jshell/ToolProviderTest.java
author coleenp
Wed, 15 Nov 2017 08:14:31 -0500
changeset 47894 352b17f62ff7
parent 47504 58ce36f43f1a
child 47840 e0f08a49f3e3
permissions -rw-r--r--
8191315: Add serviceability/sa/TestRevPtrsForInvokeDynamic.java to ProblemList.txt Reviewed-by: dholmes, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
     1
/*
47504
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
     4
 *
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
     8
 *
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    13
 * accompanied this code).
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    14
 *
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    18
 *
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    21
 * questions.
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    22
 */
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    23
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    24
import java.io.ByteArrayInputStream;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    25
import java.io.ByteArrayOutputStream;
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    26
import java.io.InputStream;
47504
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
    27
import java.nio.file.Path;
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    28
import java.util.ServiceLoader;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    29
import java.util.function.Consumer;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    30
import javax.tools.Tool;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    31
import org.testng.annotations.BeforeMethod;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    32
import org.testng.annotations.Test;
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    33
import static org.testng.Assert.assertTrue;
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    34
import static org.testng.Assert.fail;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    35
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    36
/*
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    37
 * @test
47504
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
    38
 * @bug 8170044 8171343 8179856
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    39
 * @summary Test ServiceLoader launching of jshell tool
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    40
 * @modules jdk.compiler/com.sun.tools.javac.api
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    41
 *          jdk.compiler/com.sun.tools.javac.main
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    42
 *          jdk.jdeps/com.sun.tools.javap
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    43
 *          jdk.jshell/jdk.internal.jshell.tool
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    44
 * @library /tools/lib
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    45
 * @build Compiler toolbox.ToolBox
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    46
 * @run testng ToolProviderTest
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    47
 */
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    48
@Test
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    49
public class ToolProviderTest extends StartOptionTest {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    50
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    51
    private ByteArrayOutputStream cmdout;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    52
    private ByteArrayOutputStream cmderr;
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    53
    private InputStream cmdInStream;
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    54
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    55
    @BeforeMethod
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    56
    @Override
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    57
    public void setUp() {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    58
        cmdout = new ByteArrayOutputStream();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    59
        cmderr = new ByteArrayOutputStream();
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    60
        cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    61
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    62
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    63
    @Override
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    64
    protected void start(Consumer<String> checkCmdOutput,
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    65
            Consumer<String> checkUserOutput, Consumer<String> checkError,
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    66
            String... args) throws Exception {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    67
        if (runShellServiceLoader(args) != 0) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    68
            fail("Repl tool failed");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    69
        }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    70
        check(cmdout, checkCmdOutput, "cmdout");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    71
        check(cmderr, checkError, "cmderr");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    72
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    73
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    74
    private int runShellServiceLoader(String... args) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    75
        ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class);
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    76
        for (Tool provider : sl) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    77
            if (provider.name().equals("jshell")) {
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    78
                return provider.run(cmdInStream, cmdout, cmderr, args);
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    79
            }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    80
        }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    81
        throw new AssertionError("Repl tool not found by ServiceLoader: " + sl);
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    82
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    83
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    84
    @Override
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    85
    public void testCommandFile() throws Exception {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    86
        String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    87
        start("1 : String str = \"Hello \";" + "\n" + "Hello Hello", "", "--no-startup", fn, "-s");
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    88
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    89
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
    90
    @Override
43367
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    91
    public void testShowVersion() throws Exception {
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    92
        start(
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    93
                s -> {
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    94
                    assertTrue(s.startsWith("jshell "), "unexpected version: " + s);
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    95
                    assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    96
                    assertTrue(s.trim().contains("jshell>"), "Expected prompt, got: " + s);
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    97
                },
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    98
                null, null,
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
    99
                "--show-version");
7797472a9ed5 8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents: 42843
diff changeset
   100
    }
47504
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   101
    /**
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   102
     * Test that input is read with "-" and there is no extra output.
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   103
     * @throws Exception
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   104
     */
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   105
    @Override
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   106
    public void testHypenFile() throws Exception {
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   107
        cmdInStream = new ByteArrayInputStream("System.out.print(\"Hello\");\n".getBytes());
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   108
        start("Hello", "", "-");
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   109
        cmdInStream = new ByteArrayInputStream("System.out.print(\"Hello\");\n".getBytes());
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   110
        start("Hello", "", "-", "-");
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   111
        Compiler compiler = new Compiler();
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   112
        Path path = compiler.getPath("markload.jsh");
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   113
        compiler.writeToFile(path, "System.out.print(\"===\");");
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   114
        cmdInStream = new ByteArrayInputStream("System.out.print(\"Hello\");\n".getBytes());
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   115
        start("===Hello===", "", path.toString(), "-", path.toString());
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   116
    }
58ce36f43f1a 8179856: jshell tool: not suitable for pipeline use
rfield
parents: 47216
diff changeset
   117
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
diff changeset
   118
}