Example usage for org.springframework.core.env StandardEnvironment getPropertySources

List of usage examples for org.springframework.core.env StandardEnvironment getPropertySources

Introduction

In this page you can find the example usage for org.springframework.core.env StandardEnvironment getPropertySources.

Prototype

@Override
    public MutablePropertySources getPropertySources() 

Source Link

Usage

From source file:org.cloudfoundry.identity.uaa.config.EnvironmentPropertiesFactoryBeanTests.java

@Test
public void testNullProperties() throws Exception {
    EnvironmentPropertiesFactoryBean factory = new EnvironmentPropertiesFactoryBean();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources()
            .addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", null)));
    factory.setEnvironment(environment);
    Properties properties = factory.getObject();
    assertEquals("", properties.get("foo"));
}

From source file:org.cloudfoundry.identity.uaa.config.EnvironmentMapFactoryBeanTests.java

@Test
public void testOverrideProperties() throws Exception {
    EnvironmentMapFactoryBean factory = new EnvironmentMapFactoryBean();
    factory.setDefaultProperties(getProperties("foo=foo"));
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addLast(new NestedMapPropertySource("override", getProperties("foo=bar")));
    factory.setEnvironment(environment);
    Map<String, ?> properties = factory.getObject();
    assertEquals("bar", properties.get("foo"));
}

From source file:org.cloudfoundry.identity.uaa.config.EnvironmentMapFactoryBeanTests.java

@Test
public void testPlaceholderProperties() throws Exception {
    EnvironmentMapFactoryBean factory = new EnvironmentMapFactoryBean();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources()
            .addLast(new NestedMapPropertySource("override", getProperties("bar=${spam}")));
    factory.setEnvironment(environment);
    factory.setDefaultProperties(getProperties("foo=baz"));
    Map<String, ?> properties = factory.getObject();
    assertEquals("baz", properties.get("foo"));
    assertEquals("${spam}", properties.get("bar"));
}

From source file:com.kixeye.chassis.support.test.eureka.metadata.MetadataCollectorTest.java

@Before
public void setup() {
    ConfigurationManager.getConfigInstance().setProperty("chassis.eureka.disable", "true");
    ConfigurationManager.getConfigInstance().setProperty("eureka.metadata.prop1", "propValue");
    ConfigurationManager.getConfigInstance().setProperty("eureka.datacenter", "default");

    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new ArchaiusSpringPropertySource());
    context = new AnnotationConfigApplicationContext();
    context.setEnvironment(environment);
    context.register(MetadataCollectorConfiguration.class);
    context.refresh();/* w  w w  . j a  v a  2s  .c o  m*/
    context.start();
}

From source file:org.ventiv.webjars.requirejs.config.RequireJsConfigBuilderTest.java

@Test
public void overrideShim() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put("webjars.requirejs.newModules", "kendo-angular,kendo-colorpicker,kendo-ui-core");
    properties.put("webjars.requirejs.dependencies.kendo-angular", "angular,kendo-ui-core");
    properties.put("webjars.requirejs.dependencies.kendo-ui-core", "jquery");

    StandardEnvironment env = new StandardEnvironment();
    env.getPropertySources().addFirst(new MapPropertySource("props", properties));

    RequireJsConfigBuilder builder = new RequireJsConfigBuilder("/webjars/", env);
    Map<String, Object> requireJsConfig = builder.buildConfig();

    Map<String, Object> shim = (Map<String, Object>) requireJsConfig.get("shim");
    assertEquals(Arrays.asList("angular"), shim.get("angular-animate"));
    assertEquals(Arrays.asList("angular", "kendo-ui-core"), shim.get("kendo-angular"));
    assertEquals(Arrays.asList("jquery"), shim.get("kendo-ui-core"));
    assertEquals(null, shim.get("kendo-colorpicker"));
}

From source file:org.ventiv.webjars.requirejs.config.RequireJsConfigBuilderTest.java

@Test
public void overridePath() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put("webjars.requirejs.newModules", "kendo-angular,kendo-colorpicker");
    properties.put("webjars.requirejs.paths.kendo-ui-core", "js/kendo.core.min");
    properties.put("webjars.requirejs.paths.kendo-angular",
            "/webjars/kendo-ui-core/2014.2.716/js/kendo.angular.min");
    properties.put("webjars.requirejs.paths.kendo-colorpicker",
            "/webjars/kendo-ui-core/2014.2.716/js/kendo.colorpicker.min");

    StandardEnvironment env = new StandardEnvironment();
    env.getPropertySources().addFirst(new MapPropertySource("props", properties));

    RequireJsConfigBuilder builder = new RequireJsConfigBuilder("/webjars/", env);
    Map<String, Object> requireJsConfig = builder.buildConfig();

    Map<String, String> paths = (Map<String, String>) requireJsConfig.get("paths");
    assertEquals(paths.size(), 170);/*w  w w .  ja v a2  s.c  o  m*/
    assertEquals(paths.get("kendo-ui-core"), "/webjars/kendo-ui-core/2014.2.716/js/kendo.core.min");
    assertEquals(paths.get("kendo-angular"), "/webjars/kendo-ui-core/2014.2.716/js/kendo.angular.min");
    assertEquals(paths.get("kendo-colorpicker"), "/webjars/kendo-ui-core/2014.2.716/js/kendo.colorpicker.min");
}

