@Test
public void location() {
// with system property: -Dtest1=test1.props
System.setProperty("test1", "test1.props");
factory = new XmlApplicationContext(new FileSystemResource(new File(srcdir, "property-placeholder-2.xml")));
List<?> list = (List<?>) factory.getBean("list");
assertArrayEquals(new Object[] { "111", "222", "defaultValue" }, list.toArray(EMPTY_OBJECT_ARRAY));
// no system property: -Dtest1, but has default value
System.clearProperty("test1");
factory = new XmlApplicationContext(new FileSystemResource(new File(srcdir, "property-placeholder-2.xml")));
list = (List<?>) factory.getBean("list");
assertArrayEquals(new Object[] { "111", "222", "defaultValue" }, list.toArray(EMPTY_OBJECT_ARRAY));
// override default value
System.setProperty("test1", "test3.props");
factory = new XmlApplicationContext(new FileSystemResource(new File(srcdir, "property-placeholder-2.xml")));
list = (List<?>) factory.getBean("list");
assertArrayEquals(new Object[] { "11111", "222", "defaultValue" }, list.toArray(EMPTY_OBJECT_ARRAY));
// no system property and no default value
System.clearProperty("test1");
try {
new XmlApplicationContext(new FileSystemResource(new File(srcdir, "property-placeholder-4.xml")));
fail();
} catch (BeansException e) {
assertThat(e, exception("${test1}"));
}
// no system property and with empty default value
System.clearProperty("test1");
factory = new XmlApplicationContext(new FileSystemResource(new File(srcdir, "property-placeholder-5.xml")));
list = (List<?>) factory.getBean("list");
// ${x:} with empty default value
assertArrayEquals(new Object[] { "", "222", "defaultValue" }, list.toArray(EMPTY_OBJECT_ARRAY));
}