Search in sources :

Example 11 with MockPropertySource

use of cn.taketoday.mock.env.MockPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributorsTests method getBinderWhenHasInactiveIgnoresInactive.

@Test
void getBinderWhenHasInactiveIgnoresInactive() {
    MockPropertySource firstPropertySource = new MockPropertySource();
    firstPropertySource.setProperty("test", "one");
    firstPropertySource.setProperty("context.config.activate.on-profile", "production");
    MockPropertySource secondPropertySource = new MockPropertySource();
    secondPropertySource.setProperty("test", "two");
    ConfigData configData = new ConfigData(Arrays.asList(firstPropertySource, secondPropertySource));
    ConfigDataEnvironmentContributor firstContributor = createBoundImportContributor(configData, 0);
    ConfigDataEnvironmentContributor secondContributor = createBoundImportContributor(configData, 1);
    ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.bootstrapContext, Arrays.asList(firstContributor, secondContributor));
    Binder binder = contributors.getBinder(this.activationContext);
    assertThat(binder.bind("test", String.class).get()).isEqualTo("two");
}
Also used : Binder(cn.taketoday.context.properties.bind.Binder) MockPropertySource(cn.taketoday.mock.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 12 with MockPropertySource

use of cn.taketoday.mock.env.MockPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributorsTests method withProcessedImportsWhenHasNoUnprocessedImportsReturnsSameInstance.

@Test
void withProcessedImportsWhenHasNoUnprocessedImportsReturnsSameInstance() {
    ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(new MockPropertySource());
    ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.bootstrapContext, Arrays.asList(contributor));
    ConfigDataEnvironmentContributors withProcessedImports = contributors.withProcessedImports(this.importer, this.activationContext);
    assertThat(withProcessedImports).isSameAs(contributors);
}
Also used : MockPropertySource(cn.taketoday.mock.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 13 with MockPropertySource

use of cn.taketoday.mock.env.MockPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributorsTests method withProcessedImportsProvidesLoaderContextWithAccessToBootstrapRegistry.

@Test
void withProcessedImportsProvidesLoaderContextWithAccessToBootstrapRegistry() {
    MockPropertySource existingPropertySource = new MockPropertySource();
    existingPropertySource.setProperty("test", "springboot");
    ConfigDataEnvironmentContributor existingContributor = ConfigDataEnvironmentContributor.ofExisting(existingPropertySource);
    this.importer = mock(ConfigDataImporter.class);
    List<ConfigDataLocation> locations = List.of(LOCATION_1);
    MockPropertySource propertySource = new MockPropertySource();
    Map<ConfigDataResolutionResult, ConfigData> imported = new LinkedHashMap<>();
    imported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a'"), false), new ConfigData(List.of(propertySource)));
    given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations))).willReturn(imported);
    ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
    ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.bootstrapContext, Arrays.asList(existingContributor, contributor));
    contributors.withProcessedImports(this.importer, this.activationContext);
    then(this.importer).should().resolveAndLoad(any(), any(), this.loaderContext.capture(), any());
    ConfigDataLoaderContext context = this.loaderContext.getValue();
    assertThat(context.getBootstrapContext()).isSameAs(this.bootstrapContext);
}
Also used : MockPropertySource(cn.taketoday.mock.env.MockPropertySource) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 14 with MockPropertySource

use of cn.taketoday.mock.env.MockPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributorsTests method getBinderWhenHasPlaceholderResolvesPlaceholder.

@Test
void getBinderWhenHasPlaceholderResolvesPlaceholder() {
    MockPropertySource propertySource = new MockPropertySource();
    propertySource.setProperty("test", "${other}");
    propertySource.setProperty("other", "springboot");
    ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource);
    ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.bootstrapContext, List.of(contributor));
    Binder binder = contributors.getBinder(this.activationContext);
    assertThat(binder.bind("test", String.class).get()).isEqualTo("springboot");
}
Also used : Binder(cn.taketoday.context.properties.bind.Binder) MockPropertySource(cn.taketoday.mock.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 15 with MockPropertySource

use of cn.taketoday.mock.env.MockPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributorsTests method withProcessedImportsProvidesLocationResolverContextWithAccessToParent.

@Test
void withProcessedImportsProvidesLocationResolverContextWithAccessToParent() {
    this.importer = mock(ConfigDataImporter.class);
    List<ConfigDataLocation> initialLocations = List.of(LOCATION_1);
    MockPropertySource initialPropertySource = new MockPropertySource();
    initialPropertySource.setProperty("context.config.import", "location2");
    Map<ConfigDataResolutionResult, ConfigData> initialImported = new LinkedHashMap<>();
    initialImported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a"), false), new ConfigData(List.of(initialPropertySource)));
    given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(initialLocations))).willReturn(initialImported);
    List<ConfigDataLocation> secondLocations = List.of(LOCATION_2);
    MockPropertySource secondPropertySource = new MockPropertySource();
    Map<ConfigDataResolutionResult, ConfigData> secondImported = new LinkedHashMap<>();
    secondImported.put(new ConfigDataResolutionResult(LOCATION_2, new TestConfigDataResource("b"), false), new ConfigData(List.of(secondPropertySource)));
    given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(secondLocations))).willReturn(secondImported);
    ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
    ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.bootstrapContext, List.of(contributor));
    contributors.withProcessedImports(this.importer, this.activationContext);
    then(this.importer).should().resolveAndLoad(any(), this.locationResolverContext.capture(), any(), eq(secondLocations));
    ConfigDataLocationResolverContext context = this.locationResolverContext.getValue();
    assertThat(context.getParent()).hasToString("a");
}
Also used : MockPropertySource(cn.taketoday.mock.env.MockPropertySource) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

MockPropertySource (cn.taketoday.mock.env.MockPropertySource)88 Test (org.junit.jupiter.api.Test)84 Binder (cn.taketoday.context.properties.bind.Binder)16 LinkedHashMap (java.util.LinkedHashMap)12 MockServletContext (cn.taketoday.mock.web.MockServletContext)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 PropertySource (cn.taketoday.core.env.PropertySource)4 MockServletConfig (cn.taketoday.mock.web.MockServletConfig)4 MapPropertySource (cn.taketoday.core.env.MapPropertySource)2 AddedPropertySource (cn.taketoday.framework.context.config.TestConfigDataEnvironmentUpdateListener.AddedPropertySource)2 AbstractRefreshableWebApplicationContext (cn.taketoday.web.context.support.AbstractRefreshableWebApplicationContext)2 AnnotationConfigWebApplicationContext (cn.taketoday.web.context.support.AnnotationConfigWebApplicationContext)2 GenericWebServletApplicationContext (cn.taketoday.web.context.support.GenericWebServletApplicationContext)2 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)2