PropertyResourceConfigurerTests.java 28.8 KB
Newer Older
1
/*
2
 * Copyright 2002-2010 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.beans.factory.config;

19 20 21 22 23 24 25 26
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;
import static test.util.TestResourceUtils.qualifiedResource;

27
import java.util.Collections;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.prefs.Preferences;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.ManagedSet;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.io.Resource;

48 49 50
import test.beans.IndexedTestBean;
import test.beans.TestBean;

51 52 53 54 55
/**
 * Unit tests for various {@link PropertyResourceConfigurer} implementations including:
 * {@link PropertyPlaceholderConfigurer}, {@link PropertyOverrideConfigurer} and
 * {@link PreferencesPlaceholderConfigurer}.
 * 
56
 * @see PropertyPlaceholderConfigurerTests
57 58 59 60
 * @since 02.10.2003
 * @author Juergen Hoeller
 * @author Chris Beams
 */
C
Chris Beams 已提交
61
public final class PropertyResourceConfigurerTests {
62

C
Chris Beams 已提交
63 64 65 66 67
	private static final Class<?> CLASS = PropertyResourceConfigurerTests.class;
	private static final Resource TEST_PROPS = qualifiedResource(CLASS, "test.properties");
	private static final Resource XTEST_PROPS = qualifiedResource(CLASS, "xtest.properties"); // does not exist
	private static final Resource TEST_PROPS_XML = qualifiedResource(CLASS, "test.properties.xml");
	
68
	private DefaultListableBeanFactory factory;
69

70 71 72 73 74 75 76 77 78 79 80
	@Before
	public void setUp() {
		factory = new DefaultListableBeanFactory();
	}

