changeset 3918:919a15cb34bb

8171343: jshell tool: missing options: --help-extra --show-version Reviewed-by: jlahoda
author rfield
date Wed, 25 Jan 2017 10:43:41 -0800
parents edd091e7a9a3
children 3fdaf9e50f5c
files src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties test/jdk/jshell/StartOptionTest.java test/jdk/jshell/ToolProviderTest.java
diffstat 4 files changed, 72 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Wed Jan 25 10:22:06 2017 -0800
+++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Wed Jan 25 10:43:41 2017 -0800
@@ -426,7 +426,8 @@
         private final OptionSpecBuilder argHelp = parser.acceptsAll(asList("h", "help"));
         private final OptionSpecBuilder argVersion = parser.accepts("version");
         private final OptionSpecBuilder argFullVersion = parser.accepts("full-version");
-        private final OptionSpecBuilder argX = parser.accepts("X");
+        private final OptionSpecBuilder argShowVersion = parser.accepts("show-version");
+        private final OptionSpecBuilder argHelpExtra = parser.acceptsAll(asList("X", "help-extra"));
 
         private String feedbackMode = null;
         private Startup initialStartup = null;
@@ -450,7 +451,7 @@
                 printUsage();
                 return null;
             }
-            if (options.has(argX)) {
+            if (options.has(argHelpExtra)) {
                 printUsageX();
                 return null;
             }
@@ -462,6 +463,9 @@
                 cmdout.printf("jshell %s\n", fullVersion());
                 return null;
             }
+            if (options.has(argShowVersion)) {
+                cmdout.printf("jshell %s\n", version());
+            }
             if ((options.valuesOf(argFeedback).size() +
                     (options.has(argQ) ? 1 : 0) +
                     (options.has(argS) ? 1 : 0) +
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties	Wed Jan 25 10:22:06 2017 -0800
+++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties	Wed Jan 25 10:43:41 2017 -0800
@@ -187,9 +187,10 @@
 \                            Use one -R for each remote flag or flag argument\n\
 \    -C<flag>              Pass <flag> to the compiler.\n\
 \                            Use one -C for each compiler flag or flag argument\n\
-\    --help                Print this synopsis of standard options\n\
-\    --version             Version information\n\
-\    -X                    Print help on non-standard options\n
+\    --version             Print version information and exit\n\
+\    --show-version        Print version information and continue\n\
+\    --help                Print this synopsis of standard options and exit\n\
+\    --help-extra, -X      Print help on non-standard options and exit\n
 help.usage.x = \
 \    --add-exports <module>/<package>   Export specified module-private package to snippets\n\
 \    --execution <spec>                 Specify an alternate execution engine.\n\
--- a/test/jdk/jshell/StartOptionTest.java	Wed Jan 25 10:22:06 2017 -0800
+++ b/test/jdk/jshell/StartOptionTest.java	Wed Jan 25 10:43:41 2017 -0800
@@ -22,7 +22,7 @@
  */
 
 /*
- * @test 8151754 8080883 8160089 8170162 8166581 8172102
+ * @test 8151754 8080883 8160089 8170162 8166581 8172102 8171343
  * @summary Testing start-up options.
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -33,7 +33,9 @@
  * @run testng StartOptionTest
  */
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
 import java.io.PrintStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Path;
@@ -46,6 +48,7 @@
 import org.testng.annotations.Test;
 import jdk.jshell.tool.JavaShellToolBuilder;
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
@@ -57,12 +60,14 @@
     private ByteArrayOutputStream console;
     private ByteArrayOutputStream userout;
     private ByteArrayOutputStream usererr;
+    private InputStream cmdInStream;
 
     private JavaShellToolBuilder builder() {
         return JavaShellToolBuilder
                     .builder()
                     .out(new PrintStream(cmdout), new PrintStream(console), new PrintStream(userout))
                     .err(new PrintStream(cmderr), new PrintStream(usererr))
+                    .in(cmdInStream, null)
                     .persistence(new HashMap<>())
                     .env(new HashMap<>())
                     .locale(Locale.ROOT);
@@ -119,6 +124,7 @@
         console = new ByteArrayOutputStream();
         userout = new ByteArrayOutputStream();
         usererr = new ByteArrayOutputStream();
+        cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());
     }
 
     protected String writeToFile(String stuff) throws Exception {
@@ -138,6 +144,19 @@
             start(s -> {
                 assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
                 assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start: " + s);
+                assertTrue(s.contains("--show-version"), "Expected help: " + s);
+                assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
+            }, null, null, opt);
+        }
+    }
+
+    public void testHelpExtra() throws Exception {
+        for (String opt : new String[]{"-X", "--help-extra"}) {
+            start(s -> {
+                assertTrue(s.split("\n").length >= 5, "Not enough help-extra lines: " + s);
+                assertTrue(s.contains("--add-exports"), "Expected --add-exports: " + s);
+                assertTrue(s.contains("--execution"), "Expected --add-exports: " + s);
+                assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
             }, null, null, opt);
         }
     }
