1 16 17 package org.springframework.beans.factory.config; 18 19 import java.io.FileNotFoundException ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 import java.util.Set ; 24 import java.util.prefs.Preferences ; 25 26 import junit.framework.TestCase; 27 28 import org.springframework.beans.IndexedTestBean; 29 import org.springframework.beans.MutablePropertyValues; 30 import org.springframework.beans.TestBean; 31 import org.springframework.beans.factory.BeanDefinitionStoreException; 32 import org.springframework.beans.factory.BeanInitializationException; 33 import org.springframework.beans.factory.support.ChildBeanDefinition; 34 import org.springframework.beans.factory.support.ManagedList; 35 import org.springframework.beans.factory.support.ManagedMap; 36 import org.springframework.beans.factory.support.ManagedSet; 37 import org.springframework.beans.factory.support.RootBeanDefinition; 38 import org.springframework.context.support.StaticApplicationContext; 39 import org.springframework.core.JdkVersion; 40 41 45 public class PropertyResourceConfigurerTests extends TestCase { 46 47 public void testPropertyOverrideConfigurer() { 48 StaticApplicationContext ac = new StaticApplicationContext(); 49 ac.registerSingleton("tb1", TestBean.class); 50 ac.registerSingleton("tb2", TestBean.class); 51 MutablePropertyValues pvs = new MutablePropertyValues(); 52 pvs.addPropertyValue("properties", "tb1.age=99\ntb2.name=test"); 53 ac.registerSingleton("configurer1", PropertyOverrideConfigurer.class, pvs); 54 pvs = new MutablePropertyValues(); 55 pvs.addPropertyValue("properties", "tb2.age=99\ntb2.name=test2"); 56 pvs.addPropertyValue("order", "0"); 57 ac.registerSingleton("configurer2", PropertyOverrideConfigurer.class, pvs); 58 ac.refresh(); 59 TestBean tb1 = (TestBean) ac.getBean("tb1"); 60 TestBean tb2 = (TestBean) ac.getBean("tb2"); 61 assertEquals(99, tb1.getAge()); 62 assertEquals(99, tb2.getAge()); 63 assertEquals(null, tb1.getName()); 64 assertEquals("test", tb2.getName()); 65 } 66 67 public void testPropertyOverrideConfigurerWithNestedProperty() { 68 StaticApplicationContext ac = new StaticApplicationContext(); 69 ac.registerSingleton("tb", IndexedTestBean.class); 70 MutablePropertyValues pvs = new MutablePropertyValues(); 71 pvs.addPropertyValue("properties", "tb.array[0].age=99\ntb.list[1].name=test"); 72 ac.registerSingleton("configurer", PropertyOverrideConfigurer.class, pvs); 73 ac.refresh(); 74 IndexedTestBean tb = (IndexedTestBean) ac.getBean("tb"); 75 assertEquals(99, tb.getArray()[0].getAge()); 76 assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); 77 } 78 79 public void testPropertyOverrideConfigurerWithNestedPropertyAndDotInBeanName() { 80 StaticApplicationContext ac = new StaticApplicationContext(); 81 ac.registerSingleton("my.tb", IndexedTestBean.class); 82 MutablePropertyValues pvs = new MutablePropertyValues(); 83 pvs.addPropertyValue("properties", "my.tb_array[0].age=99\nmy.tb_list[1].name=test"); 84 pvs.addPropertyValue("beanNameSeparator", "_"); 85 ac.registerSingleton("configurer", PropertyOverrideConfigurer.class, pvs); 86 ac.refresh(); 87 IndexedTestBean tb = (IndexedTestBean) ac.getBean("my.tb"); 88 assertEquals(99, tb.getArray()[0].getAge()); 89 assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); 90 } 91 92 public void testPropertyOverrideConfigurerWithPropertiesFile() { 93 StaticApplicationContext ac = new StaticApplicationContext(); 94 ac.registerSingleton("tb", IndexedTestBean.class); 95 MutablePropertyValues pvs = new MutablePropertyValues(); 96 pvs.addPropertyValue("location", "classpath:org/springframework/beans/factory/config/test.properties"); 97 ac.registerSingleton("configurer", PropertyOverrideConfigurer.class, pvs); 98 ac.refresh(); 99 IndexedTestBean tb = (IndexedTestBean) ac.getBean("tb"); 100 assertEquals(99, tb.getArray()[0].getAge()); 101 assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); 102 } 103 104 public void testPropertyOverrideConfigurerWithInvalidPropertiesFile() { 105 StaticApplicationContext ac = new StaticApplicationContext(); 106 ac.registerSingleton("tb", IndexedTestBean.class); 107 MutablePropertyValues pvs = new MutablePropertyValues(); 108 pvs.addPropertyValue("locations", 109 new String [] {"classpath:org/springframework/beans/factory/config/test.properties", 110 "classpath:org/springframework/beans/factory/config/xtest.properties"}); 111 pvs.addPropertyValue("ignoreResourceNotFound", Boolean.TRUE); 112 ac.registerSingleton("configurer", PropertyOverrideConfigurer.class, pvs); 113 ac.refresh(); 114 IndexedTestBean tb = (IndexedTestBean) ac.getBean("tb"); 115 assertEquals(99, tb.getArray()[0].getAge()); 116 assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); 117 } 118 119 public void testPropertyOverrideConfigurerWithPropertiesXmlFile() { 120 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) { 122 return; 123 } 124 125 StaticApplicationContext ac = new StaticApplicationContext(); 126 ac.registerSingleton("tb", IndexedTestBean.class); 127 MutablePropertyValues pvs = new MutablePropertyValues(); 128 pvs.addPropertyValue("location", "classpath:org/springframework/beans/factory/config/test-properties.xml"); 129 ac.registerSingleton("configurer", PropertyOverrideConfigurer.class, pvs); 130 ac.refresh(); 131 IndexedTestBean tb = (IndexedTestBean) ac.getBean("tb"); 132 assertEquals(99, tb.getArray()[0].getAge()); 133 assertEquals("test", ((TestBean) tb.getList().get(1)).getName()); 134 } 135 136 public void testPropertyOverrideConfigurerWithConvertProperties() { 137 StaticApplicationContext ac = new StaticApplicationContext(); 138 ac.registerSingleton("tb", IndexedTestBean.class); 139 MutablePropertyValues pvs = new MutablePropertyValues(); 140 pvs.addPropertyValue("properties", "tb.array[0].name=99\ntb.list[1].name=test"); 141 ac.registerSingleton("configurer", ConvertingOverrideConfigurer.class, pvs); 142 ac.refresh(); 143 IndexedTestBean tb = (IndexedTestBean) ac.getBean("tb"); 144 assertEquals("X99", tb.getArray()[0].getName()); 145 assertEquals("Xtest", ((TestBean) tb.getList().get(1)).getName()); 146 } 147 148 public void testPropertyOverrideConfigurerWithInvalidKey() { 149 StaticApplicationContext ac = new StaticApplicationContext(); 150 ac.registerSingleton("tb1", TestBean.class); 151 ac.registerSingleton("tb2", TestBean.class); 152 MutablePropertyValues pvs = new MutablePropertyValues(); 153 pvs.addPropertyValue("properties", "argh=hgra\ntb1.age=99\ntb2.name=test"); 154 pvs.addPropertyValue("ignoreInvalidKeys", "true"); 155 ac.registerSingleton("configurer1", PropertyOverrideConfigurer.class, pvs); 156 pvs = new MutablePropertyValues(); 157 pvs.addPropertyValue("properties", "tb2.age=99\ntb2.name=test2"); 158 pvs.addPropertyValue("order", "0"); 159 ac.registerSingleton("configurer2", PropertyOverrideConfigurer.class, pvs); 160 try { 161 ac.refresh(); 162 } 163 catch (BeanInitializationException ex) { 164 assertTrue(ex.getMessage().toLowerCase().indexOf("argh") != -1); 165 } 166 } 167 168 public void testPropertyOverrideConfigurerWithIgnoreInvalidKeys() { 169 StaticApplicationContext ac = new StaticApplicationContext(); 170 ac.registerSingleton("tb1", TestBean.class); 171 ac.registerSingleton("tb2", TestBean.class); 172 MutablePropertyValues pvs = new MutablePropertyValues(); 173 pvs.addPropertyValue("properties", "argh=hgra\ntb1.age=99\ntb2.name=test"); 174 pvs.addPropertyValue("ignoreInvalidKeys", "true"); 175 ac.registerSingleton("configurer1", PropertyOverrideConfigurer.class, pvs); 176 pvs = new MutablePropertyValues(); 177 pvs.addPropertyValue("properties", "tb2.age=99\ntb2.name=test2"); 178 pvs.addPropertyValue("order", "0"); 179 ac.registerSingleton("configurer2", PropertyOverrideConfigurer.class, pvs); 180 ac.refresh(); 181 TestBean tb1 = (TestBean) ac.getBean("tb1"); 182 TestBean tb2 = (TestBean) ac.getBean("tb2"); 183 assertEquals(99, tb1.getAge()); 184 assertEquals(99, tb2.getAge()); 185 assertEquals(null, tb1.getName()); 186 assertEquals("test", tb2.getName()); 187 } 188 189 public void testPropertyPlaceholderConfigurer() { 190 StaticApplicationContext ac = new StaticApplicationContext(); 191 192 MutablePropertyValues pvs = new MutablePropertyValues(); 193 pvs.addPropertyValue("age", "${age}"); 194 pvs.addPropertyValue("name", "name${var}${var}${"); 195 pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}")); 196 ac.registerSingleton("tb1", TestBean.class, pvs); 197 198 ConstructorArgumentValues cas = new ConstructorArgumentValues(); 199 cas.addIndexedArgumentValue(1, "${age}"); 200 cas.addGenericArgumentValue("${var}name${age}"); 201 202 pvs = new MutablePropertyValues(); 203 List friends = new ManagedList(); 204 friends.add("na${age}me"); 205 friends.add(new RuntimeBeanReference("${ref}")); 206 pvs.addPropertyValue("friends", friends); 207 208 Set someSet = new ManagedSet(); 209 someSet.add("na${age}me"); 210 someSet.add(new RuntimeBeanReference("${ref}")); 211 someSet.add(new TypedStringValue("${age}", Integer .class)); 212 pvs.addPropertyValue("someSet", someSet); 213 214 Map someMap = new ManagedMap(); 215 someMap.put("key1", new RuntimeBeanReference("${ref}")); 216 someMap.put("key2", "${age}name"); 217 MutablePropertyValues innerPvs = new MutablePropertyValues(); 218 innerPvs.addPropertyValue("touchy", "${os.name}"); 219 someMap.put("key3", new RootBeanDefinition(TestBean.class, innerPvs)); 220 MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs); 221 someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child")); 222 pvs.addPropertyValue("someMap", someMap); 223 224 RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs); 225 ac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd); 226 227 pvs = new MutablePropertyValues(); 228 pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=mykey4"); 229 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 230 ac.refresh(); 231 232 TestBean tb1 = (TestBean) ac.getBean("tb1"); 233 TestBean tb2 = (TestBean) ac.getBean("tb2"); 234 assertEquals(98, tb1.getAge()); 235 assertEquals(98, tb2.getAge()); 236 assertEquals("namemyvarmyvar${", tb1.getName()); 237 assertEquals("myvarname98", tb2.getName()); 238 assertEquals(tb2, tb1.getSpouse()); 239 assertEquals(2, tb2.getFriends().size()); 240 assertEquals("na98me", tb2.getFriends().iterator().next()); 241 assertEquals(tb2, ((List ) tb2.getFriends()).get(1)); 242 assertEquals(3, tb2.getSomeSet().size()); 243 assertTrue(tb2.getSomeSet().contains("na98me")); 244 assertTrue(tb2.getSomeSet().contains(tb2)); 245 assertTrue(tb2.getSomeSet().contains(new Integer (98))); 246 assertEquals(4, tb2.getSomeMap().size()); 247 assertEquals(tb2, tb2.getSomeMap().get("key1")); 248 assertEquals("98name", tb2.getSomeMap().get("key2")); 249 TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3"); 250 TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4"); 251 assertEquals(0, inner1.getAge()); 252 assertEquals(null, inner1.getName()); 253 assertEquals(System.getProperty("os.name"), inner1.getTouchy()); 254 assertEquals(98, inner2.getAge()); 255 assertEquals("namemyvarmyvar${", inner2.getName()); 256 assertEquals(System.getProperty("os.name"), inner2.getTouchy()); 257 } 258 259 public void testPropertyPlaceholderConfigurerWithSystemPropertyFallback() { 260 StaticApplicationContext ac = new StaticApplicationContext(); 261 MutablePropertyValues pvs = new MutablePropertyValues(); 262 pvs.addPropertyValue("touchy", "${os.name}"); 263 ac.registerSingleton("tb", TestBean.class, pvs); 264 pvs = new MutablePropertyValues(); 265 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 266 ac.refresh(); 267 TestBean tb = (TestBean) ac.getBean("tb"); 268 assertEquals(System.getProperty("os.name"), tb.getTouchy()); 269 } 270 271 public void testPropertyPlaceholderConfigurerWithSystemPropertyNotUsed() { 272 StaticApplicationContext ac = new StaticApplicationContext(); 273 MutablePropertyValues pvs = new MutablePropertyValues(); 274 pvs.addPropertyValue("touchy", "${os.name}"); 275 ac.registerSingleton("tb", TestBean.class, pvs); 276 pvs = new MutablePropertyValues(); 277 Properties props = new Properties (); 278 props.put("os.name", "myos"); 279 pvs.addPropertyValue("properties", props); 280 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 281 ac.refresh(); 282 TestBean tb = (TestBean) ac.getBean("tb"); 283 assertEquals("myos", tb.getTouchy()); 284 } 285 286 public void testPropertyPlaceholderConfigurerWithOverridingSystemProperty() { 287 StaticApplicationContext ac = new StaticApplicationContext(); 288 MutablePropertyValues pvs = new MutablePropertyValues(); 289 pvs.addPropertyValue("touchy", "${os.name}"); 290 ac.registerSingleton("tb", TestBean.class, pvs); 291 pvs = new MutablePropertyValues(); 292 Properties props = new Properties (); 293 props.put("os.name", "myos"); 294 pvs.addPropertyValue("properties", props); 295 pvs.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_OVERRIDE"); 296 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 297 ac.refresh(); 298 TestBean tb = (TestBean) ac.getBean("tb"); 299 assertEquals(System.getProperty("os.name"), tb.getTouchy()); 300 } 301 302 public void testPropertyPlaceholderConfigurerWithUnresolvableSystemProperty() { 303 StaticApplicationContext ac = new StaticApplicationContext(); 304 MutablePropertyValues pvs = new MutablePropertyValues(); 305 pvs.addPropertyValue("touchy", "${user.dir}"); 306 ac.registerSingleton("tb", TestBean.class, pvs); 307 pvs = new MutablePropertyValues(); 308 pvs.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_NEVER"); 309 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 310 try { 311 ac.refresh(); 312 fail("Should have thrown BeanDefinitionStoreException"); 313 } 314 catch (BeanDefinitionStoreException ex) { 315 assertTrue(ex.getMessage().indexOf("user.dir") != -1); 317 } 318 } 319 320 public void testPropertyPlaceholderConfigurerWithUnresolvablePlaceholder() { 321 StaticApplicationContext ac = new StaticApplicationContext(); 322 MutablePropertyValues pvs = new MutablePropertyValues(); 323 pvs.addPropertyValue("name", "${ref}"); 324 ac.registerSingleton("tb", TestBean.class, pvs); 325 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, null); 326 try { 327 ac.refresh(); 328 fail("Should have thrown BeanDefinitionStoreException"); 329 } 330 catch (BeanDefinitionStoreException ex) { 331 assertTrue(ex.getMessage().indexOf("ref") != -1); 333 } 334 } 335 336 public void testPropertyPlaceholderConfigurerWithIgnoreUnresolvablePlaceholder() { 337 StaticApplicationContext ac = new StaticApplicationContext(); 338 MutablePropertyValues pvs = new MutablePropertyValues(); 339 pvs.addPropertyValue("name", "${ref}"); 340 ac.registerSingleton("tb", TestBean.class, pvs); 341 pvs = new MutablePropertyValues(); 342 pvs.addPropertyValue("ignoreUnresolvablePlaceholders", Boolean.TRUE); 343 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 344 try { 345 ac.refresh(); 346 TestBean tb = (TestBean) ac.getBean("tb"); 347 assertEquals("${ref}", tb.getName()); 348 } 349 catch (BeanDefinitionStoreException ex) { 350 fail("Should not have thrown BeanDefinitionStoreException"); 351 } 352 } 353 354 public void testPropertyPlaceholderConfigurerWithSystemPropertyInLocation() { 355 StaticApplicationContext ac = new StaticApplicationContext(); 356 MutablePropertyValues pvs = new MutablePropertyValues(); 357 pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}")); 358 ac.registerSingleton("tb", TestBean.class, pvs); 359 pvs = new MutablePropertyValues(); 360 pvs.addPropertyValue("location", "${user.dir}/test"); 361 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 362 try { 363 ac.refresh(); 364 fail("Should have thrown BeanDefinitionStoreException"); 365 } 366 catch (BeanInitializationException ex) { 367 assertTrue(ex.getCause() instanceof FileNotFoundException ); 369 String userDir = System.getProperty("user.dir"); 371 if (userDir.startsWith("/")) 372 userDir = userDir.substring(1); 373 assertTrue(ex.getMessage().indexOf(userDir) != -1); 374 } 375 } 376 377 public void testPropertyPlaceholderConfigurerWithSystemPropertiesInLocation() { 378 StaticApplicationContext ac = new StaticApplicationContext(); 379 MutablePropertyValues pvs = new MutablePropertyValues(); 380 pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}")); 381 ac.registerSingleton("tb", TestBean.class, pvs); 382 pvs = new MutablePropertyValues(); 383 pvs.addPropertyValue("location", "${user.dir}/test/${user.dir}"); 384 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 385 try { 386 ac.refresh(); 387 fail("Should have thrown BeanDefinitionStoreException"); 388 } 389 catch (BeanInitializationException ex) { 390 assertTrue(ex.getCause() instanceof FileNotFoundException ); 392 String userDir = System.getProperty("user.dir"); 394 if (userDir.startsWith("/")) { 395 userDir = userDir.substring(1); 396 } 397 398 400 assertTrue( 402 ex.getMessage().indexOf(userDir + "/test/" + userDir) != -1 403 || 404 ex.getMessage().indexOf(userDir + "/test//" + userDir) != -1 405 ); 406 407 } 408 } 409 410 public void testPropertyPlaceholderConfigurerWithUnresolvableSystemPropertiesInLocation() { 411 StaticApplicationContext ac = new StaticApplicationContext(); 412 MutablePropertyValues pvs = new MutablePropertyValues(); 413 pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}")); 414 ac.registerSingleton("tb", TestBean.class, pvs); 415 pvs = new MutablePropertyValues(); 416 pvs.addPropertyValue("location", "${myprop}/test/${myprop}"); 417 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 418 try { 419 ac.refresh(); 420 fail("Should have thrown BeanDefinitionStoreException"); 421 } 422 catch (BeanInitializationException ex) { 423 assertTrue(ex.getCause() instanceof FileNotFoundException ); 425 assertTrue(ex.getMessage().indexOf("myprop") != -1); 426 } 427 } 428 429 public void testPropertyPlaceholderConfigurerWithCircularReference() { 430 StaticApplicationContext ac = new StaticApplicationContext(); 431 MutablePropertyValues pvs = new MutablePropertyValues(); 432 pvs.addPropertyValue("age", "${age}"); 433 pvs.addPropertyValue("name", "name${var}"); 434 pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}")); 435 ac.registerSingleton("tb1", TestBean.class, pvs); 436 pvs = new MutablePropertyValues(); 437 pvs.addPropertyValue("age", "${age}"); 438 pvs.addPropertyValue("name", "name${age}"); 439 ac.registerSingleton("tb2", TestBean.class, pvs); 440 pvs = new MutablePropertyValues(); 441 pvs.addPropertyValue("properties", "age=99\nvar=${m}var\nref=tb2\nm=${var}"); 442 ac.registerSingleton("configurer1", PropertyPlaceholderConfigurer.class, pvs); 443 pvs = new MutablePropertyValues(); 444 pvs.addPropertyValue("properties", "age=98"); 445 pvs.addPropertyValue("order", "0"); 446 ac.registerSingleton("configurer2", PropertyPlaceholderConfigurer.class, pvs); 447 try { 448 ac.refresh(); 449 fail("Should have thrown BeanDefinitionStoreException"); 450 } 451 catch (BeanDefinitionStoreException ex) { 452 } 454 } 455 456 public void testPropertyPlaceholderConfigurerWithDefaultProperties() { 457 StaticApplicationContext ac = new StaticApplicationContext(); 458 MutablePropertyValues pvs = new MutablePropertyValues(); 459 pvs.addPropertyValue("touchy", "${test}"); 460 ac.registerSingleton("tb", TestBean.class, pvs); 461 pvs = new MutablePropertyValues(); 462 Properties props = new Properties (); 463 props.put("test", "mytest"); 464 pvs.addPropertyValue("properties", new Properties (props)); 465 ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs); 466 ac.refresh(); 467 TestBean tb = (TestBean) ac.getBean("tb"); 468 assertEquals("mytest", tb.getTouchy()); 469 } 470 471 public void testPreferencesPlaceholderConfigurer() { 472 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) { 474 return; 475 } 476 477 StaticApplicationContext ac = new StaticApplicationContext(); 478 MutablePropertyValues pvs = new MutablePropertyValues(); 479 pvs.addPropertyValue("name", "${myName}"); 480 pvs.addPropertyValue("age", "${myAge}"); 481 pvs.addPropertyValue("touchy", "${myTouchy}"); 482 ac.registerSingleton("tb", TestBean.class, pvs); 483 pvs = new MutablePropertyValues(); 484 Properties props = new Properties (); 485 props.put("myAge", "99"); 486 pvs.addPropertyValue("properties", props); 487 ac.registerSingleton("configurer", PreferencesPlaceholderConfigurer.class, pvs); 488 Preferences.systemRoot().put("myName", "myNameValue"); 489 Preferences.systemRoot().put("myTouchy", "myTouchyValue"); 490 Preferences.userRoot().put("myTouchy", "myOtherTouchyValue"); 491 ac.refresh(); 492 TestBean tb = (TestBean) ac.getBean("tb"); 493 assertEquals("myNameValue", tb.getName()); 494 assertEquals(99, tb.getAge()); 495 assertEquals("myOtherTouchyValue", tb.getTouchy()); 496 Preferences.userRoot().remove("myTouchy"); 497 Preferences.systemRoot().remove("myTouchy"); 498 Preferences.systemRoot().remove("myName"); 499 } 500 501 public void testPreferencesPlaceholderConfigurerWithCustomTreePaths() { 502 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) { 504 return; 505 } 506 507 StaticApplicationContext ac = new StaticApplicationContext(); 508 MutablePropertyValues pvs = new MutablePropertyValues(); 509 pvs.addPropertyValue("name", "${myName}"); 510 pvs.addPropertyValue("age", "${myAge}"); 511 pvs.addPropertyValue("touchy", "${myTouchy}"); 512 ac.registerSingleton("tb", TestBean.class, pvs); 513 pvs = new MutablePropertyValues(); 514 Properties props = new Properties (); 515 props.put("myAge", "99"); 516 pvs.addPropertyValue("properties", props); 517 pvs.addPropertyValue("systemTreePath", "mySystemPath"); 518 pvs.addPropertyValue("userTreePath", "myUserPath"); 519 ac.registerSingleton("configurer", PreferencesPlaceholderConfigurer.class, pvs); 520 Preferences.systemRoot().node("mySystemPath").put("myName", "myNameValue"); 521 Preferences.systemRoot().node("mySystemPath").put("myTouchy", "myTouchyValue"); 522 Preferences.userRoot().node("myUserPath").put("myTouchy", "myOtherTouchyValue"); 523 ac.refresh(); 524 TestBean tb = (TestBean) ac.getBean("tb"); 525 assertEquals("myNameValue", tb.getName()); 526 assertEquals(99, tb.getAge()); 527 assertEquals("myOtherTouchyValue", tb.getTouchy()); 528 Preferences.userRoot().node("myUserPath").remove("myTouchy"); 529 Preferences.systemRoot().node("mySystemPath").remove("myTouchy"); 530 Preferences.systemRoot().node("mySystemPath").remove("myName"); 531 } 532 533 534 private static class ConvertingOverrideConfigurer extends PropertyOverrideConfigurer { 535 536 protected String convertPropertyValue(String originalValue) { 537 return "X" + originalValue; 538 } 539 } 540 541 } 542 | Popular Tags |