	@Test
	public void testPropertyOverrideConfigurer() {
		BeanDefinition def1 = BeanDefinitionBuilder
			.genericBeanDefinition(TestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb1", def1);
81

82 83 84 85
		BeanDefinition def2 = BeanDefinitionBuilder
			.genericBeanDefinition(TestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb2", def2);
86

87 88
		PropertyOverrideConfigurer poc1;
		PropertyOverrideConfigurer poc2;
89

90 91 92 93 94 95 96
		{
			poc1 = new PropertyOverrideConfigurer();
			Properties props = new Properties();
			props.setProperty("tb1.age", "99");
			props.setProperty("tb2.name", "test");
			poc1.setProperties(props);
		}
97

98 99 100 101 102 103 104
		{
			poc2 = new PropertyOverrideConfigurer();
			Properties props = new Properties();
			props.setProperty("tb2.age", "99");
			props.setProperty("tb2.name", "test2");
			poc2.setProperties(props);
		}
105

106 107 108
		// emulate what happens when BFPPs are added to an application context: It's LIFO-based
		poc2.postProcessBeanFactory(factory);
		poc1.postProcessBeanFactory(factory);
109

110 111
		TestBean tb1 = (TestBean) factory.getBean("tb1");
		TestBean tb2 = (TestBean) factory.getBean("tb2");
112

113 114 115 116 117 118 119 120 121 122 123 124
		assertEquals(99, tb1.getAge());
		assertEquals(99, tb2.getAge());
		assertEquals(null, tb1.getName());
		assertEquals("test", tb2.getName());
	}

	@Test
	public void testPropertyOverrideConfigurerWithNestedProperty() {
		BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(IndexedTestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb", def);
125

126 127 128 129 130 131 132
		PropertyOverrideConfigurer poc;
		poc = new PropertyOverrideConfigurer();
		Properties props = new Properties();
		props.setProperty("tb.array[0].age", "99");
		props.setProperty("tb.list[1].name", "test");
		poc.setProperties(props);
		poc.postProcessBeanFactory(factory);
133

134 135 136 137 138 139 140 141 142 143 144
		IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
		assertEquals(99, tb.getArray()[0].getAge());
		assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
	}

	@Test
	public void testPropertyOverrideConfigurerWithNestedPropertyAndDotInBeanName() {
		BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(IndexedTestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("my.tb", def);
145

146 147 148 149 150 151 152 153
		PropertyOverrideConfigurer poc;
		poc = new PropertyOverrideConfigurer();
		Properties props = new Properties();
		props.setProperty("my.tb_array[0].age", "99");
		props.setProperty("my.tb_list[1].name", "test");
		poc.setProperties(props);
		poc.setBeanNameSeparator("_");
		poc.postProcessBeanFactory(factory);
154

155 156 157 158 159 160 161 162 163 164 165
		IndexedTestBean tb = (IndexedTestBean) factory.getBean("my.tb");
		assertEquals(99, tb.getArray()[0].getAge());
		assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
	}

	@Test
	public void testPropertyOverrideConfigurerWithNestedMapPropertyAndDotInMapKey() {
		BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(IndexedTestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb", def);
166

167 168 169 170 171 172 173
		PropertyOverrideConfigurer poc;
		poc = new PropertyOverrideConfigurer();
		Properties props = new Properties();
		props.setProperty("tb.map[key1]", "99");
		props.setProperty("tb.map[key2.ext]", "test");
		poc.setProperties(props);
		poc.postProcessBeanFactory(factory);
174

175 176 177 178
		IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
		assertEquals("99", tb.getMap().get("key1"));
		assertEquals("test", tb.getMap().get("key2.ext"));
	}
179

180 181 182 183 184 185
	@Test
	public void testPropertyOverrideConfigurerWithHeldProperties() {
		BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(PropertiesHolder.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb", def);
186

187 188 189 190 191 192
		PropertyOverrideConfigurer poc;
		poc = new PropertyOverrideConfigurer();
		Properties props = new Properties();
		props.setProperty("tb.heldProperties[mail.smtp.auth]", "true");
		poc.setProperties(props);
		poc.postProcessBeanFactory(factory);
193

194 195 196
		PropertiesHolder tb = (PropertiesHolder) factory.getBean("tb");
		assertEquals("true", tb.getHeldProperties().getProperty("mail.smtp.auth"));
	}
197

198
	static class PropertiesHolder {
199
		private Properties props = new Properties();
200

201 202 203
		public Properties getHeldProperties() {
			return props;
		}
204

205 206 207
		public void setHeldProperties(Properties props) {
			this.props = props;
		}
208 209 210 211 212 213 214 215
	}

	@Test
	public void testPropertyOverrideConfigurerWithPropertiesFile() {
		BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(IndexedTestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb", def);
216

217
		PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
C
Chris Beams 已提交
218
		poc.setLocation(TEST_PROPS);
219
		poc.postProcessBeanFactory(factory);
220

221 222 223 224 225 226 227 228 229 230 231
		IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
		assertEquals(99, tb.getArray()[0].getAge());
		assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
	}

	@Test
	public void testPropertyOverrideConfigurerWithInvalidPropertiesFile() {
		BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(IndexedTestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb", def);
232

233
		PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
C
Chris Beams 已提交
234
		poc.setLocations(new Resource[] { TEST_PROPS, XTEST_PROPS });
235 236
		poc.setIgnoreResourceNotFound(true);
		poc.postProcessBeanFactory(factory);
237

238 239 240 241 242 243 244 245 246 247 248
		IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
		assertEquals(99, tb.getArray()[0].getAge());
		assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
	}

	@Test
	public void testPropertyOverrideConfigurerWithPropertiesXmlFile() {
		BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(IndexedTestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb", def);
249

250
		PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
C
Chris Beams 已提交
251
		poc.setLocation(TEST_PROPS_XML);
252
		poc.postProcessBeanFactory(factory);
253

254 255 256 257 258 259 260 261 262 263 264
		IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
		assertEquals(99, tb.getArray()[0].getAge());
		assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
	}

	@Test
	public void testPropertyOverrideConfigurerWithConvertProperties() {
		BeanDefinition def = BeanDefinitionBuilder
			.genericBeanDefinition(IndexedTestBean.class)
			.getBeanDefinition();
		factory.registerBeanDefinition("tb", def);
265

266 267 268 269 270 271
		ConvertingOverrideConfigurer bfpp = new ConvertingOverrideConfigurer();
		Properties props = new Properties();
		props.setProperty("tb.array[0].name", "99");
		props.setProperty("tb.list[1].name", "test");
		bfpp.setProperties(props);
		bfpp.postProcessBeanFactory(factory);
272

273 274 275 276 277 278 279 280 281
		IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
		assertEquals("X99", tb.getArray()[0].getName());
		assertEquals("Xtest", ((TestBean) tb.getList().get(1)).getName());
	}

	@Test
	public void testPropertyOverrideConfigurerWithInvalidKey() {
		factory.registerBeanDefinition("tb1", genericBeanDefinition(TestBean.class).getBeanDefinition());
		factory.registerBeanDefinition("tb2", genericBeanDefinition(TestBean.class).getBeanDefinition());
282

283 284 285 286 287 288
		{
			PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
			poc.setIgnoreInvalidKeys(true);
			Properties props = new Properties();
			props.setProperty("argh", "hgra");
			props.setProperty("tb2.name", "test");
289 290
			props.setProperty("tb2.nam", "test");
			props.setProperty("tb3.name", "test");
291 292
			poc.setProperties(props);
			poc.postProcessBeanFactory(factory);
293
			assertEquals("test", factory.getBean("tb2", TestBean.class).getName());
294 295 296 297 298 299 300 301 302 303 304
		}
		{
			PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
			Properties props = new Properties();
			props.setProperty("argh", "hgra");
			props.setProperty("tb2.age", "99");
			props.setProperty("tb2.name", "test2");
			poc.setProperties(props);
			poc.setOrder(0); // won't actually do anything since we're not processing through an app ctx
			try {
				poc.postProcessBeanFactory(factory);
305 306
			}
			catch (BeanInitializationException ex) {
307
				// prove that the processor chokes on the invalid key
308
				assertTrue(ex.getMessage().toLowerCase().contains("argh"));
309 310 311 312 313 314 315 316
			}
		}
	}

	@Test
	public void testPropertyOverrideConfigurerWithIgnoreInvalidKeys() {
		factory.registerBeanDefinition("tb1", genericBeanDefinition(TestBean.class).getBeanDefinition());
		factory.registerBeanDefinition("tb2", genericBeanDefinition(TestBean.class).getBeanDefinition());
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		{
			PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
			Properties props = new Properties();
			props.setProperty("tb2.age", "99");
			props.setProperty("tb2.name", "test2");
			poc.setProperties(props);
			poc.setOrder(0); // won't actually do anything since we're not processing through an app ctx
			poc.postProcessBeanFactory(factory);
		}
		{
			PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
			poc.setIgnoreInvalidKeys(true);
			Properties props = new Properties();
			props.setProperty("argh", "hgra");
			props.setProperty("tb1.age", "99");
			props.setProperty("tb2.name", "test");
			poc.setProperties(props);
			poc.postProcessBeanFactory(factory);
		}
337

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
		TestBean tb1 = (TestBean) factory.getBean("tb1");
		TestBean tb2 = (TestBean) factory.getBean("tb2");
		assertEquals(99, tb1.getAge());
		assertEquals(99, tb2.getAge());
		assertEquals(null, tb1.getName());
		assertEquals("test", tb2.getName());
	}

	@Test
	public void testPropertyPlaceholderConfigurer() {
		doTestPropertyPlaceholderConfigurer(false);
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithParentChildSeparation() {
		doTestPropertyPlaceholderConfigurer(true);
	}

	private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) {
357
		Map singletonMap = Collections.singletonMap("myKey", "myValue");
358 359
		if (parentChildSeparation) {
			MutablePropertyValues pvs1 = new MutablePropertyValues();
360
			pvs1.add("age", "${age}");
361
			MutablePropertyValues pvs2 = new MutablePropertyValues();
362 363 364
			pvs2.add("name", "name${var}${var}${");
			pvs2.add("spouse", new RuntimeBeanReference("${ref}"));
			pvs2.add("someMap", singletonMap);
365 366 367 368 369 370 371
			RootBeanDefinition parent = new RootBeanDefinition(TestBean.class, pvs1);
			ChildBeanDefinition bd = new ChildBeanDefinition("${parent}", pvs2);
			factory.registerBeanDefinition("parent1", parent);
			factory.registerBeanDefinition("tb1", bd);
		}
		else {
			MutablePropertyValues pvs = new MutablePropertyValues();
372 373 374 375
			pvs.add("age", "${age}");
			pvs.add("name", "name${var}${var}${");
			pvs.add("spouse", new RuntimeBeanReference("${ref}"));
			pvs.add("someMap", singletonMap);
376 377 378 379 380 381 382 383 384
			RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
			factory.registerBeanDefinition("tb1", bd);
		}

		ConstructorArgumentValues cas = new ConstructorArgumentValues();
		cas.addIndexedArgumentValue(1, "${age}");
		cas.addGenericArgumentValue("${var}name${age}");

		MutablePropertyValues pvs = new MutablePropertyValues();
385 386 387

		pvs.add("stringArray", new String[] {"${os.name}", "${age}"});

388 389 390
		List<Object> friends = new ManagedList<Object>();
		friends.add("na${age}me");
		friends.add(new RuntimeBeanReference("${ref}"));
391
		pvs.add("friends", friends);
392 393 394 395 396

		Set<Object> someSet = new ManagedSet<Object>();
		someSet.add("na${age}me");
		someSet.add(new RuntimeBeanReference("${ref}"));
		someSet.add(new TypedStringValue("${age}", Integer.class));
397
		pvs.add("someSet", someSet);
398 399 400 401 402 403 404

		Map<Object, Object> someMap = new ManagedMap<Object, Object>();
		someMap.put(new TypedStringValue("key${age}"), new TypedStringValue("${age}"));
		someMap.put(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}"));
		someMap.put("key1", new RuntimeBeanReference("${ref}"));
		someMap.put("key2", "${age}name");
		MutablePropertyValues innerPvs = new MutablePropertyValues();
405
		innerPvs.add("touchy", "${os.name}");
406 407 408
		someMap.put("key3", new RootBeanDefinition(TestBean.class, innerPvs));
		MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs);
		someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
409
		pvs.add("someMap", someMap);
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

		RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs);
		factory.registerBeanDefinition("tb2", bd);

		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.setProperty("age", "98");
		props.setProperty("var", "${m}var");
		props.setProperty("ref", "tb2");
		props.setProperty("m", "my");
		props.setProperty("key4", "mykey4");
		props.setProperty("parent", "parent1");
		ppc.setProperties(props);
		ppc.postProcessBeanFactory(factory);

		TestBean tb1 = (TestBean) factory.getBean("tb1");
		TestBean tb2 = (TestBean) factory.getBean("tb2");
		assertEquals(98, tb1.getAge());
		assertEquals(98, tb2.getAge());
		assertEquals("namemyvarmyvar${", tb1.getName());
		assertEquals("myvarname98", tb2.getName());
		assertEquals(tb2, tb1.getSpouse());
432 433
		assertEquals(1, tb1.getSomeMap().size());
		assertEquals("myValue", tb1.getSomeMap().get("myKey"));
434 435 436
		assertEquals(2, tb2.getStringArray().length);
		assertEquals(System.getProperty("os.name"), tb2.getStringArray()[0]);
		assertEquals("98", tb2.getStringArray()[1]);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
		assertEquals(2, tb2.getFriends().size());
		assertEquals("na98me", tb2.getFriends().iterator().next());
		assertEquals(tb2, tb2.getFriends().toArray()[1]);
		assertEquals(3, tb2.getSomeSet().size());
		assertTrue(tb2.getSomeSet().contains("na98me"));
		assertTrue(tb2.getSomeSet().contains(tb2));
		assertTrue(tb2.getSomeSet().contains(new Integer(98)));
		assertEquals(6, tb2.getSomeMap().size());
		assertEquals("98", tb2.getSomeMap().get("key98"));
		assertEquals(tb2, tb2.getSomeMap().get("key98ref"));
		assertEquals(tb2, tb2.getSomeMap().get("key1"));
		assertEquals("98name", tb2.getSomeMap().get("key2"));
		TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3");
		TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4");
		assertEquals(0, inner1.getAge());
		assertEquals(null, inner1.getName());
		assertEquals(System.getProperty("os.name"), inner1.getTouchy());
		assertEquals(98, inner2.getAge());
		assertEquals("namemyvarmyvar${", inner2.getName());
		assertEquals(System.getProperty("os.name"), inner2.getTouchy());
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithSystemPropertyFallback() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("touchy", "${os.name}").getBeanDefinition());
464

465 466 467 468 469 470 471 472 473 474 475 476
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.postProcessBeanFactory(factory);

		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals(System.getProperty("os.name"), tb.getTouchy());
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithSystemPropertyNotUsed() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("touchy", "${os.name}").getBeanDefinition());
477

478 479 480 481 482
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.setProperty("os.name", "myos");
		ppc.setProperties(props);
		ppc.postProcessBeanFactory(factory);
483

484 485 486 487 488 489 490 491 492
		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("myos", tb.getTouchy());
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithOverridingSystemProperty() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("touchy", "${os.name}").getBeanDefinition());
493

494 495 496 497 498 499
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("os.name", "myos");
		ppc.setProperties(props);
		ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
		ppc.postProcessBeanFactory(factory);
500

501 502 503 504 505 506 507 508 509
		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals(System.getProperty("os.name"), tb.getTouchy());
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithUnresolvableSystemProperty() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("touchy", "${user.dir}").getBeanDefinition());
510

511 512
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_NEVER);
513

514 515 516 517 518 519
		try {
			ppc.postProcessBeanFactory(factory);
			fail("Should have thrown BeanDefinitionStoreException");
		}
		catch (BeanDefinitionStoreException ex) {
			// expected
520
			assertTrue(ex.getMessage().contains("user.dir"));
521 522 523 524 525 526 527 528
		}
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithUnresolvablePlaceholder() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("name", "${ref}").getBeanDefinition());
529

530
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
531

532 533 534 535 536 537
		try {
			ppc.postProcessBeanFactory(factory);
			fail("Should have thrown BeanDefinitionStoreException");
		}
		catch (BeanDefinitionStoreException ex) {
			// expected
538
			assertTrue(ex.getMessage().contains("ref"));
539 540 541 542 543 544 545 546
		}
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithIgnoreUnresolvablePlaceholder() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("name", "${ref}").getBeanDefinition());
547

548 549 550
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.setIgnoreUnresolvablePlaceholders(true);
		ppc.postProcessBeanFactory(factory);
551

552 553 554 555 556 557 558 559 560
		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("${ref}", tb.getName());
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithEmptyStringAsNull() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("name", "").getBeanDefinition());
561

562 563 564
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.setNullValue("");
		ppc.postProcessBeanFactory(factory);
565

566 567 568 569 570 571 572 573 574
		TestBean tb = (TestBean) factory.getBean("tb");
		assertNull(tb.getName());
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithEmptyStringInPlaceholderAsNull() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("name", "${ref}").getBeanDefinition());
575

