public static void finishAndRelocate(MutablePropertySources propertySources) { String name = APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME; ConfigurationPropertySources removed = (ConfigurationPropertySources) propertySources .get(name); if (removed != null) { for (PropertySource<?> propertySource : removed.sources) { if (propertySource instanceof EnumerableCompositePropertySource) { EnumerableCompositePropertySource composite = (EnumerableCompositePropertySource) propertySource; for (PropertySource<?> nested : composite.getSource()) { propertySources.addAfter(name, nested); name = nested.getName(); } } else { propertySources.addAfter(name, propertySource); } } propertySources.remove(APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME); } }
public static void finishAndRelocate(MutablePropertySources propertySources) { ConfigurationPropertySources removed = (ConfigurationPropertySources) propertySources .get(ConfigurationPropertySources.NAME); String name = ConfigurationPropertySources.NAME; if (removed != null) { for (PropertySource<?> propertySource : removed.sources) { if (propertySource instanceof EnumerableCompositePropertySource) { EnumerableCompositePropertySource composite = (EnumerableCompositePropertySource) propertySource; for (PropertySource<?> nested : composite.getSource()) { propertySources.addAfter(name, nested); name = nested.getName(); } } else { propertySources.addAfter(name, propertySource); } } propertySources.remove(ConfigurationPropertySources.NAME); } }
@Test public void yamlSetsProfiles() throws Exception { this.initializer.setSearchNames("testsetprofiles"); this.initializer.postProcessEnvironment(this.environment, this.application); assertThat(this.environment.getActiveProfiles()).containsExactly("dev"); String property = this.environment.getProperty("my.property"); assertThat(this.environment.getActiveProfiles()).contains("dev"); assertThat(property).isEqualTo("fromdevprofile"); ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment .getPropertySources() .get(ConfigFileApplicationListener.APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME); Collection<org.springframework.core.env.PropertySource<?>> sources = propertySource .getSource(); assertThat(sources).hasSize(2); List<String> names = new ArrayList<String>(); for (org.springframework.core.env.PropertySource<?> source : sources) { if (source instanceof EnumerableCompositePropertySource) { for (org.springframework.core.env.PropertySource<?> nested : ((EnumerableCompositePropertySource) source) .getSource()) { names.add(nested.getName()); } } else { names.add(source.getName()); } } assertThat(names).contains( "applicationConfig: [classpath:/testsetprofiles.yml]#dev", "applicationConfig: [classpath:/testsetprofiles.yml]"); }
@Test public void yamlSetsProfiles() throws Exception { this.initializer.setSearchNames("testsetprofiles"); this.initializer.postProcessEnvironment(this.environment, this.application); assertEquals("dev", StringUtils .arrayToCommaDelimitedString(this.environment.getActiveProfiles())); String property = this.environment.getProperty("my.property"); assertThat(Arrays.asList(this.environment.getActiveProfiles()), contains("dev")); assertThat(property, equalTo("fromdevprofile")); ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment .getPropertySources().get("applicationConfigurationProperties"); Collection<org.springframework.core.env.PropertySource<?>> sources = propertySource .getSource(); assertEquals(2, sources.size()); List<String> names = new ArrayList<String>(); for (org.springframework.core.env.PropertySource<?> source : sources) { if (source instanceof EnumerableCompositePropertySource) { for (org.springframework.core.env.PropertySource<?> nested : ((EnumerableCompositePropertySource) source) .getSource()) { names.add(nested.getName()); } } else { names.add(source.getName()); } } assertThat(names, contains("applicationConfig: [classpath:/testsetprofiles.yml]#dev", "applicationConfig: [classpath:/testsetprofiles.yml]")); }