changeset 984:9bebac5faf9b

Fix PR1231, Inconsistent shell command usage errors Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005761.html
author Jon VanAlten <vanaltj@gmail.com>
date Tue, 19 Feb 2013 17:59:24 -0500
parents cb50a6f7f0d3
children dc3bedd9bf00
files distribution/config/commands/ping.properties launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java
diffstat 3 files changed, 47 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/distribution/config/commands/ping.properties	Tue Feb 19 12:50:23 2013 -0500
+++ b/distribution/config/commands/ping.properties	Tue Feb 19 17:59:24 2013 -0500
@@ -8,6 +8,7 @@
           gson.jar, \
           mongo.jar, \
           commons-beanutils.jar, \
+          commons-codec.jar, \
           commons-collections.jar, \
           commons-logging.jar, \
           netty.jar
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Tue Feb 19 12:50:23 2013 -0500
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Tue Feb 19 17:59:24 2013 -0500
@@ -243,7 +243,14 @@
             }
         }
         Options options = cmd.getOptions();
-        Arguments args = parseCommandArguments(cmdArgs, options);
+        Arguments args = null;
+        try {
+            args = parseCommandArguments(cmdArgs, options);
+        } catch (CommandLineArgumentParseException e) {
+            out.println(e.getMessage());
+            runHelpCommandFor(cmdName);
+            return;
+        }
         setupLogLevel(args);
         CommandContext ctx = setupCommandContext(cmd, args);
         cmd.run(ctx);
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Tue Feb 19 12:50:23 2013 -0500
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Tue Feb 19 17:59:24 2013 -0500
@@ -39,7 +39,6 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.isA;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -89,7 +88,6 @@
 import com.redhat.thermostat.common.locale.LocaleResources;
 import com.redhat.thermostat.common.locale.Translate;
 import com.redhat.thermostat.common.tools.ApplicationState;
-import com.redhat.thermostat.common.utils.OSGIUtils;
 import com.redhat.thermostat.launcher.BundleManager;
 import com.redhat.thermostat.launcher.TestCommand;
 import com.redhat.thermostat.launcher.internal.HelpCommand;
@@ -162,8 +160,10 @@
         TestCommand cmd1 = new TestCommand(name1, new TestCmd1());
         CommandInfo info1 = mock(CommandInfo.class);
         when(info1.getName()).thenReturn(name1);
+        when(info1.getUsage()).thenReturn(name1 + " <--arg1 <arg>> [--arg2 <arg>]");
         Options options1 = new Options();
         Option opt1 = new Option(null, "arg1", true, null);
+        opt1.setRequired(true);
         options1.addOption(opt1);
         Option opt2 = new Option(null, "arg2", true, null);
         options1.addOption(opt2);
@@ -365,6 +365,42 @@
     }
 
     @Test
+    public void testBadOption() {
+        String expected = "Unrecognized option: --argNotAccepted\n"
+                + "usage: thermostat test1 <--arg1 <arg>> [--arg2 <arg>]\n"
+                + "                  description 1\n"
+                + "thermostat test1\n"
+                + "     --arg1 <arg>\n"
+                + "     --arg2 <arg>\n"
+                + "  -l,--logLevel <arg>\n";
+        runAndVerifyCommand(new String[] {"test1", "--arg1", "arg1value", "--argNotAccepted"}, expected, false);
+    }
+
+    @Test
+    public void testMissingRequiredOption() {
+        String expected = "Missing required option: --arg1\n"
+                + "usage: thermostat test1 <--arg1 <arg>> [--arg2 <arg>]\n"
+                + "                  description 1\n"
+                + "thermostat test1\n"
+                + "     --arg1 <arg>\n"
+                + "     --arg2 <arg>\n"
+                + "  -l,--logLevel <arg>\n";
+        runAndVerifyCommand(new String[] {"test1"}, expected, false);
+    }
+
+    @Test
+    public void testOptionMissingRequiredArgument() {
+        String expected = "Missing argument for option: arg1\n"
+                + "usage: thermostat test1 <--arg1 <arg>> [--arg2 <arg>]\n"
+                + "                  description 1\n"
+                + "thermostat test1\n"
+                + "     --arg1 <arg>\n"
+                + "     --arg2 <arg>\n"
+                + "  -l,--logLevel <arg>\n";
+        runAndVerifyCommand(new String[] {"test1", "--arg1"}, expected, false);
+    }
+
+    @Test
     public void testCommandInfoNotFound() throws CommandInfoNotFoundException, BundleException, IOException {
         when(infos.getCommandInfo("foo")).thenThrow(new CommandInfoNotFoundException("foo"));
         doThrow(new CommandInfoNotFoundException("foo")).when(registry).addBundlesFor("foo");