576 577 578 579 580 581
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.setNullValue("");
		Properties props = new Properties();
		props.put("ref", "");
		ppc.setProperties(props);
		ppc.postProcessBeanFactory(factory);
582

583 584 585 586 587 588 589 590 591
		TestBean tb = (TestBean) factory.getBean("tb");
		assertNull(tb.getName());
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithNestedPlaceholderInKey() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("name", "${my${key}key}").getBeanDefinition());
592

593 594 595 596 597 598
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("key", "new");
		props.put("mynewkey", "myname");
		ppc.setProperties(props);
		ppc.postProcessBeanFactory(factory);
599

600 601 602
		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("myname", tb.getName());
	}
603

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	@Test
	public void testPropertyPlaceholderConfigurerWithPlaceholderInAlias() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class).getBeanDefinition());
		factory.registerAlias("tb", "${alias}");

		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("alias", "tb2");
		ppc.setProperties(props);
		ppc.postProcessBeanFactory(factory);

		TestBean tb = (TestBean) factory.getBean("tb");
		TestBean tb2 = (TestBean) factory.getBean("tb2");
		assertSame(tb, tb2);
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithSelfReferencingPlaceholderInAlias() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class).getBeanDefinition());
		factory.registerAlias("tb", "${alias}");

		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("alias", "tb");
		ppc.setProperties(props);
		ppc.postProcessBeanFactory(factory);

		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals(0, factory.getAliases("tb").length);
	}

