Java Code Examples for org.springframework.mock.env.MockEnvironment#setProperty()

The following examples show how to use org.springframework.mock.env.MockEnvironment#setProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void replacementFromEnvironmentProperties() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MockEnvironment env = new MockEnvironment();
	env.setProperty("my.name", "myValue");

	PropertySourcesPlaceholderConfigurer ppc =
		new PropertySourcesPlaceholderConfigurer();
	ppc.setEnvironment(env);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue"));
	assertThat(ppc.getAppliedPropertySources(), not(nullValue()));
}
 
Example 2
Source File: ConfigurationHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBigDecimalRequiredPropertyValueConversionFail()
{
    ConfigurationValue configurationValue = ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE;

    MockEnvironment environment = new MockEnvironment();
    environment.setProperty(configurationValue.getKey(), "INVALID_BigDecimal_VALUE");

    try
    {
        configurationHelper.getBigDecimalRequiredProperty(configurationValue, environment);
        fail("Should throw an IllegalStatueException when property value is not BigDecimal.");
    }
    catch (IllegalStateException e)
    {
        assertEquals(String.format("Configuration \"%s\" has an invalid BigDecimal value: \"INVALID_BigDecimal_VALUE\".", configurationValue.getKey()),
            e.getMessage());
    }
}
 
Example 3
Source File: EnableMBeanExportConfigurationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPlaceholderBased() throws Exception {
	MockEnvironment env = new MockEnvironment();
	env.setProperty("serverName", "server");
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.setEnvironment(env);
	ctx.register(PlaceholderBasedConfiguration.class);
	ctx.refresh();
	try {
		MBeanServer server = (MBeanServer) ctx.getBean("server");
		ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4");
		assertNotNull(server.getObjectInstance(oname));
		String name = (String) server.getAttribute(oname, "Name");
		assertEquals("Invalid name returned", "TEST", name);
	}
	finally {
		ctx.close();
	}
}
 
Example 4
Source File: DataSourceMapSetterTest.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@SneakyThrows
@Test
public void assetMergedReplaceAndAdd() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("spring.shardingsphere.datasource.names", "ds0,ds1");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.common.type", "org.apache.commons.dbcp2.BasicDataSource");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.common.driver-class-name", "org.h2.Driver");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.common.max-total", "100");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.common.username", "Asa");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.common.password", "123");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.url", "jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.username", "sa");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.max-total", "50");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.password", "");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.url", "jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.username", "sa");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.max-total", "150");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.password", "");
    StandardEnvironment standardEnvironment = new StandardEnvironment();
    standardEnvironment.merge(mockEnvironment);
    Map<String, DataSource> dataSourceMap = DataSourceMapSetter.getDataSourceMap(standardEnvironment);
    assertThat(dataSourceMap.size(), is(2));
    assertThat(dataSourceMap.get("ds0").getConnection().getMetaData().getUserName(), is("SA"));
    assertThat(dataSourceMap.get("ds1").getConnection().getMetaData().getUserName(), is("SA"));
}
 
Example 5
Source File: YandexPostgresDatabaseProviderTest.java    From embedded-database-spring-test with Apache License 2.0 5 votes vote down vote up
@Test
public void providersWithSameConfigurationShouldEquals() {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("zonky.test.database.postgres.yandex-provider.postgres-version", "postgres-version");
    environment.setProperty("zonky.test.database.postgres.initdb.properties.xxx", "xxx-value");
    environment.setProperty("zonky.test.database.postgres.server.properties.yyy", "yyy-value");
    environment.setProperty("zonky.test.database.postgres.client.properties.zzz", "zzz-value");

    YandexPostgresDatabaseProvider provider1 = new YandexPostgresDatabaseProvider(environment);
    YandexPostgresDatabaseProvider provider2 = new YandexPostgresDatabaseProvider(environment);

    assertThat(provider1).isEqualTo(provider2);
}
 
