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");
}
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));
}
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();
}
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");
}
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");
}
Aggregations