Example usage for org.springframework.core.env MutablePropertySources MutablePropertySources

List of usage examples for org.springframework.core.env MutablePropertySources MutablePropertySources

Introduction

In this page you can find the example usage for org.springframework.core.env MutablePropertySources MutablePropertySources.

Prototype

public MutablePropertySources() 

Source Link

Document

Create a new MutablePropertySources object.

Usage

From source file:org.apache.servicecomb.config.TestConfigurationSpringInitializer.java

@Test
public void testSetEnvironment() {
    ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
    MutablePropertySources propertySources = new MutablePropertySources();
    Map<String, String> propertyMap = new HashMap<>();
    final String map0Key0 = "map0-Key0";
    final String map1Key0 = "map1-Key0";
    final String map2Key0 = "map2-Key0";
    final String map3Key0 = "map3-Key0";
    propertyMap.put(map0Key0, "map0-Value0");
    propertyMap.put(map1Key0, "map1-Value0");
    propertyMap.put(map2Key0, "map2-Value0");
    propertyMap.put(map3Key0, "map3-Value0");

    /*//from  ww w.  j a v a  2  s.co m
    propertySources
    |- compositePropertySource0
    |  |- mapPropertySource0
    |  |  |- map0-Key0 = map0-Value0
    |  |- compositePropertySource1
    |     |- mapPropertySource1
    |     |  |- map1-Key0 = map1-Value0
    |     |- mapPropertySource2
    |        |- map2-Key0 = map2-Value0
    |     |- JndiPropertySource(mocked)
    |- mapPropertySource3
      |- map3-Key0 = map3-Value0
     */
    CompositePropertySource compositePropertySource0 = new CompositePropertySource("compositePropertySource0");
    propertySources.addFirst(compositePropertySource0);

    HashMap<String, Object> map0 = new HashMap<>();
    map0.put(map0Key0, propertyMap.get(map0Key0));
    MapPropertySource mapPropertySource0 = new MapPropertySource("mapPropertySource0", map0);
    compositePropertySource0.addFirstPropertySource(mapPropertySource0);

    CompositePropertySource compositePropertySource1 = new CompositePropertySource("compositePropertySource1");
    compositePropertySource0.addPropertySource(compositePropertySource1);
    HashMap<String, Object> map1 = new HashMap<>();
    map1.put(map1Key0, propertyMap.get(map1Key0));
    MapPropertySource mapPropertySource1 = new MapPropertySource("mapPropertySource1", map1);
    compositePropertySource1.addPropertySource(mapPropertySource1);
    HashMap<String, Object> map2 = new HashMap<>();
    map2.put(map2Key0, propertyMap.get(map2Key0));
    MapPropertySource mapPropertySource2 = new MapPropertySource("mapPropertySource2", map2);
    compositePropertySource1.addPropertySource(mapPropertySource2);
    compositePropertySource1.addPropertySource(Mockito.mock(JndiPropertySource.class));

    HashMap<String, Object> map3 = new HashMap<>();
    map3.put(map3Key0, propertyMap.get(map3Key0));
    MapPropertySource mapPropertySource3 = new MapPropertySource("mapPropertySource3", map3);
    compositePropertySource0.addPropertySource(mapPropertySource3);

    Mockito.when(environment.getPropertySources()).thenReturn(propertySources);
    Mockito.doAnswer((Answer<String>) invocation -> {
        Object[] args = invocation.getArguments();
        String propertyName = (String) args[0];

        if ("spring.config.name".equals(propertyName) || "spring.application.name".equals(propertyName)) {
            return null;
        }

        String value = propertyMap.get(propertyName);
        if (null == value) {
            fail("get unexpected property name: " + propertyName);
        }
        return value;
    }).when(environment).getProperty(anyString());

    new ConfigurationSpringInitializer().setEnvironment(environment);

    Map<String, Map<String, Object>> extraLocalConfig = getExtraConfigMapFromConfigUtil();
    assertFalse(extraLocalConfig.isEmpty());
    Map<String, Object> extraProperties = extraLocalConfig
            .get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + environment.getClass().getName()
                    + "@" + environment.hashCode());
    assertNotNull(extraLocalConfig);
    for (Entry<String, String> entry : propertyMap.entrySet()) {
        assertEquals(entry.getValue(), extraProperties.get(entry.getKey()));
    }
}

From source file:org.apache.servicecomb.config.TestConfigurationSpringInitializer.java

