Mercurial > hg > release > thermostat-0.13
changeset 212:9561fdda8fbc
Implement central CLI launcher.
Reviewed-by: omajid, vanaltj
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000724.html
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Tue, 10 Apr 2012 23:19:45 +0200 |
parents | d951bf8be570 |
children | 4b7c4bfec7a6 |
files | common/src/main/java/com/redhat/thermostat/cli/HelpCommand.java common/src/main/java/com/redhat/thermostat/cli/Launcher.java common/src/test/java/com/redhat/thermostat/cli/HelpCommandTest.java common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java tools/src/main/java/com/redhat/thermostat/tools/Thermostat.java tools/src/main/resources/META-INF/services/com.redhat.thermostat.cli.Command tools/src/test/java/com/redhat/thermostat/tools/ThermostatTest.java |
diffstat | 7 files changed, 131 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/common/src/main/java/com/redhat/thermostat/cli/HelpCommand.java Tue Apr 10 23:18:38 2012 +0200 +++ b/common/src/main/java/com/redhat/thermostat/cli/HelpCommand.java Tue Apr 10 23:19:45 2012 +0200 @@ -40,6 +40,10 @@ public class HelpCommand implements Command { + /** + * + */ + private static final int COMMANDS_COLUMNS_WIDTH = 15; private static final String NAME = "help"; private static final String DESCRIPTION = "show help for a given command or help overview"; private static final String USAGE = "help [COMMAND]\n\n" @@ -73,7 +77,9 @@ private void printCommandSummary(StringBuilder out, Command cmd) { out.append(" "); out.append(cmd.getName()); - out.append("\t\t"); + for (int i = 0; i < COMMANDS_COLUMNS_WIDTH - cmd.getName().length() - 1; i++) { + out.append(" "); + } out.append(cmd.getDescription()); out.append("\n"); }
--- a/common/src/main/java/com/redhat/thermostat/cli/Launcher.java Tue Apr 10 23:18:38 2012 +0200 +++ b/common/src/main/java/com/redhat/thermostat/cli/Launcher.java Tue Apr 10 23:19:45 2012 +0200 @@ -41,17 +41,10 @@ public class Launcher { - public static void main(String[] args) { - new Launcher(args).run(); - } - - private Launcher(String[] args) { - this.args = args; - } - private String[] args; - private void run() { + public void run(String[] args) { + this.args = args; registerDefaultCommands(); if (hasNoArguments()) { runHelpCommand();
--- a/common/src/test/java/com/redhat/thermostat/cli/HelpCommandTest.java Tue Apr 10 23:18:38 2012 +0200 +++ b/common/src/test/java/com/redhat/thermostat/cli/HelpCommandTest.java Tue Apr 10 23:19:45 2012 +0200 @@ -84,15 +84,15 @@ TestCommand cmd1 = new TestCommand("test1"); cmd1.setDescription("test command 1"); - TestCommand cmd2 = new TestCommand("test2"); + TestCommand cmd2 = new TestCommand("test2longname"); cmd2.setDescription("test command 2"); ctxFactory.getCommandRegistry().registerCommands(Arrays.asList(cmd1, cmd2)); HelpCommand cmd = new HelpCommand(); cmd.run(ctxFactory.createContext(new String[0])); String expected = "list of commands:\n\n" - + " test1\t\ttest command 1\n" - + " test2\t\ttest command 2\n"; + + " test1 test command 1\n" + + " test2longname test command 2\n"; String actual = ctxFactory.getOutput(); assertEquals(expected, actual); } @@ -122,7 +122,7 @@ cmd.run(ctxFactory.createContext(new String[] { "test12" })); String expected = "list of commands:\n\n" - + " test1\t\ttest command 1\n"; + + " test1 test command 1\n"; String actual = ctxFactory.getOutput(); assertEquals(expected, actual); }
--- a/common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java Tue Apr 10 23:18:38 2012 +0200 +++ b/common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java Tue Apr 10 23:19:45 2012 +0200 @@ -96,9 +96,9 @@ @Test public void testMainNoArgs() { String expected = "list of commands:\n\n" - + " help\t\tshow help for a given command or help overview\n" - + " test1\t\tdescription 1\n" - + " test2\t\tdescription 2\n"; + + " help show help for a given command or help overview\n" + + " test1 description 1\n" + + " test2 description 2\n"; runAndVerifyCommand(new String[0], expected); } @@ -113,13 +113,13 @@ }); ctxFactory.getCommandRegistry().registerCommands(Arrays.asList(errorCmd)); - Launcher.main(new String[] { "error" }); + new Launcher().run(new String[] { "error" }); assertEquals("test error\n", ctxFactory.getError()); } private void runAndVerifyCommand(String[] args, String expected) { - Launcher.main(args); + new Launcher().run(args); assertEquals(expected, ctxFactory.getOutput()); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/src/main/java/com/redhat/thermostat/tools/Thermostat.java Tue Apr 10 23:19:45 2012 +0200 @@ -0,0 +1,53 @@ +/* + * 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.tools; + +import com.redhat.thermostat.cli.Launcher; + +public class Thermostat { + + private static Launcher launcher = new Launcher(); + + public static void main(String[] args) { + launcher.run(args); + } + + static void setLauncher(Launcher launcher) { + Thermostat.launcher = launcher; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/src/main/resources/META-INF/services/com.redhat.thermostat.cli.Command Tue Apr 10 23:19:45 2012 +0200 @@ -0,0 +1,4 @@ +com.redhat.thermostat.cli.HelpCommand +com.redhat.thermostat.tools.ThermostatService +com.redhat.thermostat.tools.db.DBService +com.redhat.thermostat.agent.AgentApplication
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/src/test/java/com/redhat/thermostat/tools/ThermostatTest.java Tue Apr 10 23:19:45 2012 +0200 @@ -0,0 +1,56 @@ +/* + * 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.tools; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import org.junit.Test; + +import com.redhat.thermostat.cli.Launcher; + + +public class ThermostatTest { + + @Test + public void testThermostatMain() { + Launcher launcher = mock(Launcher.class); + Thermostat.setLauncher(launcher); + Thermostat.main(new String[] { "test1", "test2" }); + verify(launcher).run(new String[] { "test1", "test2" }); + } +} \ No newline at end of file