Example 6
Source File: ZonkyPostgresDatabaseProviderTest.java    From embedded-database-spring-test with Apache License 2.0 5 votes vote down vote up
@Test
public void providersWithSameConfigurationShouldEquals() {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("zonky.test.database.postgres.zonky-provider.preparer-isolation", "database");
    environment.setProperty("zonky.test.database.postgres.initdb.properties.xxx", "xxx-value");
    environment.setProperty("zonky.test.database.postgres.server.properties.yyy", "yyy-value");
    environment.setProperty("zonky.test.database.postgres.client.properties.zzz", "zzz-value");

    ZonkyPostgresDatabaseProvider provider1 = new ZonkyPostgresDatabaseProvider(environment, databaseCustomizers);
    ZonkyPostgresDatabaseProvider provider2 = new ZonkyPostgresDatabaseProvider(environment, databaseCustomizers);

    assertThat(provider1).isEqualTo(provider2);
}
 
Example 7
Source File: YandexPostgresDatabaseProviderTest.java    From embedded-database-spring-test with Apache License 2.0 5 votes vote down vote up
@Test
public void providersWithDifferentConfigurationShouldNotEquals() {
    Map<String, String> mockProperties = new HashMap<>();
    mockProperties.put("zonky.test.database.postgres.yandex-provider.postgres-version", "postgres-version");
    mockProperties.put("zonky.test.database.postgres.initdb.properties.xxx", "xxx-value");
    mockProperties.put("zonky.test.database.postgres.server.properties.yyy", "yyy-value");
    mockProperties.put("zonky.test.database.postgres.client.properties.zzz", "zzz-value");

    Map<String, String> diffProperties = new HashMap<>();
    diffProperties.put("zonky.test.database.postgres.yandex-provider.postgres-version", "diff-pg-version");
    diffProperties.put("zonky.test.database.postgres.initdb.properties.xxx", "xxx-diff-value");
    diffProperties.put("zonky.test.database.postgres.server.properties.yyy", "yyy-diff-value");
    diffProperties.put("zonky.test.database.postgres.client.properties.zzz", "zzz-diff-value");

    for (Map.Entry<String, String> diffProperty : diffProperties.entrySet()) {
        MockEnvironment environment1 = new MockEnvironment();
        MockEnvironment environment2 = new MockEnvironment();

        for (Map.Entry<String, String> mockProperty : mockProperties.entrySet()) {
            environment1.setProperty(mockProperty.getKey(), mockProperty.getValue());
            environment2.setProperty(mockProperty.getKey(), mockProperty.getValue());
        }

        environment2.setProperty(diffProperty.getKey(), diffProperty.getValue());

        YandexPostgresDatabaseProvider provider1 = new YandexPostgresDatabaseProvider(environment1);
        YandexPostgresDatabaseProvider provider2 = new YandexPostgresDatabaseProvider(environment2);

        assertThat(provider1).isNotEqualTo(provider2);
    }
}
 
Example 8
Source File: DockerPostgresDatabaseProviderTest.java    From embedded-database-spring-test with Apache License 2.0 5 votes vote down vote up
@Test
public void providersWithDifferentConfigurationShouldNotEquals() {
    Map<String, String> mockProperties = new HashMap<>();
    mockProperties.put("zonky.test.database.postgres.docker.image", "test-image");
    mockProperties.put("zonky.test.database.postgres.docker.tmpfs.options", "mount-options");
    mockProperties.put("zonky.test.database.postgres.docker.tmpfs.enabled", "true");
    mockProperties.put("zonky.test.database.postgres.initdb.properties.xxx", "xxx-value");
    mockProperties.put("zonky.test.database.postgres.server.properties.yyy", "yyy-value");
    mockProperties.put("zonky.test.database.postgres.client.properties.zzz", "zzz-value");

    Map<String, String> diffProperties = new HashMap<>();
    diffProperties.put("zonky.test.database.postgres.docker.image", "diff-test-image");
    diffProperties.put("zonky.test.database.postgres.docker.tmpfs.options", "diff-mount-options");
    diffProperties.put("zonky.test.database.postgres.docker.tmpfs.enabled", "false");
    diffProperties.put("zonky.test.database.postgres.initdb.properties.xxx", "xxx-diff-value");
    diffProperties.put("zonky.test.database.postgres.server.properties.yyy", "yyy-diff-value");
    diffProperties.put("zonky.test.database.postgres.client.properties.zzz", "zzz-diff-value");

    for (Entry<String, String> diffProperty : diffProperties.entrySet()) {
        MockEnvironment environment1 = new MockEnvironment();
        MockEnvironment environment2 = new MockEnvironment();

        for (Entry<String, String> mockProperty : mockProperties.entrySet()) {
            environment1.setProperty(mockProperty.getKey(), mockProperty.getValue());
            environment2.setProperty(mockProperty.getKey(), mockProperty.getValue());
        }

        environment2.setProperty(diffProperty.getKey(), diffProperty.getValue());

        DockerPostgresDatabaseProvider provider1 = new DockerPostgresDatabaseProvider(environment1, containerCustomizers);
        DockerPostgresDatabaseProvider provider2 = new DockerPostgresDatabaseProvider(environment2, containerCustomizers);

        assertThat(provider1).isNotEqualTo(provider2);
    }
}
 
