Repository: james-project Updated Branches: refs/heads/master 96d1c4ede -> 28cfa6d6f
JAMES-2056 Add default values for host & port in the CLI Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/10037483 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/10037483 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/10037483 Branch: refs/heads/master Commit: 1003748307955a723b38dec7efdfa69556c9b10f Parents: 96d1c4e Author: Antoine Duprat <[email protected]> Authored: Tue Jun 13 13:51:02 2017 +0200 Committer: Antoine Duprat <[email protected]> Committed: Tue Jun 13 13:51:02 2017 +0200 ---------------------------------------------------------------------- .../java/org/apache/james/cli/ServerCmd.java | 25 +++++++++----- .../org/apache/james/cli/ServerCmdTest.java | 35 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/10037483/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java ---------------------------------------------------------------------- diff --git a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java index a9f6e69..efbf4b6 100644 --- a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java +++ b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java @@ -28,7 +28,6 @@ import java.util.Map.Entry; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; @@ -58,6 +57,7 @@ import org.slf4j.LoggerFactory; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; +import com.google.common.base.Strings; /** * Command line utility for managing various aspect of the James server. @@ -68,16 +68,14 @@ public class ServerCmd { public static final String PORT_OPT_LONG = "port"; public static final String PORT_OPT_SHORT = "p"; + private static final String DEFAULT_HOST = "127.0.0.1"; private static final int DEFAULT_PORT = 9999; private static final Logger LOG = LoggerFactory.getLogger(ServerCmd.class); private static Options createOptions() { - Options options = new Options(); - Option optHost = new Option(HOST_OPT_SHORT, HOST_OPT_LONG, true, "node hostname or ip address"); - optHost.setRequired(true); - options.addOption(optHost); - options.addOption(PORT_OPT_SHORT, PORT_OPT_LONG, true, "remote jmx agent port number"); - return options; + return new Options() + .addOption(HOST_OPT_SHORT, HOST_OPT_LONG, true, "node hostname or ip address") + .addOption(PORT_OPT_SHORT, PORT_OPT_LONG, true, "remote jmx agent port number"); } /** @@ -110,7 +108,7 @@ public class ServerCmd { StopWatch stopWatch = new StopWatch(); stopWatch.start(); CommandLine cmd = parseCommandLine(args); - JmxConnection jmxConnection = new JmxConnection(cmd.getOptionValue(HOST_OPT_LONG), getPort(cmd)); + JmxConnection jmxConnection = new JmxConnection(getHost(cmd), getPort(cmd)); CmdType cmdType = new ServerCmd( new JmxDataProbe().connect(jmxConnection), new JmxMailboxProbe().connect(jmxConnection), @@ -148,9 +146,18 @@ public class ServerCmd { } @VisibleForTesting + static String getHost(CommandLine cmd) { + String host = cmd.getOptionValue(HOST_OPT_LONG); + if (Strings.isNullOrEmpty(host)) { + return DEFAULT_HOST; + } + return host; + } + + @VisibleForTesting static int getPort(CommandLine cmd) throws ParseException { String portNum = cmd.getOptionValue(PORT_OPT_LONG); - if (portNum != null) { + if (!Strings.isNullOrEmpty(portNum)) { try { return validatePortNumber(Integer.parseInt(portNum)); } catch (NumberFormatException e) { http://git-wip-us.apache.org/repos/asf/james-project/blob/10037483/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java ---------------------------------------------------------------------- diff --git a/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java b/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java index 2e2ae1e..570167b 100644 --- a/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java +++ b/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java @@ -1267,6 +1267,41 @@ public class ServerCmdTest { } @Test + public void getHostShouldUseDefaultValueWhenNone() throws Exception { + String[] arguments = { "-p", "9999", "command", "arg1", "arg2", "arg3" }; + CommandLine commandLine = ServerCmd.parseCommandLine(arguments); + assertThat(ServerCmd.getHost(commandLine)).isEqualTo("127.0.0.1"); + } + + @Test + public void getHostShouldUseDefaultValueWhenEmpty() throws Exception { + String[] arguments = { "-h", "", "-p", "9999", "command", "arg1", "arg2", "arg3" }; + CommandLine commandLine = ServerCmd.parseCommandLine(arguments); + assertThat(ServerCmd.getHost(commandLine)).isEqualTo("127.0.0.1"); + } + + @Test + public void getHostShouldReturnValueWhenGiven() throws Exception { + String[] arguments = { "-h", "123.4.5.6", "-p", "9999", "command", "arg1", "arg2", "arg3" }; + CommandLine commandLine = ServerCmd.parseCommandLine(arguments); + assertThat(ServerCmd.getHost(commandLine)).isEqualTo("123.4.5.6"); + } + + @Test + public void getPortShouldUseDefaultValueWhenNone() throws Exception { + String[] arguments = { "-h", "127.0.0.1", "command", "arg1", "arg2", "arg3" }; + CommandLine commandLine = ServerCmd.parseCommandLine(arguments); + assertThat(ServerCmd.getPort(commandLine)).isEqualTo(9999); + } + + @Test + public void getPortShouldUseDefaultValueWhenEmpty() throws Exception { + String[] arguments = { "-h", "127.0.0.1", "-p", "", "command", "arg1", "arg2", "arg3" }; + CommandLine commandLine = ServerCmd.parseCommandLine(arguments); + assertThat(ServerCmd.getPort(commandLine)).isEqualTo(9999); + } + + @Test public void getPortShouldRetrievePort() throws Exception { String[] arguments = { "-h", "127.0.0.1", "-p", "9999", "command", "arg1", "arg2", "arg3" }; CommandLine commandLine = ServerCmd.parseCommandLine(arguments); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
