Test for AgentShell.loadProperties - stream closed after properties load (with commons io) - test added
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/eb798d31 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/eb798d31 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/eb798d31 Branch: refs/heads/master Commit: eb798d319841f0c4849b556dae2290f2448960cd Parents: 8d67e15 Author: Laszlo Hornyak <[email protected]> Authored: Tue Oct 22 22:14:49 2013 +0200 Committer: Laszlo Hornyak <[email protected]> Committed: Tue Oct 22 23:35:09 2013 +0200 ---------------------------------------------------------------------- agent/pom.xml | 4 ++++ agent/src/com/cloud/agent/AgentShell.java | 12 +++++++++--- agent/test/com/cloud/agent/AgentShellTest.java | 7 +++++++ 3 files changed, 20 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/eb798d31/agent/pom.xml ---------------------------------------------------------------------- diff --git a/agent/pom.xml b/agent/pom.xml index 7b00a93..1413322 100644 --- a/agent/pom.xml +++ b/agent/pom.xml @@ -37,6 +37,10 @@ <version>${project.version}</version> </dependency> <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + <dependency> <groupId>commons-daemon</groupId> <artifactId>commons-daemon</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/eb798d31/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 70eaceb..900a13f 100644 --- a/agent/src/com/cloud/agent/AgentShell.java +++ b/agent/src/com/cloud/agent/AgentShell.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -36,6 +37,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.io.IOUtils; import org.apache.commons.lang.math.NumberUtils; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; @@ -165,7 +167,7 @@ public class AgentShell implements IAgentShell, Daemon { _storage.persist(name, value); } - private void loadProperties() throws ConfigurationException { + void loadProperties() throws ConfigurationException { final File file = PropertiesUtil.findConfigFile("agent.properties"); if (file == null) { throw new ConfigurationException("Unable to find agent.properties."); @@ -173,14 +175,18 @@ public class AgentShell implements IAgentShell, Daemon { s_logger.info("agent.properties found at " + file.getAbsolutePath()); + InputStream propertiesStream = null; try { - _properties.load(new FileInputStream(file)); + propertiesStream = new FileInputStream(file); + _properties.load(propertiesStream); } catch (final FileNotFoundException ex) { throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex); } catch (final IOException ex) { throw new CloudRuntimeException("IOException in reading " + file.getAbsolutePath(), ex); + } finally { + IOUtils.closeQuietly(propertiesStream); } } @@ -304,7 +310,7 @@ public class AgentShell implements IAgentShell, Daemon { // For KVM agent, do it specially here File file = new File("/etc/cloudstack/agent/log4j-cloud.xml"); - if(file == null || !file.exists()) { + if(!file.exists()) { file = PropertiesUtil.findConfigFile("log4j-cloud.xml"); } DOMConfigurator.configureAndWatch(file.getAbsolutePath()); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/eb798d31/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 index 883790f..e33de48 100644 --- a/agent/test/com/cloud/agent/AgentShellTest.java +++ b/agent/test/com/cloud/agent/AgentShellTest.java @@ -22,4 +22,11 @@ public class AgentShellTest { Assert.assertEquals("pod1", shell.getPod()); Assert.assertEquals("zone1", shell.getZone()); } + @Test + public void loadProperties() throws ConfigurationException { + AgentShell shell = new AgentShell(); + shell.loadProperties(); + Assert.assertNotNull(shell.getProperties()); + Assert.assertFalse(shell.getProperties().entrySet().isEmpty()); + } }
