/** * Customize the set of {@link PropertySources} to be used by this configurer. * <p>Setting this property indicates that environment property sources and * local properties should be ignored. * @see #postProcessBeanFactory */ public void setPropertySources(PropertySources propertySources) { this.propertySources = new MutablePropertySources(propertySources); }
@Test(expected = IllegalArgumentException.class) public void resolveRequiredPlaceholders_withNullInput() { new PropertySourcesPropertyResolver(new MutablePropertySources()).resolveRequiredPlaceholders(null); }
@Test(expected = IllegalArgumentException.class) public void resolvePlaceholders_withNullInput() { new PropertySourcesPropertyResolver(new MutablePropertySources()).resolvePlaceholders(null); }
@Test public void iteratorIsEmptyForEmptySources() { MutablePropertySources sources = new MutablePropertySources(); Iterator<PropertySource<?>> it = sources.iterator(); assertFalse(it.hasNext()); }
@Test public void getNonExistentPropertySourceReturnsNull() { MutablePropertySources sources = new MutablePropertySources(); assertThat(sources.get("bogus"), nullValue()); }
@Test public void streamIsEmptyForEmptySources() { MutablePropertySources sources = new MutablePropertySources(); assertThat(sources.stream(), notNullValue()); assertThat(sources.stream().count(), is(0L)); }
@Before public void setUp() { propertySources = new MutablePropertySources(); propertyResolver = new PropertySourcesPropertyResolver(propertySources); testProperties = new Properties(); propertySources.addFirst(new PropertiesPropertySource("testProperties", testProperties)); }
@Test public void streamContainsPropertySource() { MutablePropertySources sources = new MutablePropertySources(); sources.addLast(new MockPropertySource("test")); assertThat(sources.stream(), notNullValue()); assertThat(sources.stream().count(), is(1L)); assertThat(sources.stream().anyMatch(source -> "test".equals(source.getName())), is(true)); assertThat(sources.stream().anyMatch(source -> "bogus".equals(source.getName())), is(false)); }
@Test public void getProperty_doesNotCache_replaceExistingKeyPostConstruction() { String key = "foo"; String value1 = "bar"; String value2 = "biz"; HashMap<String, Object> map = new HashMap<>(); map.put(key, value1); // before construction MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MapPropertySource("testProperties", map)); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.getProperty(key), equalTo(value1)); map.put(key, value2); // after construction and first resolution assertThat(propertyResolver.getProperty(key), equalTo(value2)); }
@Test public void getProperty_doesNotCache_addNewKeyPostConstruction() { HashMap<String, Object> map = new HashMap<>(); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MapPropertySource("testProperties", map)); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.getProperty("foo"), equalTo(null)); map.put("foo", "42"); assertThat(propertyResolver.getProperty("foo"), equalTo("42")); }
@Test public void iteratorContainsPropertySource() { MutablePropertySources sources = new MutablePropertySources(); sources.addLast(new MockPropertySource("test")); Iterator<PropertySource<?>> it = sources.iterator(); assertTrue(it.hasNext()); assertEquals("test", it.next().getName()); try { it.remove(); fail("Should have thrown UnsupportedOperationException"); } catch (UnsupportedOperationException ex) { // expected } assertFalse(it.hasNext()); }
@Test public void resolvePlaceholders() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(resolver.resolvePlaceholders("Replace this ${key}"), equalTo("Replace this value")); }
@Test public void resolveRequiredPlaceholders_withDefaultValue() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown:defaultValue}"), equalTo("Replace this value plus defaultValue")); }
@Test public void resolvePlaceholders_withUnresolvable() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(resolver.resolvePlaceholders("Replace this ${key} plus ${unknown}"), equalTo("Replace this value plus ${unknown}")); }
@Test public void resolveRequiredPlaceholders() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key}"), equalTo("Replace this value")); }
@Test public void resolvePlaceholders_withDefaultValue() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(resolver.resolvePlaceholders("Replace this ${key} plus ${unknown:defaultValue}"), equalTo("Replace this value plus defaultValue")); }
@Test public void explicitPropertySources() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo")); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
@Test(expected = IllegalArgumentException.class) public void resolveRequiredPlaceholders_withUnresolvable() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown}"); }
@Test public void getProperty_propertySourceSearchOrderIsFIFO() { MutablePropertySources sources = new MutablePropertySources(); PropertyResolver resolver = new PropertySourcesPropertyResolver(sources); sources.addFirst(new MockPropertySource("ps1").withProperty("pName", "ps1Value")); assertThat(resolver.getProperty("pName"), equalTo("ps1Value")); sources.addFirst(new MockPropertySource("ps2").withProperty("pName", "ps2Value")); assertThat(resolver.getProperty("pName"), equalTo("ps2Value")); sources.addFirst(new MockPropertySource("ps3").withProperty("pName", "ps3Value")); assertThat(resolver.getProperty("pName"), equalTo("ps3Value")); }
@Test public void getPropertySources_replacePropertySource() { propertySources = new MutablePropertySources(); propertyResolver = new PropertySourcesPropertyResolver(propertySources); propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue")); propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue")); // 'local' was added first so has precedence assertThat(propertyResolver.getProperty("foo"), equalTo("localValue")); // replace 'local' with new property source propertySources.replace("local", new MockPropertySource("new").withProperty("foo", "newValue")); // 'system' now has precedence assertThat(propertyResolver.getProperty("foo"), equalTo("newValue")); assertThat(propertySources.size(), is(2)); }