test/langtools/jdk/jshell/ToolProviderTest.java

Print this page




   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.nio.file.Path;
  28 import java.util.ServiceLoader;
  29 import java.util.function.Consumer;
  30 import javax.tools.Tool;
  31 import org.testng.annotations.BeforeMethod;
  32 import org.testng.annotations.Test;
  33 import static org.testng.Assert.assertTrue;
  34 import static org.testng.Assert.fail;
  35 
  36 /*
  37  * @test
  38  * @bug 8170044 8171343 8179856
  39  * @summary Test ServiceLoader launching of jshell tool
  40  * @modules jdk.compiler/com.sun.tools.javac.api
  41  *          jdk.compiler/com.sun.tools.javac.main
  42  *          jdk.jdeps/com.sun.tools.javap
  43  *          jdk.jshell/jdk.internal.jshell.tool
  44  * @library /tools/lib
  45  * @build Compiler toolbox.ToolBox
  46  * @run testng ToolProviderTest
  47  */
  48 @Test
  49 public class ToolProviderTest extends StartOptionTest {
  50 
  51     private ByteArrayOutputStream cmdout;
  52     private ByteArrayOutputStream cmderr;
  53     private InputStream cmdInStream;
  54 
  55     @BeforeMethod
  56     @Override
  57     public void setUp() {
  58         cmdout = new ByteArrayOutputStream();
  59         cmderr = new ByteArrayOutputStream();
  60         cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());







  61     }
  62 
  63     @Override
  64     protected void start(Consumer<String> checkCmdOutput,
  65             Consumer<String> checkUserOutput, Consumer<String> checkError,
  66             String... args) throws Exception {
  67         if (runShellServiceLoader(args) != 0) {
  68             fail("Repl tool failed");
  69         }
  70         check(cmdout, checkCmdOutput, "cmdout");
  71         check(cmderr, checkError, "cmderr");
  72     }
  73 
  74     private int runShellServiceLoader(String... args) {
  75         ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class);
  76         for (Tool provider : sl) {
  77             if (provider.name().equals("jshell")) {
  78                 return provider.run(cmdInStream, cmdout, cmderr, args);
  79             }
  80         }
  81         throw new AssertionError("Repl tool not found by ServiceLoader: " + sl);
  82     }
  83 

  84     @Override
  85     public void testCommandFile() throws Exception {
  86         String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
  87         start("1 : String str = \"Hello \";" + "\n" + "Hello Hello", "", "--no-startup", fn, "-s");
  88     }
  89 
  90     @Override
  91     public void testShowVersion() throws Exception {
  92         start(
  93                 s -> {
  94                     assertTrue(s.startsWith("jshell "), "unexpected version: " + s);
  95                     assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
  96                     assertTrue(s.trim().contains("jshell>"), "Expected prompt, got: " + s);
  97                 },
  98                 null, null,
  99                 "--show-version");
 100     }
 101     /**
 102      * Test that input is read with "-" and there is no extra output.
 103      * @throws Exception
 104      */
 105     @Override
 106     public void testHypenFile() throws Exception {
 107         cmdInStream = new ByteArrayInputStream("System.out.print(\"Hello\");\n".getBytes());
 108         start("Hello", "", "-");
 109         cmdInStream = new ByteArrayInputStream("System.out.print(\"Hello\");\n".getBytes());
 110         start("Hello", "", "-", "-");
 111         Compiler compiler = new Compiler();
 112         Path path = compiler.getPath("markload.jsh");
 113         compiler.writeToFile(path, "System.out.print(\"===\");");
 114         cmdInStream = new ByteArrayInputStream("System.out.print(\"Hello\");\n".getBytes());
 115         start("===Hello===", "", path.toString(), "-", path.toString());
 116     }
 117 
 118 }


   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
  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
  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
  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 }