langtools/test/jdk/jshell/ToolBasicTest.java
changeset 43856 fcdebb803c62
parent 43038 7b8b8750a78e
child 44065 d0a8a4c41c85
equal deleted inserted replaced
43773:8d8593871575 43856:fcdebb803c62
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405
    26  * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797
    27  * @summary Tests for Basic tests for REPL tool
    27  * @summary Tests for Basic tests for REPL tool
    28  * @modules jdk.compiler/com.sun.tools.javac.api
    28  * @modules jdk.compiler/com.sun.tools.javac.api
    29  *          jdk.compiler/com.sun.tools.javac.main
    29  *          jdk.compiler/com.sun.tools.javac.main
    30  *          jdk.jdeps/com.sun.tools.javap
    30  *          jdk.jdeps/com.sun.tools.javap
    31  *          jdk.jshell/jdk.internal.jshell.tool
    31  *          jdk.jshell/jdk.internal.jshell.tool
    33  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
    33  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
    34  * @build KullaTesting TestingInputStream Compiler
    34  * @build KullaTesting TestingInputStream Compiler
    35  * @run testng/timeout=600 ToolBasicTest
    35  * @run testng/timeout=600 ToolBasicTest
    36  */
    36  */
    37 
    37 
       
    38 import java.io.File;
    38 import java.io.IOException;
    39 import java.io.IOException;
    39 import java.io.PrintWriter;
    40 import java.io.PrintWriter;
    40 import java.io.StringWriter;
    41 import java.io.StringWriter;
    41 import java.nio.file.Files;
    42 import java.nio.file.Files;
    42 import java.nio.file.Path;
    43 import java.nio.file.Path;
   270         test(new String[] { "--class-path", classpath.toString() },
   271         test(new String[] { "--class-path", classpath.toString() },
   271                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
   272                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
   272         );
   273         );
   273     }
   274     }
   274 
   275 
   275     public void testClasspathJar() {
   276     private String makeSimpleJar() {
   276         Compiler compiler = new Compiler();
   277         Compiler compiler = new Compiler();
   277         Path outDir = Paths.get("testClasspathJar");
   278         Path outDir = Paths.get("testClasspathJar");
   278         compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
   279         compiler.compile(outDir, "package pkg; public class A { public String toString() { return \"A\"; } }");
   279         String jarName = "test.jar";
   280         String jarName = "test.jar";
   280         compiler.jar(outDir, jarName, "pkg/A.class");
   281         compiler.jar(outDir, jarName, "pkg/A.class");
   281         Path jarPath = compiler.getPath(outDir).resolve(jarName);
   282         return compiler.getPath(outDir).resolve(jarName).toString();
       
   283     }
       
   284 
       
   285     public void testClasspathJar() {
       
   286         String jarPath = makeSimpleJar();
   282         test(
   287         test(
   283                 (a) -> assertCommand(a, "/env --class-path " + jarPath,
   288                 (a) -> assertCommand(a, "/env --class-path " + jarPath,
   284                         "|  Setting new options and restoring state."),
   289                         "|  Setting new options and restoring state."),
   285                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
   290                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
   286         );
   291         );
   287         test(new String[] { "--class-path", jarPath.toString() },
   292         test(new String[] { "--class-path", jarPath },
   288                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
   293                 (a) -> evaluateExpression(a, "pkg.A", "new pkg.A();", "A")
       
   294         );
       
   295     }
       
   296 
       
   297     public void testClasspathUserHomeExpansion() {
       
   298         String jarPath = makeSimpleJar();
       
   299         String tilde = "~" + File.separator;
       
   300         test(
       
   301                 (a) -> assertCommand(a, "/env --class-path " + tilde + "forblato",
       
   302                         "|  File '" + System.getProperty("user.home") + File.separator
       
   303                                 + "forblato' for '--class-path' is not found."),
       
   304                 (a) -> assertCommand(a, "/env --class-path " + jarPath + File.pathSeparator
       
   305                                                             + tilde + "forblato",
       
   306                         "|  File '" + System.getProperty("user.home") + File.separator
       
   307                                 + "forblato' for '--class-path' is not found.")
       
   308         );
       
   309     }
       
   310 
       
   311     public void testBadClasspath() {
       
   312         String jarPath = makeSimpleJar();
       
   313         Compiler compiler = new Compiler();
       
   314         Path t1 = compiler.getPath("whatever/thing.zip");
       
   315         compiler.writeToFile(t1, "");
       
   316         Path t2 = compiler.getPath("whatever/thing.jmod");
       
   317         compiler.writeToFile(t2, "");
       
   318         test(
       
   319                 (a) -> assertCommand(a, "/env --class-path " + t1.toString(),
       
   320                         "|  Invalid '--class-path' argument: " + t1.toString()),
       
   321                 (a) -> assertCommand(a, "/env --class-path " + jarPath + File.pathSeparator + t1.toString(),
       
   322                         "|  Invalid '--class-path' argument: " + t1.toString()),
       
   323                 (a) -> assertCommand(a, "/env --class-path " + t2.toString(),
       
   324                         "|  Invalid '--class-path' argument: " + t2.toString())
   289         );
   325         );
   290     }
   326     }
   291 
   327 
   292     public void testModulePath() {
   328     public void testModulePath() {
   293         Compiler compiler = new Compiler();
   329         Compiler compiler = new Compiler();
   299         test(new String[] { "--module-path", modsPath.toString(), "--add-modules", "org.astro" },
   335         test(new String[] { "--module-path", modsPath.toString(), "--add-modules", "org.astro" },
   300                 (a) -> assertCommand(a, "import org.astro.World;", ""),
   336                 (a) -> assertCommand(a, "import org.astro.World;", ""),
   301                 (a) -> evaluateExpression(a, "String",
   337                 (a) -> evaluateExpression(a, "String",
   302                         "String.format(\"Greetings %s!\", World.name());",
   338                         "String.format(\"Greetings %s!\", World.name());",
   303                         "\"Greetings world!\"")
   339                         "\"Greetings world!\"")
       
   340         );
       
   341     }
       
   342 
       
   343     public void testModulePathUserHomeExpansion() {
       
   344         String tilde = "~" + File.separatorChar;
       
   345         test(
       
   346                 (a) -> assertCommand(a, "/env --module-path " + tilde + "snardugol",
       
   347                         "|  File '" + System.getProperty("user.home")
       
   348                                 + File.separatorChar + "snardugol' for '--module-path' is not found.")
       
   349         );
       
   350     }
       
   351 
       
   352     public void testBadModulePath() {
       
   353         Compiler compiler = new Compiler();
       
   354         Path t1 = compiler.getPath("whatever/thing.zip");
       
   355         compiler.writeToFile(t1, "");
       
   356         test(
       
   357                 (a) -> assertCommand(a, "/env --module-path " + t1.toString(),
       
   358                         "|  Invalid '--module-path' argument: " + t1.toString())
   304         );
   359         );
   305     }
   360     }
   306 
   361 
   307     public void testStartupFileOption() {
   362     public void testStartupFileOption() {
   308         Compiler compiler = new Compiler();
   363         Compiler compiler = new Compiler();