From source file:com.kixeye.chassis.transport.MetricsTest.java

@Test
public void testMetricsPing() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("admin.enabled", "true");
    properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("admin.hostname", "localhost");

    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");

    properties.put("http.enabled", "true");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.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(MetricsConfiguration.class);

    RestTemplate httpClient = new RestTemplate();

    try {/*from ww  w . j a v  a  2s .c o m*/
        context.refresh();

        httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        for (MessageSerDe serDe : context.getBeansOfType(MessageSerDe.class).values()) {
            messageConverters.add(new SerDeHttpMessageConverter(serDe));
        }
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        httpClient.setMessageConverters(messageConverters);

        ResponseEntity<String> response = httpClient.getForEntity(
                new URI("http://localhost:" + properties.get("admin.port") + "/metrics/ping"), String.class);

        logger.info("Got response: [{}]", response);

        Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
        Assert.assertEquals("pong".trim(), response.getBody().trim());
    } finally {
        context.close();
    }
}

From source file:it.tidalwave.northernwind.util.test.TestHelper.java

/*******************************************************************************************************************
 *
 ******************************************************************************************************************/
@Nonnull//  w w  w.j a v a  2 s  .c o m
private ApplicationContext createSpringContext(final @Nonnull Map<String, Object> properties,
        final @Nonnull Collection<String> configurationFiles) {
    configurationFiles.add(test.getClass().getSimpleName() + "/TestBeans.xml");

    if (properties.isEmpty()) {
        return new ClassPathXmlApplicationContext(configurationFiles.toArray(new String[0]));
    } else {
        final StandardEnvironment environment = new StandardEnvironment();
        environment.getPropertySources().addFirst(new MapPropertySource("test", properties));
        final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
        context.setEnvironment(environment);
        context.load(configurationFiles.toArray(new String[0]));
        context.refresh();
        return context;
    }
}

From source file:com.kixeye.chassis.transport.HybridServiceTest.java

@Test
public void testHybridService() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");

    properties.put("http.enabled", "true");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.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(TestCombinedService.class);

    WebSocketClient wsClient = new WebSocketClient();

    RestTemplate httpClient = new RestTemplate();

    try {/* www .  j  av a  2  s. c  o m*/
        context.refresh();

        final MessageSerDe serDe = context.getBean(ProtobufMessageSerDe.class);

        final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);

        messageRegistry.registerType("stuff", TestObject.class);

        wsClient.start();

        httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
            messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
        }
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        httpClient.setMessageConverters(messageConverters);

        QueuingWebSocketListener webSocket = new QueuingWebSocketListener(serDe, messageRegistry, null);

        Session session = wsClient
                .connect(webSocket, new URI("ws://localhost:" + properties.get("websocket.port") + "/protobuf"))
                .get(5000, TimeUnit.MILLISECONDS);

        Envelope envelope = new Envelope("getStuff", null, null, null);

        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));

        TestObject response = webSocket.getResponse(5, TimeUnit.SECONDS);

        Assert.assertNotNull(response);
        Assert.assertEquals("stuff", response.value);

        byte[] rawStuff = serDe.serialize(new TestObject("more stuff"));

        envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));

        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));

        response = webSocket.getResponse(5, TimeUnit.SECONDS);

        Assert.assertNotNull(response);
        Assert.assertEquals("stuff", response.value);

        envelope = new Envelope("getStuff", null, null, null);

        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));

        response = webSocket.getResponse(5, TimeUnit.SECONDS);

        Assert.assertNotNull(response);
        Assert.assertEquals("more 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);

        response = httpClient.postForObject(
                new URI("http://localhost:" + properties.get("http.port") + "/stuff/"),
                new TestObject("even more stuff"), TestObject.class);

        Assert.assertNotNull(response);
        Assert.assertEquals("more stuff", response.value);

        response = httpClient.getForObject(
                new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), TestObject.class);

        Assert.assertNotNull(response);
        Assert.assertEquals("even more stuff", response.value);
    } finally {
        try {
            wsClient.stop();
        } finally {
            context.close();
        }
    }
}

From source file:com.kixeye.chassis.transport.shared.SharedTest.java

@Test
public void testClassPath() throws Exception {

    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("admin.enabled", "true");
    properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("admin.hostname", "localhost");

    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");

    properties.put("http.enabled", "true");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.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(MetricsConfiguration.class);
    RestTemplate httpClient = new RestTemplate();

    try {// w  w  w .j  a  v  a 2s .com
        context.refresh();

        httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
            messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
        }
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        httpClient.setMessageConverters(messageConverters);

        ResponseEntity<String> response = httpClient.getForEntity(
                new URI("http://localhost:" + properties.get("admin.port") + "/admin/classpath"), String.class);

        logger.info("Got response: [{}]", response);

        Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
        Assert.assertTrue(response.getBody().length() > 0);
    } finally {
        context.close();
    }
}