changeset 1010:6f00af7150b2

Better error message when a command is not available Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005904.html
author Omair Majid <omajid@redhat.com>
date Wed, 27 Feb 2013 12:17:57 -0500
parents e053b0c2d48f
children 412c3e8caf4b
files 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/LauncherImplTest.java
diffstat 4 files changed, 22 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Wed Feb 27 12:54:23 2013 -0500
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Wed Feb 27 12:17:57 2013 -0500
@@ -76,6 +76,8 @@
 
 public class LauncherImpl implements Launcher {
 
+    private static final Translate<LocaleResources> t = LocaleResources.createLocalizer();
+
     private ClientPreferences prefs;
 
     private String[] args;
@@ -89,8 +91,6 @@
     private BundleManager registry;
     private final DbServiceFactory dbServiceFactory;
 
-    private Translate<LocaleResources> t = LocaleResources.createLocalizer();
-    
     public LauncherImpl(BundleContext context, CommandContextFactory cmdCtxFactory, BundleManager registry) {
         this(context, cmdCtxFactory, registry, new LoggingInitializer(), new DbServiceFactory());
     }
@@ -213,6 +213,7 @@
     		Collection<ActionListener<ApplicationState>> listeners, boolean inShell) throws CommandException {
 
         PrintStream out = cmdCtxFactory.getConsole().getOutput();
+        PrintStream err = cmdCtxFactory.getConsole().getError();
         try {
             registry.addBundlesFor(cmdName);
         } catch (BundleException | IOException e) {
@@ -228,7 +229,7 @@
 
         Command cmd = getCommand(cmdName);
         if (cmd == null) {
-            runHelpCommandFor(cmdName);
+            err.println(t.localize(LocaleResources.COMMAND_DESCRIBED_BUT_NOT_AVAILALBE, cmdName));
             return;
         }
         if ((inShell && !cmd.isAvailableInShell()) || (!inShell && !cmd.isAvailableOutsideShell())) {
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java	Wed Feb 27 12:54:23 2013 -0500
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java	Wed Feb 27 12:17:57 2013 -0500
@@ -42,8 +42,10 @@
 
     CANNOT_GET_COMMAND_INFO,
     UNKNOWN_COMMAND,
+    COMMAND_DESCRIBED_BUT_NOT_AVAILALBE,
 
     COMMAND_HELP_COMMAND_LIST_HEADER,
+
     OPTION_DB_URL_DESC,
     OPTION_USERNAME_DESC,
     OPTION_PASSWORD_DESC,
--- a/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties	Wed Feb 27 12:54:23 2013 -0500
+++ b/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties	Wed Feb 27 12:17:57 2013 -0500
@@ -1,5 +1,6 @@
 CANNOT_GET_COMMAND_INFO = no information about commands
 UNKNOWN_COMMAND = unknown command ''{0}''\n
+COMMAND_DESCRIBED_BUT_NOT_AVAILALBE = ERROR: Information about the command {0} is provided, but the command itself is not available. Was the Command object registered as an OSGi service? Was the bundle providing the command activated?
 
 COMMAND_HELP_COMMAND_LIST_HEADER = list of commands:\n\n
 
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Wed Feb 27 12:54:23 2013 -0500
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Wed Feb 27 12:17:57 2013 -0500
@@ -39,6 +39,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -62,6 +63,8 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
@@ -154,7 +157,7 @@
     private Storage storage;
 
     @Before
-    public void setUp() {
+    public void setUp() throws CommandInfoNotFoundException, BundleException, IOException {
         setupCommandContextFactory();
 
         TestCommand cmd1 = new TestCommand(name1, new TestCmd1());
@@ -221,8 +224,6 @@
 
         ctxFactory.getCommandRegistry().registerCommands(Arrays.asList(helpCommand, cmd1, cmd2, cmd3, basicCmd));
 
-        registry = mock(BundleManager.class);
-
         infos = mock(CommandInfoSource.class);
         when(infos.getCommandInfo(name1)).thenReturn(info1);
         when(infos.getCommandInfo(name2)).thenReturn(info2);
@@ -240,6 +241,17 @@
 
         helpCommand.setCommandInfoSource(infos);
 
+        registry = mock(BundleManager.class);
+        doAnswer(new Answer() {
+            @Override
+            public Void answer(InvocationOnMock invocation) throws Throwable {
+                // simulate the real BundleManager which tries to find a CommandInfo
+                // needed to propagate/handle exceptions properly
+                infos.getCommandInfo((String) invocation.getArguments()[0]);
+                return null;
+            }
+        }).when(registry).addBundlesFor(anyString());
+
         PowerMockito.mockStatic(FrameworkUtil.class);
 
         storage = mock(Storage.class);