Test for AgentShell.loadProperties

- stream closed after properties load (with commons io)
- test added

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
This commit is contained in:
Laszlo Hornyak 2013-10-22 22:14:49 +02:00
parent 8d67e15365
commit eb798d3198
3 changed files with 20 additions and 3 deletions

View File

@ -36,6 +36,10 @@
<artifactId>cloud-utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-daemon</groupId>
<artifactId>commons-daemon</artifactId>

View File

@ -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());

View File

@ -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());
}
}