test/langtools/jdk/jshell/ToolProviderTest.java
changeset 47216 71c04702a3d5
parent 44683 610dc2b48954
child 47504 58ce36f43f1a
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2016, 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.io.ByteArrayInputStream;
       
    25 import java.io.ByteArrayOutputStream;
       
    26 import java.io.InputStream;
       
    27 import java.util.ServiceLoader;
       
    28 import java.util.function.Consumer;
       
    29 import javax.tools.Tool;
       
    30 import org.testng.annotations.BeforeMethod;
       
    31 import org.testng.annotations.Test;
       
    32 import static org.testng.Assert.assertTrue;
       
    33 import static org.testng.Assert.fail;
       
    34 
       
    35 /*
       
    36  * @test
       
    37  * @bug 8170044 8171343
       
    38  * @summary Test ServiceLoader launching of jshell tool
       
    39  * @modules jdk.compiler/com.sun.tools.javac.api
       
    40  *          jdk.compiler/com.sun.tools.javac.main
       
    41  *          jdk.jdeps/com.sun.tools.javap
       
    42  *          jdk.jshell/jdk.internal.jshell.tool
       
    43  * @library /tools/lib
       
    44  * @build Compiler toolbox.ToolBox
       
    45  * @run testng ToolProviderTest
       
    46  */
       
    47 @Test
       
    48 public class ToolProviderTest extends StartOptionTest {
       
    49 
       
    50     private ByteArrayOutputStream cmdout;
       
    51     private ByteArrayOutputStream cmderr;
       
    52     private InputStream cmdInStream;
       
    53 
       
    54     @BeforeMethod
       
    55     @Override
       
    56     public void setUp() {
       
    57         cmdout = new ByteArrayOutputStream();
       
    58         cmderr = new ByteArrayOutputStream();
       
    59         cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());
       
    60     }
       
    61 
       
    62     @Override
       
    63     protected void start(Consumer<String> checkCmdOutput,
       
    64             Consumer<String> checkUserOutput, Consumer<String> checkError,
       
    65             String... args) throws Exception {
       
    66         if (runShellServiceLoader(args) != 0) {
       
    67             fail("Repl tool failed");
       
    68         }
       
    69         check(cmdout, checkCmdOutput, "cmdout");
       
    70         check(cmderr, checkError, "cmderr");
       
    71     }
       
    72 
       
    73     private int runShellServiceLoader(String... args) {
       
    74         ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class);
       
    75         for (Tool provider : sl) {
       
    76             if (provider.name().equals("jshell")) {
       
    77                 return provider.run(cmdInStream, cmdout, cmderr, args);
       
    78             }
       
    79         }
       
    80         throw new AssertionError("Repl tool not found by ServiceLoader: " + sl);
       
    81     }
       
    82 
       
    83     @Override
       
    84     public void testCommandFile() throws Exception {
       
    85         String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
       
    86         start("1 : String str = \"Hello \";" + "\n" + "Hello Hello", "", "--no-startup", fn, "-s");
       
    87     }
       
    88 
       
    89     @Override
       
    90     public void testShowVersion() throws Exception {
       
    91         start(
       
    92                 s -> {
       
    93                     assertTrue(s.startsWith("jshell "), "unexpected version: " + s);
       
    94                     assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
       
    95                     assertTrue(s.trim().contains("jshell>"), "Expected prompt, got: " + s);
       
    96                 },
       
    97                 null, null,
       
    98                 "--show-version");
       
    99     }
       
   100 }