Search in sources :

Example 16 with MockPropertySource

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

the class ConfigDataEnvironmentContributorsTests method getBinderWhenHasPlaceholderAndInactiveResolvesPlaceholderOnlyFromActive.

@Test
void getBinderWhenHasPlaceholderAndInactiveResolvesPlaceholderOnlyFromActive() {
    MockPropertySource firstPropertySource = new MockPropertySource();
    firstPropertySource.setProperty("other", "one");
    firstPropertySource.setProperty("context.config.activate.on-profile", "production");
    MockPropertySource secondPropertySource = new MockPropertySource();
    secondPropertySource.setProperty("other", "two");
    secondPropertySource.setProperty("test", "${other}");
    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 17 with MockPropertySource

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

the class ConfigDataEnvironmentContributorsTests method getBinderWhenFailOnBindToInactiveSourceWithFirstInactiveThrowsException.

@Test
void getBinderWhenFailOnBindToInactiveSourceWithFirstInactiveThrowsException() {
    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, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> binder.bind("test", String.class)).satisfies((ex) -> assertThat(ex.getCause()).isInstanceOf(InactiveConfigDataAccessException.class));
}
Also used : Binder(cn.taketoday.context.properties.bind.Binder) MockPropertySource(cn.taketoday.mock.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 18 with MockPropertySource

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

the class ConfigDataEnvironmentContributorsTests method withProcessedImportsResolvesAndLoads.

@Test
void withProcessedImportsResolvesAndLoads() {
    this.importer = mock(ConfigDataImporter.class);
    List<ConfigDataLocation> locations = Arrays.asList(LOCATION_1);
    MockPropertySource propertySource = new MockPropertySource();
    Map<ConfigDataResolutionResult, ConfigData> imported = new LinkedHashMap<>();
    imported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a"), false), new ConfigData(Arrays.asList(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(contributor));
    ConfigDataEnvironmentContributors withProcessedImports = contributors.withProcessedImports(this.importer, this.activationContext);
    Iterator<ConfigDataEnvironmentContributor> iterator = withProcessedImports.iterator();
    assertThat(iterator.next().getPropertySource()).isSameAs(propertySource);
    assertThat(iterator.next().getKind()).isEqualTo(Kind.INITIAL_IMPORT);
    assertThat(iterator.next().getKind()).isEqualTo(Kind.ROOT);
    assertThat(iterator.hasNext()).isFalse();
}
Also used : MockPropertySource(cn.taketoday.mock.env.MockPropertySource) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 19 with MockPropertySource

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

the class ConfigDataEnvironmentContributorsTests method withProcessedImportsProvidesLocationResolverContextWithAccessToBinder.

@Test
void withProcessedImportsProvidesLocationResolverContextWithAccessToBinder() {
    MockPropertySource existingPropertySource = new MockPropertySource();
    existingPropertySource.setProperty("test", "springboot");
    ConfigDataEnvironmentContributor existingContributor = ConfigDataEnvironmentContributor.ofExisting(existingPropertySource);
    this.importer = mock(ConfigDataImporter.class);
    List<ConfigDataLocation> locations = Arrays.asList(LOCATION_1);
    MockPropertySource propertySource = new MockPropertySource();
    Map<ConfigDataResolutionResult, ConfigData> imported = new LinkedHashMap<>();
    imported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a'"), false), new ConfigData(Arrays.asList(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(), this.locationResolverContext.capture(), any(), any());
    ConfigDataLocationResolverContext context = this.locationResolverContext.getValue();
    assertThat(context.getBinder().bind("test", String.class).get()).isEqualTo("springboot");
}
Also used : LinkedHashMap(java.util.LinkedHashMap) MockPropertySource(cn.taketoday.mock.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 20 with MockPropertySource

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

the class ConfigurationPropertySourceTests method fromCreatesConfigurationPropertySourcesPropertySource.

@Test
void fromCreatesConfigurationPropertySourcesPropertySource() {
    MockPropertySource source = new MockPropertySource();
    source.setProperty("spring", "boot");
    ConfigurationPropertySource adapted = ConfigurationPropertySource.from(source);
    assertThat(adapted.getConfigurationProperty(ConfigurationPropertyName.of("spring")).getValue()).isEqualTo("boot");
}
Also used : MockPropertySource(cn.taketoday.mock.env.MockPropertySource) 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