8157953: JShell tests: reenable ToolBasicTest
authorrfield
Thu, 26 May 2016 12:38:21 -0700
changeset 38613 2a8e50869b08
parent 38612 43c4bd1d4cb5
child 38614 364c1a6618c0
8157953: JShell tests: reenable ToolBasicTest 8080883: JShell tool: tool does not report errors if -startup and -nostartup flags are specified Reviewed-by: vromero
langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties
langtools/test/jdk/jshell/StartOptionTest.java
langtools/test/jdk/jshell/ToolBasicTest.java
--- a/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Thu May 26 10:46:21 2016 -0700
+++ b/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Thu May 26 12:38:21 2016 -0700
@@ -373,7 +373,11 @@
      */
     @Override
     public void errormsg(String key, Object... args) {
-        cmdout.println(prefix(messageFormat(key, args), feedback.getErrorPre()));
+        if (isRunningInteractive()) {
+            cmdout.println(prefix(messageFormat(key, args), feedback.getErrorPre()));
+        } else {
+            startmsg(key, args);
+        }
     }
 
     /**
@@ -558,17 +562,17 @@
                         break;
                     case "-startup":
                         if (startup != null) {
-                            startmsg("jshell.err.opt.startup.conflict");
+                            startmsg("jshell.err.opt.startup.one");
                             return null;
                         }
-                        startup = readFile(ai.hasNext()? ai.next() : null, "'-startup'");
+                        startup = readFile(ai.hasNext()? ai.next() : null, "-startup");
                         if (startup == null) {
                             return null;
                         }
                         break;
                     case "-nostartup":
-                        if (startup != null && !startup.isEmpty()) {
-                            startmsg("jshell.err.opt.startup.conflict");
+                        if (startup != null) {
+                            startmsg("jshell.err.opt.startup.one");
                             return null;
                         }
                         startup = "";
@@ -660,6 +664,10 @@
         currentNameSpace = mainNamespace;
     }
 
+    private boolean isRunningInteractive() {
+        return currentNameSpace != null && currentNameSpace == mainNamespace;
+    }
+
     //where -- one-time per run initialization of feedback modes
     private void initFeedback() {
         // No fluff, no prefix, for init failures
@@ -722,7 +730,7 @@
             String incomplete = "";
             while (live) {
                 String prompt;
-                if (currentNameSpace == mainNamespace) {
+                if (isRunningInteractive()) {
                     prompt = testPrompt
                                     ? incomplete.isEmpty()
                                             ? "\u0005" //ENQ
@@ -770,7 +778,7 @@
     }
 
     private void addToReplayHistory(String s) {
-        if (currentNameSpace == mainNamespace) {
+        if (isRunningInteractive()) {
             replayableHistory.add(s);
         }
     }
--- a/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties	Thu May 26 10:46:21 2016 -0700
+++ b/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties	Thu May 26 12:38:21 2016 -0700
@@ -29,7 +29,7 @@
 jshell.err.opt.classpath.conflict = Conflicting -classpath option.
 jshell.err.opt.classpath.arg = Argument to -classpath missing.
 jshell.err.opt.feedback.arg = Argument to -feedback missing. Mode required.
-jshell.err.opt.startup.conflict = Conflicting -startup or -nostartup option.
+jshell.err.opt.startup.one = Only one -startup or -nostartup option may be used.
 jshell.err.opt.unknown = Unknown option: {0}
 
 jshell.msg.terminated =\
--- a/langtools/test/jdk/jshell/StartOptionTest.java	Thu May 26 10:46:21 2016 -0700
+++ b/langtools/test/jdk/jshell/StartOptionTest.java	Thu May 26 12:38:21 2016 -0700
@@ -22,7 +22,7 @@
  */
 
 /*
- * @test 8151754
+ * @test 8151754 8080883
  * @summary Testing start-up options.
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -120,15 +120,20 @@
         }, s -> assertEquals(s.trim(), "Unknown option: -unknown"), "-unknown");
     }
 
-    @Test(enabled = false) // TODO 8080883
     public void testStartup() throws Exception {
         Compiler compiler = new Compiler();
         Path p = compiler.getPath("file.txt");
         compiler.writeToFile(p);
         start("", "'-startup' requires a filename argument.", "-startup");
-        start("", "Conflicting -startup or -nostartup option.", "-startup", p.toString(), "-startup", p.toString());
-        start("", "Conflicting -startup or -nostartup option.", "-nostartup", "-startup", p.toString());
-        start("", "Conflicting -startup or -nostartup option.", "-startup", p.toString(), "-nostartup");
+        start("", "Only one -startup or -nostartup option may be used.", "-startup", p.toString(), "-startup", p.toString());
+        start("", "Only one -startup or -nostartup option may be used.", "-nostartup", "-startup", p.toString());
+        start("", "Only one -startup or -nostartup option may be used.", "-startup", p.toString(), "-nostartup");
+        start("", "Only one -startup or -nostartup option may be used.", "-nostartup", "-nostartup");
+        start("", "Only one -startup or -nostartup option may be used.", "-nostartup", "-startup");
+    }
+
+    public void testStartupUnknown() throws Exception {
+        start("", "File 'UNKNOWN' for '-startup' is not found.", "-startup", "UNKNOWN");
     }
 
     @Test
--- a/langtools/test/jdk/jshell/ToolBasicTest.java	Thu May 26 10:46:21 2016 -0700
+++ b/langtools/test/jdk/jshell/ToolBasicTest.java	Thu May 26 12:38:21 2016 -0700
@@ -23,21 +23,18 @@
 
 /*
  * @test
- * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955
+ * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953
  * @summary Tests for Basic tests for REPL tool
- * @requires os.family != "solaris"
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
  *          jdk.jdeps/com.sun.tools.javap
  *          jdk.jshell/jdk.internal.jshell.tool
  * @library /tools/lib
- * @ignore 8139873
  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
  * @build KullaTesting TestingInputStream Compiler
  * @run testng/timeout=600 ToolBasicTest
  */
 
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
@@ -68,7 +65,7 @@
 
     public void elideStartUpFromList() {
         test(
-                (a) -> assertCommandOutputContains(a, "123", "type int"),
+                (a) -> assertCommandOutputContains(a, "123", "==> 123"),
                 (a) -> assertCommandCheckOutput(a, "/list", (s) -> {
                     int cnt;
                     try (Scanner scanner = new Scanner(s)) {
@@ -89,7 +86,7 @@
         Compiler compiler = new Compiler();
         Path path = compiler.getPath("myfile");
         test(
-                (a) -> assertCommandOutputContains(a, "123", "type int"),
+                (a) -> assertCommandOutputContains(a, "123", "==> 123"),
                 (a) -> assertCommand(a, "/save " + path.toString(), "")
         );
         try (Stream<String> lines = Files.lines(path)) {
@@ -187,12 +184,11 @@
 
     public void testStop() {
         test(
-                (a) -> assertStop(a, "while (true) {}", "Killed."),
-                (a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "Killed.")
+                (a) -> assertStop(a, "while (true) {}", ""),
+                (a) -> assertStop(a, "while (true) { try { Thread.sleep(100); } catch (InterruptedException ex) { } }", "")
         );
     }
 
-    @Test(enabled = false) // TODO 8130450
     public void testRerun() {
         test(false, new String[] {"-nostartup"},
                 (a) -> assertCommand(a, "/0", "|  No such command or snippet id: /0\n|  Type /help for help."),
@@ -209,6 +205,7 @@
         for (String s : codes) {
             tests.add((a) -> assertCommand(a, s, null));
         }
+        // Test /1 through /5 -- assure references are correct
         for (int i = 0; i < codes.length; ++i) {
             final int finalI = i;
             Consumer<String> check = (s) -> {
@@ -218,6 +215,7 @@
             };
             tests.add((a) -> assertCommandCheckOutput(a, "/" + (finalI + 1), check));
         }
+        // Test /-1 ... note that the snippets added by history must be stepped over
         for (int i = 0; i < codes.length; ++i) {
             final int finalI = i;
             Consumer<String> check = (s) -> {
@@ -225,9 +223,9 @@
                 assertEquals(ss[0], codes[codes.length - finalI - 1]);
                 assertTrue(ss.length > 1, s);
             };
-            tests.add((a) -> assertCommandCheckOutput(a, "/-" + (finalI + 1), check));
+            tests.add((a) -> assertCommandCheckOutput(a, "/-" + (2 * finalI + 1), check));
         }
-        tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("void g() { h(); }")));
+        tests.add((a) -> assertCommandCheckOutput(a, "/!", assertStartsWith("int a = 0;")));
         test(false, new String[]{"-nostartup"},
                 tests.toArray(new ReplTest[tests.size()]));
     }
@@ -308,8 +306,9 @@
             test(new String[]{"-nostartup"},
                     (a) -> assertCommandCheckOutput(a, "printf(\"\")", assertStartsWith("|  Error:\n|  cannot find symbol"))
             );
-            test((a) -> assertCommand(a, "printf(\"A\")", "", "", null, "A", ""));
-            test(Locale.ROOT, false, new String[]{"-startup", "UNKNOWN"}, "|  File 'UNKNOWN' for start-up is not found.");
+            test(
+                    (a) -> assertCommand(a, "printf(\"A\")", "", "", null, "A", "")
+            );
         } finally {
             removeStartup();
         }
@@ -325,8 +324,8 @@
         );
         Path unknown = compiler.getPath("UNKNOWN.jar");
         test(Locale.ROOT, true, new String[]{unknown.toString()},
-                "|  File " + unknown
-                + " is not found: " + unresolvableMessage(unknown));
+                "|  File '" + unknown
+                + "' for 'jshell' is not found.");
     }
 
     public void testReset() {
@@ -422,13 +421,13 @@
                         output.addAll(Stream.of(out.split("\n"))
                                 .filter(str -> !str.isEmpty())
                                 .collect(Collectors.toList()))),
-                (a) -> assertCommand(a, "/save history " + path.toString(), "")
+                (a) -> assertCommand(a, "/save -history " + path.toString(), "")
         );