Example 9
Source File: EnableNacosDiscoveryTest.java    From nacos-spring-project with Apache License 2.0 5 votes vote down vote up
private void testResolvePlaceholder(String placeholder, String propertyName,
		String propertyValue, String expectValue) {
	MockEnvironment environment = new MockEnvironment();
	environment.setProperty(propertyName, propertyValue);
	String resolvedValue = environment.resolvePlaceholders(placeholder);
	Assert.assertEquals(expectValue, resolvedValue);
}
 
Example 10
Source File: MonitorComponentConditionalTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Test
public void testMatch() {
    MonitorComponentConditional m = new MonitorComponentConditional();
    ConditionContext context = Mockito.mock(ConditionContext.class);
    MockEnvironment env = new MockEnvironment();
    env.setProperty("components.active", "metrics");
    when(context.getEnvironment()).thenReturn(env);

    AnnotatedTypeMetadata atm = Mockito.mock(AnnotatedTypeMetadata.class);
    MultiValueMap<String, Object> map = new LinkedMultiValueMap();
    map.put("value", Lists.newArrayList("metrics"));

    when(atm.getAllAnnotationAttributes(anyString())).thenReturn(map);
    Assert.assertTrue(m.matches(context, atm));
}
 
Example 11
Source File: ShardingSpringBootConditionTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertNotMatch() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type", "AES");
    ConditionContext context = Mockito.mock(ConditionContext.class);
    AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
    when(context.getEnvironment()).thenReturn(mockEnvironment);
    ShardingSpringBootCondition condition = new ShardingSpringBootCondition();
    ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata);
    assertThat(matchOutcome.isMatch(), is(false));
}
 
Example 12
Source File: ShadowSpringBootConditionTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertNotMatch() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type", "AES");
    ConditionContext context = Mockito.mock(ConditionContext.class);
    AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
    when(context.getEnvironment()).thenReturn(mockEnvironment);
    ShadowSpringBootCondition condition = new ShadowSpringBootCondition();
    ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata);
    assertThat(matchOutcome.isMatch(), is(false));
}
 
Example 13
Source File: IndexControllerConfigActionWithEnvironmentVariablesTest.java    From SMSC with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("admin.api.url", "/admin");
    environment.setProperty("admin.i18n.path", "/admin/i18n");
    environment.setProperty("admin.debug", "true");
    applicationContext.setEnvironment(environment);
}
 
Example 14
Source File: Dbcp2DataSourcePropertiesSetterTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.type", "org.apache.commons.dbcp2.BasicDataSource");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.connection-properties.test", "test");
    mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.connection-properties.xxx", "yyy");
    environment = mockEnvironment;
}
 
