InputStream use fix in PorpoertiesStorage

- Properties object polulation using PropertiesUtil.loadFromFile
- test added
- the separate FileNotFoundException handling block was removed as the next IOException block is catching it and it is only logging

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
This commit is contained in:
Laszlo Hornyak 2013-10-24 21:20:56 +02:00
parent 7902315287
commit 5e1ea1a3e4
2 changed files with 54 additions and 11 deletions

View File

@ -17,7 +17,6 @@
package com.cloud.agent.dao.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@ -26,6 +25,7 @@ import java.util.Properties;
import javax.ejb.Local;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import com.cloud.agent.dao.StorageComponent;
@ -59,18 +59,10 @@ public class PropertiesStorage implements StorageComponent {
_properties.store(output, _name);
output.flush();
output.close();
} catch (FileNotFoundException e) {
s_logger.error("Who deleted the file? ", e);
} catch (IOException e) {
s_logger.error("Uh-oh: ", e);
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
// ignore.
}
}
IOUtils.closeQuietly(output);
}
}
@ -99,7 +91,7 @@ public class PropertiesStorage implements StorageComponent {
}
try {
_properties.load(new FileInputStream(file));
PropertiesUtil.loadFromFile(_properties, file);
_file = file;
} catch (FileNotFoundException e) {
s_logger.error("How did we get here? ", e);

View File

@ -0,0 +1,51 @@
package com.cloud.agent.dao.impl;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import junit.framework.Assert;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
public class PropertiesStorageTest {
@Test
public void configureWithNotExistingFile() {
String fileName = "target/notyetexistingfile"
+ System.currentTimeMillis();
File file = new File(fileName);
PropertiesStorage storage = new PropertiesStorage();
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("path", fileName);
Assert.assertTrue(storage.configure("test", params));
Assert.assertTrue(file.exists());
storage.persist("foo", "bar");
Assert.assertEquals("bar", storage.get("foo"));
storage.stop();
file.delete();
}
@Test
public void configureWithExistingFile() throws IOException {
String fileName = "target/existingfile"
+ System.currentTimeMillis();
File file = new File(fileName);
FileUtils.writeStringToFile(file, "a=b\n\n");
PropertiesStorage storage = new PropertiesStorage();
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("path", fileName);
Assert.assertTrue(storage.configure("test", params));
Assert.assertEquals("b", storage.get("a"));
Assert.assertTrue(file.exists());
storage.persist("foo", "bar");
Assert.assertEquals("bar", storage.get("foo"));
storage.stop();
file.delete();
}
}