001: package com.ecyrd.jspwiki.util;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.io.OutputStream;
008: import java.net.URI;
009: import java.net.URISyntaxException;
010: import java.net.URL;
011: import java.util.Properties;
012:
013: import junit.framework.Test;
014: import junit.framework.TestCase;
015: import junit.framework.TestSuite;
016:
017: public class CommentedPropertiesTest extends TestCase {
018: Properties m_props = new CommentedProperties();
019:
020: public void setUp() throws IOException {
021: InputStream in = CommentedPropertiesTest.class.getClassLoader()
022: .getResourceAsStream("test.properties");
023: m_props.load(in);
024: in.close();
025: }
026:
027: public void testLoadProperties() {
028: assertEquals(5, m_props.keySet().size());
029: assertEquals("Foo", m_props.get("testProp1"));
030: assertEquals("Bar", m_props.get("testProp2"));
031: assertEquals("", m_props.get("testProp3"));
032: assertEquals("FooAgain", m_props.get("testProp4"));
033: assertEquals("BarAgain", m_props.get("testProp5"));
034: assertNull(m_props.get("testProp6"));
035:
036: // String we read in, including comments is 208 bytes
037: assertEquals(208, m_props.toString().length());
038: }
039:
040: public void testSetProperty() {
041: m_props.setProperty("testProp1", "newValue");
042:
043: // Length of stored string should now be 5 bytes more
044: assertEquals(208 + 5, m_props.toString().length());
045: assertTrue(m_props.toString().indexOf("newValue") != -1);
046:
047: // Create new property; should add 21 (1+7+3+9+1) bytes
048: m_props.setProperty("newProp", "newValue2");
049: m_props.containsKey("newProp");
050: m_props.containsValue("newValue2");
051: assertEquals(208 + 5 + 21, m_props.toString().length());
052: assertTrue(m_props.toString().indexOf("newProp = newValue2") != -1);
053: }
054:
055: public void testRemove() {
056: // Remove prop 1; length of stored string should be 14 (1+9+1+3) bytes less
057: m_props.remove("testProp1");
058: assertFalse(m_props.containsKey("testProp1"));
059: assertEquals(208 - 14, m_props.toString().length());
060:
061: // Remove prop 2; length of stored string should be 15 (1+9+2+3) bytes less
062: m_props.remove("testProp2");
063: assertFalse(m_props.containsKey("testProp2"));
064: assertEquals(208 - 14 - 15, m_props.toString().length());
065:
066: // Remove prop 3; length of stored string should be 11 (1+9+1) bytes less
067: m_props.remove("testProp3");
068: assertFalse(m_props.containsKey("testProp3"));
069: assertEquals(208 - 14 - 15 - 11, m_props.toString().length());
070:
071: // Remove prop 4; length of stored string should be 19 (1+9+1+8) bytes less
072: m_props.remove("testProp4");
073: assertFalse(m_props.containsKey("testProp4"));
074: assertEquals(208 - 14 - 15 - 11 - 19, m_props.toString()
075: .length());
076:
077: // Remove prop 5; length of stored string should be 19 (1+9+1+8) bytes less
078: m_props.remove("testProp5");
079: assertFalse(m_props.containsKey("testProp5"));
080: assertEquals(208 - 14 - 15 - 11 - 19 - 19, m_props.toString()
081: .length());
082: }
083:
084: public void testStore() throws Exception {
085: // Write results to a new file
086: File outFile = createFile("test2.properties");
087: OutputStream out = new FileOutputStream(outFile);
088: m_props.store(out, null);
089: out.close();
090:
091: // Load the file into new props object; should return identical strings
092: Properties props2 = new CommentedProperties();
093: InputStream in = CommentedPropertiesTest.class.getClassLoader()
094: .getResourceAsStream("test2.properties");
095: props2.load(in);
096: in.close();
097: assertEquals(m_props.toString(), props2.toString());
098:
099: // Remove props1, 2, 3 & resave props to new file
100: m_props.remove("testProp1");
101: m_props.remove("testProp2");
102: m_props.remove("testProp3");
103: outFile = createFile("test3.properties");
104: out = new FileOutputStream(outFile);
105: m_props.store(out, null);
106: out.close();
107:
108: // Load the new file; should not have props1/2/3 & is shorter
109: Properties props3 = new CommentedProperties();
110: in = CommentedPropertiesTest.class.getClassLoader()
111: .getResourceAsStream("test3.properties");
112: props3.load(in);
113: in.close();
114: assertNotSame(m_props.toString(), props3.toString());
115: assertFalse(props3.containsKey("testProp1"));
116: assertFalse(props3.containsKey("testProp2"));
117: assertFalse(props3.containsKey("testProp3"));
118: assertTrue(props3.containsKey("testProp4"));
119: assertTrue(props3.containsKey("testProp5"));
120:
121: // Clean up
122: File file = getFile("test2.properties");
123: if (file != null && file.exists()) {
124: file.delete();
125: }
126: file = getFile("test3.properties");
127: if (file != null && file.exists()) {
128: file.delete();
129: }
130: }
131:
132: private File createFile(String file) throws URISyntaxException {
133: // Get the test.properties file
134: URL url = CommentedPropertiesTest.class.getClassLoader()
135: .getResource("test.properties");
136: if (url == null) {
137: throw new IllegalStateException(
138: "Very odd. We can't find test.properties!");
139: }
140:
141: // Construct new file in same directory
142: File testFile = new File(new URI(url.toString()));
143: File dir = testFile.getParentFile();
144: return new File(dir, file);
145: }
146:
147: private File getFile(String name) {
148: // Get the test.properties file
149: URL url = CommentedPropertiesTest.class.getClassLoader()
150: .getResource(name);
151: if (url == null) {
152: throw new IllegalStateException(
153: "Very odd. We can't find test.properties!");
154: }
155: // Return the file
156: File file = null;
157:
158: try {
159: file = new File(new URI(url.toString()));
160: } catch (URISyntaxException e) {
161: // TODO Auto-generated catch block
162: e.printStackTrace();
163: }
164:
165: return file;
166: }
167:
168: public static Test suite() {
169: return new TestSuite(CommentedPropertiesTest.class);
170: }
171: }
|