@Test
public void testSetEnvironmentOnEnvironmentName() {
    // get environment name from spring.config.name
    ConfigurableEnvironment environment0 = Mockito.mock(ConfigurableEnvironment.class);
    MutablePropertySources propertySources0 = new MutablePropertySources();
    Mockito.when(environment0.getPropertySources()).thenReturn(propertySources0);
    Map<String, Object> map0 = new HashMap<>(1);
    map0.put("spring.config.name", "application");
    propertySources0.addFirst(new MapPropertySource("mapPropertySource0", map0));
    Mockito.when(environment0.getProperty("spring.config.name")).thenReturn("application");

    // get environment name from spring.application.name
    ConfigurableEnvironment environment1 = Mockito.mock(ConfigurableEnvironment.class);
    MutablePropertySources propertySources1 = new MutablePropertySources();
    Mockito.when(environment1.getPropertySources()).thenReturn(propertySources1);
    Map<String, Object> map1 = new HashMap<>(1);
    map1.put("spring.application.name", "bootstrap");
    propertySources1.addFirst(new MapPropertySource("mapPropertySource1", map1));
    Mockito.when(environment1.getProperty("spring.application.name")).thenReturn("bootstrap");

    // get environment name from className+hashcode
    ConfigurableEnvironment environment2 = Mockito.mock(ConfigurableEnvironment.class);
    MutablePropertySources propertySources2 = new MutablePropertySources();
    Mockito.when(environment2.getPropertySources()).thenReturn(propertySources2);
    Map<String, Object> map2 = new HashMap<>(1);
    map2.put("key2", "value2");
    propertySources2.addFirst(new MapPropertySource("mapPropertySource2", map2));
    Mockito.when(environment2.getProperty("key2")).thenReturn("value2");

    ConfigurationSpringInitializer configurationSpringInitializer = new ConfigurationSpringInitializer();
    configurationSpringInitializer.setEnvironment(environment0);
    configurationSpringInitializer.setEnvironment(environment1);
    configurationSpringInitializer.setEnvironment(environment2);

    Map<String, Map<String, Object>> extraConfig = getExtraConfigMapFromConfigUtil();
    assertEquals(3, extraConfig.size());

    Map<String, Object> extraProperties = extraConfig
            .get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + "application");
    assertEquals(1, extraProperties.size());
    assertEquals("application", extraProperties.get("spring.config.name"));

    extraProperties = extraConfig.get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + "bootstrap");
    assertEquals(1, extraProperties.size());
    assertEquals("bootstrap", extraProperties.get("spring.application.name"));

    extraProperties = extraConfig.get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX
            + environment2.getClass().getName() + "@" + environment2.hashCode());
    assertEquals(1, extraProperties.size());
    assertEquals("value2", extraProperties.get("key2"));
}

From source file:org.dspace.servicemanager.config.DSpaceConfigurationPlaceholderConfigurer.java

public DSpaceConfigurationPlaceholderConfigurer(Configuration configuration) {
    ConfigurationPropertySource apacheCommonsConfigPropertySource = new ConfigurationPropertySource(
            configuration.getClass().getName(), configuration);
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(apacheCommonsConfigPropertySource);
    setPropertySources(propertySources);
}

From source file:org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.java

private PropertySources deducePropertySources() {
    PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
    if (configurer != null) {
        // Flatten the sources into a single list so they can be iterated
        return new FlatPropertySources(configurer.getAppliedPropertySources());
    }/*ww w.  j  a v  a 2 s .  com*/
    if (this.environment instanceof ConfigurableEnvironment) {
        MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment)
                .getPropertySources();
        return new FlatPropertySources(propertySources);
    }
    // empty, so not very useful, but fulfils the contract
    logger.warn(
            "Unable to obtain PropertySources from " + "PropertySourcesPlaceholderConfigurer or Environment");
    return new MutablePropertySources();
}

From source file:org.springframework.boot.env.PropertySourcesLoader.java

/**
 * Create a new {@link PropertySourceLoader} instance backed by a new
 * {@link MutablePropertySources}.//from   w w  w . j  a v a 2s.c o m
 */
public PropertySourcesLoader() {
    this(new MutablePropertySources());
}

From source file:org.springframework.boot.ResourceBanner.java

private PropertyResolver getVersionResolver(Class<?> sourceClass) {
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MapPropertySource("version", getVersionsMap(sourceClass)));
    return new PropertySourcesPropertyResolver(propertySources);
}

From source file:org.springframework.boot.ResourceBanner.java

private PropertyResolver getAnsiResolver() {
    MutablePropertySources sources = new MutablePropertySources();
    sources.addFirst(new AnsiPropertySource("ansi", true));
    return new PropertySourcesPropertyResolver(sources);
}