Search in sources :

Example 61 with StandardEnvironment

use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.

the class EnvironmentConverterTests method convertedEnvironmentHasSameConversionService.

@Test
void convertedEnvironmentHasSameConversionService() {
    AbstractEnvironment originalEnvironment = new MockEnvironment();
    ConfigurableConversionService conversionService = mock(ConfigurableConversionService.class);
    originalEnvironment.setConversionService(conversionService);
    StandardEnvironment convertedEnvironment = this.environmentConverter.convertEnvironmentIfNecessary(originalEnvironment, StandardEnvironment.class);
    assertThat(convertedEnvironment.getConversionService()).isEqualTo(conversionService);
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 62 with StandardEnvironment

use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.

the class ConfigTreePropertySourceTests method getPropertyViaEnvironmentSupportsConversion.

@Test
void getPropertyViaEnvironmentSupportsConversion() throws Exception {
    StandardEnvironment environment = new StandardEnvironment();
    ConversionService conversionService = ApplicationConversionService.getSharedInstance();
    environment.setConversionService((ConfigurableConversionService) conversionService);
    environment.getPropertySources().addFirst(getFlatPropertySource());
    assertThat(environment.getProperty("a")).isEqualTo("A");
    assertThat(environment.getProperty("b")).isEqualTo("B");
    assertThat(environment.getProperty("c", InputStreamSource.class).getInputStream()).hasContent("C");
    assertThat(environment.getProperty("c", byte[].class)).contains('C');
    assertThat(environment.getProperty("one", Integer.class)).isEqualTo(1);
}
Also used : InputStreamSource(org.springframework.core.io.InputStreamSource) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) ApplicationConversionService(org.springframework.boot.convert.ApplicationConversionService) ConversionService(org.springframework.core.convert.ConversionService) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 63 with StandardEnvironment

use of org.springframework.core.env.StandardEnvironment in project spring-boot by spring-projects.

the class LoggingSystemPropertiesTests method environment.

private Environment environment(String key, Object value) {
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addLast(new MapPropertySource("test", Collections.singletonMap(key, value)));
    return environment;
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment)

Example 64 with StandardEnvironment

use of org.springframework.core.env.StandardEnvironment in project dubbo by alibaba.

the class EnvironmentUtilsTest method testExtraProperties.

@Test
public void testExtraProperties() {
    System.setProperty("user.name", "mercyblitz");
    StandardEnvironment environment = new StandardEnvironment();
    Map<String, Object> map = new HashMap<>();
    map.put("user.name", "Mercy");
    MapPropertySource propertySource = new MapPropertySource("first", map);
    CompositePropertySource compositePropertySource = new CompositePropertySource("comp");
    compositePropertySource.addFirstPropertySource(propertySource);
    MutablePropertySources propertySources = environment.getPropertySources();
    propertySources.addFirst(compositePropertySource);
    Map<String, Object> properties = EnvironmentUtils.extractProperties(environment);
    Assert.assertEquals("Mercy", properties.get("user.name"));
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) HashMap(java.util.HashMap) MapPropertySource(org.springframework.core.env.MapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 65 with StandardEnvironment

use of org.springframework.core.env.StandardEnvironment in project canal by alibaba.

the class CanalAdapterLoader method loadAdapter.

private void loadAdapter(OuterAdapterConfig config, List<OuterAdapter> canalOutConnectors) {
    try {
        OuterAdapter adapter;
        adapter = loader.getExtension(config.getName(), StringUtils.trimToEmpty(config.getKey()));
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        // 替换ClassLoader
        Thread.currentThread().setContextClassLoader(adapter.getClass().getClassLoader());
        Environment env = (Environment) SpringContext.getBean(Environment.class);
        Properties evnProperties = null;
        if (env instanceof StandardEnvironment) {
            evnProperties = new Properties();
            for (PropertySource<?> propertySource : ((StandardEnvironment) env).getPropertySources()) {
                if (propertySource instanceof EnumerablePropertySource) {
                    String[] names = ((EnumerablePropertySource<?>) propertySource).getPropertyNames();
                    for (String name : names) {
                        Object val = env.getProperty(name);
                        if (val != null) {
                            evnProperties.put(name, val);
                        }
                    }
                }
            }
        }
        adapter.init(config, evnProperties);
        Thread.currentThread().setContextClassLoader(cl);
        canalOutConnectors.add(adapter);
        logger.info("Load canal adapter: {} succeed", config.getName());
    } catch (Exception e) {
        logger.error("Load canal adapter: {} failed", config.getName(), e);
    }
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.core.env.Environment) Properties(java.util.Properties) OuterAdapter(com.alibaba.otter.canal.client.adapter.OuterAdapter) StandardEnvironment(org.springframework.core.env.StandardEnvironment)

Aggregations

StandardEnvironment (org.springframework.core.env.StandardEnvironment)146 Test (org.junit.jupiter.api.Test)63 MapPropertySource (org.springframework.core.env.MapPropertySource)52 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)42 Test (org.junit.Test)39 HashMap (java.util.HashMap)37 URI (java.net.URI)23 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)23 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)21 MutablePropertySources (org.springframework.core.env.MutablePropertySources)18 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)15 RestTemplate (org.springframework.web.client.RestTemplate)15 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)14 JsonJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe)14 XmlMessageSerDe (com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe)14 YamlJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe)14 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)13 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)13 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)9 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)9