changeset 251:1bf239bebc6d

CLI should handle unknown commands Reviewed-by: neugens, rkennke Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000966.html
author Omair Majid <omajid@redhat.com>
date Fri, 20 Apr 2012 10:25:33 -0400
parents 452e9d9a7109
children 738b0e7472dd
files common/src/main/java/com/redhat/thermostat/cli/Launcher.java common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java
diffstat 2 files changed, 45 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/common/src/main/java/com/redhat/thermostat/cli/Launcher.java	Fri Apr 20 10:23:22 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/cli/Launcher.java	Fri Apr 20 10:25:33 2012 -0400
@@ -46,7 +46,7 @@
     public void run(String[] args) {
         this.args = args;
         registerDefaultCommands();
-        if (hasNoArguments()) {
+        if (hasNoArguments() || unknownCommand()) {
             runHelpCommand();
         } else {
             runCommandFromArguments();
@@ -57,6 +57,13 @@
         return args.length == 0;
     }
 
+    private boolean unknownCommand() {
+        CommandContextFactory cmdCtxFactory = CommandContextFactory.getInstance();
+        CommandRegistry registry = cmdCtxFactory.getCommandRegistry();
+        Command cmd = registry.getCommand(args[0]);
+        return cmd == null;
+    }
+
     private void runHelpCommand() {
         runCommand("help", new String[0]);
     }
--- a/common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java	Fri Apr 20 10:23:22 2012 -0400
+++ b/common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java	Fri Apr 20 10:25:33 2012 -0400
@@ -121,9 +121,45 @@
     }
 
     @Test
+    public void testMainBadCommand1() {
+        String expected = "list of commands:\n\n"
+            + " help          show help for a given command or help overview\n"
+            + " test1         description 1\n"
+            + " test2         description 2\n";
+        runAndVerifyCommand(new String[] {"--help"}, expected);
+    }
+
+    @Test
+    public void testMainBadCommand2() {
+        String expected = "list of commands:\n\n"
+            + " help          show help for a given command or help overview\n"
+            + " test1         description 1\n"
+            + " test2         description 2\n";
+        runAndVerifyCommand(new String[] {"-help"}, expected);
+    }
+
+    @Test
+    public void testMainBadCommand3() {
+        String expected = "list of commands:\n\n"
+            + " help          show help for a given command or help overview\n"
+            + " test1         description 1\n"
+            + " test2         description 2\n";
+        runAndVerifyCommand(new String[] {"foobarbaz"}, expected);
+    }
+
+    @Test
+    public void testMainBadCommand4() {
+        String expected = "list of commands:\n\n"
+            + " help          show help for a given command or help overview\n"
+            + " test1         description 1\n"
+            + " test2         description 2\n";
+        runAndVerifyCommand(new String[] {"foo",  "--bar", "baz"}, expected);
+    }
+
+    @Test
     public void testMainExceptionInCommand() {
         TestCommand errorCmd = new TestCommand("error", new TestCommand.Handle() {
-            
+
             @Override
             public void run(CommandContext ctx) throws CommandException {
                 throw new CommandException("test error");