Search in sources :

Example 56 with Environment

use of org.springframework.core.env.Environment in project java-chassis by ServiceComb.

the class TestSpringConfiguration method should_resolve_placeholder_in_list_by_spring_environment.

@Test
public void should_resolve_placeholder_in_list_by_spring_environment() {
    Environment environment = BeanUtils.getContext().getEnvironment();
    String value = environment.getProperty("placeholder");
    assertThat(value).isEqualTo("actual-value1,actual-value2");
    @SuppressWarnings("unchecked") List<String> list = environment.getProperty("placeholder", List.class);
    assertThat(list).containsExactly("actual-value1", "actual-value2");
}
Also used : Environment(org.springframework.core.env.Environment) Test(org.junit.Test)

Example 57 with Environment

use of org.springframework.core.env.Environment in project xm-ms-entity by xm-online.

the class EntityApp method main.

/**
 * Main method, used to run the application.
 *
 * @param args the command line arguments
 * @throws UnknownHostException if the local host name could not be resolved into an address
 */
public static void main(String[] args) throws UnknownHostException {
    MdcUtils.putRid();
    SpringApplication app = new SpringApplication(EntityApp.class);
    DefaultProfileUtil.addDefaultProfile(app);
    Environment env = app.run(args).getEnvironment();
    String protocol = "http";
    if (env.getProperty("server.ssl.key-store") != null) {
        protocol = "https";
    }
    log.info("\n----------------------------------------------------------\n\t" + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t{}://localhost:{}\n\t" + "External: \t{}://{}:{}\n\t" + "Profile(s): \t{}\n----------------------------------------------------------", env.getProperty("spring.application.name"), protocol, env.getProperty("server.port"), protocol, InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"), env.getActiveProfiles());
    String configServerStatus = env.getProperty("configserver.status");
    log.info("\n----------------------------------------------------------\n\t" + "Config Server: \t{}\n----------------------------------------------------------", configServerStatus == null ? "Not found or not setup for this application" : configServerStatus);
}
Also used : SpringApplication(org.springframework.boot.SpringApplication) Environment(org.springframework.core.env.Environment)

Example 58 with Environment

use of org.springframework.core.env.Environment in project spring-framework by spring-projects.

the class BeanDefinitionRegistrarTests method registerWithInjectedMethod.

@Test
void registerWithInjectedMethod() {
    Environment environment = mock(Environment.class);
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.registerSingleton("environment", environment);
    BeanDefinitionRegistrar.of("test", InjectionSample.class).instanceSupplier(instanceContext -> {
        InjectionSample bean = new InjectionSample();
        instanceContext.method("setEnvironment", Environment.class).invoke(beanFactory, attributes -> bean.setEnvironment(attributes.get(0)));
        return bean;
    }).register(beanFactory);
    assertBeanFactory(beanFactory, () -> {
        assertThat(beanFactory.containsBean("test")).isTrue();
        assertThat(beanFactory.getBean(InjectionSample.class).environment).isEqualTo(environment);
    });
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) InOrder(org.mockito.InOrder) ResourceLoader(org.springframework.core.io.ResourceLoader) FatalBeanException(org.springframework.beans.FatalBeanException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanInstanceContext(org.springframework.beans.factory.generator.config.BeanDefinitionRegistrar.BeanInstanceContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Field(java.lang.reflect.Field) Test(org.junit.jupiter.api.Test) Value(org.springframework.beans.factory.annotation.Value) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BeanCreationException(org.springframework.beans.factory.BeanCreationException) FactoryBean(org.springframework.beans.factory.FactoryBean) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Environment(org.springframework.core.env.Environment) ReflectionUtils(org.springframework.util.ReflectionUtils) Mockito.inOrder(org.mockito.Mockito.inOrder) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ResolvableType(org.springframework.core.ResolvableType) Mockito.mock(org.mockito.Mockito.mock) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Environment(org.springframework.core.env.Environment) Test(org.junit.jupiter.api.Test)

Example 59 with Environment

use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.

the class EmailEnvironmentHandlerFactoryTestIT method testCleanEnvironment.

@Test
void testCleanEnvironment() {
    Environment environment = setupMockedEnvironment();
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    EnvironmentVariableHandlerFactory factory = new EmailEnvironmentVariableHandlerFactory(emailGlobalConfigAccessor, environmentVariableUtility, validator);
    EnvironmentVariableHandler handler = factory.build();
    EnvironmentProcessingResult result = handler.updateFromEnvironment();
    assertEquals(ChannelKeys.EMAIL.getDisplayName(), handler.getName());
    assertTrue(result.hasValues());
    assertEquals(TEST_AUTH_REQUIRED, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.AUTH_REQUIRED_KEY).orElse("Auth required value missing"));
    assertEquals(TEST_FROM, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.EMAIL_FROM_KEY).orElse("SMTP from value missing"));
    assertEquals(TEST_SMTP_HOST, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.EMAIL_HOST_KEY).orElse("SMTP host value missing"));
    assertEquals(AlertConstants.MASKED_VALUE, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.AUTH_PASSWORD_KEY).orElse("Auth password value missing"));
    assertEquals(TEST_PORT, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.EMAIL_PORT_KEY).orElse("SMTP port value missing"));
    assertEquals(TEST_USER, result.getVariableValue(EmailEnvironmentVariableHandlerFactory.AUTH_USER_KEY).orElse("Auth user value missing"));
}
Also used : EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Environment(org.springframework.core.env.Environment) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 60 with Environment

use of org.springframework.core.env.Environment in project hub-alert by blackducksoftware.

the class EmailEnvironmentHandlerFactoryTestIT method setupMockedEnvironment.

private Environment setupMockedEnvironment() {
    Environment environment = Mockito.mock(Environment.class);
    Predicate<String> hasEnvVarCheck = (variableName) -> !EmailEnvironmentVariableHandlerFactory.OLD_ADDITIONAL_PROPERTY_KEYSET.contains(variableName);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.AUTH_REQUIRED_KEY, TEST_AUTH_REQUIRED);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.EMAIL_FROM_KEY, TEST_FROM);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.EMAIL_HOST_KEY, TEST_SMTP_HOST);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.AUTH_PASSWORD_KEY, TEST_PASSWORD);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.EMAIL_PORT_KEY, TEST_PORT);
    EnvironmentVariableMockingUtil.addEnvironmentVariableValueToMock(environment, hasEnvVarCheck, EmailEnvironmentVariableHandlerFactory.AUTH_USER_KEY, TEST_USER);
    return environment;
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Predicate(java.util.function.Predicate) Autowired(org.springframework.beans.factory.annotation.Autowired) EnvironmentVariableMockingUtil(com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) AlertRestConstants(com.synopsys.integration.alert.common.rest.AlertRestConstants) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) EnvironmentVariableHandlerFactory(com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Environment(org.springframework.core.env.Environment) EnvironmentVariableHandler(com.synopsys.integration.alert.environment.EnvironmentVariableHandler) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) EmailGlobalConfigurationValidator(com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) ChannelKeys(com.synopsys.integration.alert.descriptor.api.model.ChannelKeys) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) AlertConstants(com.synopsys.integration.alert.api.common.model.AlertConstants) Environment(org.springframework.core.env.Environment)

Aggregations

Environment (org.springframework.core.env.Environment)161 Test (org.junit.jupiter.api.Test)68 StandardEnvironment (org.springframework.core.env.StandardEnvironment)30 EnvironmentVariableUtility (com.synopsys.integration.alert.environment.EnvironmentVariableUtility)26 MockEnvironment (org.springframework.mock.env.MockEnvironment)24 SpringApplication (org.springframework.boot.SpringApplication)21 EnvironmentProcessingResult (com.synopsys.integration.alert.environment.EnvironmentProcessingResult)19 EnvironmentVariableHandler (com.synopsys.integration.alert.environment.EnvironmentVariableHandler)19 EnvironmentVariableHandlerFactory (com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory)19 HashMap (java.util.HashMap)13 ResourceLoader (org.springframework.core.io.ResourceLoader)13 Collectors (java.util.stream.Collectors)11 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)10 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)9 MutablePropertySources (org.springframework.core.env.MutablePropertySources)8 AlertConstants (com.synopsys.integration.alert.api.common.model.AlertConstants)7 ChannelKeys (com.synopsys.integration.alert.descriptor.api.model.ChannelKeys)7 EnvironmentVariableMockingUtil (com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil)7 Map (java.util.Map)7 Predicate (java.util.function.Predicate)7