Mercurial > hg > thermostat-ng > agent
changeset 2694:80b17b58e7a7
Make "agent" command default (and only) option
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023749.html
reviewed-by: jmatsuok, jerboaa
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentOptionParser.java Wed Jun 14 12:33:08 2017 +0200 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentOptionParser.java Mon Jun 19 20:10:51 2017 +0200 @@ -59,18 +59,6 @@ if (args.hasArgument("saveOnExit")) { configuration.setPurge(false); } - - if (args.hasArgument("dbUrl")) { - String url = args.getArgument("dbUrl"); - configuration.setDatabaseURL(url); - } else { - if (configuration.getDBConnectionString() == null) { - System.err.println("database url not specified... must be " + - "either set in config or passed on " + - "the command line"); - isHelp = true; - } - } } public boolean isHelp() {
--- a/agent/core/src/test/java/com/redhat/thermostat/agent/config/AgentOptionParserTest.java Wed Jun 14 12:33:08 2017 +0200 +++ b/agent/core/src/test/java/com/redhat/thermostat/agent/config/AgentOptionParserTest.java Mon Jun 19 20:10:51 2017 +0200 @@ -59,7 +59,6 @@ Properties agentProperties = new Properties(); agentProperties.setProperty("SAVE_ON_EXIT", "true"); - agentProperties.setProperty("CONFIG_LISTEN_ADDRESS", "42.42.42.42:42"); tmpFile = new File(TestUtils.setupAgentConfigs(agentProperties)); } @@ -69,34 +68,17 @@ tmpFile.delete(); } - @Test - public void testConfigs1() throws IOException, InvalidConfigurationException { - - SimpleArguments args = new SimpleArguments(); - args.addArgument("dbUrl", "testURL"); - - AgentStartupConfiguration configs = new AgentStartupConfiguration(); - configs.setDatabaseURL("Not the right URL"); - configs.setPurge(true); - AgentOptionParser parser = new AgentOptionParser(configs, args); - parser.parse(); - - Assert.assertEquals("testURL", configs.getDBConnectionString()); - Assert.assertTrue(configs.purge()); - } - + @Test public void testConfigs2() throws IOException, InvalidConfigurationException { SimpleArguments args = new SimpleArguments(); - args.addArgument("dbUrl", "testURL2"); args.addArgument("saveOnExit", "--saveOnExit"); AgentStartupConfiguration configs = new AgentStartupConfiguration(); AgentOptionParser parser = new AgentOptionParser(configs, args); parser.parse(); - Assert.assertEquals("testURL2", configs.getDBConnectionString()); Assert.assertFalse(configs.purge()); } }
--- a/distribution/config/agent.properties Wed Jun 14 12:33:08 2017 +0200 +++ b/distribution/config/agent.properties Mon Jun 19 20:10:51 2017 +0200 @@ -1,7 +1,3 @@ # Indicates if this agent will save its data to the database on exit # or rather will purge the db SAVE_ON_EXIT=true - -# Connection URL to storage. This can be overridden with the -d option -# on the command line. -DB_URL=http://127.0.0.1:8999/thermostat/storage
--- a/distribution/config/commands/agent.properties Wed Jun 14 12:33:08 2017 +0200 +++ b/distribution/config/commands/agent.properties Mon Jun 19 20:10:51 2017 +0200 @@ -11,9 +11,9 @@ also performs user-trigged actions on-demand. The agent will stop when this \ process exits. -usage = agent [-d <url>] [-s] [-l <level>] +usage = [-s] [-l <level>] -options = AUTO_LOG_OPTION, AUTO_DB_OPTIONS, saveOnExit +options = AUTO_LOG_OPTION, saveOnExit saveOnExit.short = s saveOnExit.long = saveOnExit
--- a/distribution/tools/verify-bash-completion.sh Wed Jun 14 12:33:08 2017 +0200 +++ b/distribution/tools/verify-bash-completion.sh Mon Jun 19 20:10:51 2017 +0200 @@ -50,6 +50,5 @@ __check_completion "thermostat --v" "--version" __check_completion "thermostat --p" "--print-osgi-info" -__check_completion "thermostat ag" "agent" exit $errors
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/BuiltInCommandInfo.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/BuiltInCommandInfo.java Mon Jun 19 20:10:51 2017 +0200 @@ -146,14 +146,6 @@ Options propertiesOptions = new Options(); // For checking for conflicts only, will contain OptionGroup members Options propertiesOptionsToAdd = new Options(); // For adding to this.options later, will not contain OptionGroup members - if (optionNames.contains(CommonOptions.OPTIONS_COMMON_DB_OPTIONS)) { - for (Option opt: CommonOptions.getDbOptions()) { - commonOptionsToAdd.addOption(opt); - } - while (optionNames.contains(CommonOptions.OPTIONS_COMMON_DB_OPTIONS)) { - optionNames.remove(CommonOptions.OPTIONS_COMMON_DB_OPTIONS); - } - } if (optionNames.contains(CommonOptions.OPTIONS_COMMON_LOG_OPTION)) { Option opt = CommonOptions.getLogOption(); commonOptionsToAdd.addOption(opt);
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommonOptions.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommonOptions.java Mon Jun 19 20:10:51 2017 +0200 @@ -53,34 +53,21 @@ // The launcher uses username, password and dbUrl options for establishing a // DB connection before the command is run. These options can be added via // this special option in the options section of command.properties. - static final String OPTIONS_COMMON_DB_OPTIONS = "AUTO_DB_OPTIONS"; // The launcher will auto-add a logLevel option if this special option is // specified in the command.properties option section. static final String OPTIONS_COMMON_LOG_OPTION = "AUTO_LOG_OPTION"; static final String LOG_LEVEL_ARG = "logLevel"; - static final String DB_URL_ARG = "dbUrl"; static final String HELP_ARG = "help"; static final Set<String> ALL_COMMON_OPTIONS = new HashSet<>(4); static { ALL_COMMON_OPTIONS.add(LOG_LEVEL_ARG); - ALL_COMMON_OPTIONS.add(DB_URL_ARG); } static final Translate<LocaleResources> t = LocaleResources.createLocalizer(); - - static List<Option> getDbOptions() { - String dbUrlDesc = t.localize(LocaleResources.OPTION_DB_URL_DESC).getContents(); - Option dbUrlOption = new Option("d", DB_URL_ARG, true, dbUrlDesc); - dbUrlOption.setRequired(false); - dbUrlOption.setArgName(DB_URL_ARG); - List<Option> options = new ArrayList<>(1); - options.add(dbUrlOption); - return options; - } - + static Option getLogOption() { String desc = t.localize(LocaleResources.OPTION_LOG_LEVEL_DESC).getContents(); Option logOption = new Option("l", LOG_LEVEL_ARG, true, desc);
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java Mon Jun 19 20:10:51 2017 +0200 @@ -113,7 +113,6 @@ if (nonParsed.isEmpty()) { printOptionSummaries(ctx); - printCommandSummaries(ctx); } else { printCommandUsage(ctx, nonParsed.get(0)); } @@ -137,7 +136,6 @@ String groupName = entry.getKey(); CommandGroupMetadata metadata = commandGroupMetadataMap.get(groupName); if (metadata == null) { - logger.warning("No metadata provided for command group \"" + groupName + "\""); metadata = new CommandGroupMetadata(groupName, groupName, Integer.MAX_VALUE); commandGroupMetadataMap.put(groupName, metadata); } @@ -153,8 +151,6 @@ } private void printCommandSummaries(CommandContext ctx) { - ctx.getConsole().getOutput().print(translator.localize(LocaleResources.COMMAND_HELP_COMMAND_LIST_HEADER).getContents()); - TableRenderer renderer = new TableRenderer(2, COMMANDS_COLUMNS_WIDTH); for (Map.Entry<CommandGroupMetadata, SortedSet<CommandInfo>> group : commandGroupMap.entrySet()) { @@ -177,8 +173,6 @@ } private void printOptionSummaries(CommandContext ctx) { - ctx.getConsole().getOutput().print(translator.localize(LocaleResources.COMMAND_HELP_COMMAND_OPTION_HEADER).getContents()); - TableRenderer renderer = new TableRenderer(2, COMMANDS_COLUMNS_WIDTH); renderer.printLine(" " + Version.VERSION_OPTION, "display the version of the current thermostat installation"); @@ -213,11 +207,10 @@ Options options = info.getOptions(); String usage = APP_NAME + " " + info.getUsage() + "\n" + info.getDescription(); - String header = ""; - header = header + "\n" + APP_NAME + " " + info.getName(); + Option help = CommonOptions.getHelpOption(); options.addOption(help); - helpFormatter.printHelp(pw, MAX_COLUMN_WIDTH, usage, header, options, 2, 4, null); + helpFormatter.printHelp(pw, MAX_COLUMN_WIDTH, usage, "", options, 2, 4, null); if (!info.getSubcommands().isEmpty()) { pw.println();
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java Mon Jun 19 20:10:51 2017 +0200 @@ -132,19 +132,54 @@ run(args, null); } + private void help(String[] args, Collection<ActionListener<ApplicationState>> listeners) { + showVersion(); + runHelpCommandFor("agent"); + runCommandFromArguments(args, listeners); + } + @Override public void run(String[] args, Collection<ActionListener<ApplicationState>> listeners) { usageCount.incrementAndGet(); try { - if (hasNoArguments(args)) { - runHelpCommand(); - } else if (isVersionQuery(args)) { + + boolean noArgs = hasNoArguments(args); + if (noArgs) { + runCommandFromArguments(new String [] {"agent"}, listeners); + return; + } + + if (isVersionQuery(args)) { showVersion(); + } else if (isInfoQuery(args)) { showInfo(); + } else { - runCommandFromArguments(args, listeners); + + if (args[0].equalsIgnoreCase("help")) { + help(args, listeners); + + } else { + + List<String> realArgs = new ArrayList<>(); + if (!args[0].equalsIgnoreCase("agent")) { + // prepend agent to the command line argument + // and execute + realArgs.add("agent"); + } + + for (String arg : args) { + realArgs.add(arg); + if (arg.equalsIgnoreCase("--help")) { + help(new String[] { "help" }, listeners); + return; + } + } + + runCommandFromArguments(realArgs.toArray(new String[0]), listeners); + } } } catch (NoClassDefFoundError e) { // This could mean pom is missing <Private-Package> or <Export-Package> lines. @@ -386,6 +421,8 @@ } private void showInfo() { + showVersion(); + PrintStream stdOut = cmdCtxFactory.getConsole().getOutput(); stdOut.println(CommonPaths.THERMOSTAT_HOME + "=" + paths.getSystemThermostatHome().getAbsolutePath()); stdOut.println(CommonPaths.USER_THERMOSTAT_HOME + "=" + paths.getUserThermostatHome().getAbsolutePath());
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java Mon Jun 19 20:10:51 2017 +0200 @@ -52,9 +52,6 @@ COMMAND_AVAILABLE_OUTSIDE_SHELL, COMMAND_AVAILABLE_OUTSIDE_SHELL_ONLY, - COMMAND_HELP_COMMAND_LIST_HEADER, - COMMAND_HELP_COMMAND_OPTION_HEADER, - COMMAND_GROUP_HEADER, COMMAND_SHELL_USER_GUIDE, @@ -65,7 +62,6 @@ SEE_ALSO_HEADER, - OPTION_DB_URL_DESC, OPTION_LOG_LEVEL_DESC, OPTION_HELP_DESC, @@ -78,7 +74,6 @@ LAUNCHER_CONNECTION_ERROR, LAUNCHER_FIRST_LAUNCH_MSG, - INVALID_DB_URL, PARSE_ISSUES_CALLED_BEFORE_PARSE, PARSER_ERROR, PARSER_WARNING,
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParser.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParser.java Mon Jun 19 20:10:51 2017 +0200 @@ -600,7 +600,6 @@ } List<Option> allKnownOptions = new ArrayList<Option>(); - allKnownOptions.addAll(CommonOptions.getDbOptions()); allKnownOptions.add(CommonOptions.getLogOption()); for (Option knownOption : allKnownOptions) {
--- a/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties Mon Jun 19 20:10:51 2017 +0200 @@ -14,9 +14,6 @@ # Error Message COMMAND_AVAILABLE_OUTSIDE_SHELL_ONLY = The {0} command is not supported from within the thermostat shell. -COMMAND_HELP_COMMAND_LIST_HEADER = list of commands:\n\n -COMMAND_HELP_COMMAND_OPTION_HEADER = list of global options:\n\n - COMMAND_GROUP_HEADER = {0}: SUBCOMMANDS_SECTION_HEADER=Subcommands: @@ -27,7 +24,6 @@ COMMAND_SHELL_USER_GUIDE = Please see the User Guide at {0} COMMAND_SHELL_IO_EXCEPTION = IOException caught during Thermostat shell session. -OPTION_DB_URL_DESC = connect to the given URL OPTION_LOG_LEVEL_DESC = sets the log level for this invocation. Possible values \ for <logLevel> in decreasing severity are: SEVERE, WARNING, INFO, CONFIG, FINE, \ FINER, FINEST and OFF @@ -43,8 +39,6 @@ LAUNCHER_FIRST_LAUNCH_MSG = This appears to be the first time Thermostat has been launched.\n\ Please run ''{0}''. Please see the User Guide for more details: {1}. -INVALID_DB_URL = Warning: Shell encountered a invalid database connection url: {0} Connection to storage may be corrupted. - PARSE_ISSUES_CALLED_BEFORE_PARSE = ShellArgsParser#getParseIssues called before ShellArgsParser#parse PARSER_ERROR = Could not parse input:\n{0} PARSER_WARNING = Malformed input, attempting to proceed anyway:\n{0}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/BuiltInCommandInfoTest.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/BuiltInCommandInfoTest.java Mon Jun 19 20:10:51 2017 +0200 @@ -163,42 +163,6 @@ } @Test - public void canAddCommonDBOptions() { - Properties props = new Properties(); - String name = "name"; - props.put("options", "AUTO_DB_OPTIONS"); - BuiltInCommandInfo info = new BuiltInCommandInfo(name, props); - - Options options = info.getOptions(); - assertTrue(options.hasOption(CommonOptions.DB_URL_ARG)); - assertFalse(options.getOption(CommonOptions.DB_URL_ARG).isRequired()); - Option dbUrlOption = options.getOption(CommonOptions.DB_URL_ARG); - Translate<LocaleResources> t = LocaleResources.createLocalizer(); - assertEquals(t.localize(LocaleResources.OPTION_DB_URL_DESC).getContents(), dbUrlOption.getDescription()); - assertEquals("d", dbUrlOption.getOpt()); - assertEquals("dbUrl", dbUrlOption.getLongOpt()); - } - - @Test - public void requiredCommandPropertyOverridesCommonDbOptions() { - Properties props = new Properties(); - String name = "name"; - props.put("options", "AUTO_DB_OPTIONS, dbUrl"); - props.put("dbUrl.required", "true"); - BuiltInCommandInfo info = new BuiltInCommandInfo(name, props); - - Options options = info.getOptions(); - assertTrue(options.hasOption(CommonOptions.DB_URL_ARG)); - assertTrue(options.hasOption("d")); - Option dbUrlOption1 = options.getOption(CommonOptions.DB_URL_ARG); - Option dbUrlOption2 = options.getOption("d"); - assertSame(dbUrlOption1, dbUrlOption2); - assertTrue(dbUrlOption1.isRequired()); - assertEquals("dbUrl", dbUrlOption1.getLongOpt()); - assertEquals("d", dbUrlOption1.getOpt()); - } - - @Test public void canAddLogOption() { Properties props = new Properties(); String name = "name"; @@ -276,40 +240,5 @@ BuiltInCommandInfo info = new BuiltInCommandInfo(name, props); } - @Test(expected=RuntimeException.class) - public void verifyConflictsWithCommonShortOption() { - Properties props = new Properties(); - String name = "name"; - props.put("options", "AUTO_DB_OPTIONS, dbUrl"); - props.put("dbUrl.short", "x"); - props.put("dbUrl.long", "dbUrl"); - props.put("dbUrl.required", "true"); - @SuppressWarnings("unused") - BuiltInCommandInfo info = new BuiltInCommandInfo(name, props); - } - - @Test(expected=RuntimeException.class) - public void verifyConflictsWithCommonLongOption() { - Properties props = new Properties(); - String name = "name"; - props.put("options", "AUTO_DB_OPTIONS, dbUrl"); - props.put("dbUrl.short", "d"); - props.put("dbUrl.long", "notDbUrl"); - props.put("dbUrl.required", "true"); - @SuppressWarnings("unused") - BuiltInCommandInfo info = new BuiltInCommandInfo(name, props); - } - - @Test(expected=RuntimeException.class) - public void verifyDescriptionConflictsWithCommonOption() { - Properties props = new Properties(); - String name = "name"; - props.put("options", "AUTO_DB_OPTIONS, dbUrl"); - props.put("dbUrl.description", "An attempt to cause confusion."); - props.put("dbUrl.required", "true"); - @SuppressWarnings("unused") - BuiltInCommandInfo info = new BuiltInCommandInfo(name, props); - } - }
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommonOptionsTest.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommonOptionsTest.java Mon Jun 19 20:10:51 2017 +0200 @@ -36,12 +36,9 @@ package com.redhat.thermostat.launcher.internal; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.util.List; - import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.junit.Test; @@ -59,20 +56,6 @@ assertFalse(logOption.isRequired()); assertTrue(logOption.hasArg()); } - - @Test - public void canGetDbOptions() { - List<Option> opts = CommonOptions.getDbOptions(); - Options options = new Options(); - for (Option opt: opts) { - options.addOption(opt); - } - assertTrue(options.hasOption("dbUrl")); - assertFalse(options.getOption("dbUrl").isRequired()); - assertTrue(options.getOption("dbUrl").hasArg()); - Option dbUrlOption = options.getOption("dbUrl"); - assertEquals(CommonOptions.DB_URL_ARG, dbUrlOption.getArgName()); - } @Test public void canGetHelpOption() {
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java Mon Jun 19 20:10:51 2017 +0200 @@ -115,10 +115,7 @@ cmd.setCommandGroupMetadataSource(commandGroupMetadataSource); Arguments args = mock(Arguments.class); cmd.run(ctxFactory.createContext(args)); - String expected = "list of global options:\n\n" - + GLOBAL_OPTIONS - + "\n" - + "list of commands:\n\n"; + String expected = GLOBAL_OPTIONS + "\n"; String actual = ctxFactory.getOutput(); assertEquals(expected, actual); } @@ -146,12 +143,7 @@ Arguments args = mock(Arguments.class); cmd.run(ctxFactory.createContext(args)); - String expected = "list of global options:\n\n" - + GLOBAL_OPTIONS - + "\n" - + "list of commands:\n\n" - + " test1 test command 1\n" - + " test2longname test command 2\n"; + String expected = GLOBAL_OPTIONS + "\n"; String actual = ctxFactory.getOutput(); assertEquals(expected, actual); } @@ -184,8 +176,7 @@ String actual = ctxFactory.getOutput(); assertEquals("usage: thermostat usage of test command\n" + - " description of test command\n\n" + - "thermostat test1\n" + + " description of test command\n" + " --help show usage of command\n\n" + "Subcommands:\n" + "\n" + @@ -226,15 +217,7 @@ cmd.run(ctxFactory.createContext(args)); String actual = ctxFactory.getOutput(); - String expected = "list of global options:\n\n" - + GLOBAL_OPTIONS - + "\n" - + "list of commands:\n\n" - + " help show help\n" - + " test1 test command 1\n" - + " test2 test command 2\n" - + " test3 test command 3\n" - + " test4 test command 4\n"; + String expected = GLOBAL_OPTIONS + "\n"; assertEquals(expected, actual); } @@ -249,8 +232,7 @@ args.addNonOptionArgument("test1"); cmd.run(ctxFactory.createContext(args)); - String expected = "unknown command 'test1'\n" - + "list of commands:\n\n"; + String expected = "unknown command 'test1'\n"; String actual = ctxFactory.getOutput(); assertEquals(expected, actual); @@ -294,8 +276,6 @@ String actual = ctxFactory.getOutput(); assertEquals("usage: thermostat usage of test1 command\n" + " description of test1 command\n" + - "\n" + - "thermostat test1\n" + " --help show usage of command\n" + "\n" + "See also:\n" + @@ -326,21 +306,11 @@ cmd.run(ctxFactory.createContext(args)); String output = ctxFactory.getOutput(); - assertEquals("list of global options:\n" + - "\n" + - " --version display the version of the current thermostat installation\n" + + assertEquals(" --version display the version of the current thermostat installation\n" + " --print-osgi-info print debug information related to the OSGi framework's boot/shutdown process\n" + " --ignore-bundle-versions ignore exact bundle versions and use whatever version is available\n" + " --boot-delegation boot delegation string passed on to the OSGi framework\n" + - "\n" + - "list of commands:\n" + - "\n" + - "Group Name: \n" + - " test1 summary of test1 command\n" + - " \n" + - "group2: \n" + - " test1 summary of test1 command\n" + - " \n", output); + "\n", output); } }
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java Mon Jun 19 20:10:51 2017 +0200 @@ -93,10 +93,8 @@ public class LauncherImplTest { - private static final String name1 = "test1"; - private static final String name2 = "test2"; - private static final String name3 = "test3"; - private static final String name4 = "test4"; + private static final String name1 = "agent"; + private static SecurityManager secMan; private CommandInfo info1; private File systemPluginRoot; @@ -143,7 +141,6 @@ private Version version; private CommandInfoSource infos; private CommandGroupMetadataSource commandGroupMetadataSource; - private ActionNotifier<ApplicationState> notifier; private LauncherImpl launcher; @@ -157,7 +154,7 @@ TestCommand cmd1 = new TestCommand(new TestCmd1()); info1 = mock(CommandInfo.class); when(info1.getName()).thenReturn(name1); - when(info1.getUsage()).thenReturn(name1 + " <--arg1 <arg>> [--arg2 <arg>]"); + when(info1.getUsage()).thenReturn(" <--arg1 <arg>> [--arg2 <arg>]"); Options options1 = new Options(); Option opt1 = new Option(null, "arg1", true, null); opt1.setRequired(true); @@ -172,43 +169,6 @@ when(info1.getDescription()).thenReturn("description 1"); when(info1.getOptions()).thenReturn(options1); - TestCommand cmd2 = new TestCommand(new TestCmd2()); - CommandInfo info2 = mock(CommandInfo.class); - when(info2.getName()).thenReturn(name2); - Options options2 = new Options(); - Option opt3 = new Option(null, "arg3", true, null); - options2.addOption(opt3); - Option opt4 = new Option(null, "arg4", true, null); - options2.addOption(opt4); - when(info2.getSummary()).thenReturn("description 2"); - when(info2.getOptions()).thenReturn(options2); - - TestCommand cmd3 = new TestCommand(); - CommandInfo info3 = mock(CommandInfo.class); - when(info3.getName()).thenReturn(name3); - cmd3.setStorageRequired(true); - when(info3.getSummary()).thenReturn("description 3"); - when(info3.getOptions()).thenReturn(new Options()); - - // This TestCommand object doesn't need to connect to storage, - // and it is used to test commands without any required option - TestCommand cmd4 = new TestCommand(); - CommandInfo info4 = mock(CommandInfo.class); - when(info4.getName()).thenReturn(name4); - cmd4.setStorageRequired(false); - when(info4.getSummary()).thenReturn("description 4"); - when(info4.getOptions()).thenReturn(new Options()); - - AbstractStateNotifyingCommand basicCmd = mock(AbstractStateNotifyingCommand.class); - CommandInfo basicInfo = mock(CommandInfo.class); - when(basicInfo.getName()).thenReturn("basic"); - when(basicInfo.getSummary()).thenReturn("nothing that means anything"); - when(basicCmd.isStorageRequired()).thenReturn(false); - Options options = new Options(); - when(basicInfo.getOptions()).thenReturn(options); - notifier = mock(ActionNotifier.class); - when(basicCmd.getNotifier()).thenReturn(notifier); - CommandInfo helpCommandInfo = mock(CommandInfo.class); when(helpCommandInfo.getName()).thenReturn("help"); when(helpCommandInfo.getSummary()).thenReturn("print help information"); @@ -221,28 +181,15 @@ CommandRegistry reg = ctxFactory.getCommandRegistry(); reg.registerCommand("help", helpCommand); reg.registerCommand(name1, cmd1); - reg.registerCommand(name2, cmd2); - reg.registerCommand(name3, cmd3); - reg.registerCommand(name4, cmd4); - reg.registerCommand("basic", basicCmd); infos = mock(CommandInfoSource.class); bundleContext.registerService(CommandInfoSource.class, infos, null); when(infos.getCommandInfo(name1)).thenReturn(info1); - when(infos.getCommandInfo(name2)).thenReturn(info2); - when(infos.getCommandInfo(name3)).thenReturn(info3); - when(infos.getCommandInfo(name4)).thenReturn(info4); - when(infos.getCommandInfo("basic")).thenReturn(basicInfo); when(infos.getCommandInfo("help")).thenReturn(helpCommandInfo); - when(infos.getCommandInfo("setup")).thenReturn(mock(CommandInfo.class)); Collection<CommandInfo> infoList = new ArrayList<CommandInfo>(); infoList.add(helpCommandInfo); - infoList.add(basicInfo); infoList.add(info1); - infoList.add(info2); - infoList.add(info3); - infoList.add(info4); when(infos.getCommandInfos()).thenReturn(infoList); @@ -305,24 +252,17 @@ @Test public void testMain() { runAndVerifyCommand(new String[] {name1, "--arg1", "Hello", "--arg2", "World"}, "Hello, World"); - - ctxFactory.reset(); - - runAndVerifyCommand(new String[] {"test2", "--arg3", "Hello", "--arg4", "World"}, "World: Hello"); } @Test public void testMainNoArgs() { - String expected = "list of global options:\n\n" - + HelpCommandTest.GLOBAL_OPTIONS - + "\n" - + "list of commands:\n\n" - + " help print help information\n" - + " basic nothing that means anything\n" - + " test1 description 1\n" - + " test2 description 2\n" - + " test3 description 3\n" - + " test4 description 4\n"; + String expected = "Missing required option: --arg1\n" + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + + " description 1\n" + + " --arg1 <arg>\n" + + " --arg2 <arg>\n" + + " --help show usage of command\n" + + " -l,--logLevel <arg>\n"; runAndVerifyCommand(new String[0], expected); } @@ -337,14 +277,17 @@ 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" - + " basic nothing that means anything\n" - + " test1 description 1\n" - + " test2 description 2\n" - + " test3 description 3\n" - + " test4 description 4\n"; + String expected = "null\n" + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + + " description 1\n" + + " --arg1 <arg>\n" + + " --arg2 <arg>\n" + + " --help show usage of command\n" + + " -l,--logLevel <arg>\n" + + " --version display the version of the current thermostat installation\n" + + " --print-osgi-info print debug information related to the OSGi framework's boot/shutdown process\n" + + " --ignore-bundle-versions ignore exact bundle versions and use whatever version is available\n" + + " --boot-delegation boot delegation string passed on to the OSGi framework\n\n"; runAndVerifyCommand(new String[] {"--help"}, expected); } @@ -352,14 +295,13 @@ 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" - + " basic nothing that means anything\n" - + " test1 description 1\n" - + " test2 description 2\n" - + " test3 description 3\n" - + " test4 description 4\n"; + String expected = "Could not parse options: Unrecognized option: -help\n" + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + + " description 1\n" + + " --arg1 <arg>\n" + + " --arg2 <arg>\n" + + " --help show usage of command\n" + + " -l,--logLevel <arg>\n"; runAndVerifyCommand(new String[] {"-help"}, expected); } @@ -367,14 +309,13 @@ 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" - + " basic nothing that means anything\n" - + " test1 description 1\n" - + " test2 description 2\n" - + " test3 description 3\n" - + " test4 description 4\n"; + String expected = "Missing required option: --arg1\n" + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + + " description 1\n" + + " --arg1 <arg>\n" + + " --arg2 <arg>\n" + + " --help show usage of command\n" + + " -l,--logLevel <arg>\n"; runAndVerifyCommand(new String[] {"foobarbaz"}, expected); } @@ -382,14 +323,13 @@ 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" - + " basic nothing that means anything\n" - + " test1 description 1\n" - + " test2 description 2\n" - + " test3 description 3\n" - + " test4 description 4\n"; + String expected = "Could not parse options: Unrecognized option: --bar\n" + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + + " description 1\n" + + " --arg1 <arg>\n" + + " --arg2 <arg>\n" + + " --help show usage of command\n" + + " -l,--logLevel <arg>\n"; runAndVerifyCommand(new String[] {"foo", "--bar", "baz"}, expected); } @@ -424,10 +364,8 @@ when(info1.getSubcommands()).thenReturn(Collections.singletonList(subInfo)); String expected = "Missing required option: -o\n" + - "usage: thermostat test1 <--arg1 <arg>> [--arg2 <arg>]\n" + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + " description 1\n" + - "\n" + - "thermostat test1\n" + " --arg1 <arg>\n" + " --arg2 <arg>\n" + " --help show usage of command\n" + @@ -554,44 +492,32 @@ @Test public void testBadOption() { String expected = "Could not parse options: Unrecognized option: --argNotAccepted\n" - + "usage: thermostat test1 <--arg1 <arg>> [--arg2 <arg>]\n" - + " description 1\n" - + "\n" - + "thermostat test1\n" - + " --arg1 <arg>\n" - + " --arg2 <arg>\n" - + " --help show usage of command\n" - + " -l,--logLevel <arg>\n"; - runAndVerifyCommand(new String[] {"test1", "--arg1", "arg1value", "--argNotAccepted"}, expected); + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + + " description 1\n" + + " --arg1 <arg>\n" + + " --arg2 <arg>\n" + + " --help show usage of command\n" + + " -l,--logLevel <arg>\n"; + runAndVerifyCommand(new String[] {"agent", "--arg1", "arg1value", "--argNotAccepted"}, expected); } @Test public void testMissingRequiredOption() { String expected = "Missing required option: --arg1\n" - + "usage: thermostat test1 <--arg1 <arg>> [--arg2 <arg>]\n" + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + " description 1\n" - + "\n" - + "thermostat test1\n" + " --arg1 <arg>\n" + " --arg2 <arg>\n" + " --help show usage of command\n" + " -l,--logLevel <arg>\n"; runAndVerifyCommand(new String[] {"test1"}, expected); } - - @Test - public void testMissingNotRequiredOption() { - String expected = ""; - runAndVerifyCommand(new String[] {"test4"}, expected); - } @Test public void testOptionMissingRequiredArgument() { String expected = "Could not parse options: Missing argument for option: arg1\n" - + "usage: thermostat test1 <--arg1 <arg>> [--arg2 <arg>]\n" + + "usage: thermostat <--arg1 <arg>> [--arg2 <arg>]\n" + " description 1\n" - + "\n" - + "thermostat test1\n" + " --arg1 <arg>\n" + " --arg2 <arg>\n" + " --help show usage of command\n" @@ -599,42 +525,6 @@ runAndVerifyCommand(new String[] {"test1", "--arg1"}, expected); } - @Test - public void testCommandInfoNotFound() throws CommandInfoNotFoundException, BundleException, IOException { - when(infos.getCommandInfo("foo")).thenThrow(new CommandInfoNotFoundException("foo")); - - String expected = "unknown command 'foo'\n" - + "list of commands:\n\n" - + " help print help information\n" - + " basic nothing that means anything\n" - + " test1 description 1\n" - + " test2 description 2\n" - + " test3 description 3\n" - + " test4 description 4\n"; - runAndVerifyCommand(new String[] {"foo"}, expected); - } - - @Test - public void testMainExceptionInCommand() { - TestCommand errorCmd = new TestCommand(new TestCommand.Handle() { - - @Override - public void run(CommandContext ctx) throws CommandException { - throw new CommandException(new LocalizedString("test error")); - } - - }); - ctxFactory.getCommandRegistry().registerCommand("error", errorCmd); - CommandInfo cmdInfo = mock(CommandInfo.class); - when(cmdInfo.getName()).thenReturn("error"); - when(cmdInfo.getOptions()).thenReturn(new Options()); - when(infos.getCommandInfo("error")).thenReturn(cmdInfo); - - wrappedRun(launcher, new String[] { "error" }); - assertEquals("test error\n", ctxFactory.getError()); - - } - private void runAndVerifyCommand(String[] args, String expected) { wrappedRun(launcher, args); assertEquals(expected, ctxFactory.getOutput()); @@ -705,18 +595,6 @@ } @Test - public void verifyListenersAdded() { - @SuppressWarnings("unchecked") - ActionListener<ApplicationState> listener = mock(ActionListener.class); - Collection<ActionListener<ApplicationState>> listeners = new ArrayList<>(); - listeners.add(listener); - String[] args = new String[] {"basic"}; - - wrappedRun(launcher, args, listeners); - verify(notifier).addActionListener(listener); - } - - @Test public void verifyShutdown() throws BundleException { wrappedRun(launcher, new String[] { "test1" });
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParserTest.java Wed Jun 14 12:33:08 2017 +0200 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParserTest.java Mon Jun 19 20:10:51 2017 +0200 @@ -609,19 +609,6 @@ Options opts = command.getOptions(); assertTrue(opts.getRequiredOptions().isEmpty()); - - Option dbUrlOption = opts.getOption("d"); - assertNotNull(dbUrlOption); - - Option otherDbUrlOption = opts.getOption("dbUrl"); - assertSame(dbUrlOption, otherDbUrlOption); - - Translate<LocaleResources> t = LocaleResources.createLocalizer(); - - assertEquals("dbUrl", dbUrlOption.getArgName()); - assertEquals(1, dbUrlOption.getArgs()); - assertEquals(t.localize(LocaleResources.OPTION_DB_URL_DESC).getContents(), dbUrlOption.getDescription()); - assertFalse(dbUrlOption.isRequired()); } @Test