diff options
Diffstat (limited to 'spring-test/src/test/java/org/springframework/test/context/web')
3 files changed, 211 insertions, 44 deletions
diff --git a/spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java b/spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java index 0353a870..55201245 100644 --- a/spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,11 +38,12 @@ import static org.springframework.test.context.web.ServletTestExecutionListener. * Unit tests for {@link ServletTestExecutionListener}. * * @author Sam Brannen + * @author Phillip Webb * @since 3.2.6 */ public class ServletTestExecutionListenerTests { - private static final String SET_UP_OUTSIDE_OF_STEL = "SET_UP_OUTSIDE_OF_STEL"; + private static final String SET_UP_OUTSIDE_OF_STEL = "setUpOutsideOfStel"; private final WebApplicationContext wac = mock(WebApplicationContext.class); private final MockServletContext mockServletContext = new MockServletContext(); @@ -50,30 +51,6 @@ public class ServletTestExecutionListenerTests { private final ServletTestExecutionListener listener = new ServletTestExecutionListener(); - private void assertAttributesAvailable() { - assertNotNull("request attributes should be available", RequestContextHolder.getRequestAttributes()); - } - - private void assertAttributesNotAvailable() { - assertNull("request attributes should not be available", RequestContextHolder.getRequestAttributes()); - } - - private void assertAttributeExists() { - RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); - assertNotNull("request attributes should exist", requestAttributes); - Object setUpOutsideOfStel = requestAttributes.getAttribute(SET_UP_OUTSIDE_OF_STEL, - RequestAttributes.SCOPE_REQUEST); - assertNotNull(SET_UP_OUTSIDE_OF_STEL + " should exist as a request attribute", setUpOutsideOfStel); - } - - private void assertAttributeDoesNotExist() { - RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); - assertNotNull("request attributes should exist", requestAttributes); - Object setUpOutsideOfStel = requestAttributes.getAttribute(SET_UP_OUTSIDE_OF_STEL, - RequestAttributes.SCOPE_REQUEST); - assertNull(SET_UP_OUTSIDE_OF_STEL + " should NOT exist as a request attribute", setUpOutsideOfStel); - } - @Before public void setUp() { given(wac.getServletContext()).willReturn(mockServletContext); @@ -86,7 +63,7 @@ public class ServletTestExecutionListenerTests { request.setAttribute(SET_UP_OUTSIDE_OF_STEL, "true"); RequestContextHolder.setRequestAttributes(servletWebRequest); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); } @Test @@ -95,16 +72,16 @@ public class ServletTestExecutionListenerTests { given(testContext.getApplicationContext()).willReturn(mock(ApplicationContext.class)); listener.beforeTestClass(testContext); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); listener.prepareTestInstance(testContext); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); listener.beforeTestMethod(testContext); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); listener.afterTestMethod(testContext); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); } @Test @@ -112,22 +89,22 @@ public class ServletTestExecutionListenerTests { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(LegacyWebTestCase.class); RequestContextHolder.resetRequestAttributes(); - assertAttributesNotAvailable(); + assertRequestAttributesDoNotExist(); listener.beforeTestClass(testContext); listener.prepareTestInstance(testContext); - assertAttributesNotAvailable(); + assertRequestAttributesDoNotExist(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.beforeTestMethod(testContext); - assertAttributesNotAvailable(); + assertRequestAttributesDoNotExist(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); listener.afterTestMethod(testContext); verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); - assertAttributesNotAvailable(); + assertRequestAttributesDoNotExist(); } @Test @@ -135,21 +112,21 @@ public class ServletTestExecutionListenerTests { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(LegacyWebTestCase.class); listener.beforeTestClass(testContext); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); listener.prepareTestInstance(testContext); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.beforeTestMethod(testContext); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.afterTestMethod(testContext); verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); - assertAttributeExists(); + assertSetUpOutsideOfStelAttributeExists(); } @Test @@ -158,7 +135,7 @@ public class ServletTestExecutionListenerTests { RequestContextHolder.resetRequestAttributes(); listener.beforeTestClass(testContext); - assertAttributesNotAvailable(); + assertRequestAttributesDoNotExist(); assertWebAppConfigTestCase(); } @@ -168,28 +145,70 @@ public class ServletTestExecutionListenerTests { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(AtWebAppConfigWebTestCase.class); listener.beforeTestClass(testContext); - assertAttributesAvailable(); + assertRequestAttributesExist(); assertWebAppConfigTestCase(); } + /** + * @since 4.3 + */ + @Test + public void activateListenerWithoutExistingRequestAttributes() throws Exception { + BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(NoAtWebAppConfigWebTestCase.class); + given(testContext.getAttribute(ServletTestExecutionListener.ACTIVATE_LISTENER)).willReturn(true); + + RequestContextHolder.resetRequestAttributes(); + listener.beforeTestClass(testContext); + assertRequestAttributesDoNotExist(); + + assertWebAppConfigTestCase(); + } + + + private RequestAttributes assertRequestAttributesExist() { + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + assertNotNull("request attributes should exist", requestAttributes); + return requestAttributes; + } + + private void assertRequestAttributesDoNotExist() { + assertNull("request attributes should not exist", RequestContextHolder.getRequestAttributes()); + } + + private void assertSetUpOutsideOfStelAttributeExists() { + RequestAttributes requestAttributes = assertRequestAttributesExist(); + Object setUpOutsideOfStel = requestAttributes.getAttribute(SET_UP_OUTSIDE_OF_STEL, + RequestAttributes.SCOPE_REQUEST); + assertNotNull(SET_UP_OUTSIDE_OF_STEL + " should exist as a request attribute", setUpOutsideOfStel); + } + + private void assertSetUpOutsideOfStelAttributeDoesNotExist() { + RequestAttributes requestAttributes = assertRequestAttributesExist(); + Object setUpOutsideOfStel = requestAttributes.getAttribute(SET_UP_OUTSIDE_OF_STEL, + RequestAttributes.SCOPE_REQUEST); + assertNull(SET_UP_OUTSIDE_OF_STEL + " should NOT exist as a request attribute", setUpOutsideOfStel); + } + private void assertWebAppConfigTestCase() throws Exception { listener.prepareTestInstance(testContext); - assertAttributeDoesNotExist(); + assertRequestAttributesExist(); + assertSetUpOutsideOfStelAttributeDoesNotExist(); verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); given(testContext.getAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(Boolean.TRUE); given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(Boolean.TRUE); listener.beforeTestMethod(testContext); - assertAttributeDoesNotExist(); + assertRequestAttributesExist(); + assertSetUpOutsideOfStelAttributeDoesNotExist(); verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); listener.afterTestMethod(testContext); verify(testContext).removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); verify(testContext).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); - assertAttributesNotAvailable(); + assertRequestAttributesDoNotExist(); } @@ -200,4 +219,7 @@ public class ServletTestExecutionListenerTests { static class AtWebAppConfigWebTestCase { } + static class NoAtWebAppConfigWebTestCase { + } + } diff --git a/spring-test/src/test/java/org/springframework/test/context/web/WebAppConfigurationBootstrapWithTests.java b/spring-test/src/test/java/org/springframework/test/context/web/WebAppConfigurationBootstrapWithTests.java new file mode 100644 index 00000000..ad03bb81 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/web/WebAppConfigurationBootstrapWithTests.java @@ -0,0 +1,78 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.web; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.test.context.BootstrapWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.MergedContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfigurationBootstrapWithTests.CustomWebTestContextBootstrapper; +import org.springframework.web.context.WebApplicationContext; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * JUnit-based integration tests that verify support for loading a + * {@link WebApplicationContext} with a custom {@link WebTestContextBootstrapper}. + * + * @author Sam Brannen + * @author Phillip Webb + * @since 4.3 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration +@WebAppConfiguration +@BootstrapWith(CustomWebTestContextBootstrapper.class) +public class WebAppConfigurationBootstrapWithTests { + + @Autowired + WebApplicationContext wac; + + + @Test + public void webApplicationContextIsLoaded() { + // from: src/test/webapp/resources/Spring.js + Resource resource = wac.getResource("/resources/Spring.js"); + assertNotNull(resource); + assertTrue(resource.exists()); + } + + + @Configuration + static class Config { + } + + /** + * Custom {@link WebTestContextBootstrapper} that requires {@code @WebAppConfiguration} + * but hard codes the resource base path. + */ + static class CustomWebTestContextBootstrapper extends WebTestContextBootstrapper { + + @Override + protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) { + return new WebMergedContextConfiguration(mergedConfig, "src/test/webapp"); + } + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/web/socket/WebSocketServletServerContainerFactoryBeanTests.java b/spring-test/src/test/java/org/springframework/test/context/web/socket/WebSocketServletServerContainerFactoryBeanTests.java new file mode 100644 index 00000000..f35b87f9 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/web/socket/WebSocketServletServerContainerFactoryBeanTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.web.socket; + +import javax.websocket.server.ServerContainer; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean; + +import static org.junit.Assert.*; + +/** + * Integration tests that validate support for {@link ServletServerContainerFactoryBean} + * in conjunction with {@link WebAppConfiguration @WebAppConfiguration} and the + * Spring TestContext Framework. + * + * @author Sam Brannen + * @since 4.3.1 + */ +@RunWith(SpringRunner.class) +@WebAppConfiguration +public class WebSocketServletServerContainerFactoryBeanTests { + + @Autowired + ServerContainer serverContainer; + + + @Test + public void servletServerContainerFactoryBeanSupport() { + assertEquals(42, serverContainer.getDefaultMaxTextMessageBufferSize()); + } + + + @Configuration + @EnableWebSocket + static class WebSocketConfig { + + @Bean + ServletServerContainerFactoryBean createWebSocketContainer() { + ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); + container.setMaxTextMessageBufferSize(42); + return container; + } + } + +} |