# HG changeset patch # User Omair Majid # Date 1361471674 18000 # Node ID a379ce735df595e80ee59170519f0a0f10ada6dd # Parent ff1970bc34a4ae9073d7ed486ad24f6884901929 Print thermostat version when shell starts Reviewed-by: jerboaa, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005788.html diff -r ff1970bc34a4 -r a379ce735df5 client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java --- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java Wed Feb 20 16:33:38 2013 -0500 +++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java Thu Feb 21 13:34:34 2013 -0500 @@ -53,6 +53,7 @@ import org.osgi.framework.ServiceReference; import com.redhat.thermostat.common.Launcher; +import com.redhat.thermostat.common.Version; import com.redhat.thermostat.common.cli.AbstractCommand; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; @@ -72,6 +73,7 @@ private static final String PROMPT = "Thermostat > "; private HistoryProvider historyProvider; + private Version version; private BundleContext bundleContext; @@ -88,12 +90,13 @@ } public ShellCommand() { - this(FrameworkUtil.getBundle(ShellCommand.class).getBundleContext(), new HistoryProvider()); + this(FrameworkUtil.getBundle(ShellCommand.class).getBundleContext(), new Version(), new HistoryProvider()); } - ShellCommand(BundleContext context, HistoryProvider provider) { + ShellCommand(BundleContext context, Version version, HistoryProvider provider) { this.historyProvider = provider; this.bundleContext = context; + this.version = version; } @Override @@ -102,6 +105,7 @@ PersistentHistory history = historyProvider.get(); try { + ctx.getConsole().getOutput().println(version.getVersionInfo()); shellMainLoop(ctx, history, term); } catch (IOException ex) { throw new CommandException(ex); diff -r ff1970bc34a4 -r a379ce735df5 client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ShellCommandTest.java --- a/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ShellCommandTest.java Wed Feb 20 16:33:38 2013 -0500 +++ b/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ShellCommandTest.java Thu Feb 21 13:34:34 2013 -0500 @@ -60,6 +60,7 @@ import com.redhat.thermostat.client.cli.internal.ShellCommand.HistoryProvider; import com.redhat.thermostat.common.Launcher; +import com.redhat.thermostat.common.Version; import com.redhat.thermostat.common.cli.Arguments; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; @@ -68,13 +69,23 @@ public class ShellCommandTest { + static private final String VERSION = "Thermostat some version"; + static private final String VERSION_OUTPUT = VERSION + "\n"; + private ShellCommand cmd; + private BundleContext bundleContext; + private HistoryProvider historyProvider; + private Version version; @Before public void setUp() { bundleContext = mock(BundleContext.class); - cmd = new ShellCommand(bundleContext, new HistoryProvider()); + historyProvider = mock(HistoryProvider.class); + version = mock(Version.class); + when(version.getVersionInfo()).thenReturn(VERSION); + + cmd = new ShellCommand(bundleContext, version, historyProvider); } @After @@ -108,7 +119,7 @@ Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals("Thermostat > quit\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + "Thermostat > quit\n", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); } @@ -119,7 +130,7 @@ Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals("Thermostat > q\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + "Thermostat > q\n", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); } @@ -130,7 +141,7 @@ Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals("Thermostat > ", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + "Thermostat > ", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); } @@ -141,7 +152,7 @@ Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals("Thermostat > \nThermostat > exit\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + "Thermostat > \nThermostat > exit\n", ctxFactory.getOutput()); } @Test @@ -150,8 +161,7 @@ when(history.previous()).thenReturn(true); when(history.current()).thenReturn("old-history-value"); - HistoryProvider provider = mock(HistoryProvider.class); - when(provider.get()).thenReturn(history); + when(historyProvider.get()).thenReturn(history); ServiceReference ref = mock(ServiceReference.class); @@ -160,15 +170,13 @@ when(bundleContext.getService(ref)).thenReturn(launcher); TestCommandContextFactory ctxFactory = new TestCommandContextFactory(bundleContext); - cmd = new ShellCommand(bundleContext, provider); - // "\u001b[A" is the escape code for up-arrow. use xxd -p to generate ctxFactory.setInput("\u001b[A\nexit\n"); Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals("Thermostat > old-history-value\nThermostat > exit\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + "Thermostat > old-history-value\nThermostat > exit\n", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); verify(launcher).setArgs(new String[] {"old-history-value"}); @@ -178,8 +186,7 @@ @Test public void testHistoryIsUpdated() throws CommandException, IOException { PersistentHistory mockHistory = mock(PersistentHistory.class); - HistoryProvider provider = mock(HistoryProvider.class); - when(provider.get()).thenReturn(mockHistory); + when(historyProvider.get()).thenReturn(mockHistory); ServiceReference ref = mock(ServiceReference.class); when(bundleContext.getServiceReference(Launcher.class.getName())).thenReturn(ref); @@ -187,8 +194,6 @@ when(bundleContext.getService(ref)).thenReturn(launcher); TestCommandContextFactory ctxFactory = new TestCommandContextFactory(bundleContext); - cmd = new ShellCommand(bundleContext, provider); - ctxFactory.setInput("add-to-history\nexit\n"); Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); @@ -199,7 +204,7 @@ verify(mockHistory).add("add-to-history"); verify(mockHistory).flush(); - assertEquals("Thermostat > add-to-history\nThermostat > exit\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + "Thermostat > add-to-history\nThermostat > exit\n", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); } diff -r ff1970bc34a4 -r a379ce735df5 integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java --- a/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java Wed Feb 20 16:33:38 2013 -0500 +++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java Thu Feb 21 13:34:34 2013 -0500 @@ -134,6 +134,16 @@ shell.expectClose(); } + + @Test + public void testShellPrintsVersionOnStartup() throws Exception { + Spawn shell = spawnThermostat("shell"); + + shell.expect(SHELL_PROMPT); + + String stdOut = shell.getCurrentStandardOutContents(); + assertTrue(stdOut.contains("Thermostat version ")); + } @Test public void versionArgumentInShellIsNotAllowed() throws Exception { @@ -147,7 +157,6 @@ String stdOut = shell.getCurrentStandardOutContents(); String stdErr = shell.getCurrentStandardErrContents(); - assertFalse(stdOut.matches("Thermostat version \\d+\\.\\d+\\.\\d+\n")); assertMatchesHelpCommandList(shell.getCurrentStandardOutContents()); // use the Pattern.DOTALL flag (?s) so that line terminators match with // ".*". stdOut contains the SHELL_PROMPT too.