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