637 638 639 640 641 642 643
	@Test
	public void testPropertyPlaceholderConfigurerWithCircularReference() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("age", "${age}")
			.addPropertyValue("name", "name${var}")
			.getBeanDefinition());
644

645 646 647 648 649 650
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.setProperty("age", "99");
		props.setProperty("var", "${m}");
		props.setProperty("m", "${var}");
		ppc.setProperties(props);
651

652 653 654 655 656 657 658 659 660 661 662 663 664 665
		try {
			ppc.postProcessBeanFactory(factory);
			fail("Should have thrown BeanDefinitionStoreException");
		}
		catch (BeanDefinitionStoreException ex) {
			// expected
		}
	}

	@Test
	public void testPropertyPlaceholderConfigurerWithDefaultProperties() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("touchy", "${test}").getBeanDefinition());
666

667 668 669 670 671
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("test", "mytest");
		ppc.setProperties(props);
		ppc.postProcessBeanFactory(factory);
672

673 674 675 676
		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("mytest", tb.getTouchy());
	}

677 678 679 680 681 682 683 684 685 686 687 688 689
	@Test
	public void testPropertyPlaceholderConfigurerWithInlineDefault() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("touchy", "${test:mytest}").getBeanDefinition());

		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.postProcessBeanFactory(factory);

		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("mytest", tb.getTouchy());
	}

