1 16 package org.apache.commons.collections; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 26 35 public class TestExtendedProperties extends TestCase { 36 37 protected ExtendedProperties eprop = new ExtendedProperties(); 38 39 public TestExtendedProperties(String testName) { 40 super(testName); 41 } 42 43 public static Test suite() { 44 return new TestSuite(TestExtendedProperties.class); 45 } 46 47 public static void main(String args[]) { 48 String [] testCaseName = { TestExtendedProperties.class.getName()}; 49 junit.textui.TestRunner.main(testCaseName); 50 } 51 52 public void testRetrieve() { 53 56 assertEquals("This returns null", eprop.getProperty("foo"), null); 57 58 61 eprop.setProperty("number", "1"); 62 assertEquals("This returns '1'", eprop.getProperty("number"), "1"); 63 assertEquals("This returns '1'", eprop.getString("number"), "1"); 64 65 68 eprop.addProperty("number", "2"); 69 assertTrue("This returns array", (eprop.getVector("number") instanceof java.util.Vector )); 70 71 76 assertTrue("This returns scalar", (eprop.getString("number") instanceof String )); 77 78 81 String prop = "hey, that's a test"; 82 eprop.setProperty("prop.string", prop); 83 assertTrue("This returns vector", (eprop.getVector("prop.string") instanceof java.util.Vector )); 84 85 String prop2 = "hey\\, that's a test"; 86 eprop.remove("prop.string"); 87 eprop.setProperty("prop.string", prop2); 88 assertTrue("This returns array", (eprop.getString("prop.string") instanceof java.lang.String )); 89 90 94 95 ExtendedProperties subEprop = eprop.subset("prop"); 96 97 assertTrue("Returns the full string", subEprop.getString("string").equals(prop)); 98 assertTrue("This returns string for subset", (subEprop.getString("string") instanceof java.lang.String )); 99 assertTrue("This returns array for subset", (subEprop.getVector("string") instanceof java.util.Vector )); 100 101 } 102 103 public void testInterpolation() { 104 eprop.setProperty("applicationRoot", "/home/applicationRoot"); 105 eprop.setProperty("db", "${applicationRoot}/db/hypersonic"); 106 String dbProp = "/home/applicationRoot/db/hypersonic"; 107 assertTrue("Checking interpolated variable", eprop.getString("db").equals(dbProp)); 108 } 109 110 public void testSaveAndLoad() { 111 ExtendedProperties ep1 = new ExtendedProperties(); 112 ExtendedProperties ep2 = new ExtendedProperties(); 113 114 try { 115 120 String s1 = "one=Hello\\World\ntwo=Hello\\,World\nthree=Hello,World"; 121 byte[] bytes = s1.getBytes(); 122 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 123 ep1.load(bais); 124 assertEquals("Back-slashes not interpreted properly", 125 "Hello\\World", ep1.getString("one")); 126 assertEquals("Escaped commas not interpreted properly", 127 "Hello,World", ep1.getString("two")); 128 assertEquals("Commas not interpreted properly", 129 2, ep1.getVector("three").size()); 130 assertEquals("Commas not interpreted properly", 131 "Hello", ep1.getVector("three").get(0)); 132 assertEquals("Commas not interpreted properly", 133 "World", ep1.getVector("three").get(1)); 134 135 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 136 ep1.save(baos, null); 137 bytes = baos.toByteArray(); 138 bais = new ByteArrayInputStream (bytes); 139 ep2.load(bais); 140 assertEquals("Back-slash not same after being saved and loaded", 141 ep1.getString("one"), ep2.getString("one")); 142 assertEquals("Escaped comma not same after being saved and loaded", 143 ep1.getString("two"), ep2.getString("two")); 144 assertEquals("Comma not same after being saved and loaded", 145 ep1.getString("three"), ep2.getString("three")); 146 } catch (IOException ioe) { 147 fail("There was an exception saving and loading the EP"); 148 } 149 } 150 151 public void testTrailingBackSlash() { 152 ExtendedProperties ep1 = new ExtendedProperties(); 153 154 try { 155 161 String s1 = "one=ONE\ntwo=TWO \\\\\nthree=THREE"; 162 byte[] bytes = s1.getBytes(); 163 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 164 ep1.load(bais); 165 assertEquals("Trailing back-slashes not interpreted properly", 166 3, ep1.size()); 167 assertEquals("Back-slash not escaped properly", 168 "TWO \\", ep1.getString("two")); 169 } catch (IOException ioe) { 170 fail("There was an exception loading the EP"); 171 } 172 } 173 174 public void testMultipleSameKey1() throws Exception { 175 ExtendedProperties ep1 = new ExtendedProperties(); 176 177 182 String s1 = "one=a\none=b,c\n"; 183 byte[] bytes = s1.getBytes(); 184 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 185 ep1.load(bais); 186 assertEquals(1, ep1.size()); 187 assertEquals(3, ep1.getVector("one").size()); 188 assertEquals("a", ep1.getVector("one").get(0)); 189 assertEquals("b", ep1.getVector("one").get(1)); 190 assertEquals("c", ep1.getVector("one").get(2)); 191 } 192 193 public void testMultipleSameKey2() throws Exception { 194 ExtendedProperties ep1 = new ExtendedProperties(); 195 196 201 String s1 = "one=a,b\none=c,d\n"; 202 byte[] bytes = s1.getBytes(); 203 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 204 ep1.load(bais); 205 assertEquals(1, ep1.size()); 206 assertEquals(4, ep1.getVector("one").size()); 207 assertEquals("a", ep1.getVector("one").get(0)); 208 assertEquals("b", ep1.getVector("one").get(1)); 209 assertEquals("c", ep1.getVector("one").get(2)); 210 assertEquals("d", ep1.getVector("one").get(3)); 211 } 212 213 public void testMultipleSameKey3() throws Exception { 214 ExtendedProperties ep1 = new ExtendedProperties(); 215 216 221 String s1 = "one=a,b\none=c\n"; 222 byte[] bytes = s1.getBytes(); 223 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 224 ep1.load(bais); 225 assertEquals(1, ep1.size()); 226 assertEquals(3, ep1.getVector("one").size()); 227 assertEquals("a", ep1.getVector("one").get(0)); 228 assertEquals("b", ep1.getVector("one").get(1)); 229 assertEquals("c", ep1.getVector("one").get(2)); 230 } 231 232 public void testMultipleSameKeyByCode() throws Exception { 233 ExtendedProperties ep1 = new ExtendedProperties(); 234 235 ep1.addProperty("one", "a"); 236 assertEquals(1, ep1.size()); 237 assertEquals(1, ep1.getVector("one").size()); 238 assertEquals("a", ep1.getVector("one").get(0)); 239 240 ep1.addProperty("one", Boolean.TRUE); 241 assertEquals(1, ep1.size()); 242 assertEquals(2, ep1.getVector("one").size()); 243 assertEquals("a", ep1.getVector("one").get(0)); 244 assertEquals(Boolean.TRUE, ep1.getVector("one").get(1)); 245 246 ep1.addProperty("one", "c,d"); 247 assertEquals(1, ep1.size()); 248 assertEquals(4, ep1.getVector("one").size()); 249 assertEquals("a", ep1.getVector("one").get(0)); 250 assertEquals(Boolean.TRUE, ep1.getVector("one").get(1)); 251 assertEquals("c", ep1.getVector("one").get(2)); 252 assertEquals("d", ep1.getVector("one").get(3)); 253 } 254 255 } 256 | Popular Tags |