1 19 20 package org.netbeans.spi.project.support.ant; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.OutputStream ; 31 import java.io.Reader ; 32 import java.net.URI ; 33 import java.net.URL ; 34 import java.util.Arrays ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 import org.netbeans.junit.NbTestCase; 40 41 public class EditablePropertiesTest extends NbTestCase { 42 43 public EditablePropertiesTest(String name) { 44 super(name); 45 } 46 47 public void testLoad() throws Exception { 48 Map <String ,String > content = new HashMap <String ,String >(); 49 for (int i=1; i<=26; i++) { 50 content.put("key"+i, "value"+i); 51 } 52 content.put("@!#$%^keyA", "valueA!@#$%^&*(){}"); 53 content.put(" =:keyB", "valueB =:"); 54 content.put(""+(char)0x1234+"keyC", "valueC"+(char)0x9876); 55 content.put("keyD", ""); 56 content.put("keyE", ""); 57 content.put("keyF", ""); 58 content.put("keyG", ""); 59 content.put("keyH", "value#this is not comment"); 60 content.put("keyI", "incorrect end: \\u123"); 61 content.put("keyJ", "malformed Unicode escape: \\uabyz"); 63 64 EditableProperties ep = loadTestProperties(); 65 66 for (Map.Entry <String ,String > entry : content.entrySet()) { 67 String key = entry.getKey(); 68 String value = entry.getValue(); 69 String epValue = ep.getProperty(key); 70 assertEquals("Expected value for key "+key+" is different", value, epValue); 71 } 72 int count = 0; 73 for (Map.Entry <String ,String > entry : ep.entrySet()) { 74 if (entry.getKey() != null) { 75 count++; 76 } 77 } 78 assertEquals("Number of items in property file", content.keySet().size(), count); 79 } 80 81 95 96 public void testSave() throws Exception { 97 clearWorkDir(); 98 EditableProperties ep = loadTestProperties(); 99 String dest = getWorkDirPath()+File.separatorChar+"new.properties"; 100 saveProperties(ep, dest); 101 assertFile("Saved properties must be the same as original one", filenameOfTestProperties(), dest, (String )null); 102 } 103 104 public void testClonability() throws Exception { 105 clearWorkDir(); 106 EditableProperties ep = loadTestProperties(); 107 108 EditableProperties ep2 = ep.cloneProperties(); 109 String dest = getWorkDirPath()+File.separatorChar+"new2.properties"; 110 saveProperties(ep2, dest); 111 assertFile("Saved cloned properties must be the same as original one", filenameOfTestProperties(), dest, (String )null); 112 113 EditableProperties ep3 = (EditableProperties)ep.clone(); 114 dest = getWorkDirPath()+File.separatorChar+"new3.properties"; 115 saveProperties(ep3, dest); 116 assertFile("Saved cloned properties must be the same as original one", filenameOfTestProperties(), dest, (String )null); 117 } 118 119 public void testVersionability() throws Exception { 121 clearWorkDir(); 122 123 EditableProperties ep = loadTestProperties(); 124 125 EditableProperties ep2 = ep.cloneProperties(); 126 ep2.setProperty("key24", "new value of key 24"); 127 String dest = getWorkDirPath()+File.separatorChar+"mod1.properties"; 128 saveProperties(ep2, dest); 129 int res[] = compare(filenameOfTestProperties(), dest); 130 assertEquals("One line modified", 1, res[0]); 131 assertEquals("No lines added", 0, res[1]); 132 assertEquals("No lines removed", 0, res[2]); 133 134 ep2 = ep.cloneProperties(); 135 ep2.setProperty("key23", "new value of key23"); 136 dest = getWorkDirPath()+File.separatorChar+"mod2.properties"; 137 saveProperties(ep2, dest); 138 res = compare(filenameOfTestProperties(), dest); 139 assertEquals("Four lines modified", 4, res[0]); 140 assertEquals("No lines added", 0, res[1]); 141 assertEquals("No lines removed", 0, res[2]); 142 143 ep2 = ep.cloneProperties(); 144 ep2.put("newkey", "new value"); 145 dest = getWorkDirPath()+File.separatorChar+"mod3.properties"; 146 saveProperties(ep2, dest); 147 res = compare(filenameOfTestProperties(), dest); 148 assertEquals("No lines modified", 0, res[0]); 149 assertEquals("One line added", 1, res[1]); 150 assertEquals("No lines removed", 0, res[2]); 151 152 ep2 = ep.cloneProperties(); 153 assertNotNull(ep2.get("key14")); 154 ep2.remove("key14"); 155 assertNull(ep2.get("key14")); 156 dest = getWorkDirPath()+File.separatorChar+"mod4.properties"; 157 saveProperties(ep2, dest); 158 res = compare(filenameOfTestProperties(), dest); 159 assertEquals("No lines modified", 0, res[0]); 160 assertEquals("No lines added", 0, res[1]); 161 assertEquals("Two lines removed", 2, res[2]); 162 163 ep2 = ep.cloneProperties(); 164 ep2.setProperty("key21", new String []{"first line;", "second line;", "third line"}); 165 dest = getWorkDirPath()+File.separatorChar+"mod5.properties"; 166 saveProperties(ep2, dest); 167 res = compare(filenameOfTestProperties(), dest); 168 assertEquals("Four lines modified", 4, res[0]); 169 assertEquals("No lines added", 0, res[1]); 170 assertEquals("No lines removed", 0, res[2]); 171 ep2.setProperty("key21", "first line;second line;third line"); 172 String dest2 = getWorkDirPath()+File.separatorChar+"mod6.properties"; 173 saveProperties(ep2, dest2); 174 res = compare(dest, dest2); 175 assertEquals("Four lines modified", 4, res[0]); 176 assertEquals("No lines added", 0, res[1]); 177 assertEquals("No lines removed", 0, res[2]); 178 } 179 180 public void testArrayValues() throws Exception { 182 EditableProperties ep = new EditableProperties(false); 183 ep.setProperty("key1", new String []{"1. line;", "2. line;", "3. line"}); 184 ep.setProperty("key2", "1. line;2. line;3. line"); 185 String output = getAsString(ep); 186 String expected = 187 "key1=\\"+System.getProperty("line.separator")+ 188 " 1. line;\\"+System.getProperty("line.separator")+ 189 " 2. line;\\"+System.getProperty("line.separator")+ 190 " 3. line"+System.getProperty("line.separator")+ 191 "key2=1. line;2. line;3. line"+System.getProperty("line.separator"); 192 assertEquals(expected, output); 193 assertEquals(ep.getProperty("key1"), "1. line;2. line;3. line"); 194 assertEquals(ep.getProperty("key2"), "1. line;2. line;3. line"); 195 ep.setProperty("key1", "one; two; three"); 196 output = getAsString(ep); 197 expected = 198 "key1=one; two; three"+System.getProperty("line.separator")+ 199 "key2=1. line;2. line;3. line"+System.getProperty("line.separator"); 200 assertEquals(expected, output); 201 assertEquals(ep.getProperty("key1"), "one; two; three"); 202 assertEquals(ep.getProperty("key2"), "1. line;2. line;3. line"); 203 ep.setProperty("key2", new String []{"1. line;", "2. line;", "3. line", "one;", "more;", "line;"}); 204 ep.setProperty("key", new String [0]); 205 output = getAsString(ep); 206 expected = 207 "key1=one; two; three"+System.getProperty("line.separator")+ 208 "key2=\\"+System.getProperty("line.separator")+ 209 " 1. line;\\"+System.getProperty("line.separator")+ 210 " 2. line;\\"+System.getProperty("line.separator")+ 211 " 3. line\\"+System.getProperty("line.separator")+ 212 " one;\\"+System.getProperty("line.separator")+ 213 " more;\\"+System.getProperty("line.separator")+ 214 " line;"+System.getProperty("line.separator")+ 215 "key="+System.getProperty("line.separator"); assertEquals(expected, output); 217 assertEquals(ep.getProperty("key1"), "one; two; three"); 218 assertEquals(ep.getProperty("key2"), "1. line;2. line;3. lineone;more;line;"); 219 assertEquals(ep.getProperty("key"), ""); 220 } 221 222 public void testSorting() throws Exception { 223 EditableProperties ep = new EditableProperties(false); 224 ep.setProperty("a", "val-a"); 225 ep.setProperty("c", "val-c"); 226 ep.put("b", "val-b"); 227 String output = getAsString(ep); 228 String expected = "a=val-a"+System.getProperty("line.separator")+"c=val-c"+ 229 System.getProperty("line.separator")+"b=val-b"+ 230 System.getProperty("line.separator"); 231 assertEquals(expected, output); 232 233 ep = new EditableProperties(); 234 ep.setProperty("a", "val-a"); 235 ep.setProperty("c", "val-c"); 236 ep.put("b", "val-b"); 237 output = getAsString(ep); 238 expected = "a=val-a"+System.getProperty("line.separator")+"c=val-c"+ 239 System.getProperty("line.separator")+"b=val-b"+ 240 System.getProperty("line.separator"); 241 assertEquals(expected, output); 242 243 ep = new EditableProperties(true); 244 ep.setProperty("a", "val-a"); 245 ep.setProperty("c", "val-c"); 246 ep.put("b", "val-b"); 247 output = getAsString(ep); 248 expected = "a=val-a"+System.getProperty("line.separator")+"b=val-b"+ 249 System.getProperty("line.separator")+"c=val-c"+ 250 System.getProperty("line.separator"); 251 assertEquals(expected, output); 252 } 253 254 public void testComment() throws Exception { 256 clearWorkDir(); 257 258 EditableProperties ep = loadTestProperties(); 259 260 EditableProperties ep2 = ep.cloneProperties(); 261 ep2.setComment("key10", new String []{"# this is new comment for property key 10"}, false); 262 String dest = getWorkDirPath()+File.separatorChar+"comment1.properties"; 263 saveProperties(ep2, dest); 264 int res[] = compare(filenameOfTestProperties(), dest); 265 assertEquals("No lines modified", 0, res[0]); 266 assertEquals("One line added", 1, res[1]); 267 assertEquals("No lines removed", 0, res[2]); 268 269 ep2 = ep.cloneProperties(); 270 ep2.setComment("key1", new String []{"# new comment", "# new comment second line"}, true); 271 dest = getWorkDirPath()+File.separatorChar+"comment2.properties"; 272 saveProperties(ep2, dest); 273 res = compare(filenameOfTestProperties(), dest); 274 assertEquals("No lines modified", 0, res[0]); 275 assertEquals("Two lines added", 2, res[1]); 276 assertEquals("No lines removed", 0, res[2]); 277 278 ep2 = ep.cloneProperties(); 279 ep2.setComment("key26", new String []{"# changed comment"}, false); 280 dest = getWorkDirPath()+File.separatorChar+"comment3.properties"; 281 saveProperties(ep2, dest); 282 res = compare(filenameOfTestProperties(), dest); 283 assertEquals("One line modified", 1, res[0]); 284 assertEquals("No lines added", 0, res[1]); 285 assertEquals("No lines removed", 0, res[2]); 286 287 ep2 = ep.cloneProperties(); 288 ep2.setComment("key25", new String []{"# one line comment"}, false); 289 dest = getWorkDirPath()+File.separatorChar+"comment4.properties"; 290 saveProperties(ep2, dest); 291 res = compare(filenameOfTestProperties(), dest); 292 assertEquals("Two lines modified", 2, res[0]); 293 assertEquals("No lines added", 0, res[1]); 294 assertEquals("No lines removed", 0, res[2]); 295 296 ep2 = ep.cloneProperties(); 297 ep2.setComment("key26", ep2.getComment("key26"), true); 298 dest = getWorkDirPath()+File.separatorChar+"comment5.properties"; 299 saveProperties(ep2, dest); 300 res = compare(filenameOfTestProperties(), dest); 301 assertEquals("No line modified", 0, res[0]); 302 assertEquals("One line added", 1, res[1]); 303 assertEquals("No lines removed", 0, res[2]); 304 305 } 306 307 public void testEscaping() throws Exception { 309 String umlaut = "" + (char)252; 310 EditableProperties ep = new EditableProperties(false); 311 ep.setProperty("a a", "a space a"); 312 ep.setProperty("b"+(char)0x4567, "val"+(char)0x1234); 313 ep.setProperty("@!#$%^\\", "!@#$%^&*(){}\\"); 314 ep.setProperty("d\nd", "d\nnewline\nd"); 315 ep.setProperty("umlaut", umlaut); 316 ep.setProperty("_a a", new String []{"a space a"}); 317 ep.setProperty("_b"+(char)0x4567, new String []{"val"+(char)0x1234}); 318 ep.setProperty("_@!#$%^\\", new String []{"!@#$%^&*\\", "(){}\\"}); 319 ep.setProperty("_d\nd", new String []{"d\nnew","line\nd", "\n", "end"}); 320 ep.setProperty("_umlaut", new String []{umlaut, umlaut}); 321 String output = getAsString(ep); 322 String expected = "a\\ a=a space a"+System.getProperty("line.separator")+ 323 "b\\u4567=val\\u1234"+System.getProperty("line.separator")+ 324 "@!#$%^\\\\=!@#$%^&*(){}\\\\"+System.getProperty("line.separator")+ 325 "d\\nd=d\\nnewline\\nd"+System.getProperty("line.separator")+ 326 "umlaut=\\u00fc"+System.getProperty("line.separator")+ 327 "_a\\ a=\\"+System.getProperty("line.separator")+" a space a"+System.getProperty("line.separator")+ 328 "_b\\u4567=\\"+System.getProperty("line.separator")+" val\\u1234"+System.getProperty("line.separator")+ 329 "_@!#$%^\\\\=\\"+System.getProperty("line.separator")+" !@#$%^&*\\\\\\"+System.getProperty("line.separator")+ 330 " (){}\\\\"+System.getProperty("line.separator")+ 331 "_d\\nd=\\"+System.getProperty("line.separator")+" d\\nnew\\"+System.getProperty("line.separator")+ 332 " line\\nd\\"+System.getProperty("line.separator")+ 333 " \\n\\"+System.getProperty("line.separator")+ 334 " end"+System.getProperty("line.separator")+ 335 "_umlaut=\\" +System.getProperty("line.separator")+" \\u00fc\\"+System.getProperty("line.separator")+ 336 " \\u00fc"+System.getProperty("line.separator"); 337 assertEquals(expected, output); 338 assertEquals("a space a", ep.getProperty("a a")); 339 assertEquals("val"+(char)0x1234, ep.getProperty("b"+(char)0x4567)); 340 assertEquals("!@#$%^&*(){}\\", ep.getProperty("@!#$%^\\")); 341 assertEquals("d\nnewline\nd", ep.getProperty("d\nd")); 342 assertEquals(umlaut, ep.getProperty("umlaut")); 343 assertEquals("a space a", ep.getProperty("_a a")); 344 assertEquals("val"+(char)0x1234, ep.getProperty("_b"+(char)0x4567)); 345 assertEquals("!@#$%^&*\\(){}\\", ep.getProperty("_@!#$%^\\")); 346 assertEquals("d\nnewline\nd\nend", ep.getProperty("_d\nd")); 347 assertEquals(umlaut+umlaut, ep.getProperty("_umlaut")); 348 } 349 350 public void testIterator() throws Exception { 352 EditableProperties ep = loadTestProperties(); 353 Iterator <Map.Entry <String ,String >> it1 = ep.entrySet().iterator(); 354 while (it1.hasNext()) { 355 it1.next(); 356 } 357 Iterator <String > it2 = ep.keySet().iterator(); 358 while (it2.hasNext()) { 359 it2.next(); 360 } 361 it2 = ep.keySet().iterator(); 362 while (it2.hasNext()) { 363 it2.next(); 364 it2.remove(); 365 } 366 ep.put("a", "aval"); 367 ep.remove("a"); 368 ep = loadTestProperties(); 369 it1 = ep.entrySet().iterator(); 370 while (it1.hasNext()) { 371 Map.Entry <String ,String > entry = it1.next(); 372 assertNotNull("Property key cannot be null", entry.getKey()); 373 assertNotNull("Property value cannot be null", entry.getValue()); 374 entry.setValue(entry.getValue()+"-something-new"); 375 } 376 it1 = ep.entrySet().iterator(); 377 while (it1.hasNext()) { 378 it1.next(); 379 it1.remove(); 380 } 381 } 382 383 public void testInvalidPropertiesFile() throws Exception { 385 String invalidProperty = "key=value without correct end\\"; 386 ByteArrayInputStream is = new ByteArrayInputStream (invalidProperty.getBytes()); 387 EditableProperties ep = new EditableProperties(); 388 ep.load(is); 389 assertEquals("Syntax error should be resolved", 1, ep.keySet().size()); 390 assertEquals("value without correct end", ep.getProperty("key")); 391 } 392 393 public void testNonLatinComments() throws Exception { 394 String lsep = System.getProperty("line.separator"); 396 EditableProperties p = new EditableProperties(); 397 p.setProperty("k", "v"); 398 p.setComment("k", new String [] {"# \u0158ekni koment teda!"}, false); 399 String expected = "# \\u0158ekni koment teda!" + lsep + "k=v" + lsep; 400 assertEquals("Storing non-Latin chars in comments works", expected, getAsString(p)); 401 p = new EditableProperties(); 402 p.load(new ByteArrayInputStream (expected.getBytes("ISO-8859-1"))); 403 assertEquals("Reading non-Latin chars in comments works", Collections.singletonList("# \u0158ekni koment teda!"), Arrays.asList(p.getComment("k"))); 404 p.setProperty("k", "v2"); 405 expected = "# \\u0158ekni koment teda!" + lsep + "k=v2" + lsep; 406 assertEquals("Reading and re-writing non-Latin chars in comments works", expected, getAsString(p)); 407 } 408 409 410 412 413 private String filenameOfTestProperties() { 414 return new File (URI.create(EditablePropertiesTest.class.getResource("data/test.properties").toExternalForm())).getAbsolutePath(); 416 } 417 418 private EditableProperties loadTestProperties() throws IOException { 419 URL u = EditablePropertiesTest.class.getResource("data/test.properties"); 420 EditableProperties ep = new EditableProperties(); 421 InputStream is = u.openStream(); 422 try { 423 ep.load(is); 424 } finally { 425 is.close(); 426 } 427 return ep; 428 } 429 430 443 444 private void saveProperties(EditableProperties ep, String path) throws Exception { 445 OutputStream os = new FileOutputStream (path); 446 try { 447 ep.store(os); 448 } finally { 449 os.close(); 450 } 451 } 452 453 private int[] compare(String f1, String f2) throws Exception { 454 Reader r1 = null; 455 Reader r2 = null; 456 try { 457 r1 = new InputStreamReader (new FileInputStream (f1), "ISO-8859-1"); 458 r2 = new InputStreamReader (new FileInputStream (f2), "ISO-8859-1"); 459 return AntBasedTestUtil.countTextDiffs(r1, r2); 460 } finally { 461 if (r1 != null) { 462 r1.close(); 463 } 464 if (r2 != null) { 465 r2.close(); 466 } 467 } 468 } 469 470 private String getAsString(EditableProperties ep) throws Exception { 471 ByteArrayOutputStream os = new ByteArrayOutputStream (); 472 ep.store(os); 473 os.close(); 474 return os.toString("ISO-8859-1"); 475 } 476 477 } 478 | Popular Tags |