private PropertyResolver getPatternsResolver(Environment environment) { if (environment == null) { return new PropertySourcesPropertyResolver(null); } if (environment instanceof ConfigurableEnvironment) { PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver( ((ConfigurableEnvironment) environment).getPropertySources()); resolver.setIgnoreUnresolvableNestedPlaceholders(true); return resolver; } return environment; }
private PropertyResolver getPropertyResolver() { if (this.environment instanceof ConfigurableEnvironment) { PropertyResolver resolver = new PropertySourcesPropertyResolver( ((ConfigurableEnvironment) this.environment).getPropertySources()); ((PropertySourcesPropertyResolver) resolver) .setIgnoreUnresolvableNestedPlaceholders(true); return resolver; } return this.environment; }
private PropertyResolver getAnsiResolver() { MutablePropertySources sources = new MutablePropertySources(); sources.addFirst(new AnsiPropertySource("ansi", true)); return new PropertySourcesPropertyResolver(sources); }
private PropertyResolver getTitleResolver(Class<?> sourceClass) { MutablePropertySources sources = new MutablePropertySources(); String applicationTitle = getApplicationTitle(sourceClass); Map<String, Object> titleMap = Collections.singletonMap("application.title", (applicationTitle != null) ? applicationTitle : ""); sources.addFirst(new MapPropertySource("title", titleMap)); return new PropertySourcesPropertyResolver(sources); }
@Before public void setUp() { propertySources = new MutablePropertySources(); propertyResolver = new PropertySourcesPropertyResolver(propertySources); testProperties = new Properties(); propertySources.addFirst(new PropertiesPropertySource("testProperties", testProperties)); }
@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 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(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 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() { 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 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 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_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(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}"); }
processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources)); this.appliedPropertySources = this.propertySources;
@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")); }
.withProperty("pR", "${pL}") // cyclic reference right ); ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps); assertThat(pr.getProperty("p1"), equalTo("v1")); assertThat(pr.getProperty("p2"), equalTo("v2"));
.withProperty("p4", "${p1}:${p2}:${bogus}") // unresolvable placeholder ); ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps); assertThat(pr.getProperty("p1"), equalTo("v1")); assertThat(pr.getProperty("p2"), equalTo("v2"));
@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)); }