< prev index next >

test/langtools/jdk/jshell/ToolBasicTest.java

Print this page




   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 /*
  25  * @test
  26  * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8166232 8196133
  27  * @summary Tests for Basic tests for REPL tool
  28  * @modules jdk.compiler/com.sun.tools.javac.api
  29  *          jdk.compiler/com.sun.tools.javac.main
  30  *          jdk.jdeps/com.sun.tools.javap
  31  *          jdk.jshell/jdk.internal.jshell.tool
  32  * @library /tools/lib
  33  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
  34  * @build KullaTesting TestingInputStream Compiler
  35  * @run testng/timeout=600 ToolBasicTest
  36  */
  37 
  38 import java.io.File;
  39 import java.io.IOException;
  40 import java.io.PrintWriter;
  41 import java.io.StringWriter;


  42 import java.nio.file.Files;
  43 import java.nio.file.Path;
  44 import java.nio.file.Paths;
  45 import java.util.ArrayList;
  46 import java.util.Arrays;
  47 import java.util.List;
  48 import java.util.Scanner;
  49 import java.util.function.BiFunction;
  50 import java.util.function.Consumer;
  51 import java.util.function.Function;
  52 import java.util.stream.Collectors;
  53 import java.util.stream.Stream;
  54 

  55 import org.testng.annotations.Test;
  56 
  57 import static org.testng.Assert.assertEquals;
  58 import static org.testng.Assert.assertTrue;
  59 import static org.testng.Assert.fail;
  60 
  61 @Test
  62 public class ToolBasicTest extends ReplToolTesting {
  63 
  64     public void elideStartUpFromList() {
  65         test(
  66                 (a) -> assertCommandOutputContains(a, "123", "==> 123"),
  67                 (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
  68                     int cnt;
  69                     try (Scanner scanner = new Scanner(s)) {
  70                         cnt = 0;
  71                         while (scanner.hasNextLine()) {
  72                             String line = scanner.nextLine();
  73                             if (!line.trim().isEmpty()) {
  74                                 ++cnt;


 471                     (a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"),
 472                     (a) -> {
 473                         loadVariable(a, "double", "x", "20.0", "20.0");
 474                         loadVariable(a, "double", "a", "10.0", "10.0");
 475                         loadVariable(a, "A", "$7", "new A();", "A");
 476                         loadVariable(a, "long", "$8", "Stream.of(\"A\").count();", "1");
 477                         loadClass(a, "class A { public String toString() { return \"A\"; } }",
 478                                 "class", "A");
 479                         loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*");
 480                         assertCommandCheckOutput(a, "/types", assertClasses());
 481                     },
 482                     (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 483                     (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 484                     (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
 485             );
 486             Path unknown = compiler.getPath("UNKNOWN.repl");
 487             test(
 488                     (a) -> assertCommand(a, s + " " + unknown,
 489                             "|  File '" + unknown + "' for '/open' is not found.")
 490             );











































 491         }
 492     }
 493 
 494     public void testOpenResource() {
 495         test(
 496                 (a) -> assertCommand(a, "/open PRINTING", ""),
 497                 (a) -> assertCommandOutputContains(a, "/list",
 498                         "void println", "System.out.printf"),
 499                 (a) -> assertCommand(a, "printf(\"%4.2f\", Math.PI)",
 500                         "", "", null, "3.14", "")
 501         );
 502     }
 503 
 504     public void testSave() throws IOException {
 505         Compiler compiler = new Compiler();
 506         Path path = compiler.getPath("testSave.repl");
 507         {
 508             List<String> list = Arrays.asList(
 509                     "int a;",
 510                     "class A { public String toString() { return \"A\"; } }"




   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 /*
  25  * @test
  26  * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8166232 8196133 8199912
  27  * @summary Tests for Basic tests for REPL tool
  28  * @modules jdk.compiler/com.sun.tools.javac.api
  29  *          jdk.compiler/com.sun.tools.javac.main
  30  *          jdk.jdeps/com.sun.tools.javap
  31  *          jdk.jshell/jdk.internal.jshell.tool
  32  * @library /tools/lib
  33  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
  34  * @build KullaTesting TestingInputStream Compiler
  35  * @run testng/timeout=600 ToolBasicTest
  36  */
  37 
  38 import java.io.File;
  39 import java.io.IOException;
  40 import java.io.PrintWriter;
  41 import java.io.StringWriter;
  42 import java.net.InetAddress;
  43 import java.net.InetSocketAddress;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.util.ArrayList;
  48 import java.util.Arrays;
  49 import java.util.List;
  50 import java.util.Scanner;
  51 import java.util.function.BiFunction;
  52 import java.util.function.Consumer;
  53 import java.util.function.Function;
  54 import java.util.stream.Collectors;
  55 import java.util.stream.Stream;
  56 
  57 import com.sun.net.httpserver.HttpServer;
  58 import org.testng.annotations.Test;
  59 
  60 import static org.testng.Assert.assertEquals;
  61 import static org.testng.Assert.assertTrue;
  62 import static org.testng.Assert.fail;
  63 
  64 @Test
  65 public class ToolBasicTest extends ReplToolTesting {
  66 
  67     public void elideStartUpFromList() {
  68         test(
  69                 (a) -> assertCommandOutputContains(a, "123", "==> 123"),
  70                 (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
  71                     int cnt;
  72                     try (Scanner scanner = new Scanner(s)) {
  73                         cnt = 0;
  74                         while (scanner.hasNextLine()) {
  75                             String line = scanner.nextLine();
  76                             if (!line.trim().isEmpty()) {
  77                                 ++cnt;


 474                     (a) -> evaluateExpression(a, "long", "Stream.of(\"A\").count();", "1"),
 475                     (a) -> {
 476                         loadVariable(a, "double", "x", "20.0", "20.0");
 477                         loadVariable(a, "double", "a", "10.0", "10.0");
 478                         loadVariable(a, "A", "$7", "new A();", "A");
 479                         loadVariable(a, "long", "$8", "Stream.of(\"A\").count();", "1");
 480                         loadClass(a, "class A { public String toString() { return \"A\"; } }",
 481                                 "class", "A");
 482                         loadImport(a, "import java.util.stream.*;", "", "java.util.stream.*");
 483                         assertCommandCheckOutput(a, "/types", assertClasses());
 484                     },
 485                     (a) -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 486                     (a) -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 487                     (a) -> assertCommandCheckOutput(a, "/imports", assertImports())
 488             );
 489             Path unknown = compiler.getPath("UNKNOWN.repl");
 490             test(
 491                     (a) -> assertCommand(a, s + " " + unknown,
 492                             "|  File '" + unknown + "' for '/open' is not found.")
 493             );
 494         }
 495     }
 496 
 497     public void testOpenLocalFileUrl() {
 498         Compiler compiler = new Compiler();
 499         Path path = compiler.getPath("testOpen.repl");
 500         compiler.writeToFile(path, "int a = 10;int b = 20;int c = a + b;\n");
 501         for (String s : new String[]{"/o", "/open"}) {
 502             test(
 503                     (a) -> assertCommand(a, s + " file://" + path.toString(), ""),
 504                     (a) -> assertCommand(a, "a", "a ==> 10"),
 505                     (a) -> assertCommand(a, "b", "b ==> 20"),
 506                     (a) -> assertCommand(a, "c", "c ==> 30")
 507             );
 508         }
 509     }
 510 
 511     public void testOpenFileOverHttp() throws IOException {
 512         var script = "int a = 10;int b = 20;int c = a + b;";
 513 
 514         var localhostAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), 0);
 515         var httpServer = HttpServer.create(localhostAddress, 0);
 516         try {
 517             httpServer.createContext("/script", exchange -> {
 518                 exchange.sendResponseHeaders(200, script.length());
 519                 try (var output = exchange.getResponseBody()) {
 520                     output.write(script.getBytes());
 521                 }
 522             });
 523             httpServer.setExecutor(null);
 524             httpServer.start();
 525 
 526             var urlAddress = "http:/" + httpServer.getAddress().toString() + "/script";
 527             for (String s : new String[]{"/o", "/open"}) {
 528                 test(
 529                         (a) -> assertCommand(a, s + " " + urlAddress, ""),
 530                         (a) -> assertCommand(a, "a", "a ==> 10"),
 531                         (a) -> assertCommand(a, "b", "b ==> 20"),
 532                         (a) -> assertCommand(a, "c", "c ==> 30")
 533                 );
 534             }
 535         } finally {
 536             httpServer.stop(0);
 537         }
 538     }
 539 
 540     public void testOpenResource() {
 541         test(
 542                 (a) -> assertCommand(a, "/open PRINTING", ""),
 543                 (a) -> assertCommandOutputContains(a, "/list",
 544                         "void println", "System.out.printf"),
 545                 (a) -> assertCommand(a, "printf(\"%4.2f\", Math.PI)",
 546                         "", "", null, "3.14", "")
 547         );
 548     }
 549 
 550     public void testSave() throws IOException {
 551         Compiler compiler = new Compiler();
 552         Path path = compiler.getPath("testSave.repl");
 553         {
 554             List<String> list = Arrays.asList(
 555                     "int a;",
 556                     "class A { public String toString() { return \"A\"; } }"


< prev index next >