Search in sources :

Example 6 with ServerPortInfoApplicationContextInitializer

use of org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method localPortIsAvailable.

@Test
public void localPortIsAvailable() throws Exception {
    addWebServerFactoryBean();
    new ServerPortInfoApplicationContextInitializer().initialize(this.context);
    this.context.refresh();
    ConfigurableEnvironment environment = this.context.getEnvironment();
    assertThat(environment.containsProperty("local.server.port")).isTrue();
    assertThat(environment.getProperty("local.server.port")).isEqualTo("8080");
}
Also used : ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Test(org.junit.Test)

Example 7 with ServerPortInfoApplicationContextInitializer

use of org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer in project spring-boot by spring-projects.

the class BootCuriesHrefIntegrationTests method load.

private int load(String... properties) {
    this.context = new AnnotationConfigServletWebServerApplicationContext();
    this.context.setClassLoader(new ClassLoader(getClass().getClassLoader()) {

        @Override
        public URL getResource(String name) {
            if ("META-INF/resources/spring-boot-actuator/docs/index.html".equals(name)) {
                return super.getResource("actuator-docs-index.html");
            }
            return super.getResource(name);
        }
    });
    EnvironmentTestUtils.addEnvironment(this.context, properties);
    this.context.register(TestConfiguration.class);
    new ServerPortInfoApplicationContextInitializer().initialize(this.context);
    this.context.refresh();
    return Integer.parseInt(this.context.getEnvironment().getProperty("local.management.port"));
}
Also used : ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) URL(java.net.URL)

Example 8 with ServerPortInfoApplicationContextInitializer

use of org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer in project spring-boot by spring-projects.

the class WebSocketMessagingAutoConfigurationTests method performStompSubscription.

private Object performStompSubscription(final String topic) throws Throwable {
    EnvironmentTestUtils.addEnvironment(this.context, "server.port:0", "spring.jackson.serialization.indent-output:true");
    this.context.register(WebSocketMessagingConfiguration.class);
    new ServerPortInfoApplicationContextInitializer().initialize(this.context);
    this.context.refresh();
    WebSocketStompClient stompClient = new WebSocketStompClient(this.sockJsClient);
    final AtomicReference<Throwable> failure = new AtomicReference<>();
    final AtomicReference<Object> result = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    StompSessionHandler handler = new StompSessionHandlerAdapter() {

        @Override
        public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
            session.subscribe(topic, new StompFrameHandler() {

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    result.set(payload);
                    latch.countDown();
                }

                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return Object.class;
                }
            });
        }

        @Override
        public void handleFrame(StompHeaders headers, Object payload) {
            latch.countDown();
        }

        @Override
        public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
            failure.set(exception);
            latch.countDown();
        }

        @Override
        public void handleTransportError(StompSession session, Throwable exception) {
            failure.set(exception);
            latch.countDown();
        }
    };
    stompClient.setMessageConverter(new SimpleMessageConverter());
    stompClient.connect("ws://localhost:{port}/messaging", handler, this.context.getEnvironment().getProperty("local.server.port"));
    if (!latch.await(30000, TimeUnit.SECONDS)) {
        if (failure.get() != null) {
            throw failure.get();
        }
        fail("Response was not received within 30 seconds");
    }
    return result.get();
}
Also used : ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) StompFrameHandler(org.springframework.messaging.simp.stomp.StompFrameHandler) StompSession(org.springframework.messaging.simp.stomp.StompSession) StompSessionHandler(org.springframework.messaging.simp.stomp.StompSessionHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) StompCommand(org.springframework.messaging.simp.stomp.StompCommand) Type(java.lang.reflect.Type) WebSocketStompClient(org.springframework.web.socket.messaging.WebSocketStompClient) SimpleMessageConverter(org.springframework.messaging.converter.SimpleMessageConverter) StompSessionHandlerAdapter(org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)

Aggregations

ServerPortInfoApplicationContextInitializer (org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer)8 Test (org.junit.Test)6 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)3 RestTemplate (org.springframework.web.client.RestTemplate)2 Type (java.lang.reflect.Type)1 URI (java.net.URI)1 URL (java.net.URL)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 MultipartConfigElement (javax.servlet.MultipartConfigElement)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1 TestMultipartServlet (org.springframework.boot.web.servlet.testcomponents.TestMultipartServlet)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 HttpHeaders (org.springframework.http.HttpHeaders)1 RequestEntity (org.springframework.http.RequestEntity)1 SimpleMessageConverter (org.springframework.messaging.converter.SimpleMessageConverter)1 StompCommand (org.springframework.messaging.simp.stomp.StompCommand)1 StompFrameHandler (org.springframework.messaging.simp.stomp.StompFrameHandler)1 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)1