Search in sources :

Example 1 with CompositePropertySource

use of cn.taketoday.core.env.CompositePropertySource in project today-framework by TAKETODAY.

the class ApplicationTests method commandLinePropertySourceEnhancesEnvironment.

@Test
void commandLinePropertySourceEnhancesEnvironment() {
    Application application = new Application(ExampleConfig.class);
    application.setApplicationType(ApplicationType.NONE_WEB);
    ConfigurableEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("commandLineArgs", Collections.singletonMap("foo", "original")));
    application.setEnvironment(environment);
    this.context = application.run("--foo=bar", "--bar=foo");
    assertThat(environment).has(matchingPropertySource(CompositePropertySource.class, "commandLineArgs"));
    assertThat(environment.getProperty("bar")).isEqualTo("foo");
    // New command line properties take precedence
    assertThat(environment.getProperty("foo")).isEqualTo("bar");
    CompositePropertySource composite = (CompositePropertySource) environment.getPropertySources().get("commandLineArgs");
    assertThat(composite.getPropertySources()).hasSize(2);
    assertThat(composite.getPropertySources()).first().matches((source) -> source.getName().equals("applicationCommandLineArgs"), "is named applicationCommandLineArgs");
    assertThat(composite.getPropertySources()).element(1).matches((source) -> source.getName().equals("commandLineArgs"), "is named commandLineArgs");
}
Also used : ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) CompositePropertySource(cn.taketoday.core.env.CompositePropertySource) MapPropertySource(cn.taketoday.core.env.MapPropertySource) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 2 with CompositePropertySource

use of cn.taketoday.core.env.CompositePropertySource in project today-infrastructure by TAKETODAY.

the class ApplicationTests method commandLinePropertySourceEnhancesEnvironment.

@Test
void commandLinePropertySourceEnhancesEnvironment() {
    Application application = new Application(ExampleConfig.class);
    application.setApplicationType(ApplicationType.NONE_WEB);
    ConfigurableEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("commandLineArgs", Collections.singletonMap("foo", "original")));
    application.setEnvironment(environment);
    this.context = application.run("--foo=bar", "--bar=foo");
    assertThat(environment).has(matchingPropertySource(CompositePropertySource.class, "commandLineArgs"));
    assertThat(environment.getProperty("bar")).isEqualTo("foo");
    // New command line properties take precedence
    assertThat(environment.getProperty("foo")).isEqualTo("bar");
    CompositePropertySource composite = (CompositePropertySource) environment.getPropertySources().get("commandLineArgs");
    assertThat(composite.getPropertySources()).hasSize(2);
    assertThat(composite.getPropertySources()).first().matches((source) -> source.getName().equals("applicationCommandLineArgs"), "is named applicationCommandLineArgs");
    assertThat(composite.getPropertySources()).element(1).matches((source) -> source.getName().equals("commandLineArgs"), "is named commandLineArgs");
}
Also used : ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) CompositePropertySource(cn.taketoday.core.env.CompositePropertySource) MapPropertySource(cn.taketoday.core.env.MapPropertySource) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

CompositePropertySource (cn.taketoday.core.env.CompositePropertySource)2 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)2 MapPropertySource (cn.taketoday.core.env.MapPropertySource)2 StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)2 Test (org.junit.jupiter.api.Test)2