Skip to content

Commit 5e1ea1a

Browse files
committed
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>
1 parent 7902315 commit 5e1ea1a

2 files changed

Lines changed: 54 additions & 11 deletions

File tree

agent/src/com/cloud/agent/dao/impl/PropertiesStorage.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.cloud.agent.dao.impl;
1818

1919
import java.io.File;
20-
import java.io.FileInputStream;
2120
import java.io.FileNotFoundException;
2221
import java.io.FileOutputStream;
2322
import java.io.IOException;
@@ -26,6 +25,7 @@
2625

2726
import javax.ejb.Local;
2827

28+
import org.apache.commons.io.IOUtils;
2929
import org.apache.log4j.Logger;
3030

3131
import com.cloud.agent.dao.StorageComponent;
@@ -59,18 +59,10 @@ public synchronized void persist(String key, String value) {
5959
_properties.store(output, _name);
6060
output.flush();
6161
output.close();
62-
} catch (FileNotFoundException e) {
63-
s_logger.error("Who deleted the file? ", e);
6462
} catch (IOException e) {
6563
s_logger.error("Uh-oh: ", e);
6664
} finally {
67-
if (output != null) {
68-
try {
69-
output.close();
70-
} catch (IOException e) {
71-
// ignore.
72-
}
73-
}
65+
IOUtils.closeQuietly(output);
7466
}
7567
}
7668

@@ -99,7 +91,7 @@ public boolean configure(String name, Map<String, Object> params) {
9991
}
10092

10193
try {
102-
_properties.load(new FileInputStream(file));
94+
PropertiesUtil.loadFromFile(_properties, file);
10395
_file = file;
10496
} catch (FileNotFoundException e) {
10597
s_logger.error("How did we get here? ", e);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.cloud.agent.dao.impl;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.HashMap;
6+
7+
import junit.framework.Assert;
8+
9+
import org.apache.commons.io.FileUtils;
10+
import org.junit.Test;
11+
12+
public class PropertiesStorageTest {
13+
@Test
14+
public void configureWithNotExistingFile() {
15+
String fileName = "target/notyetexistingfile"
16+
+ System.currentTimeMillis();
17+
File file = new File(fileName);
18+
19+
PropertiesStorage storage = new PropertiesStorage();
20+
HashMap<String, Object> params = new HashMap<String, Object>();
21+
params.put("path", fileName);
22+
Assert.assertTrue(storage.configure("test", params));
23+
Assert.assertTrue(file.exists());
24+
storage.persist("foo", "bar");
25+
Assert.assertEquals("bar", storage.get("foo"));
26+
27+
storage.stop();
28+
file.delete();
29+
}
30+
31+
@Test
32+
public void configureWithExistingFile() throws IOException {
33+
String fileName = "target/existingfile"
34+
+ System.currentTimeMillis();
35+
File file = new File(fileName);
36+
37+
FileUtils.writeStringToFile(file, "a=b\n\n");
38+
39+
PropertiesStorage storage = new PropertiesStorage();
40+
HashMap<String, Object> params = new HashMap<String, Object>();
41+
params.put("path", fileName);
42+
Assert.assertTrue(storage.configure("test", params));
43+
Assert.assertEquals("b", storage.get("a"));
44+
Assert.assertTrue(file.exists());
45+
storage.persist("foo", "bar");
46+
Assert.assertEquals("bar", storage.get("foo"));
47+
48+
storage.stop();
49+
file.delete();
50+
}
51+
}

0 commit comments

Comments
 (0)