Example 15
Source File: SpringConfigurationPropertySourceTests.java    From synopsys-detect with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySpringReturnsValue() throws InvalidPropertyException {
    final MockEnvironment m = new MockEnvironment();
    m.setProperty("example.key", "value");

    final List<PropertySource> sources = new ArrayList<>(SpringConfigurationPropertySource.fromConfigurableEnvironment(m));
    final PropertyConfiguration config = new PropertyConfiguration(sources);

    final NullableProperty<String> property = new NullableStringProperty("example.key");
    Assertions.assertEquals(Optional.of("value"), config.getValue(property));
    Assertions.assertEquals(Optional.of("mockProperties"), config.getPropertySource(property));
}
 
Example 16
Source File: SpringBootAdminClientEnabledConditionTest.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Test
public void test_nonEmptyUrl_enabled() {
	MockEnvironment environment = new MockEnvironment();
	environment.setProperty("spring.boot.admin.client.url", "http://localhost:8080/management");
	BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
	assertThat(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch()).isTrue();
}
 
Example 17
Source File: SpringBootAdminClientEnabledConditionTest.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Test
public void test_nonEmptyUrl_disabled() {
	MockEnvironment environment = new MockEnvironment();
	environment.setProperty("spring.boot.admin.client.enabled", "false");
	environment.setProperty("spring.boot.admin.client.url", "http://localhost:8080/management");
	BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
	assertThat(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch()).isFalse();
}
 
Example 18
Source File: VelocityToolsBasePackagesConditionTest.java    From velocity-spring-boot-project with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetMatchOutcome() {

    VelocityToolsBasePackagesCondition condition = new VelocityToolsBasePackagesCondition();

    MockEnvironment environment = new MockEnvironment();

    MockConditionContext conditionContext = new MockConditionContext();

    conditionContext.setEnvironment(environment);

    ConditionOutcome conditionOutcome = condition.getMatchOutcome(conditionContext, null);

    Assert.assertFalse(conditionOutcome.isMatch());

    environment.setProperty("spring.velocity.tools-base-packages", "com.alibaba.boot.velocity.tools");

    conditionOutcome = condition.getMatchOutcome(conditionContext, null);

    Assert.assertTrue(conditionOutcome.isMatch());

}
 
Example 19
Source File: PropertiesPlaceholderResolverTest.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Test
public void testResolve() {

	MockEnvironment environment = new MockEnvironment();

	PropertiesPlaceholderResolver resolver = new PropertiesPlaceholderResolver(
			environment);

	Map properties = new HashMap();
	properties.put("my.name", "${my.name}");
	properties.put("my.age", 18);

	environment.setProperty("my.name", "mercyblitz");
	environment.setProperty("my.age", "18");

	Properties resolvedProperties = resolver.resolve(properties);

	Assert.assertEquals(resolvedProperties.get("my.name"), "mercyblitz");
	Assert.assertNull(resolvedProperties.get("my.age"));

}
 
Example 20
Source File: VelocityLayoutConditionTest.java    From velocity-spring-boot-project with Apache License 2.0 3 votes vote down vote up
@Test
public void testGetMatchOutcome() {

    VelocityLayoutCondition condition = new VelocityLayoutCondition();

    MockEnvironment environment = new MockEnvironment();

    MockConditionContext conditionContext = new MockConditionContext();

    conditionContext.setEnvironment(environment);

    ConditionOutcome conditionOutcome = condition.getMatchOutcome(conditionContext, null);

    Assert.assertTrue(conditionOutcome.isMatch());

    environment.setProperty("spring.velocity.enabled", "true");
    environment.setProperty("spring.velocity.layout-enabled", "true");

    conditionOutcome = condition.getMatchOutcome(conditionContext, null);
    Assert.assertTrue(conditionOutcome.isMatch());

    environment.setProperty("spring.velocity.enabled", "false");

    conditionOutcome = condition.getMatchOutcome(conditionContext, null);

    Assert.assertFalse(conditionOutcome.isMatch());

    environment.setProperty("spring.velocity.enabled", "true");
    environment.setProperty("spring.velocity.layout-enabled", "false");

    conditionOutcome = condition.getMatchOutcome(conditionContext, null);

    Assert.assertFalse(conditionOutcome.isMatch());

}