-        output.add("/save history " + path.toString());
+        output.add("/save -history " + path.toString());
         assertEquals(Files.readAllLines(path), output);
     }
 
-    public void testStartSet() throws BackingStoreException {
+    public void testStartRetain() throws BackingStoreException {
         try {
             Compiler compiler = new Compiler();
             Path startUpFile = compiler.getPath("startUp.txt");
@@ -438,12 +437,12 @@
                     (a) -> assertMethod(a, "void f() {}", "()V", "f"),
                     (a) -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
                     (a) -> assertCommand(a, "/save " + startUpFile.toString(), null),
-                    (a) -> assertCommand(a, "/set start " + startUpFile.toString(), null)
+                    (a) -> assertCommand(a, "/retain start " + startUpFile.toString(), null)
             );
             Path unknown = compiler.getPath("UNKNOWN");
             test(
-                    (a) -> assertCommandOutputStartsWith(a, "/set start " + unknown.toString(),
-                            "|  File '" + unknown + "' for '/set start' is not found.")
+                    (a) -> assertCommandOutputStartsWith(a, "/retain start " + unknown.toString(),
+                            "|  File '" + unknown + "' for '/retain start' is not found.")
             );
             test(false, new String[0],
                     (a) -> {
@@ -561,15 +560,6 @@
         );
     }
 
-    private String unresolvableMessage(Path p) {
-        try {
-            new FileInputStream(p.toFile());
-            throw new AssertionError("Expected exception did not occur.");
-        } catch (IOException ex) {
-            return ex.getMessage();
-        }
-    }
-
     public void testHeadlessEditPad() {
         String prevHeadless = System.getProperty("java.awt.headless");
         try {