Search in sources :

Example 11 with AnnotationConfigServletWebApplicationContext

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

the class RemoteDevToolsAutoConfigurationTests method getContext.

private AnnotationConfigServletWebApplicationContext getContext(Supplier<AnnotationConfigServletWebApplicationContext> supplier) throws Exception {
    AtomicReference<AnnotationConfigServletWebApplicationContext> atomicReference = new AtomicReference<>();
    Thread thread = new Thread(() -> {
        AnnotationConfigServletWebApplicationContext context = supplier.get();
        atomicReference.getAndSet(context);
    });
    thread.start();
    thread.join();
    return atomicReference.get();
}
Also used : AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 12 with AnnotationConfigServletWebApplicationContext

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

the class RemoteDevToolsAutoConfigurationTests method securityConfigurationWhenWebSecurityConfigurerAdapterIsFound2.

@Test
void securityConfigurationWhenWebSecurityConfigurerAdapterIsFound2() throws Exception {
    this.context = getContext(() -> {
        AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
        context.setServletContext(new MockServletContext());
        context.register(Config.class, PropertyPlaceholderAutoConfiguration.class, TestWebSecurityConfigurerAdapter.class);
        TestPropertyValues.of("spring.devtools.remote.secret:supersecret").applyTo(context);
        context.refresh();
        return context;
    });
    DispatcherFilter filter = this.context.getBean(DispatcherFilter.class);
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).addFilter(filter).build();
    mockMvc.perform(MockMvcRequestBuilders.get(DEFAULT_CONTEXT_PATH + "/restart").header(DEFAULT_SECRET_HEADER_NAME, "supersecret")).andExpect(status().isOk());
    assertRestartInvoked(true);
}
Also used : AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) PropertyPlaceholderAutoConfiguration(org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration) MockServletContext(org.springframework.mock.web.MockServletContext) DispatcherFilter(org.springframework.boot.devtools.remote.server.DispatcherFilter) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.jupiter.api.Test)

Example 13 with AnnotationConfigServletWebApplicationContext

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

the class RemoteDevToolsAutoConfigurationTests method loadContext.

private AnnotationConfigServletWebApplicationContext loadContext(String... properties) {
    AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(Config.class, PropertyPlaceholderAutoConfiguration.class);
    TestPropertyValues.of(properties).applyTo(context);
    context.refresh();
    return context;
}
Also used : AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 14 with AnnotationConfigServletWebApplicationContext

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

the class SpringBootMockMvcBuilderCustomizerTests method customizeShouldAddFilters.

@Test
@SuppressWarnings("unchecked")
void customizeShouldAddFilters() {
    AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
    MockServletContext servletContext = new MockServletContext();
    context.setServletContext(servletContext);
    context.register(ServletConfiguration.class, FilterConfiguration.class);
    context.refresh();
    DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(context);
    this.customizer = new SpringBootMockMvcBuilderCustomizer(context);
    this.customizer.customize(builder);
    FilterRegistrationBean<?> registrationBean = (FilterRegistrationBean<?>) context.getBean("filterRegistrationBean");
    Filter testFilter = (Filter) context.getBean("testFilter");
    Filter otherTestFilter = registrationBean.getFilter();
    List<Filter> filters = (List<Filter>) ReflectionTestUtils.getField(builder, "filters");
    assertThat(filters).containsExactlyInAnyOrder(testFilter, otherTestFilter);
}
Also used : DefaultMockMvcBuilder(org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder) AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) Filter(jakarta.servlet.Filter) ArrayList(java.util.ArrayList) List(java.util.List) MockServletContext(org.springframework.mock.web.MockServletContext) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) Test(org.junit.jupiter.api.Test)

Example 15 with AnnotationConfigServletWebApplicationContext

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

the class SpringBootMockMvcBuilderCustomizerTests method whenCalledInParallelDeferredLinesWriterSeparatesOutputByThread.

@Test
void whenCalledInParallelDeferredLinesWriterSeparatesOutputByThread() throws Exception {
    AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
    MockServletContext servletContext = new MockServletContext();
    context.setServletContext(servletContext);
    context.register(ServletConfiguration.class, FilterConfiguration.class);
    context.refresh();
    CapturingLinesWriter delegate = new CapturingLinesWriter();
    new DeferredLinesWriter(context, delegate);
    CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        Thread thread = new Thread(() -> {
            for (int j = 0; j < 1000; j++) {
                DeferredLinesWriter writer = DeferredLinesWriter.get(context);
                writer.write(Arrays.asList("1", "2", "3", "4", "5"));
                writer.writeDeferredResult();
                writer.clear();
            }
            latch.countDown();
        });
        thread.start();
    }
    latch.await(60, TimeUnit.SECONDS);
    assertThat(delegate.allWritten).hasSize(10000);
    assertThat(delegate.allWritten).allSatisfy((written) -> assertThat(written).containsExactly("1", "2", "3", "4", "5"));
}
Also used : DeferredLinesWriter(org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter) AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) CountDownLatch(java.util.concurrent.CountDownLatch) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

AnnotationConfigServletWebApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext)23 Test (org.junit.jupiter.api.Test)16 MockServletContext (org.springframework.mock.web.MockServletContext)12 MockMvc (org.springframework.test.web.servlet.MockMvc)8 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)2 Filter (jakarta.servlet.Filter)1 FilterRegistration (jakarta.servlet.FilterRegistration)1 ServletContext (jakarta.servlet.ServletContext)1 ServletRegistration (jakarta.servlet.ServletRegistration)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 WebMvcEndpointHandlerMapping (org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping)1 PropertyPlaceholderAutoConfiguration (org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration)1 DispatcherFilter (org.springframework.boot.devtools.remote.server.DispatcherFilter)1 DeferredLinesWriter (org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter)1 ReactiveWebApplicationContextRunner (org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner)1 WebApplicationContextRunner (org.springframework.boot.test.context.runner.WebApplicationContextRunner)1