@Test public void localPropertiesViaResource() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); PropertyPlaceholderConfigurer pc = new PropertyPlaceholderConfigurer(); Resource resource = new ClassPathResource("PropertyPlaceholderConfigurerTests.properties", this.getClass()); pc.setLocation(resource); pc.postProcessBeanFactory(bf); }
@Test public void resolveFromSystemProperties() { System.setProperty("otherKey", "systemValue"); p1BeanDef = rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${" + P1 + "}") .addPropertyValue("sex", "${otherKey}") .getBeanDefinition(); registerWithGeneratedName(p1BeanDef, bf); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); assertThat(bean.getSex(), equalTo("systemValue")); System.clearProperty("otherKey"); }
@Test public void trimValuesIsOffByDefault() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); System.setProperty("my.name", " myValue "); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo(" myValue ")); System.clearProperty("my.name"); }
@Test public void testPropertyPlaceholderConfigurerWithSystemPropertyNotUsed() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("country", "${os.name}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("os.name", "myos"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("myos", tb.getCountry()); }
@Test public void testPropertyPlaceholderConfigurerWithNestedPlaceholderInKey() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my${key}key}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.put("key", "new"); props.put("mynewkey", "myname"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("myname", tb.getName()); }
@Test public void testPropertyPlaceholderConfigurerWithSystemPropertyFallback() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("country", "${os.name}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals(System.getProperty("os.name"), tb.getCountry()); }
@Test public void testPropertyPlaceholderConfigurerWithDefaultProperties() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("touchy", "${test}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.put("test", "mytest"); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("mytest", tb.getTouchy()); }
@Test public void trimValuesIsApplied() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setTrimValues(true); System.setProperty("my.name", " myValue "); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue")); System.clearProperty("my.name"); }
@Test public void testPropertyPlaceholderConfigurerWithOverridingSystemProperty() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("country", "${os.name}").getBeanDefinition()); 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); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals(System.getProperty("os.name"), tb.getCountry()); }
@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()); }
@Test public void resolveFromLocalProperties() { System.clearProperty(P1); registerWithGeneratedName(p1BeanDef, bf); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); }
@Test public void nullValueIsPreserved() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setNullValue("customNull"); System.setProperty("my.name", "customNull"); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), nullValue()); System.clearProperty("my.name"); }
@Test public void testPropertyPlaceholderConfigurerWithEmptyStringInPlaceholderAsNull() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${ref}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setNullValue(""); Properties props = new Properties(); props.put("ref", ""); ppc.setProperties(props); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertNull(tb.getName()); }
@Test public void testPropertyPlaceholderConfigurerWithEmptyStringAsNull() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setNullValue(""); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertNull(tb.getName()); }
@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 testPropertyPlaceholderConfigurerWithIgnoreUnresolvablePlaceholder() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${ref}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(factory); TestBean tb = (TestBean) factory.getBean("tb"); assertEquals("${ref}", tb.getName()); }
@Test public void setSystemPropertiesMode_defaultIsFallback() { registerWithGeneratedName(p1BeanDef, bf); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); }
@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"); assertNotNull(tb); assertEquals(0, factory.getAliases("tb").length); }
@Test public void setSystemSystemPropertiesMode_toOverride_andSetSearchSystemEnvironment_toFalse() { registerWithGeneratedName(p1BeanDef, bf); System.clearProperty(P1); // will now fall all the way back to system environment ppc.setSearchSystemEnvironment(false); ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); // has to resort to local props }
@Test public void setSystemSystemPropertiesMode_toOverride_andResolveFromSystemProperties() { registerWithGeneratedName(p1BeanDef, bf); ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); ppc.postProcessBeanFactory(bf); TestBean bean = bf.getBean(TestBean.class); assertThat(bean.getName(), equalTo(P1_SYSTEM_PROPS_VAL)); }