@@ -201,7 +220,29 @@
     }
 
     public void testVersion() throws Exception {
-        start(s -> assertTrue(s.startsWith("jshell"), "unexpected version: " + s), null, null, "--version");
+        start(
+                s -> {
+                    assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
+                    assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
+                },
+                null, null,
+                "--version");
+    }
+
+    public void testShowVersion() throws Exception {
+        runShell("--show-version");
+        check(cmdout,
+                s -> {
+                    assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
+                    assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
+                },
+                "cmdout");
+        check(cmderr, null, "cmderr");
+        check(console,
+                s -> assertTrue(s.trim().startsWith("jshell>"), "Expected prompt, got: " + s),
+                "console");
+        check(userout, null, "userout");
+        check(usererr, null, "usererr");
     }
 
     @AfterMethod
@@ -211,5 +252,6 @@
         console = null;
         userout = null;
         usererr = null;
+        cmdInStream = null;
     }
 }
--- a/test/jdk/jshell/ToolProviderTest.java	Wed Jan 25 10:22:06 2017 -0800
+++ b/test/jdk/jshell/ToolProviderTest.java	Wed Jan 25 10:43:41 2017 -0800
@@ -23,16 +23,18 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
 import java.util.ServiceLoader;
 import java.util.function.Consumer;
 import javax.tools.Tool;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
+import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 /*
  * @test
- * @bug 8170044
+ * @bug 8170044 8171343
  * @summary Test ServiceLoader launching of jshell tool
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -47,12 +49,14 @@
 
     private ByteArrayOutputStream cmdout;
     private ByteArrayOutputStream cmderr;
+    private InputStream cmdInStream;
 
     @BeforeMethod
     @Override
     public void setUp() {
         cmdout = new ByteArrayOutputStream();
         cmderr = new ByteArrayOutputStream();
+        cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());
     }
 
     @Override
@@ -70,7 +74,7 @@
         ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class);
         for (Tool provider : sl) {
             if (provider.name().equals("jshell")) {
-                return provider.run(new ByteArrayInputStream(new byte[0]), cmdout, cmderr, args);
+                return provider.run(cmdInStream, cmdout, cmderr, args);
             }
         }
         throw new AssertionError("Repl tool not found by ServiceLoader: " + sl);
@@ -90,4 +94,16 @@
             check(cmderr, s -> s.startsWith("Launching JShell execution engine threw: Failed remote"), "cmderr");
         }
     }
+
+    @Override
+    public void testShowVersion() throws Exception {
+        start(
+                s -> {
+                    assertTrue(s.startsWith("jshell "), "unexpected version: " + s);
+                    assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
+                    assertTrue(s.trim().contains("jshell>"), "Expected prompt, got: " + s);
+                },
+                null, null,
+                "--show-version");
+    }
 }