@Bean public PropertyPlaceholderConfigurer ppc() { return new PropertyPlaceholderConfigurer(); }
@Before public void setup() { p1BeanDef = rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${" + P1 + "}") .getBeanDefinition(); bf = new DefaultListableBeanFactory(); ppcProperties = new Properties(); ppcProperties.setProperty(P1, P1_LOCAL_PROPS_VAL); System.setProperty(P1, P1_SYSTEM_PROPS_VAL); ppc = new PropertyPlaceholderConfigurer(); ppc.setProperties(ppcProperties); }
@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 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 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 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 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 testPropertyPlaceholderConfigurerWithUnresolvablePlaceholder() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${ref}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); try { ppc.postProcessBeanFactory(factory); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected assertTrue(ex.getMessage().contains("ref")); } }
@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 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 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 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 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 testPropertyPlaceholderConfigurerWithUnresolvableSystemProperty() { factory.registerBeanDefinition("tb", genericBeanDefinition(TestBean.class) .addPropertyValue("touchy", "${user.dir}").getBeanDefinition()); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_NEVER); try { ppc.postProcessBeanFactory(factory); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected assertTrue(ex.getMessage().contains("user.dir")); } }
@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 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 @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class)); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); formattingService = context.getBean("formattingService", FormattingConversionService.class); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
@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); }