690 691 692 693 694
	@Test
	public void testPropertyPlaceholderConfigurerWithAliases() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("touchy", "${test}").getBeanDefinition());
695

696 697
		factory.registerAlias("tb", "${myAlias}");
		factory.registerAlias("${myTarget}", "alias2");
698

699 700 701 702 703 704 705
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("test", "mytest");
		props.put("myAlias", "alias");
		props.put("myTarget", "tb");
		ppc.setProperties(props);
		ppc.postProcessBeanFactory(factory);
706

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("mytest", tb.getTouchy());
		tb = (TestBean) factory.getBean("alias");
		assertEquals("mytest", tb.getTouchy());
		tb = (TestBean) factory.getBean("alias2");
		assertEquals("mytest", tb.getTouchy());
	}

	@Test
	public void testPreferencesPlaceholderConfigurer() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("name", "${myName}")
			.addPropertyValue("age", "${myAge}")
			.addPropertyValue("touchy", "${myTouchy}")
			.getBeanDefinition());
723

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
		PreferencesPlaceholderConfigurer ppc = new PreferencesPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("myAge", "99");
		ppc.setProperties(props);
		Preferences.systemRoot().put("myName", "myNameValue");
		Preferences.systemRoot().put("myTouchy", "myTouchyValue");
		Preferences.userRoot().put("myTouchy", "myOtherTouchyValue");
		ppc.afterPropertiesSet();
		ppc.postProcessBeanFactory(factory);

		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("myNameValue", tb.getName());
		assertEquals(99, tb.getAge());
		assertEquals("myOtherTouchyValue", tb.getTouchy());
		Preferences.userRoot().remove("myTouchy");
		Preferences.systemRoot().remove("myTouchy");
		Preferences.systemRoot().remove("myName");
	}

	@Test
	public void testPreferencesPlaceholderConfigurerWithCustomTreePaths() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("name", "${myName}")
			.addPropertyValue("age", "${myAge}")
			.addPropertyValue("touchy", "${myTouchy}")
			.getBeanDefinition());
