changeset 735:d07e5cc900a3

'help' should state that a command is invalid Make help command print the unknown command message. This makes 'help foo' print out that foo is an unknown command in addition to a list of all known commands. Also add i18n in the bundle. Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-October/003871.html
author Omair Majid <omajid@redhat.com>
date Thu, 25 Oct 2012 12:48:30 -0400
parents 869649c31bb0
children fb3632bc0f7f 6adfe1936814
files launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/LocaleResourcesTest.java
diffstat 7 files changed, 151 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java	Thu Oct 25 11:12:48 2012 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java	Thu Oct 25 12:48:30 2012 -0400
@@ -56,10 +56,13 @@
 import com.redhat.thermostat.common.cli.CommandInfoSource;
 import com.redhat.thermostat.common.cli.SimpleCommand;
 import com.redhat.thermostat.common.cli.TableRenderer;
+import com.redhat.thermostat.common.locale.Translate;
 import com.redhat.thermostat.launcher.CommonCommandOptions;
 
 public class HelpCommand extends SimpleCommand {
 
+    private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer();
+
     private static final int COMMANDS_COLUMNS_WIDTH = 14;
     private static final String NAME = "help";
 
@@ -80,7 +83,7 @@
         BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
         ServiceReference infosRef = context.getServiceReference(CommandInfoSource.class);
         CommandInfoSource infos = (CommandInfoSource) context.getService(infosRef);
-        ctx.getConsole().getOutput().print("list of commands:\n\n");
+        ctx.getConsole().getOutput().print(translator.localize(LocaleResources.COMMAND_HELP_COMMAND_LIST_HEADER));
 
         TableRenderer renderer = new TableRenderer(2, COMMANDS_COLUMNS_WIDTH);
 
@@ -107,6 +110,7 @@
             CommandInfo info = infos.getCommandInfo(cmdName);
             printHelp(ctx, info);
         } catch (CommandInfoNotFoundException notFound) {
+            ctx.getConsole().getOutput().print(translator.localize(LocaleResources.UNKNOWN_COMMAND, cmdName));
             printCommandSummaries(ctx);
         } finally {
             context.ungetService(infosRef);
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Thu Oct 25 11:12:48 2012 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Thu Oct 25 12:48:30 2012 -0400
@@ -76,8 +76,6 @@
 
 public class LauncherImpl implements Launcher {
 
-    private static final String UNKNOWN_COMMAND_MESSAGE = "unknown command '%s'\n";
-
     private ClientPreferences prefs;
 
     private String[] args;
@@ -177,6 +175,10 @@
         runCommand("help", new String[0], null);
     }
 
+    private void runHelpCommandFor(String cmdName) {
+        runCommand("help", new String[] { "--", cmdName }, null);
+    }
+
     private void runCommandFromArguments(Collection<ActionListener<ApplicationState>> listeners) {
         runCommand(args[0], Arrays.copyOfRange(args, 1, args.length), listeners);
     }
@@ -201,15 +203,13 @@
             e.printStackTrace(out);
             return;
         } catch (CommandInfoNotFoundException commandNotFound) {
-            out.print(String.format(UNKNOWN_COMMAND_MESSAGE, cmdName));
-            runHelpCommand();
+            runHelpCommandFor(cmdName);
             return;
         }
 
         Command cmd = getCommand(cmdName);
         if (cmd == null) {
-            out.print(String.format(UNKNOWN_COMMAND_MESSAGE, cmdName));
-            runHelpCommand();
+            runHelpCommandFor(cmdName);
             return;
         }
         if (listeners != null && cmd instanceof BasicCommand) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java	Thu Oct 25 12:48:30 2012 -0400
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.launcher.internal;
+
+import com.redhat.thermostat.common.locale.Translate;
+
+public enum LocaleResources {
+
+    UNKNOWN_COMMAND,
+    COMMAND_HELP_COMMAND_LIST_HEADER,
+    ;
+
+    static final String RESOURCE_BUNDLE = "com.redhat.thermostat.launcher.internal.strings";
+
+    public static Translate<LocaleResources> createLocalizer() {
+        return new Translate<>(RESOURCE_BUNDLE, LocaleResources.class);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties	Thu Oct 25 12:48:30 2012 -0400
@@ -0,0 +1,3 @@
+UNKNOWN_COMMAND = unknown command ''{0}''\n
+
+COMMAND_HELP_COMMAND_LIST_HEADER = list of commands:\n\n
\ No newline at end of file
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java	Thu Oct 25 11:12:48 2012 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java	Thu Oct 25 12:48:30 2012 -0400
@@ -61,7 +61,9 @@
 
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.CommandInfo;
+import com.redhat.thermostat.common.cli.CommandInfoNotFoundException;
 import com.redhat.thermostat.common.cli.CommandInfoSource;
+import com.redhat.thermostat.common.cli.SimpleArguments;
 import com.redhat.thermostat.launcher.internal.HelpCommand;
 import com.redhat.thermostat.test.TestCommandContextFactory;
 import com.redhat.thermostat.test.cli.TestCommand;
@@ -240,22 +242,18 @@
     public void verifyHelpUnknownCmdPrintsSummaries() {
 
         CommandInfoSource infos = mock(CommandInfoSource.class);
-        Collection<CommandInfo> infoList = new ArrayList<CommandInfo>();
         
-        CommandInfo info1 = mock(CommandInfo.class);
-        when(info1.getName()).thenReturn("test1");
-        when(info1.getDescription()).thenReturn("test command 1");
-        infoList.add(info1);
-
-        when(infos.getCommandInfos()).thenReturn(infoList);
+        when(infos.getCommandInfo("test1")).thenThrow(new CommandInfoNotFoundException("test1"));
         mockCommandInfoSourceService(infos);
 
         HelpCommand cmd = new HelpCommand();
-        Arguments args = mock(Arguments.class);
+        SimpleArguments args = new SimpleArguments();
+        args.addNonOptionArgument("test1");
         cmd.run(ctxFactory.createContext(args));
 
-        String expected = "list of commands:\n\n"
-                        + " test1         test command 1\n";
+        String expected = "unknown command 'test1'\n"
+                        + "list of commands:\n\n";
+
         String actual = ctxFactory.getOutput();
         assertEquals(expected, actual);
     }
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherTest.java	Thu Oct 25 11:12:48 2012 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherTest.java	Thu Oct 25 12:48:30 2012 -0400
@@ -148,6 +148,7 @@
     private OSGiRegistry registry;
     private LoggingInitializer loggingInitializer;
     private DbServiceFactory dbServiceFactory;
+    private CommandInfoSource infos;
     private ActionNotifier<ApplicationState> notifier;
 
     private LauncherImpl launcher;
@@ -216,12 +217,13 @@
 
         registry = mock(OSGiRegistry.class);
 
-        CommandInfoSource infos = mock(CommandInfoSource.class);
+        infos = mock(CommandInfoSource.class);
         when(infos.getCommandInfo(name1)).thenReturn(info1);
         when(infos.getCommandInfo(name2)).thenReturn(info2);
         when(infos.getCommandInfo(name3)).thenReturn(info3);
         when(infos.getCommandInfo("basic")).thenReturn(basicInfo);
         when(infos.getCommandInfo("help")).thenReturn(helpCommandInfo);
+
         Collection<CommandInfo> infoList = new ArrayList<CommandInfo>();
         infoList.add(helpCommandInfo);
         infoList.add(basicInfo);
@@ -291,6 +293,8 @@
 
     @Test
     public void testMainBadCommand1() {
+        when(infos.getCommandInfo("--help")).thenThrow(new CommandInfoNotFoundException("--help"));
+
         String expected = "unknown command '--help'\n"
             + "list of commands:\n\n"
             + " help          print help information\n"
@@ -303,6 +307,8 @@
 
     @Test
     public void testMainBadCommand2() {
+        when(infos.getCommandInfo("-help")).thenThrow(new CommandInfoNotFoundException("-help"));
+
         String expected = "unknown command '-help'\n"
             + "list of commands:\n\n"
             + " help          print help information\n"
@@ -315,6 +321,8 @@
 
     @Test
     public void testMainBadCommand3() {
+        when(infos.getCommandInfo("foobarbaz")).thenThrow(new CommandInfoNotFoundException("foobarbaz"));
+
         String expected = "unknown command 'foobarbaz'\n"
             + "list of commands:\n\n"
             + " help          print help information\n"
@@ -327,6 +335,8 @@
 
     @Test
     public void testMainBadCommand4() {
+        when(infos.getCommandInfo("foo")).thenThrow(new CommandInfoNotFoundException("foo"));
+
         String expected = "unknown command 'foo'\n"
             + "list of commands:\n\n"
             + " help          print help information\n"
@@ -339,7 +349,9 @@
 
     @Test
     public void testCommandInfoNotFound() throws CommandInfoNotFoundException, BundleException, IOException {
+        when(infos.getCommandInfo("foo")).thenThrow(new CommandInfoNotFoundException("foo"));
         doThrow(new CommandInfoNotFoundException("foo")).when(registry).addBundlesFor("foo");
+
         String expected = "unknown command 'foo'\n"
                 + "list of commands:\n\n"
                 + " help          print help information\n"
@@ -477,7 +489,7 @@
 
     @Test
     public void verifyLoggingIsInitialized() {
-        launcher.setArgs(new String[] { "ignore" });
+        launcher.setArgs(new String[] { "test1" });
         launcher.run();
 
         verify(loggingInitializer).initialize();
@@ -485,7 +497,7 @@
 
     @Test
     public void verifyShutdown() throws BundleException {
-        launcher.setArgs(new String[] { "ignore" });
+        launcher.setArgs(new String[] { "test1" });
         launcher.run();
 
         verify(sysBundle).stop();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LocaleResourcesTest.java	Thu Oct 25 12:48:30 2012 -0400
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.launcher.internal;
+
+import java.io.IOException;
+import java.util.Properties;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class LocaleResourcesTest {
+
+    @Test
+    public void testLocalizedStringsArePresent() throws IOException {
+
+        String stringsResource = "/" + LocaleResources.RESOURCE_BUNDLE.replace(".", "/") + ".properties";
+
+        Properties props = new Properties();
+        props.load(getClass().getResourceAsStream(stringsResource));
+
+        Assert.assertEquals(LocaleResources.values().length, props.values().size());
+        for (LocaleResources resource : LocaleResources.values()) {
+            Assert.assertTrue("missing property from resource bound file: " + resource,
+                              props.containsKey(resource.name()));
+        }
+    }
+}
\ No newline at end of file