1 /*
2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.util.ServiceLoader;
25 import javax.tools.Tool;
26 import org.testng.annotations.Test;
27 import static org.testng.Assert.assertTrue;
28 
29 /*
30  * @test
31  * @bug 8170044 8171343 8179856 8185840 8190383
32  * @summary Test ServiceLoader launching of jshell tool
33  * @modules jdk.compiler/com.sun.tools.javac.api
34  *          jdk.compiler/com.sun.tools.javac.main
35  *          jdk.jdeps/com.sun.tools.javap
36  *          jdk.jshell/jdk.internal.jshell.tool
37  * @library /tools/lib
38  * @build Compiler toolbox.ToolBox
39  * @run testng ToolProviderTest
40  */
41 @Test
42 public class ToolProviderTest extends StartOptionTest {
43 
44     // Through the provider, the console and console go to command out (we assume,
45     // because it works with the current tests) that console and user output are
46     // after command out.
47     @Override
startExCoUoCeCn(int expectedExitCode, String expectedCmdOutput, String expectedUserOutput, String expectedError, String expectedConsole, String... args)48     protected void startExCoUoCeCn(int expectedExitCode,
49             String expectedCmdOutput,
50             String expectedUserOutput,
51             String expectedError,
52             String expectedConsole,
53             String... args) {
54         super.startExCoUoCeCn(expectedExitCode,
55                 (expectedCmdOutput  == null? "" : expectedCmdOutput) +
56                 (expectedConsole    == null? "" : expectedConsole) +
57                 (expectedUserOutput == null? "" : expectedUserOutput),
58                 null, expectedError, null, args);
59     }
60 
61     @Override
runShell(String... args)62     protected int runShell(String... args) {
63         ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class);
64         for (Tool provider : sl) {
65             if (provider.name().equals("jshell")) {
66                 return provider.run(cmdInStream, cmdout, cmderr, args);
67             }
68         }
69         throw new AssertionError("Repl tool not found by ServiceLoader: " + sl);
70     }
71 
72     // Test --show-version
73     @Override
testShowVersion()74     public void testShowVersion() {
75         startCo(s -> {
76             assertTrue(s.startsWith("jshell "), "unexpected version: " + s);
77             assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
78             assertTrue(s.trim().contains("jshell>"), "Expected prompt, got: " + s);
79         },
80                 "--show-version");
81     }
82 }
83