1 19 20 package org.netbeans.spi.project.support.ant; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.PrintWriter ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 import java.util.HashMap ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 import junit.framework.TestResult; 37 import org.netbeans.api.project.ProjectManager; 38 import org.netbeans.junit.NbTestCase; 39 import org.openide.filesystems.FileLock; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileSystem; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.util.Mutex; 44 import org.openide.util.NbCollections; 45 import org.openide.util.Utilities; 46 47 51 public class PropertyUtilsTest extends NbTestCase { 52 53 public PropertyUtilsTest(String name) { 54 super(name); 55 } 56 57 public void run(final TestResult result) { 58 ProjectManager.mutex().writeAccess(new Mutex.Action<Void >() { 59 public Void run() { 60 PropertyUtilsTest.super.run(result); 61 return null; 62 } 63 }); 64 } 65 66 private static PropertyEvaluator evaluator(Map <String ,String > predefs, List <Map <String ,String >> defs) { 67 PropertyProvider[] mainProviders = new PropertyProvider[defs.size()]; 68 int i = 0; 69 for (Map <String ,String > def : defs) { 70 mainProviders[i++] = PropertyUtils.fixedPropertyProvider(def); 71 } 72 return PropertyUtils.sequentialPropertyEvaluator(PropertyUtils.fixedPropertyProvider(predefs), mainProviders); 73 } 74 75 private static String evaluate(String prop, Map <String ,String > predefs, List <Map <String ,String >> defs) { 76 return evaluator(predefs, defs).getProperty(prop); 77 } 78 79 private static Map <String ,String > evaluateAll(Map <String ,String > predefs, List <Map <String ,String >> defs) { 80 return evaluator(predefs, defs).getProperties(); 81 } 82 83 private static String evaluateString(String text, Map <String ,String > predefs, List <Map <String ,String >> defs) { 84 return evaluator(predefs, defs).evaluate(text); 85 } 86 87 public void testEvaluate() throws Exception { 88 Map <String ,String > m1 = Collections.singletonMap("y", "val"); 91 Map <String ,String > m2 = new HashMap <String ,String >(); 92 m2.put("x", "${y}"); 93 m2.put("y", "y-${x}"); 94 List <Map <String ,String >> m1m2 = new ArrayList <Map <String ,String >>(); 95 m1m2.add(m1); 96 m1m2.add(m2); 97 assertEquals("x evaluates to former y", "val", evaluate("x", Collections.<String ,String >emptyMap(), m1m2)); 98 assertEquals("first y defines it", "val", evaluate("y", Collections.<String ,String >emptyMap(), m1m2)); 99 assertEquals("circularity error", null, evaluate("x", Collections.<String ,String >emptyMap(), Collections.singletonList(m2))); 100 assertEquals("circularity error", null, evaluate("y", Collections.<String ,String >emptyMap(), Collections.singletonList(m2))); 101 m2.clear(); 102 m2.put("y", "yval_${z}"); 103 m2.put("x", "xval_${y}"); 104 m2.put("z", "zval"); 105 Map <String ,String > all = evaluateAll(Collections.<String ,String >emptyMap(), Collections.singletonList(m2)); 106 assertNotNull("no circularity error", all); 107 assertEquals("have three properties", 3, all.size()); 108 assertEquals("double substitution", "xval_yval_zval", all.get("x")); 109 assertEquals("single substitution", "yval_zval", all.get("y")); 110 assertEquals("no substitution", "zval", all.get("z")); 111 Properties p = new Properties (); 113 p.load(new ByteArrayInputStream ("project.mylib=../mylib\njavac.classpath=${project.mylib}/build/mylib.jar\nrun.classpath=${javac.classpath}:build/classes".getBytes("US-ASCII"))); 114 all = evaluateAll(Collections.<String ,String >emptyMap(), Collections.singletonList(NbCollections.checkedMapByFilter(p, String .class, String .class, true))); 115 assertNotNull("no circularity error", all); 116 assertEquals("javac.classpath correctly substituted", "../mylib/build/mylib.jar", all.get("javac.classpath")); 117 assertEquals("run.classpath correctly substituted", "../mylib/build/mylib.jar:build/classes", all.get("run.classpath")); 118 } 119 120 public void testTokenizePath() throws Exception { 121 assertEquals("basic tokenization works on ':'", 122 Arrays.asList(new String [] {"foo", "bar"}), 123 Arrays.asList(PropertyUtils.tokenizePath("foo:bar"))); 124 assertEquals("basic tokenization works on ';'", 125 Arrays.asList(new String [] {"foo", "bar"}), 126 Arrays.asList(PropertyUtils.tokenizePath("foo;bar"))); 127 assertEquals("Unix paths work", 128 Arrays.asList(new String [] {"/foo/bar", "baz/quux"}), 129 Arrays.asList(PropertyUtils.tokenizePath("/foo/bar:baz/quux"))); 130 assertEquals("empty components are stripped with ':'", 131 Arrays.asList(new String [] {"foo", "bar"}), 132 Arrays.asList(PropertyUtils.tokenizePath(":foo::bar:"))); 133 assertEquals("empty components are stripped with ';'", 134 Arrays.asList(new String [] {"foo", "bar"}), 135 Arrays.asList(PropertyUtils.tokenizePath(";foo;;bar;"))); 136 assertEquals("DOS paths are recognized with ';'", 137 Arrays.asList(new String [] {"c:\\foo", "D:\\\\bar"}), 138 Arrays.asList(PropertyUtils.tokenizePath("c:\\foo;D:\\\\bar"))); 139 assertEquals("DOS paths are recognized with ':'", 140 Arrays.asList(new String [] {"c:\\foo", "D:\\\\bar"}), 141 Arrays.asList(PropertyUtils.tokenizePath("c:\\foo:D:\\\\bar"))); 142 assertEquals("a..z can be drive letters", 143 Arrays.asList(new String [] {"a:\\foo", "z:\\\\bar"}), 144 Arrays.asList(PropertyUtils.tokenizePath("a:\\foo:z:\\\\bar"))); 145 assertEquals("A..Z can be drive letters", 146 Arrays.asList(new String [] {"A:\\foo", "Z:\\\\bar"}), 147 Arrays.asList(PropertyUtils.tokenizePath("A:\\foo:Z:\\\\bar"))); 148 assertEquals("non-letters are not drives with ';'", 149 Arrays.asList(new String [] {"1", "\\foo", "D:\\\\bar"}), 150 Arrays.asList(PropertyUtils.tokenizePath("1;\\foo;D:\\\\bar"))); 151 assertEquals("non-letters are not drives with ':'", 152 Arrays.asList(new String [] {"1", "\\foo", "D:\\\\bar"}), 153 Arrays.asList(PropertyUtils.tokenizePath("1:\\foo:D:\\\\bar"))); 154 assertEquals(">1 letters are not drives with ';'", 155 Arrays.asList(new String [] {"ab", "\\foo", "D:\\\\bar"}), 156 Arrays.asList(PropertyUtils.tokenizePath("ab;\\foo;D:\\\\bar"))); 157 assertEquals(">1 letters are not drives with ':'", 158 Arrays.asList(new String [] {"ab", "\\foo", "D:\\\\bar"}), 159 Arrays.asList(PropertyUtils.tokenizePath("ab:\\foo:D:\\\\bar"))); 160 assertEquals("drives use ':'", 161 Arrays.asList(new String [] {"c", "\\foo", "D:\\\\bar"}), 162 Arrays.asList(PropertyUtils.tokenizePath("c;\\foo;D:\\\\bar"))); 163 assertEquals("drives use only one ':'", 164 Arrays.asList(new String [] {"c", "\\foo", "D:\\\\bar"}), 165 Arrays.asList(PropertyUtils.tokenizePath("c::\\foo;D:\\\\bar"))); 166 assertEquals("drives use only one drive letter", 167 Arrays.asList(new String [] {"c", "c:\\foo", "D:\\\\bar"}), 168 Arrays.asList(PropertyUtils.tokenizePath("c:c:\\foo;D:\\\\bar"))); 169 assertEquals("DOS paths start with '\\'", 170 Arrays.asList(new String [] {"c", "foo", "D:\\\\bar"}), 171 Arrays.asList(PropertyUtils.tokenizePath("c:foo;D:\\\\bar"))); 172 assertEquals("DOS paths start with '/'", 173 Arrays.asList(new String [] {"c", "/foo", "D:/bar", "/path"}), 174 Arrays.asList(PropertyUtils.tokenizePath("c;/foo;D:/bar:/path"))); 175 assertEquals("empty path handled", 176 Collections.EMPTY_LIST, 177 Arrays.asList(PropertyUtils.tokenizePath(""))); 178 assertEquals("effectively empty path handled", 179 Collections.EMPTY_LIST, 180 Arrays.asList(PropertyUtils.tokenizePath(":;:;"))); 181 assertEquals("one letter directories handled", 182 Arrays.asList(new String [] {"c:/foo/c", "/foo/c/bar", "c", "/foo/c", "/bar"}), 183 Arrays.asList(PropertyUtils.tokenizePath("c:/foo/c;/foo/c/bar;c;/foo/c:/bar"))); 184 assertEquals("one letter directories handled2", 185 Arrays.asList(new String [] {"c"}), 186 Arrays.asList(PropertyUtils.tokenizePath("c"))); 187 } 188 189 public void testRelativizeFile() throws Exception { 190 clearWorkDir(); 191 File tmp = getWorkDir(); 192 File d1 = new File (tmp, "d1"); 193 File d1f = new File (d1, "f"); 194 File d1s = new File (d1, "s p a c e"); 195 File d1sf = new File (d1s, "f"); 196 File d2 = new File (tmp, "d2"); 197 File d2f = new File (d2, "f"); 198 File d11 = new File (tmp, "d11"); 200 assertEquals("d1f from d1", "f", PropertyUtils.relativizeFile(d1, d1f)); 202 assertEquals("d1 from d1f", "..", PropertyUtils.relativizeFile(d1f, d1)); assertEquals("d2f from d1", "../d2/f", PropertyUtils.relativizeFile(d1, d2f)); 204 assertEquals("d1 from d1", ".", PropertyUtils.relativizeFile(d1, d1)); 205 assertEquals("d2 from d1", "../d2", PropertyUtils.relativizeFile(d1, d2)); 206 assertEquals("d1s from d1", "s p a c e", PropertyUtils.relativizeFile(d1, d1s)); 207 assertEquals("d1sf from d1", "s p a c e/f", PropertyUtils.relativizeFile(d1, d1sf)); 208 assertEquals("d11 from d1", "../d11", PropertyUtils.relativizeFile(d1, d11)); 209 assertTrue("made d1s", d1s.mkdirs()); 211 assertTrue("made d1f", d1f.createNewFile()); 212 assertTrue("made d1sf", d1sf.createNewFile()); 213 assertTrue("made d2", d2.mkdirs()); 214 assertTrue("made d2f", d2f.createNewFile()); 215 assertEquals("existing d1f from d1", "f", PropertyUtils.relativizeFile(d1, d1f)); 216 assertEquals("existing d2f from d1", "../d2/f", PropertyUtils.relativizeFile(d1, d2f)); 217 assertEquals("existing d1 from d1", ".", PropertyUtils.relativizeFile(d1, d1)); 218 assertEquals("existing d2 from d1", "../d2", PropertyUtils.relativizeFile(d1, d2)); 219 assertEquals("existing d1s from d1", "s p a c e", PropertyUtils.relativizeFile(d1, d1s)); 220 assertEquals("existing d1sf from d1", "s p a c e/f", PropertyUtils.relativizeFile(d1, d1sf)); 221 assertEquals("existing d11 from d1", "../d11", PropertyUtils.relativizeFile(d1, d11)); 222 if (Utilities.isWindows()) { 224 File f1 = new File ("C:\\folder\\one"); 226 File f2 = new File ("D:\\t e m p\\two"); 227 assertNull("different drives cannot be relative", PropertyUtils.relativizeFile(f1, f2)); 228 f1 = new File ("D:\\folder\\one"); 229 f2 = new File ("D:\\t e m p\\two"); 230 assertEquals("relativization failed for Windows absolute paths", "../../t e m p/two", PropertyUtils.relativizeFile(f1, f2)); 231 } 232 } 233 234 public void testGlobalProperties() throws Exception { 235 clearWorkDir(); 236 System.setProperty("netbeans.user", getWorkDir().getAbsolutePath()); 237 File ubp = new File (getWorkDir(), "build.properties"); 238 assertFalse("no build.properties yet", ubp.exists()); 239 assertEquals("no properties to start", Collections.EMPTY_MAP, PropertyUtils.getGlobalProperties()); 240 EditableProperties p = new EditableProperties(); 241 p.setProperty("key1", "val1"); 242 p.setProperty("key2", "val2"); 243 PropertyUtils.putGlobalProperties(p); 244 assertTrue("now have build.properties", ubp.isFile()); 245 p = PropertyUtils.getGlobalProperties(); 246 assertEquals("two definitions now", 2, p.size()); 247 assertEquals("key1 correct", "val1", p.getProperty("key1")); 248 assertEquals("key2 correct", "val2", p.getProperty("key2")); 249 Properties p2 = new Properties (); 250 InputStream is = new FileInputStream (ubp); 251 try { 252 p2.load(is); 253 } finally { 254 is.close(); 255 } 256 assertEquals("two definitions now from disk", 2, p2.size()); 257 assertEquals("key1 correct from disk", "val1", p2.getProperty("key1")); 258 assertEquals("key2 correct from disk", "val2", p2.getProperty("key2")); 259 PropertyProvider gpp = PropertyUtils.globalPropertyProvider(); 261 AntBasedTestUtil.TestCL l = new AntBasedTestUtil.TestCL(); 262 gpp.addChangeListener(l); 263 p = PropertyUtils.getGlobalProperties(); 264 assertEquals("correct initial definitions", p, gpp.getProperties()); 265 p.setProperty("key3", "val3"); 266 assertEquals("still have 2 defs", 2, gpp.getProperties().size()); 267 assertFalse("no changes yet", l.expect()); 268 PropertyUtils.putGlobalProperties(p); 269 assertTrue("got a change", l.expect()); 270 assertEquals("now have 3 defs", 3, gpp.getProperties().size()); 271 assertEquals("right val", "val3", gpp.getProperties().get("key3")); 272 assertFalse("no spurious changes", l.expect()); 273 p.setProperty("key1", "val1a"); 275 FileObject fo = FileUtil.toFileObject(ubp); 276 assertNotNull("there is USER_BUILD_PROPERTIES on disk", fo); 277 FileLock lock = fo.lock(); 278 OutputStream os = fo.getOutputStream(lock); 279 p.store(os); 280 os.close(); 281 lock.releaseLock(); 282 assertTrue("got a change from the Filesystems API", l.expect()); 283 assertEquals("still have 3 defs", 3, gpp.getProperties().size()); 284 assertEquals("right val for key1", "val1a", gpp.getProperties().get("key1")); 285 298 } 299 300 public void testEvaluateString() throws Exception { 301 Map <String ,String > predefs = new HashMap <String ,String >(); 302 predefs.put("homedir", "/home/me"); 303 Map <String ,String > defs1 = new HashMap <String ,String >(); 304 defs1.put("outdirname", "foo"); 305 defs1.put("outdir", "${homedir}/${outdirname}"); 306 Map <String ,String > defs2 = new HashMap <String ,String >(); 307 defs2.put("outdir2", "${outdir}/subdir"); 308 List <Map <String ,String >> defs12 = new ArrayList <Map <String ,String >>(); 309 defs12.add(defs1); 310 defs12.add(defs2); 311 assertEquals("correct evaluated string", 312 "/home/me/foo/subdir is in /home/me", 313 evaluateString("${outdir2} is in ${homedir}", predefs, defs12)); 314 } 315 316 public void testFixedPropertyProvider() throws Exception { 317 Map <String ,String > defs = new HashMap <String ,String >(); 318 defs.put("key1", "val1"); 319 defs.put("key2", "val2"); 320 PropertyProvider pp = PropertyUtils.fixedPropertyProvider(defs); 321 assertEquals(defs, pp.getProperties()); 322 } 323 324 public void testPropertiesFilePropertyProvider() throws Exception { 325 clearWorkDir(); 326 final FileObject scratch = FileUtil.toFileObject(getWorkDir()); 327 PropertyProvider pp = PropertyUtils.propertiesFilePropertyProvider(new File (FileUtil.toFile(scratch), "test.properties")); 328 AntBasedTestUtil.TestCL l = new AntBasedTestUtil.TestCL(); 329 pp.addChangeListener(l); 330 assertEquals("no defs yet (no file)", Collections.EMPTY_MAP, pp.getProperties()); 331 assertFalse("no changes yet", l.expect()); 332 final FileObject[] testProperties = new FileObject[1]; 333 scratch.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 334 public void run() throws IOException { 335 testProperties[0] = FileUtil.createData(scratch, "test.properties"); 336 FileLock lock = testProperties[0].lock(); 337 try { 338 OutputStream os = testProperties[0].getOutputStream(lock); 339 try { 340 341 PrintWriter pw = new PrintWriter (os); 342 pw.println("a=aval"); 343 pw.flush(); 344 } finally { 345 os.close(); 346 } 347 } finally { 348 lock.releaseLock(); 349 } 350 } 351 }); 352 assertTrue("got a change when file was created", l.expect()); 353 assertEquals("one key", Collections.singletonMap("a", "aval"), pp.getProperties()); 354 FileLock lock = testProperties[0].lock(); 355 try { 356 OutputStream os = testProperties[0].getOutputStream(lock); 357 try { 358 PrintWriter pw = new PrintWriter (os); 359 pw.println("a=aval"); 360 pw.println("b=bval"); 361 pw.flush(); 362 } finally { 363 os.close(); 364 } 365 } finally { 366 lock.releaseLock(); 367 } 368 Map <String ,String > m = new HashMap <String ,String >(); 369 m.put("a", "aval"); 370 m.put("b", "bval"); 371 assertTrue("got a change when file was changed", l.expect()); 372 assertEquals("right properties", m, pp.getProperties()); 373 testProperties[0].delete(); 374 assertTrue("got a change when file was deleted", l.expect()); 375 assertEquals("no defs again (file deleted)", Collections.emptyMap(), pp.getProperties()); 376 } 377 378 public void testSequentialEvaluatorBasic() throws Exception { 379 Map <String ,String > defs1 = new HashMap <String ,String >(); 380 defs1.put("key1", "val1"); 381 defs1.put("key2", "val2"); 382 defs1.put("key5", "5=${key1}"); 383 defs1.put("key6", "6=${key3}"); 384 Map <String ,String > defs2 = new HashMap <String ,String >(); 385 defs2.put("key3", "val3"); 386 defs2.put("key4", "4=${key1}:${key3}"); 387 defs2.put("key7", "7=${undef}"); 388 PropertyEvaluator eval = PropertyUtils.sequentialPropertyEvaluator(null, 389 PropertyUtils.fixedPropertyProvider(defs1), 390 PropertyUtils.fixedPropertyProvider(defs2)); 391 String [] vals = { 392 "val1", 393 "val2", 394 "val3", 395 "4=val1:val3", 396 "5=val1", 397 "6=${key3}", 398 "7=${undef}", 399 }; 400 Map <String ,String > all = eval.getProperties(); 401 assertEquals("right # of props", vals.length, all.size()); 402 for (int i = 1; i <= vals.length; i++) { 403 assertEquals("key" + i + " is correct", vals[i - 1], eval.getProperty("key" + i)); 404 assertEquals("key" + i + " is correct in all properties", vals[i - 1], all.get("key" + i)); 405 } 406 assertEquals("evaluate works", "5=val1 x ${undef}", eval.evaluate("${key5} x ${undef}")); 407 Map <String ,String > predefs = Collections.singletonMap("key3", "preval3"); 409 eval = PropertyUtils.sequentialPropertyEvaluator(PropertyUtils.fixedPropertyProvider(predefs), 410 PropertyUtils.fixedPropertyProvider(defs1), 411 PropertyUtils.fixedPropertyProvider(defs2)); 412 vals = new String [] { 413 "val1", 414 "val2", 415 "preval3", 416 "4=val1:preval3", 417 "5=val1", 418 "6=preval3", 419 "7=${undef}", 420 }; 421 all = eval.getProperties(); 422 assertEquals("right # of props", vals.length, all.size()); 423 for (int i = 1; i <= vals.length; i++) { 424 assertEquals("key" + i + " is correct", vals[i - 1], eval.getProperty("key" + i)); 425 assertEquals("key" + i + " is correct in all properties", vals[i - 1], all.get("key" + i)); 426 } 427 assertEquals("evaluate works", "4=val1:preval3 x ${undef} x preval3", eval.evaluate("${key4} x ${undef} x ${key3}")); 428 } 429 430 public void testSequentialEvaluatorChanges() throws Exception { 431 AntBasedTestUtil.TestMutablePropertyProvider predefs = new AntBasedTestUtil.TestMutablePropertyProvider(new HashMap <String ,String >()); 432 AntBasedTestUtil.TestMutablePropertyProvider defs1 = new AntBasedTestUtil.TestMutablePropertyProvider(new HashMap <String ,String >()); 433 AntBasedTestUtil.TestMutablePropertyProvider defs2 = new AntBasedTestUtil.TestMutablePropertyProvider(new HashMap <String ,String >()); 434 predefs.defs.put("x", "xval1"); 435 predefs.defs.put("y", "yval1"); 436 defs1.defs.put("a", "aval1"); 437 defs1.defs.put("b", "bval1=${x}"); 438 defs1.defs.put("c", "cval1=${z}"); 439 defs2.defs.put("m", "mval1"); 440 defs2.defs.put("n", "nval1=${x}:${b}"); 441 defs2.defs.put("o", "oval1=${z}"); 442 PropertyEvaluator eval = PropertyUtils.sequentialPropertyEvaluator(predefs, defs1, defs2); 443 AntBasedTestUtil.TestPCL l = new AntBasedTestUtil.TestPCL(); 444 eval.addPropertyChangeListener(l); 445 Map <String ,String > result = new HashMap <String ,String >(); 446 result.put("x", "xval1"); 447 result.put("y", "yval1"); 448 result.put("a", "aval1"); 449 result.put("b", "bval1=xval1"); 450 result.put("c", "cval1=${z}"); 451 result.put("m", "mval1"); 452 result.put("n", "nval1=xval1:bval1=xval1"); 453 result.put("o", "oval1=${z}"); 454 assertEquals("correct initial vals", result, eval.getProperties()); 455 assertEquals("no changes yet", Collections.emptySet(), l.changed); 456 predefs.defs.put("x", "xval2"); 458 predefs.mutated(); 459 Map <String ,String > oldvals = new HashMap <String ,String >(); 460 oldvals.put("x", result.get("x")); 461 oldvals.put("b", result.get("b")); 462 oldvals.put("n", result.get("n")); 463 Map <String ,String > newvals = new HashMap <String ,String >(); 464 newvals.put("x", "xval2"); 465 newvals.put("b", "bval1=xval2"); 466 newvals.put("n", "nval1=xval2:bval1=xval2"); 467 result.putAll(newvals); 468 assertEquals("some changes", newvals.keySet(), l.changed); 469 assertEquals("right old values", oldvals, l.oldvals); 470 assertEquals("right new values", newvals, l.newvals); 471 assertEquals("right total values now", result, eval.getProperties()); 472 l.reset(); 473 defs1.defs.put("z", "zval1"); 475 defs1.defs.remove("b"); 476 defs1.mutated(); 477 defs2.defs.put("m", "mval2"); 478 defs2.mutated(); 479 oldvals.clear(); 480 oldvals.put("b", result.get("b")); 481 oldvals.put("c", result.get("c")); 482 oldvals.put("m", result.get("m")); 483 oldvals.put("n", result.get("n")); 484 oldvals.put("o", result.get("o")); 485 oldvals.put("z", result.get("z")); 486 newvals.clear(); 487 newvals.put("b", null); 488 newvals.put("c", "cval1=zval1"); 489 newvals.put("m", "mval2"); 490 newvals.put("n", "nval1=xval2:${b}"); 491 newvals.put("o", "oval1=zval1"); 492 newvals.put("z", "zval1"); 493 result.putAll(newvals); 494 result.remove("b"); 495 assertEquals("some changes", newvals.keySet(), l.changed); 496 assertEquals("right old values", oldvals, l.oldvals); 497 assertEquals("right new values", newvals, l.newvals); 498 assertEquals("right total values now", result, eval.getProperties()); 499 l.reset(); 500 } 501 502 private static final String ILLEGAL_CHARS = " !\"#$%&'()*+,/:;<=>?@[\\]^`{|}~"; 503 504 public void testIsUsablePropertyName() throws Exception { 505 for (int i=0; i<ILLEGAL_CHARS.length(); i++) { 506 String s = ILLEGAL_CHARS.substring(i, i+1); 507 assertFalse("Not a valid property name: "+s, PropertyUtils.isUsablePropertyName(s)); 508 } 509 for (int i=127; i<256; i++) { 510 String s = ""+(char)i; 511 assertFalse("Not a valid property name: "+s+" - "+i, PropertyUtils.isUsablePropertyName(s)); 512 } 513 assertFalse("Not a valid property name", PropertyUtils.isUsablePropertyName(ILLEGAL_CHARS)); 514 for (int i=32; i<127; i++) { 515 String s = ""+(char)i; 516 if (ILLEGAL_CHARS.indexOf((char)i) == -1) { 517 assertTrue("Valid property name: "+s, PropertyUtils.isUsablePropertyName(s)); 518 } 519 } 520 } 521 522 public void testGetUsablePropertyName() throws Exception { 523 StringBuffer bad = new StringBuffer (); 524 StringBuffer good = new StringBuffer (); 525 for (int i=0; i<ILLEGAL_CHARS.length(); i++) { 526 bad.append(ILLEGAL_CHARS.substring(i, i+1)); 527 bad.append("x"); 528 good.append("_"); 529 good.append("x"); 530 } 531 assertEquals("Corrected property name does match", good.toString(), PropertyUtils.getUsablePropertyName(bad.toString())); 532 } 533 534 public void testSequentialPropertyEvaluatorStringAllocation() throws Exception { 535 Map <String ,String > defs = new HashMap <String ,String >(); 539 defs.put("pre-a", "pre-a-val"); 540 defs.put("pre-b", "pre-b-val"); 541 PropertyProvider preprovider = PropertyUtils.fixedPropertyProvider(defs); 542 defs = new HashMap <String ,String >(); 543 defs.put("main-1-a", "main-1-a-val"); 544 defs.put("main-1-b", "main-1-b-val+${pre-b}"); 545 PropertyProvider provider1 = PropertyUtils.fixedPropertyProvider(defs); 546 defs = new HashMap <String ,String >(); 547 defs.put("main-2-a", "main-2-a-val"); 548 defs.put("main-2-b", "main-2-b-val+${main-1-b}"); 549 PropertyProvider provider2 = PropertyUtils.fixedPropertyProvider(defs); 550 PropertyEvaluator pp = PropertyUtils.sequentialPropertyEvaluator(preprovider, provider1, provider2); 551 defs = pp.getProperties(); 552 assertSame("uncopied pre-a", "pre-a-val", defs.get("pre-a")); 553 assertSame("uncopied pre-b", "pre-b-val", defs.get("pre-b")); 554 assertSame("uncopied main-1-a", "main-1-a-val", defs.get("main-1-a")); 555 assertEquals("right main-1-b", "main-1-b-val+pre-b-val", defs.get("main-1-b")); 556 assertSame("uncopied main-2-a", "main-2-a-val", defs.get("main-2-a")); 557 assertEquals("right main-2-b", "main-2-b-val+main-1-b-val+pre-b-val", defs.get("main-2-b")); 558 } 559 560 } 561 | Popular Tags |