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

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

Introduction

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

Prototype

public int precedenceOf(PropertySource<?> propertySource) 

Source Link

Document

Return the precedence of the given property source, -1 if not found.

Usage

From source file:io.bitsquare.app.BitsquareEnvironmentTests.java

@Test
public void testPropertySourcePrecedence() {
    PropertySource commandlineProps = new MockPropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)
            .withProperty("key.x", "x.commandline");

    PropertySource filesystemProps = new MockPropertySource(BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME)
            .withProperty("key.x", "x.env").withProperty("key.y", "y.env");

    ConfigurableEnvironment env = new BitsquareEnvironment(commandlineProps) {
        @Override/*from www .  ja  v  a 2 s  . c om*/
        PropertySource<?> filesystemProperties() {
            return filesystemProps;
        }
    };
    MutablePropertySources propertySources = env.getPropertySources();

    assertThat(propertySources.precedenceOf(named(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)), equalTo(0));
    assertThat(propertySources.precedenceOf(named(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(1));
    assertThat(propertySources.precedenceOf(named(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(2));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME)), equalTo(3));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(4));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(5));
    assertThat(propertySources.size(), equalTo(6));

    assertThat(env.getProperty("key.x"), equalTo("x.commandline")); // commandline value wins due to precedence
    assertThat(env.getProperty("key.y"), equalTo("y.env")); // env value wins because it's the only one available
}