@Override public boolean contains(String name) { return this.propertySourceList.contains(PropertySource.named(name)); }
/** * Assert that the named property source is present and return its index. * @param name {@linkplain PropertySource#getName() name of the property source} to find * @throws IllegalArgumentException if the named property source is not present */ private int assertPresentAndGetIndex(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); if (index == -1) { throw new IllegalArgumentException("PropertySource named '" + name + "' does not exist"); } return index; }
/** * Remove and return the property source with the given name, {@code null} if not found. * @param name the name of the property source to find and remove */ @Nullable public PropertySource<?> remove(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); return (index != -1 ? this.propertySourceList.remove(index) : null); }
@Override @Nullable public PropertySource<?> get(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); return (index != -1 ? this.propertySourceList.get(index) : null); }
@Override public boolean contains(String name) { return this.propertySourceList.contains(PropertySource.named(name)); }
/** * Assert that the named property source is present and return its index. * @param name {@linkplain PropertySource#getName() name of the property source} to find * @throws IllegalArgumentException if the named property source is not present */ private int assertPresentAndGetIndex(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); if (index == -1) { throw new IllegalArgumentException("PropertySource named '" + name + "' does not exist"); } return index; }
/** * Remove and return the property source with the given name, {@code null} if not found. * @param name the name of the property source to find and remove */ @Nullable public PropertySource<?> remove(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); return (index != -1 ? this.propertySourceList.remove(index) : null); }
@Override @Nullable public PropertySource<?> get(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); return (index != -1 ? this.propertySourceList.get(index) : null); }
@Test public void propertySourceOrder() { ConfigurableEnvironment env = new StandardEnvironment(); MutablePropertySources sources = env.getPropertySources(); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.size(), is(2)); }
@Test public void propertySourceOrder() throws Exception { SimpleNamingContextBuilder.emptyActivatedContextBuilder(); ConfigurableEnvironment env = new StandardServletEnvironment(); MutablePropertySources sources = env.getPropertySources(); assertThat(sources.precedenceOf(PropertySource.named( StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named( StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.precedenceOf(PropertySource.named( StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2)); assertThat(sources.precedenceOf(PropertySource.named( StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3)); assertThat(sources.precedenceOf(PropertySource.named( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4)); assertThat(sources.size(), is(5)); }
@Test @SuppressWarnings("serial") public void collectionsOperations() { Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }}; Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }}; PropertySource<?> ps1 = new MapPropertySource("ps1", map1); ps1.getSource(); List<PropertySource<?>> propertySources = new ArrayList<>(); assertThat(propertySources.add(ps1), equalTo(true)); assertThat(propertySources.contains(ps1), is(true)); assertThat(propertySources.contains(PropertySource.named("ps1")), is(true)); PropertySource<?> ps1replacement = new MapPropertySource("ps1", map2); // notice - different map assertThat(propertySources.add(ps1replacement), is(true)); // true because linkedlist allows duplicates assertThat(propertySources.size(), is(2)); assertThat(propertySources.remove(PropertySource.named("ps1")), is(true)); assertThat(propertySources.size(), is(1)); assertThat(propertySources.remove(PropertySource.named("ps1")), is(true)); assertThat(propertySources.size(), is(0)); PropertySource<?> ps2 = new MapPropertySource("ps2", map2); propertySources.add(ps1); propertySources.add(ps2); assertThat(propertySources.indexOf(PropertySource.named("ps1")), is(0)); assertThat(propertySources.indexOf(PropertySource.named("ps2")), is(1)); propertySources.clear(); }
@Test public void testProfileSourcesRegisteredOverridesCommon() { Assert.assertTrue(env.getPropertySources().contains(BroadleafEnvironmentConfiguringApplicationListener.FRAMEWORK_SOURCES_NAME)); Assert.assertTrue(env.getPropertySources().contains(BroadleafEnvironmentConfiguringApplicationListener.PROFILE_AWARE_SOURCES_NAME)); String developmentSourceName = new ClassPathResource("common-test-properties/profile-aware-properties/development.properties").getDescription(); CompositePropertySource profileAwareSource = (CompositePropertySource) env.getPropertySources().get(BroadleafEnvironmentConfiguringApplicationListener.PROFILE_AWARE_SOURCES_NAME); Assert.assertTrue(profileAwareSource.getPropertySources().contains(PropertySource.named(developmentSourceName))); }
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(2)); assertThat(sources.precedenceOf(PropertySource.named("d")), is(3)); assertThat(sources.precedenceOf(PropertySource.named("f")), is(4)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(2)); assertThat(sources.precedenceOf(PropertySource.named("d")), is(3)); assertThat(sources.precedenceOf(PropertySource.named("e")), is(4)); assertThat(sources.precedenceOf(PropertySource.named("f")), is(5)); assertThat(sources.precedenceOf(PropertySource.named("g")), is(6)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(1)); assertThat(sources.precedenceOf(PropertySource.named("d")), is(2)); assertThat(sources.precedenceOf(PropertySource.named("e")), is(3)); assertThat(sources.precedenceOf(PropertySource.named("f")), is(4)); assertThat(sources.precedenceOf(PropertySource.named("g")), is(5)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(6)); assertThat(sources.precedenceOf(PropertySource.named("a")), is(0)); assertThat(sources.precedenceOf(PropertySource.named("b")), is(1)); assertThat(sources.precedenceOf(PropertySource.named("c")), is(2)); assertThat(sources.precedenceOf(PropertySource.named("d")), is(3)); assertThat(sources.precedenceOf(PropertySource.named("e")), is(4)); assertThat(sources.precedenceOf(PropertySource.named("f")), is(5)); assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));
assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)))); assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)))); assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
@Test public void registerServletParamPropertySources_GenericWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); GenericWebApplicationContext ctx = new GenericWebApplicationContext(); ctx.setServletContext(servletContext); ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); assertThat(environment, instanceOf(StandardServletEnvironment.class)); MutablePropertySources propertySources = environment.getPropertySources(); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); // ServletContext params are available assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pContext1"), is("pContext1Value")); // Servlet* PropertySources have precedence over System* PropertySources assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue"); mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value"); propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletcontext init params resolve with higher precedence than sysprops assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); }
public boolean contains(String name) { return this.propertySourceList.contains(PropertySource.named(name)); }
public PropertySource<?> get(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); return index == -1 ? null : this.propertySourceList.get(index); }
/** * Remove and return the property source with the given name, {@code null} if not found. * @param name the name of the property source to find and remove */ public PropertySource<?> remove(String name) { logger.debug(String.format("Removing [%s] PropertySource", name)); int index = this.propertySourceList.indexOf(PropertySource.named(name)); return index == -1 ? null : this.propertySourceList.remove(index); }
/** * Assert that the named property source is present and return its index. * @param name the {@linkplain PropertySource#getName() name of the property source} * to find * @throws IllegalArgumentException if the named property source is not present */ private int assertPresentAndGetIndex(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); Assert.isTrue(index >= 0, String.format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, name)); return index; }