Search in sources :

Example 1 with ServerPortInfoApplicationContextInitializer

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

the class ServletComponentScanIntegrationTests method multipartConfigIsHonoured.

@Test
public void multipartConfigIsHonoured() {
    this.context = new AnnotationConfigServletWebServerApplicationContext();
    this.context.register(TestConfiguration.class);
    new ServerPortInfoApplicationContextInitializer().initialize(this.context);
    this.context.refresh();
    @SuppressWarnings("rawtypes") Map<String, ServletRegistrationBean> beans = this.context.getBeansOfType(ServletRegistrationBean.class);
    ServletRegistrationBean<?> servletRegistrationBean = beans.get(TestMultipartServlet.class.getName());
    assertThat(servletRegistrationBean).isNotNull();
    MultipartConfigElement multipartConfig = servletRegistrationBean.getMultipartConfig();
    assertThat(multipartConfig).isNotNull();
    assertThat(multipartConfig.getLocation()).isEqualTo("test");
    assertThat(multipartConfig.getMaxRequestSize()).isEqualTo(2048);
    assertThat(multipartConfig.getMaxFileSize()).isEqualTo(1024);
    assertThat(multipartConfig.getFileSizeThreshold()).isEqualTo(512);
}
Also used : ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) MultipartConfigElement(javax.servlet.MultipartConfigElement) TestMultipartServlet(org.springframework.boot.web.servlet.testcomponents.TestMultipartServlet) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) Test(org.junit.Test)

Example 2 with ServerPortInfoApplicationContextInitializer

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

the class ReactiveWebServerApplicationContextTests method whenContextIsRefreshedThenLocalServerPortIsAvailableFromTheEnvironment.

@Test
void whenContextIsRefreshedThenLocalServerPortIsAvailableFromTheEnvironment() {
    addWebServerFactoryBean();
    addHttpHandlerBean();
    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.jupiter.api.Test)

Example 3 with ServerPortInfoApplicationContextInitializer

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

the class ServletComponentScanIntegrationTests method indexedComponentsAreRegistered.

@ParameterizedTest(name = "{0}")
@MethodSource("testConfiguration")
void indexedComponentsAreRegistered(String serverName, Class<?> configuration) throws IOException {
    writeIndex(this.temp);
    this.context = new AnnotationConfigServletWebServerApplicationContext();
    try (URLClassLoader classLoader = new URLClassLoader(new URL[] { this.temp.toURI().toURL() }, getClass().getClassLoader())) {
        this.context.setClassLoader(classLoader);
        this.context.register(configuration);
        new ServerPortInfoApplicationContextInitializer().initialize(this.context);
        this.context.refresh();
        String port = this.context.getEnvironment().getProperty("local.server.port");
        String response = new RestTemplate().getForObject("http://localhost:" + port + "/test", String.class);
        assertThat(response).isEqualTo("alpha bravo charlie");
    }
}
Also used : ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) URLClassLoader(java.net.URLClassLoader) RestTemplate(org.springframework.web.client.RestTemplate) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 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(String topic) throws Throwable {
    TestPropertyValues.of("server.port:0", "spring.jackson.serialization.indent-output:true").applyTo(this.context);
    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(30, 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)

Example 5 with ServerPortInfoApplicationContextInitializer

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

the class EndpointWebMvcAutoConfigurationTests method portPropertiesOnDifferentPort.

@Test
public void portPropertiesOnDifferentPort() throws Exception {
    EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.port=" + ports.get().management);
    new ServerPortInfoApplicationContextInitializer().initialize(this.applicationContext);
    this.applicationContext.register(RootConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class);
    this.applicationContext.refresh();
    Integer localServerPort = this.applicationContext.getEnvironment().getProperty("local.server.port", Integer.class);
    Integer localManagementPort = this.applicationContext.getEnvironment().getProperty("local.management.port", Integer.class);
    assertThat(localServerPort).isNotNull();
    assertThat(localManagementPort).isNotNull();
    assertThat(localServerPort).isNotEqualTo(localManagementPort);
}
Also used : ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) Test(org.junit.Test)

Aggregations

ServerPortInfoApplicationContextInitializer (org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer)13 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)6 Test (org.junit.Test)5 RestTemplate (org.springframework.web.client.RestTemplate)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 Type (java.lang.reflect.Type)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.jupiter.api.Test)2 TestMultipartServlet (org.springframework.boot.web.servlet.testcomponents.TestMultipartServlet)2 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 SimpleMessageConverter (org.springframework.messaging.converter.SimpleMessageConverter)2 StompCommand (org.springframework.messaging.simp.stomp.StompCommand)2 StompFrameHandler (org.springframework.messaging.simp.stomp.StompFrameHandler)2 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)2 StompSession (org.springframework.messaging.simp.stomp.StompSession)2 StompSessionHandler (org.springframework.messaging.simp.stomp.StompSessionHandler)2 StompSessionHandlerAdapter (org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)2 WebSocketStompClient (org.springframework.web.socket.messaging.WebSocketStompClient)2