# HG changeset patch # User Jon VanAlten # Date 1361314764 18000 # Node ID 9bebac5faf9bf45956cd8ac3ca8f67cfd53439cf # Parent cb50a6f7f0d3bdd96e066ec3dd7d7c66a2cc7938 Fix PR1231, Inconsistent shell command usage errors Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005761.html diff -r cb50a6f7f0d3 -r 9bebac5faf9b distribution/config/commands/ping.properties --- 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 diff -r cb50a6f7f0d3 -r 9bebac5faf9b launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java --- 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); diff -r cb50a6f7f0d3 -r 9bebac5faf9b launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java --- 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 > [--arg2 ]"); 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 > [--arg2 ]\n" + + " description 1\n" + + "thermostat test1\n" + + " --arg1 \n" + + " --arg2 \n" + + " -l,--logLevel \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 > [--arg2 ]\n" + + " description 1\n" + + "thermostat test1\n" + + " --arg1 \n" + + " --arg2 \n" + + " -l,--logLevel \n"; + runAndVerifyCommand(new String[] {"test1"}, expected, false); + } + + @Test + public void testOptionMissingRequiredArgument() { + String expected = "Missing argument for option: arg1\n" + + "usage: thermostat test1 <--arg1 > [--arg2 ]\n" + + " description 1\n" + + "thermostat test1\n" + + " --arg1 \n" + + " --arg2 \n" + + " -l,--logLevel \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");