Test for AgentShell.parseCommand - minor cleanups on the method body - java 1.5 for loop - paramName and paramValue to make the code more readable - NumbersUtil replaced by NumberUtils - Test case for the parseCommand
Signed-off-by: Laszlo Hornyak <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/8d67e153 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/8d67e153 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/8d67e153 Branch: refs/heads/master Commit: 8d67e15365782be6f41a2490e240843af72b1565 Parents: a98e038 Author: Laszlo Hornyak <[email protected]> Authored: Mon Oct 21 23:04:02 2013 +0200 Committer: Laszlo Hornyak <[email protected]> Committed: Tue Oct 22 23:35:09 2013 +0200 ---------------------------------------------------------------------- agent/src/com/cloud/agent/AgentShell.java | 47 +++++++++++---------- agent/test/com/cloud/agent/AgentShellTest.java | 25 +++++++++++ 2 files changed, 50 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8d67e153/agent/src/com/cloud/agent/AgentShell.java ---------------------------------------------------------------------- diff --git a/agent/src/com/cloud/agent/AgentShell.java b/agent/src/com/cloud/agent/AgentShell.java index 66a9783..70eaceb 100644 --- a/agent/src/com/cloud/agent/AgentShell.java +++ b/agent/src/com/cloud/agent/AgentShell.java @@ -36,6 +36,7 @@ import javax.naming.ConfigurationException; import org.apache.commons.daemon.Daemon; import org.apache.commons.daemon.DaemonContext; import org.apache.commons.daemon.DaemonInitException; +import org.apache.commons.lang.math.NumberUtils; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; @@ -191,30 +192,32 @@ public class AgentShell implements IAgentShell, Daemon { String zone = null; String pod = null; String guid = null; - for (int i = 0; i < args.length; i++) { - final String[] tokens = args[i].split("="); + for (String param : args) { + final String[] tokens = param.split("="); if (tokens.length != 2) { - System.out.println("Invalid Parameter: " + args[i]); + System.out.println("Invalid Parameter: " + param); continue; } + final String paramName = tokens[0]; + final String paramValue = tokens[1]; // save command line properties - _cmdLineProperties.put(tokens[0], tokens[1]); - - if (tokens[0].equalsIgnoreCase("port")) { - port = tokens[1]; - } else if (tokens[0].equalsIgnoreCase("threads") || tokens[0].equalsIgnoreCase("workers")) { - workers = tokens[1]; - } else if (tokens[0].equalsIgnoreCase("host")) { - host = tokens[1]; - } else if (tokens[0].equalsIgnoreCase("zone")) { - zone = tokens[1]; - } else if (tokens[0].equalsIgnoreCase("pod")) { - pod = tokens[1]; - } else if (tokens[0].equalsIgnoreCase("guid")) { - guid = tokens[1]; - } else if (tokens[0].equalsIgnoreCase("eth1ip")) { - _privateIp = tokens[1]; + _cmdLineProperties.put(paramName, paramValue); + + if (paramName.equalsIgnoreCase("port")) { + port = paramValue; + } else if (paramName.equalsIgnoreCase("threads") || paramName.equalsIgnoreCase("workers")) { + workers = paramValue; + } else if (paramName.equalsIgnoreCase("host")) { + host = paramValue; + } else if (paramName.equalsIgnoreCase("zone")) { + zone = paramValue; + } else if (paramName.equalsIgnoreCase("pod")) { + pod = paramValue; + } else if (paramName.equalsIgnoreCase("guid")) { + guid = paramValue; + } else if (paramName.equalsIgnoreCase("eth1ip")) { + _privateIp = paramValue; } } @@ -222,16 +225,16 @@ public class AgentShell implements IAgentShell, Daemon { port = getProperty(null, "port"); } - _port = NumbersUtil.parseInt(port, 8250); + _port = NumberUtils.toInt(port, 8250); - _proxyPort = NumbersUtil.parseInt( + _proxyPort = NumberUtils.toInt( getProperty(null, "consoleproxy.httpListenPort"), 443); if (workers == null) { workers = getProperty(null, "workers"); } - _workers = NumbersUtil.parseInt(workers, 5); + _workers = NumberUtils.toInt(workers, 5); if (host == null) { host = getProperty(null, "host"); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8d67e153/agent/test/com/cloud/agent/AgentShellTest.java ---------------------------------------------------------------------- diff --git a/agent/test/com/cloud/agent/AgentShellTest.java b/agent/test/com/cloud/agent/AgentShellTest.java new file mode 100644 index 0000000..883790f --- /dev/null +++ b/agent/test/com/cloud/agent/AgentShellTest.java @@ -0,0 +1,25 @@ +package com.cloud.agent; + +import java.util.UUID; + +import javax.naming.ConfigurationException; + +import junit.framework.Assert; + +import org.junit.Test; + +public class AgentShellTest { + @Test + public void parseCommand() throws ConfigurationException { + AgentShell shell = new AgentShell(); + UUID anyUuid = UUID.randomUUID(); + shell.parseCommand(new String[] { "port=55555", "threads=4", + "host=localhost", "pod=pod1", "guid=" + anyUuid, "zone=zone1" }); + Assert.assertEquals(55555, shell.getPort()); + Assert.assertEquals(4, shell.getWorkers()); + Assert.assertEquals("localhost", shell.getHost()); + Assert.assertEquals(anyUuid.toString(), shell.getGuid()); + Assert.assertEquals("pod1", shell.getPod()); + Assert.assertEquals("zone1", shell.getZone()); + } +}
