📄 propertyresourceconfigurertests.java
字号:
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("touchy", "${os.name}");
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
ac.refresh();
TestBean tb = (TestBean) ac.getBean("tb");
assertEquals(System.getProperty("os.name"), tb.getTouchy());
}
public void testPropertyPlaceholderConfigurerWithSystemPropertyNotUsed() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("touchy", "${os.name}");
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
Properties props = new Properties();
props.put("os.name", "myos");
pvs.addPropertyValue("properties", props);
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
ac.refresh();
TestBean tb = (TestBean) ac.getBean("tb");
assertEquals("myos", tb.getTouchy());
}
public void testPropertyPlaceholderConfigurerWithOverridingSystemProperty() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("touchy", "${os.name}");
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
Properties props = new Properties();
props.put("os.name", "myos");
pvs.addPropertyValue("properties", props);
pvs.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_OVERRIDE");
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
ac.refresh();
TestBean tb = (TestBean) ac.getBean("tb");
assertEquals(System.getProperty("os.name"), tb.getTouchy());
}
public void testPropertyPlaceholderConfigurerWithUnresolvableSystemProperty() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("touchy", "${user.dir}");
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_NEVER");
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
try {
ac.refresh();
fail("Should have thrown BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
// expected
assertTrue(ex.getMessage().indexOf("user.dir") != -1);
}
}
public void testPropertyPlaceholderConfigurerWithUnresolvablePlaceholder() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("name", "${ref}");
ac.registerSingleton("tb", TestBean.class, pvs);
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, null);
try {
ac.refresh();
fail("Should have thrown BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
// expected
assertTrue(ex.getMessage().indexOf("ref") != -1);
}
}
public void testPropertyPlaceholderConfigurerWithIgnoreUnresolvablePlaceholder() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("name", "${ref}");
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("ignoreUnresolvablePlaceholders", Boolean.TRUE);
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
try {
ac.refresh();
TestBean tb = (TestBean) ac.getBean("tb");
assertEquals("${ref}", tb.getName());
}
catch (BeanDefinitionStoreException ex) {
fail("Should not have thrown BeanDefinitionStoreException");
}
}
public void testPropertyPlaceholderConfigurerWithSystemPropertyInLocation() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("location", "${user.dir}/test");
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
try {
ac.refresh();
fail("Should have thrown BeanInitializationException");
}
catch (BeanInitializationException ex) {
// expected
assertTrue(ex.getCause() instanceof FileNotFoundException);
// slight hack for Linux/Unix systems
String userDir = StringUtils.cleanPath(System.getProperty("user.dir"));
if (userDir.startsWith("/")) {
userDir = userDir.substring(1);
}
assertTrue(ex.getMessage().indexOf(userDir) != -1);
}
}
public void testPropertyPlaceholderConfigurerWithSystemPropertiesInLocation() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("location", "${user.dir}/test/${user.dir}");
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
try {
ac.refresh();
fail("Should have thrown BeanInitializationException");
}
catch (BeanInitializationException ex) {
// expected
assertTrue(ex.getCause() instanceof FileNotFoundException);
// slight hack for Linux/Unix systems
String userDir = StringUtils.cleanPath(System.getProperty("user.dir"));
if (userDir.startsWith("/")) {
userDir = userDir.substring(1);
}
/* the above hack doesn't work since the exception message is created without
the leading / stripped so the test fails. Changed 17/11/04. DD */
//assertTrue(ex.getMessage().indexOf(userDir + "/test/" + userDir) != -1);
assertTrue(
ex.getMessage().indexOf(userDir + "/test/" + userDir) != -1
||
ex.getMessage().indexOf(userDir + "/test//" + userDir) != -1
);
}
}
public void testPropertyPlaceholderConfigurerWithUnresolvableSystemPropertiesInLocation() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("location", "${myprop}/test/${myprop}");
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
try {
ac.refresh();
fail("Should have thrown BeanDefinitionStoreException");
}
catch (BeanInitializationException ex) {
// expected
assertTrue(ex.getCause() instanceof FileNotFoundException);
assertTrue(ex.getMessage().indexOf("myprop") != -1);
}
}
public void testPropertyPlaceholderConfigurerWithCircularReference() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("age", "${age}");
pvs.addPropertyValue("name", "name${var}");
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
ac.registerSingleton("tb1", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("age", "${age}");
pvs.addPropertyValue("name", "name${age}");
ac.registerSingleton("tb2", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("properties", "age=99\nvar=${m}var\nref=tb2\nm=${var}");
ac.registerSingleton("configurer1", PropertyPlaceholderConfigurer.class, pvs);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("properties", "age=98");
pvs.addPropertyValue("order", "0");
ac.registerSingleton("configurer2", PropertyPlaceholderConfigurer.class, pvs);
try {
ac.refresh();
fail("Should have thrown BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
// expected
}
}
public void testPropertyPlaceholderConfigurerWithDefaultProperties() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("touchy", "${test}");
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
Properties props = new Properties();
props.put("test", "mytest");
pvs.addPropertyValue("properties", new Properties(props));
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
ac.refresh();
TestBean tb = (TestBean) ac.getBean("tb");
assertEquals("mytest", tb.getTouchy());
}
public void testPreferencesPlaceholderConfigurer() {
// ignore for JDK < 1.4
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("name", "${myName}");
pvs.addPropertyValue("age", "${myAge}");
pvs.addPropertyValue("touchy", "${myTouchy}");
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
Properties props = new Properties();
props.put("myAge", "99");
pvs.addPropertyValue("properties", props);
ac.registerSingleton("configurer", PreferencesPlaceholderConfigurer.class, pvs);
Preferences.systemRoot().put("myName", "myNameValue");
Preferences.systemRoot().put("myTouchy", "myTouchyValue");
Preferences.userRoot().put("myTouchy", "myOtherTouchyValue");
ac.refresh();
TestBean tb = (TestBean) ac.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");
}
public void testPreferencesPlaceholderConfigurerWithCustomTreePaths() {
// ignore for JDK < 1.4
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
return;
}
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("name", "${myName}");
pvs.addPropertyValue("age", "${myAge}");
pvs.addPropertyValue("touchy", "${myTouchy}");
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
Properties props = new Properties();
props.put("myAge", "99");
pvs.addPropertyValue("properties", props);
pvs.addPropertyValue("systemTreePath", "mySystemPath");
pvs.addPropertyValue("userTreePath", "myUserPath");
ac.registerSingleton("configurer", PreferencesPlaceholderConfigurer.class, pvs);
Preferences.systemRoot().node("mySystemPath").put("myName", "myNameValue");
Preferences.systemRoot().node("mySystemPath").put("myTouchy", "myTouchyValue");
Preferences.userRoot().node("myUserPath").put("myTouchy", "myOtherTouchyValue");
ac.refresh();
TestBean tb = (TestBean) ac.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");
}
private static class ConvertingOverrideConfigurer extends PropertyOverrideConfigurer {
protected String convertPropertyValue(String originalValue) {
return "X" + originalValue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -