use of org.springframework.core.env.StandardEnvironment in project spring-framework by spring-projects.
the class ProfileXmlBeanDefinitionTests method testDefaultAndNonDefaultProfile.
@Test
public void testDefaultAndNonDefaultProfile() {
assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean());
assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, "other"), not(containsTargetBean()));
{
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
ConfigurableEnvironment env = new StandardEnvironment();
env.setActiveProfiles(DEV_ACTIVE);
env.setDefaultProfiles("default");
reader.setEnvironment(env);
reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
assertThat(beanFactory, containsTargetBean());
}
{
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
ConfigurableEnvironment env = new StandardEnvironment();
// env.setActiveProfiles(DEV_ACTIVE);
env.setDefaultProfiles("default");
reader.setEnvironment(env);
reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
assertThat(beanFactory, containsTargetBean());
}
{
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
ConfigurableEnvironment env = new StandardEnvironment();
// env.setActiveProfiles(DEV_ACTIVE);
//env.setDefaultProfiles("default");
reader.setEnvironment(env);
reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
assertThat(beanFactory, containsTargetBean());
}
}
use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.
the class SpringApplicationBindContextLoader method loadContext.
@Override
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
SpringApplication application = new SpringApplication();
application.setMainApplicationClass(config.getTestClass());
application.setWebApplicationType(WebApplicationType.NONE);
application.setSources(new LinkedHashSet<Object>(Arrays.asList(config.getClasses())));
ConfigurableEnvironment environment = new StandardEnvironment();
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("spring.jmx.enabled", "false");
properties.putAll(TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties()));
environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new MapPropertySource("integrationTest", properties));
application.setEnvironment(environment);
return application.run();
}
use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.
the class AnsiOutputApplicationListenerTests method disabledViaApplicationProperties.
@Test
public void disabledViaApplicationProperties() throws Exception {
ConfigurableEnvironment environment = new StandardEnvironment();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, "spring.config.name=ansi");
SpringApplication application = new SpringApplication(Config.class);
application.setEnvironment(environment);
application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run();
assertThat(AnsiOutputEnabledValue.get()).isEqualTo(Enabled.NEVER);
}
use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.
the class EnvironmentEndpoint method getPropertySources.
private MutablePropertySources getPropertySources() {
MutablePropertySources sources;
Environment environment = getEnvironment();
if (environment != null && environment instanceof ConfigurableEnvironment) {
sources = ((ConfigurableEnvironment) environment).getPropertySources();
} else {
sources = new StandardEnvironment().getPropertySources();
}
return sources;
}
use of org.springframework.core.env.StandardEnvironment in project chassis by Kixeye.
the class HttpTransportTest method testHttpServiceWithXml.
@Test
public void testHttpServiceWithXml() throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("http.enabled", "true");
properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
properties.put("http.hostname", "localhost");
properties.put("websocket.enabled", "false");
properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
properties.put("websocket.hostname", "localhost");
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
context.setEnvironment(environment);
context.register(PropertySourcesPlaceholderConfigurer.class);
context.register(TransportConfiguration.class);
context.register(TestRestService.class);
try {
context.refresh();
final MessageSerDe serDe = context.getBean(XmlMessageSerDe.class);
RestTemplate httpClient = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
httpClient.setErrorHandler(new ResponseErrorHandler() {
public boolean hasError(ClientHttpResponse response) throws IOException {
return response.getRawStatusCode() == HttpStatus.OK.value();
}
public void handleError(ClientHttpResponse response) throws IOException {
}
});
httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
httpClient.setMessageConverters(new ArrayList<HttpMessageConverter<?>>(Lists.newArrayList(new SerDeHttpMessageConverter(serDe))));
TestObject response = httpClient.getForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), TestObject.class);
Assert.assertNotNull(response);
Assert.assertEquals("stuff", response.value);
response = httpClient.postForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), new TestObject("more stuff"), TestObject.class);
Assert.assertNotNull(response);
Assert.assertEquals("stuff", response.value);
response = httpClient.getForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), TestObject.class);
Assert.assertNotNull(response);
Assert.assertEquals("more stuff", response.value);
ResponseEntity<ServiceError> error = httpClient.postForEntity(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), new TestObject(RandomStringUtils.randomAlphabetic(100)), ServiceError.class);
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.BAD_REQUEST, error.getStatusCode());
Assert.assertEquals(ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE, error.getBody().code);
error = httpClient.getForEntity(new URI("http://localhost:" + properties.get("http.port") + "/stuff/expectedError"), ServiceError.class);
Assert.assertNotNull(response);
Assert.assertEquals(TestRestService.EXPECTED_EXCEPTION_HTTP_CODE, error.getStatusCode());
Assert.assertEquals(TestRestService.EXPECTED_EXCEPTION.code, error.getBody().code);
Assert.assertEquals(TestRestService.EXPECTED_EXCEPTION.description, error.getBody().description);
error = httpClient.getForEntity(new URI("http://localhost:" + properties.get("http.port") + "/stuff/unexpectedError"), ServiceError.class);
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, error.getStatusCode());
Assert.assertEquals(ExceptionServiceErrorMapper.UNKNOWN_ERROR_CODE, error.getBody().code);
} finally {
context.close();
}
}
Aggregations