# HG changeset patch # User Omair Majid # Date 1361985477 18000 # Node ID 6f00af7150b2bcc597468bd239fd4e15a881ad9e # Parent e053b0c2d48fa1e0aa312fb34d8458ea2c9dddcc Better error message when a command is not available Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005904.html diff -r e053b0c2d48f -r 6f00af7150b2 launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java --- 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 t = LocaleResources.createLocalizer(); + private ClientPreferences prefs; private String[] args; @@ -89,8 +91,6 @@ private BundleManager registry; private final DbServiceFactory dbServiceFactory; - private Translate t = LocaleResources.createLocalizer(); - public LauncherImpl(BundleContext context, CommandContextFactory cmdCtxFactory, BundleManager registry) { this(context, cmdCtxFactory, registry, new LoggingInitializer(), new DbServiceFactory()); } @@ -213,6 +213,7 @@ Collection> 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())) { diff -r e053b0c2d48f -r 6f00af7150b2 launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java --- 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, diff -r e053b0c2d48f -r 6f00af7150b2 launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties --- 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 diff -r e053b0c2d48f -r 6f00af7150b2 launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java --- 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);