751

752 753 754 755 756 757 758 759 760 761 762
		PreferencesPlaceholderConfigurer ppc = new PreferencesPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("myAge", "99");
		ppc.setProperties(props);
		ppc.setSystemTreePath("mySystemPath");
		ppc.setUserTreePath("myUserPath");
		Preferences.systemRoot().node("mySystemPath").put("myName", "myNameValue");
		Preferences.systemRoot().node("mySystemPath").put("myTouchy", "myTouchyValue");
		Preferences.userRoot().node("myUserPath").put("myTouchy", "myOtherTouchyValue");
		ppc.afterPropertiesSet();
		ppc.postProcessBeanFactory(factory);
763

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("myNameValue", tb.getName());
		assertEquals(99, tb.getAge());
		assertEquals("myOtherTouchyValue", tb.getTouchy());
		Preferences.userRoot().node("myUserPath").remove("myTouchy");
		Preferences.systemRoot().node("mySystemPath").remove("myTouchy");
		Preferences.systemRoot().node("mySystemPath").remove("myName");
	}

	@Test
	public void testPreferencesPlaceholderConfigurerWithPathInPlaceholder() {
		factory.registerBeanDefinition("tb",
			genericBeanDefinition(TestBean.class)
			.addPropertyValue("name", "${mypath/myName}")
			.addPropertyValue("age", "${myAge}")
			.addPropertyValue("touchy", "${myotherpath/myTouchy}")
			.getBeanDefinition());
781

782 783 784 785 786 787 788 789 790 791 792
		PreferencesPlaceholderConfigurer ppc = new PreferencesPlaceholderConfigurer();
		Properties props = new Properties();
		props.put("myAge", "99");
		ppc.setProperties(props);
		ppc.setSystemTreePath("mySystemPath");
		ppc.setUserTreePath("myUserPath");
		Preferences.systemRoot().node("mySystemPath").node("mypath").put("myName", "myNameValue");
		Preferences.systemRoot().node("mySystemPath/myotherpath").put("myTouchy", "myTouchyValue");
		Preferences.userRoot().node("myUserPath/myotherpath").put("myTouchy", "myOtherTouchyValue");
		ppc.afterPropertiesSet();
		ppc.postProcessBeanFactory(factory);
793

794 795 796 797 798 799 800 801 802 803 804
		TestBean tb = (TestBean) factory.getBean("tb");
		assertEquals("myNameValue", tb.getName());
		assertEquals(99, tb.getAge());
		assertEquals("myOtherTouchyValue", tb.getTouchy());
		Preferences.userRoot().node("myUserPath/myotherpath").remove("myTouchy");
		Preferences.systemRoot().node("mySystemPath/myotherpath").remove("myTouchy");
		Preferences.systemRoot().node("mySystemPath/mypath").remove("myName");
	}


	private static class ConvertingOverrideConfigurer extends PropertyOverrideConfigurer {
805

806 807 808 809 810 811
		protected String convertPropertyValue(String originalValue) {
			return "X" + originalValue;
		}
	}

}