Search in sources :

Example 1 with ServerPortInfoApplicationContextInitializer

use of cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer in project today-infrastructure by TAKETODAY.

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(cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer) AnnotationConfigServletWebServerApplicationContext(cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) URLClassLoader(java.net.URLClassLoader) RestTemplate(cn.taketoday.web.client.RestTemplate) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with ServerPortInfoApplicationContextInitializer

use of cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer in project today-infrastructure by TAKETODAY.

the class ServletWebServerApplicationContextTests method localPortIsAvailable.

@Test
void localPortIsAvailable() {
    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(cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) Test(org.junit.jupiter.api.Test)

Example 3 with ServerPortInfoApplicationContextInitializer

use of cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer in project today-framework by TAKETODAY.

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(cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) Test(org.junit.jupiter.api.Test)

Example 4 with ServerPortInfoApplicationContextInitializer

use of cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer in project today-framework by TAKETODAY.

the class ServletComponentScanIntegrationTests method componentsAreRegistered.

@ParameterizedTest(name = "{0}")
@MethodSource("testConfiguration")
void componentsAreRegistered(String serverName, Class<?> configuration) {
    this.context = new AnnotationConfigServletWebServerApplicationContext();
    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(cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer) AnnotationConfigServletWebServerApplicationContext(cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) RestTemplate(cn.taketoday.web.client.RestTemplate) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with ServerPortInfoApplicationContextInitializer

use of cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer in project today-framework by TAKETODAY.

the class ServletWebServerApplicationContextTests method localPortIsAvailable.

@Test
void localPortIsAvailable() {
    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(cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

ServerPortInfoApplicationContextInitializer (cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer)10 AnnotationConfigServletWebServerApplicationContext (cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)4 RestTemplate (cn.taketoday.web.client.RestTemplate)4 Test (org.junit.jupiter.api.Test)4 TestMultipartServlet (cn.taketoday.framework.web.servlet.testcomponents.TestMultipartServlet)2 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)2 URLClassLoader (java.net.URLClassLoader)2