changeset 988:06cc96778e94

Don't allow "--version" in the Thermostat shell. Reviewed-by: neugens, rkennke Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005778.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Wed, 20 Feb 2013 12:47:21 +0100
parents 527ed18dd395
children cf7a445c359b
files integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java
diffstat 2 files changed, 33 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java	Mon Feb 11 17:45:57 2013 +0100
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java	Wed Feb 20 12:47:21 2013 +0100
@@ -37,6 +37,7 @@
 package com.redhat.thermostat.itest;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -133,6 +134,30 @@
 
         shell.expectClose();
     }
+    
+    @Test
+    public void versionArgumentInShellIsNotAllowed() throws Exception {
+        Spawn shell = spawnThermostat("shell");
+
+        shell.expect(SHELL_PROMPT);
+        shell.send("--version\n");
+
+        shell.expect(SHELL_PROMPT);
+
+        String stdOut = shell.getCurrentStandardOutContents();
+        String stdErr = shell.getCurrentStandardErrContents();
+
+        assertFalse(stdOut.matches("Thermostat version \\d+\\.\\d+\\.\\d+\n"));
+        assertMatchesHelpCommandList(shell.getCurrentStandardOutContents());
+        // use the Pattern.DOTALL flag (?s) so that line terminators match with
+        // ".*". stdOut contains the SHELL_PROMPT too.
+        assertTrue(stdOut.matches("(?s)^.*\nunknown command '--version'\n.*$"));
+        assertEquals(stdErr, "");
+        
+        shell.send("exit\n");
+
+        shell.expectClose();
+    }
 
     @Test
     public void testShellHelp() throws Exception {
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Mon Feb 11 17:45:57 2013 +0100
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Wed Feb 20 12:47:21 2013 +0100
@@ -119,7 +119,7 @@
         try {
             if (hasNoArguments()) {
                 runHelpCommand();
-            } else if (isVersionQuery()) {
+            } else if (isVersionQuery(inShell)) {
                 // We want to print the version of core
                 // thermostat, so we use the no-arg constructor of Version
                 Version coreVersion = new Version();
@@ -336,8 +336,13 @@
         return ctx;
     }
 
-    private boolean isVersionQuery() {
-        return args[0].equals(Version.VERSION_OPTION);
+    private boolean isVersionQuery(boolean inShell) {
+        // don't allow --version in the shell
+        if (inShell) {
+            return false;
+        } else {
+            return args[0].equals(Version.VERSION_OPTION);
+        }
     }
 
     static class LoggingInitializer {