diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/AbstractWebRequestMatcherTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/AbstractWebRequestMatcherTests.java new file mode 100644 index 00000000000..e3fae7c1bbe --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/AbstractWebRequestMatcherTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2015 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.web.servlet.htmlunit; + +import java.net.MalformedURLException; +import java.net.URL; + +import com.gargoylesoftware.htmlunit.WebRequest; + +import static org.junit.Assert.*; + +/** + * Abstract base class for testing {@link WebRequestMatcher} implementations. + * + * @author Sam Brannen + * @since 4.2 + */ +public class AbstractWebRequestMatcherTests { + + protected void assertMatches(WebRequestMatcher matcher, String url) throws MalformedURLException { + assertTrue(matcher.matches(new WebRequest(new URL(url)))); + } + + protected void assertDoesNotMatch(WebRequestMatcher matcher, String url) throws MalformedURLException { + assertFalse(matcher.matches(new WebRequest(new URL(url)))); + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java index eed3271f979..3c045e14b50 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java @@ -69,12 +69,14 @@ public class DelegatingWebConnectionTests { private WebRequest request; private WebResponse expectedResponse; + @Before public void setUp() throws Exception { request = new WebRequest(new URL("http://localhost/")); - WebResponseData data = new WebResponseData("".getBytes("UTF-8"),200, "", Collections.emptyList()); + WebResponseData data = new WebResponseData("".getBytes("UTF-8"), 200, "", Collections. emptyList()); expectedResponse = new WebResponse(data, request, 100L); - webConnection = new DelegatingWebConnection(defaultConnection, new DelegateWebConnection(matcher1,connection1), new DelegateWebConnection(matcher2,connection2)); + webConnection = new DelegatingWebConnection(defaultConnection, + new DelegateWebConnection(matcher1, connection1), new DelegateWebConnection(matcher2, connection2)); } @Test @@ -86,7 +88,7 @@ public class DelegatingWebConnectionTests { assertThat(response, sameInstance(expectedResponse)); verify(matcher1).matches(request); verify(matcher2).matches(request); - verifyNoMoreInteractions(connection1,connection2); + verifyNoMoreInteractions(connection1, connection2); verify(defaultConnection).getResponse(request); } @@ -100,7 +102,7 @@ public class DelegatingWebConnectionTests { assertThat(response, sameInstance(expectedResponse)); verify(matcher1).matches(request); - verifyNoMoreInteractions(matcher2,connection2,defaultConnection); + verifyNoMoreInteractions(matcher2, connection2, defaultConnection); verify(connection1).getResponse(request); } @@ -114,12 +116,12 @@ public class DelegatingWebConnectionTests { assertThat(response, sameInstance(expectedResponse)); verify(matcher1).matches(request); verify(matcher2).matches(request); - verifyNoMoreInteractions(connection1,defaultConnection); + verifyNoMoreInteractions(connection1, defaultConnection); verify(connection2).getResponse(request); } @Test - public void classlevelJavadoc() throws Exception { + public void verifyExampleInClassLevelJavadoc() throws Exception { WebClient webClient = new WebClient(); MockMvc mockMvc = MockMvcBuilders.standaloneSetup(TestController.class).build(); @@ -137,6 +139,7 @@ public class DelegatingWebConnectionTests { assertThat(page.getWebResponse().getContentAsString(), not(isEmptyString())); } + @Controller static class TestController {} diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java index 80b64c75131..8180898a03e 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java @@ -18,19 +18,17 @@ package org.springframework.test.web.servlet.htmlunit; import javax.servlet.http.HttpServletRequest; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; /** * @author Rob Winch * @since 4.2 */ -@Controller +@RestController public class HelloController { @RequestMapping - @ResponseBody public String header(HttpServletRequest request) { return "hello"; } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java index 5d69bd953c1..081b94d090f 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java @@ -16,66 +16,54 @@ package org.springframework.test.web.servlet.htmlunit; -import java.net.URL; - import org.junit.Test; -import com.gargoylesoftware.htmlunit.WebRequest; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; - /** + * Unit tests for {@link HostRequestMatcher}. + * * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ -public class HostRequestMatcherTests { +public class HostRequestMatcherTests extends AbstractWebRequestMatcherTests { @Test - public void localhostMatches() throws Exception { + public void localhost() throws Exception { WebRequestMatcher matcher = new HostRequestMatcher("localhost"); - - boolean matches = matcher.matches(new WebRequest(new URL("http://localhost/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(true));; - - matches = matcher.matches(new WebRequest(new URL("http://example.com/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(false));; + assertMatches(matcher, "http://localhost/jquery-1.11.0.min.js"); + assertDoesNotMatch(matcher, "http://example.com/jquery-1.11.0.min.js"); } @Test public void multipleHosts() throws Exception { - WebRequestMatcher matcher = new HostRequestMatcher("localhost","example.com"); - - boolean matches = matcher.matches(new WebRequest(new URL("http://localhost/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(true));; - - matches = matcher.matches(new WebRequest(new URL("http://example.com/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(true));; + WebRequestMatcher matcher = new HostRequestMatcher("localhost", "example.com"); + assertMatches(matcher, "http://localhost/jquery-1.11.0.min.js"); + assertMatches(matcher, "http://example.com/jquery-1.11.0.min.js"); } @Test public void specificPort() throws Exception { WebRequestMatcher matcher = new HostRequestMatcher("localhost:8080"); - - boolean matches = matcher.matches(new WebRequest(new URL("http://localhost:8080/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(true));; - - matches = matcher.matches(new WebRequest(new URL("http://localhost:9090/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(false));; + assertMatches(matcher, "http://localhost:8080/jquery-1.11.0.min.js"); + assertDoesNotMatch(matcher, "http://localhost:9090/jquery-1.11.0.min.js"); } @Test - public void defaultPortInMatcher() throws Exception { + public void defaultHttpPort() throws Exception { WebRequestMatcher matcher = new HostRequestMatcher("localhost:80"); + assertMatches(matcher, "http://localhost:80/jquery-1.11.0.min.js"); + assertMatches(matcher, "http://localhost/jquery-1.11.0.min.js"); + assertDoesNotMatch(matcher, "https://localhost/jquery-1.11.0.min.js"); + assertDoesNotMatch(matcher, "http://localhost:9090/jquery-1.11.0.min.js"); + } - boolean matches = matcher.matches(new WebRequest(new URL("http://localhost:80/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(true));; - - matches = matcher.matches(new WebRequest(new URL("http://localhost/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(true));; - - matches = matcher.matches(new WebRequest(new URL("http://localhost:9090/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(false));; + @Test + public void defaultHttpsPort() throws Exception { + WebRequestMatcher matcher = new HostRequestMatcher("localhost:443"); + assertMatches(matcher, "https://localhost:443/jquery-1.11.0.min.js"); + assertMatches(matcher, "https://localhost/jquery-1.11.0.min.js"); + assertDoesNotMatch(matcher, "http://localhost/jquery-1.11.0.min.js"); + assertDoesNotMatch(matcher, "https://localhost:9090/jquery-1.11.0.min.js"); } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java index a7810b65610..903477b25ff 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java @@ -31,8 +31,10 @@ import javax.servlet.http.HttpSession; import org.apache.commons.io.IOUtils; import org.apache.http.auth.UsernamePasswordCredentials; + import org.junit.Before; import org.junit.Test; + import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpSession; import org.springframework.mock.web.MockServletContext; @@ -50,31 +52,30 @@ import static org.junit.Assert.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; /** + * Unit tests for {@link HtmlUnitRequestBuilder}. + * * @author Rob Winch * @since 4.2 */ public class HtmlUnitRequestBuilderTests { - private WebRequest webRequest; + private final WebClient webClient = new WebClient(); - private ServletContext servletContext; + private final ServletContext servletContext = new MockServletContext(); - private Map sessions; + private final Map sessions = new HashMap<>(); - private WebClient webClient; + private WebRequest webRequest; private HtmlUnitRequestBuilder requestBuilder; @Before public void setUp() throws Exception { - sessions = new HashMap<>(); webRequest = new WebRequest(new URL("http://example.com:80/test/this/here")); webRequest.setHttpMethod(HttpMethod.GET); webRequest.setRequestParameters(new ArrayList<>()); - webClient = new WebClient(); requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest); - servletContext = new MockServletContext(); } // --- constructor @@ -740,7 +741,7 @@ public class HtmlUnitRequestBuilderTests { String cookieName = "PARENT"; String cookieValue = "VALUE"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) - .defaultRequest(get("/").cookie(new Cookie(cookieName,cookieValue))) + .defaultRequest(get("/").cookie(new Cookie(cookieName, cookieValue))) .build(); Cookie[] cookies = mockMvc.perform(requestBuilder).andReturn().getRequest().getCookies(); @@ -756,7 +757,7 @@ public class HtmlUnitRequestBuilderTests { String attrName = "PARENT"; String attrValue = "VALUE"; MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()) - .defaultRequest(get("/").requestAttr(attrName,attrValue)) + .defaultRequest(get("/").requestAttr(attrName, attrValue)) .build(); assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getAttribute(attrName), equalTo(attrValue)); @@ -770,8 +771,7 @@ public class HtmlUnitRequestBuilderTests { return; } String actual = jsessionidCookie.getValue(); - assertThat("JSESSIONID=" + actual + - "; Path=/test; Domain=example.com", equalTo(expected)); + assertThat("JSESSIONID=" + actual + "; Path=/test; Domain=example.com", equalTo(expected)); } private void setParameter(String name, String value) { diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java index 442c6072d13..c89f4f40f9b 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java @@ -18,12 +18,12 @@ package org.springframework.test.web.servlet.htmlunit; import java.io.IOException; import java.net.URL; - import javax.servlet.http.HttpServletRequest; import org.junit.Before; 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.test.context.ContextConfiguration; @@ -47,6 +47,8 @@ import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; /** + * Integration tests for {@link MockMvcWebConnectionBuilderSupport}. + * * @author Rob Winch * @since 4.2 */ @@ -56,19 +58,18 @@ import static org.mockito.Mockito.mock; @SuppressWarnings("rawtypes") public class MockMvcConnectionBuilderSupportTests { - @Autowired - WebApplicationContext context; + private final WebConnection delegateConnection = mock(WebConnection.class); - MockMvc mockMvc; + @Autowired + private WebApplicationContext wac; - WebConnection delegateConnection; + private MockMvc mockMvc; - WebConnection connection; + private WebConnection connection; @Before public void setup() { - delegateConnection = mock(WebConnection.class); - mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); + mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); connection = new MockMvcWebConnectionBuilderSupport(mockMvc){} .createConnection(delegateConnection); @@ -86,7 +87,7 @@ public class MockMvcConnectionBuilderSupportTests { @Test public void context() throws Exception { - connection = new MockMvcWebConnectionBuilderSupport(context){} + connection = new MockMvcWebConnectionBuilderSupport(wac) {} .createConnection(delegateConnection); assertMvcProcessed("http://localhost/"); @@ -101,7 +102,7 @@ public class MockMvcConnectionBuilderSupportTests { @Test public void mockMvcExampleDotCom() throws Exception { - connection = new MockMvcWebConnectionBuilderSupport(context){} + connection = new MockMvcWebConnectionBuilderSupport(wac) {} .useMockMvcForHosts("example.com") .createConnection(delegateConnection); @@ -112,7 +113,7 @@ public class MockMvcConnectionBuilderSupportTests { @Test public void mockMvcAlwaysUseMockMvc() throws Exception { - connection = new MockMvcWebConnectionBuilderSupport(context){} + connection = new MockMvcWebConnectionBuilderSupport(wac) {} .alwaysUseMockMvc() .createConnection(delegateConnection); @@ -121,18 +122,18 @@ public class MockMvcConnectionBuilderSupportTests { @Test public void defaultContextPathEmpty() throws Exception { - connection = new MockMvcWebConnectionBuilderSupport(context){} + connection = new MockMvcWebConnectionBuilderSupport(wac) {} .createConnection(delegateConnection); - assertThat(getWebResponse("http://localhost/abc").getContentAsString(), equalTo(""));; + assertThat(getWebResponse("http://localhost/abc").getContentAsString(), equalTo("")); } @Test public void defaultContextPathCustom() throws Exception { - connection = new MockMvcWebConnectionBuilderSupport(context) { - }.contextPath("/abc").createConnection(delegateConnection); + connection = new MockMvcWebConnectionBuilderSupport(wac) {} + .contextPath("/abc").createConnection(delegateConnection); - assertThat(getWebResponse("http://localhost/abc/def").getContentAsString(), equalTo("/abc"));; + assertThat(getWebResponse("http://localhost/abc/def").getContentAsString(), equalTo("/abc")); } private void assertMvcProcessed(String url) throws Exception { @@ -147,11 +148,14 @@ public class MockMvcConnectionBuilderSupportTests { return connection.getResponse(new WebRequest(new URL(url))); } + @Configuration @EnableWebMvc static class Config { + @RestController static class ContextPathController { + @RequestMapping public String contextPath(HttpServletRequest request) { return request.getContextPath(); diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java index 71ebb43e3de..27db6941b59 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java @@ -45,6 +45,8 @@ import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; /** + * Integration tests for {@link MockMvcWebClientBuilder}. + * * @author Rob Winch * @since 4.2 */ @@ -53,17 +55,17 @@ import static org.junit.Assert.assertThat; @WebAppConfiguration public class MockMvcWebClientBuilderTests { + private WebClient webClient = new WebClient(); + @Autowired - WebApplicationContext context; + private WebApplicationContext wac; - MockMvc mockMvc; + private MockMvc mockMvc; - WebClient webClient; @Before public void setup() { - mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); - webClient = new WebClient(); + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test(expected = IllegalArgumentException.class) @@ -77,19 +79,19 @@ public class MockMvcWebClientBuilderTests { } @Test - public void mockMvcSetupconfigureWebClient() throws Exception { - webClient = MockMvcWebClientBuilder - .mockMvcSetup(mockMvc) - .configureWebClient(webClient); + public void mockMvcSetupAndConfigureWebClient() throws Exception { + this.webClient = MockMvcWebClientBuilder + .mockMvcSetup(this.mockMvc) + .configureWebClient(this.webClient); assertMvcProcessed("http://localhost/test"); assertDelegateProcessed("http://example.com/"); } @Test - public void mockMvcSetupCreateWebClient() throws Exception { - webClient = MockMvcWebClientBuilder - .mockMvcSetup(mockMvc) + public void mockMvcSetupAndCreateWebClient() throws Exception { + this.webClient = MockMvcWebClientBuilder + .mockMvcSetup(this.mockMvc) .createWebClient(); assertMvcProcessed("http://localhost/test"); @@ -97,7 +99,7 @@ public class MockMvcWebClientBuilderTests { } private void assertMvcProcessed(String url) throws Exception { - assertThat(getWebResponse(url).getContentAsString(), equalTo("mvc"));; + assertThat(getWebResponse(url).getContentAsString(), equalTo("mvc")); } private void assertDelegateProcessed(String url) throws Exception { @@ -105,14 +107,17 @@ public class MockMvcWebClientBuilderTests { } private WebResponse getWebResponse(String url) throws IOException { - return webClient.getWebConnection().getResponse(new WebRequest(new URL(url))); + return this.webClient.getWebConnection().getResponse(new WebRequest(new URL(url))); } + @Configuration @EnableWebMvc static class Config { + @RestController static class ContextPathController { + @RequestMapping public String contextPath(HttpServletRequest request) { return "mvc"; diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java index c9b1386dae7..299553b2d3a 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java @@ -20,6 +20,7 @@ import java.io.IOException; import org.junit.Before; import org.junit.Test; + import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -30,68 +31,67 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; /** + * Integration tests for {@link MockMvcWebConnection}. + * * @author Rob Winch * @since 4.2 */ public class MockMvcWebConnectionTests { - MockMvc mockMvc; + private final WebClient webClient = new WebClient(); + + private MockMvc mockMvc; - WebClient webClient; @Before public void setup() { - mockMvc = MockMvcBuilders - .standaloneSetup(new HelloController(), new ForwardController()) - .build(); - - webClient = new WebClient(); + this.mockMvc = MockMvcBuilders.standaloneSetup(new HelloController(), new ForwardController()).build(); } @Test public void contextPathNull() throws IOException { - webClient.setWebConnection(new MockMvcWebConnection(mockMvc, null)); + this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, null)); - Page page = webClient.getPage("http://localhost/context/a"); + Page page = this.webClient.getPage("http://localhost/context/a"); - assertThat(page.getWebResponse().getStatusCode(), equalTo(200));; + assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); } @Test public void contextPathExplicit() throws IOException { - webClient.setWebConnection(new MockMvcWebConnection(mockMvc, "/context")); + this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, "/context")); - Page page = webClient.getPage("http://localhost/context/a"); + Page page = this.webClient.getPage("http://localhost/context/a"); - assertThat(page.getWebResponse().getStatusCode(), equalTo(200));; + assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); } @Test public void contextPathEmpty() throws IOException { - webClient.setWebConnection(new MockMvcWebConnection(mockMvc, "")); + this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, "")); - Page page = webClient.getPage("http://localhost/context/a"); + Page page = this.webClient.getPage("http://localhost/context/a"); - assertThat(page.getWebResponse().getStatusCode(), equalTo(200));; + assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); } @Test public void forward() throws IOException { - webClient.setWebConnection(new MockMvcWebConnection(mockMvc, "")); + this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, "")); - Page page = webClient.getPage("http://localhost/forward"); + Page page = this.webClient.getPage("http://localhost/forward"); - assertThat(page.getWebResponse().getContentAsString(), equalTo("hello"));; + assertThat(page.getWebResponse().getContentAsString(), equalTo("hello")); } @Test(expected = IllegalArgumentException.class) public void contextPathDoesNotStartWithSlash() throws IOException { - new MockMvcWebConnection(mockMvc, "context"); + new MockMvcWebConnection(this.mockMvc, "context"); } @Test(expected = IllegalArgumentException.class) public void contextPathEndsWithSlash() throws IOException { - new MockMvcWebConnection(mockMvc, "/context/"); + new MockMvcWebConnection(this.mockMvc, "/context/"); } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java index 770f1e06ee8..363718f00f0 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java @@ -1,14 +1,17 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2002-2015 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 + * 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. + * 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.web.servlet.htmlunit; @@ -28,34 +31,36 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; /** + * Tests for {@link MockWebResponseBuilder}. + * * @author Rob Winch * @since 4.2 */ public class MockWebResponseBuilderTests { - private WebRequest webRequest; + private final MockHttpServletResponse response = new MockHttpServletResponse(); - private MockHttpServletResponse httpServletResponse; + private WebRequest webRequest; private MockWebResponseBuilder responseBuilder; + @Before public void setUp() throws Exception { - webRequest = new WebRequest(new URL("http://example.com:80/test/this/here")); - httpServletResponse = new MockHttpServletResponse(); + this.webRequest = new WebRequest(new URL("http://example.com:80/test/this/here")); - responseBuilder = new MockWebResponseBuilder(System.currentTimeMillis(), webRequest, httpServletResponse); + this.responseBuilder = new MockWebResponseBuilder(System.currentTimeMillis(), this.webRequest, this.response); } // --- constructor @Test(expected = IllegalArgumentException.class) - public void constructorNullWebRequest() { - new MockWebResponseBuilder(0L, null, httpServletResponse); + public void constructorWithNullWebRequest() { + new MockWebResponseBuilder(0L, null, this.response); } @Test(expected = IllegalArgumentException.class) - public void constructorNullResponse() throws Exception { + public void constructorWithNullResponse() throws Exception { new MockWebResponseBuilder(0L, new WebRequest(new URL("http://example.com:80/test/this/here")), null); } @@ -63,76 +68,75 @@ public class MockWebResponseBuilderTests { @Test public void buildContent() throws Exception { - httpServletResponse.getWriter().write("expected content"); - - WebResponse webResponse = responseBuilder.build(); + this.response.getWriter().write("expected content"); + WebResponse webResponse = this.responseBuilder.build(); - assertThat(webResponse.getContentAsString(), equalTo("expected content"));; + assertThat(webResponse.getContentAsString(), equalTo("expected content")); } @Test public void buildContentCharset() throws Exception { - httpServletResponse.addHeader("Content-Type", "text/html; charset=UTF-8"); - WebResponse webResponse = responseBuilder.build(); + this.response.addHeader("Content-Type", "text/html; charset=UTF-8"); + WebResponse webResponse = this.responseBuilder.build(); - assertThat(webResponse.getContentCharset(), equalTo("UTF-8"));; + assertThat(webResponse.getContentCharset(), equalTo("UTF-8")); } @Test public void buildContentType() throws Exception { - httpServletResponse.addHeader("Content-Type", "text/html; charset-UTF-8"); - WebResponse webResponse = responseBuilder.build(); + this.response.addHeader("Content-Type", "text/html; charset-UTF-8"); + WebResponse webResponse = this.responseBuilder.build(); - assertThat(webResponse.getContentType(), equalTo("text/html"));; + assertThat(webResponse.getContentType(), equalTo("text/html")); } @Test public void buildResponseHeaders() throws Exception { - httpServletResponse.addHeader("Content-Type", "text/html"); - httpServletResponse.addHeader("X-Test", "value"); - WebResponse webResponse = responseBuilder.build(); + this.response.addHeader("Content-Type", "text/html"); + this.response.addHeader("X-Test", "value"); + WebResponse webResponse = this.responseBuilder.build(); List responseHeaders = webResponse.getResponseHeaders(); - assertThat(responseHeaders.size(), equalTo(2));; + assertThat(responseHeaders.size(), equalTo(2)); NameValuePair header = responseHeaders.get(0); - assertThat(header.getName(), equalTo("Content-Type"));; - assertThat(header.getValue(), equalTo("text/html"));; + assertThat(header.getName(), equalTo("Content-Type")); + assertThat(header.getValue(), equalTo("text/html")); header = responseHeaders.get(1); - assertThat(header.getName(), equalTo("X-Test"));; - assertThat(header.getValue(), equalTo("value"));; + assertThat(header.getName(), equalTo("X-Test")); + assertThat(header.getValue(), equalTo("value")); } @Test public void buildStatus() throws Exception { - WebResponse webResponse = responseBuilder.build(); + WebResponse webResponse = this.responseBuilder.build(); - assertThat(webResponse.getStatusCode(), equalTo(200));; - assertThat(webResponse.getStatusMessage(), equalTo("OK"));; + assertThat(webResponse.getStatusCode(), equalTo(200)); + assertThat(webResponse.getStatusMessage(), equalTo("OK")); } @Test public void buildStatusNotOk() throws Exception { - httpServletResponse.setStatus(401); - WebResponse webResponse = responseBuilder.build(); + this.response.setStatus(401); + WebResponse webResponse = this.responseBuilder.build(); - assertThat(webResponse.getStatusCode(), equalTo(401));; - assertThat(webResponse.getStatusMessage(), equalTo("Unauthorized"));; + assertThat(webResponse.getStatusCode(), equalTo(401)); + assertThat(webResponse.getStatusMessage(), equalTo("Unauthorized")); } @Test - public void buildStatusCustomMessage() throws Exception { - httpServletResponse.sendError(401, "Custom"); - WebResponse webResponse = responseBuilder.build(); + public void buildStatusWithCustomMessage() throws Exception { + this.response.sendError(401, "Custom"); + WebResponse webResponse = this.responseBuilder.build(); - assertThat(webResponse.getStatusCode(), equalTo(401));; - assertThat(webResponse.getStatusMessage(), equalTo("Custom"));; + assertThat(webResponse.getStatusCode(), equalTo(401)); + assertThat(webResponse.getStatusMessage(), equalTo("Custom")); } @Test public void buildWebRequest() throws Exception { - WebResponse webResponse = responseBuilder.build(); + WebResponse webResponse = this.responseBuilder.build(); - assertThat(webResponse.getWebRequest(), equalTo(webRequest));; + assertThat(webResponse.getWebRequest(), equalTo(this.webRequest)); } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java index 76c9a5e757a..2baab695217 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java @@ -16,30 +16,22 @@ package org.springframework.test.web.servlet.htmlunit; -import java.net.URL; - import org.junit.Test; -import com.gargoylesoftware.htmlunit.WebRequest; - -import static org.hamcrest.MatcherAssert.*; -import static org.hamcrest.Matchers.*; - /** + * Unit tests for {@link UrlRegexRequestMatcher}. + * * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ -public class UrlRegexRequestMatcherTests { +public class UrlRegexRequestMatcherTests extends AbstractWebRequestMatcherTests { @Test - public void classlevelJavadoc() throws Exception { + public void verifyExampleInClassLevelJavadoc() throws Exception { WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*"); - - boolean matches = cdnMatcher.matches(new WebRequest(new URL("http://code.jquery.com/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(true)); - - matches = cdnMatcher.matches(new WebRequest(new URL("http://localhost/jquery-1.11.0.min.js"))); - assertThat(matches, equalTo(false)); + assertMatches(cdnMatcher, "http://code.jquery.com/jquery-1.11.0.min.js"); + assertDoesNotMatch(cdnMatcher, "http://localhost/jquery-1.11.0.min.js"); } } \ No newline at end of file diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java index 42c3e265923..a64fc677081 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java @@ -17,13 +17,13 @@ package org.springframework.test.web.servlet.htmlunit.webdriver; import java.io.IOException; + import javax.servlet.http.HttpServletRequest; -import static org.hamcrest.CoreMatchers.*; -import static org.hamcrest.MatcherAssert.assertThat; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; + import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.springframework.beans.factory.annotation.Autowired; @@ -38,8 +38,14 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + /** + * Integration tests for {@link MockMvcHtmlUnitDriverBuilder}. + * * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ @RunWith(SpringJUnit4ClassRunner.class) @@ -47,18 +53,18 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc; @WebAppConfiguration public class MockMvcHtmlUnitDriverBuilderTests { - public static final String EXPECTED_BODY = "MockMvcHtmlUnitDriverBuilderTests mvc"; + private static final String EXPECTED_BODY = "MockMvcHtmlUnitDriverBuilderTests mvc"; @Autowired - WebApplicationContext context; + private WebApplicationContext wac; - MockMvc mockMvc; + private MockMvc mockMvc; - HtmlUnitDriver driver; + private HtmlUnitDriver driver; @Before public void setup() { - mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test(expected = IllegalArgumentException.class) @@ -72,9 +78,9 @@ public class MockMvcHtmlUnitDriverBuilderTests { } @Test - public void mockMvcSetupConfigureDriver() throws Exception { - driver = MockMvcHtmlUnitDriverBuilder - .mockMvcSetup(mockMvc) + public void mockMvcSetupAndConfigureDriver() throws Exception { + this.driver = MockMvcHtmlUnitDriverBuilder + .mockMvcSetup(this.mockMvc) .configureDriver(new WebConnectionHtmlUnitDriver()); assertMvcProcessed("http://localhost/test"); @@ -82,9 +88,9 @@ public class MockMvcHtmlUnitDriverBuilderTests { } @Test - public void mockMvcSetupCreateDriver() throws Exception { - driver = MockMvcHtmlUnitDriverBuilder - .mockMvcSetup(mockMvc) + public void mockMvcSetupAndCreateDriver() throws Exception { + this.driver = MockMvcHtmlUnitDriverBuilder + .mockMvcSetup(this.mockMvc) .createDriver(); assertMvcProcessed("http://localhost/test"); @@ -92,22 +98,22 @@ public class MockMvcHtmlUnitDriverBuilderTests { } @Test - public void javascriptEnabledDefaultEnabled() { - driver = MockMvcHtmlUnitDriverBuilder - .mockMvcSetup(mockMvc) + public void javaScriptEnabledByDefault() { + this.driver = MockMvcHtmlUnitDriverBuilder + .mockMvcSetup(this.mockMvc) .createDriver(); - assertThat(driver.isJavascriptEnabled(), equalTo(true)); + assertTrue(this.driver.isJavascriptEnabled()); } @Test - public void javascriptEnabledDisabled() { - driver = MockMvcHtmlUnitDriverBuilder - .mockMvcSetup(mockMvc) + public void javaScriptDisabled() { + this.driver = MockMvcHtmlUnitDriverBuilder + .mockMvcSetup(this.mockMvc) .javascriptEnabled(false) .createDriver(); - assertThat(driver.isJavascriptEnabled(), equalTo(false)); + assertFalse(this.driver.isJavascriptEnabled()); } private void assertMvcProcessed(String url) throws Exception { @@ -119,15 +125,18 @@ public class MockMvcHtmlUnitDriverBuilderTests { } private String get(String url) throws IOException { - driver.get(url); - return driver.getPageSource(); + this.driver.get(url); + return this.driver.getPageSource(); } + @Configuration @EnableWebMvc static class Config { + @RestController static class ContextPathController { + @RequestMapping public String contextPath(HttpServletRequest request) { return EXPECTED_BODY; diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java index 627a533f113..45118ce49e6 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java @@ -17,7 +17,9 @@ package org.springframework.test.web.servlet.htmlunit.webdriver; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -29,48 +31,51 @@ import com.gargoylesoftware.htmlunit.WebRequest; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.fail; + import static org.mockito.Matchers.any; import static org.mockito.Mockito.when; /** + * Unit tests for {@link WebConnectionHtmlUnitDriver}. + * * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ @RunWith(MockitoJUnitRunner.class) public class WebConnectionHtmlUnitDriverTests { + private final WebConnectionHtmlUnitDriver driver = new WebConnectionHtmlUnitDriver(); + @Mock - WebConnection connection; + private WebConnection connection; - WebConnectionHtmlUnitDriver driver; + @Rule + public ExpectedException exception = ExpectedException.none(); @Before public void setup() throws Exception { - driver = new WebConnectionHtmlUnitDriver(); - - when(connection.getResponse(any(WebRequest.class))).thenThrow(new InternalError("")); + when(this.connection.getResponse(any(WebRequest.class))).thenThrow(new InternalError("")); } @Test public void getWebConnectionDefaultNotNull() { - assertThat(driver.getWebConnection(), notNullValue()); + assertThat(this.driver.getWebConnection(), notNullValue()); } @Test - public void setWebConnection() { - driver.setWebConnection(connection); - - assertThat(driver.getWebConnection(), equalTo(connection)); - try { - driver.get("https://example.com"); - fail("Expected Exception"); - } catch (InternalError success) {} + public void setWebConnectionToNull() { + this.exception.expect(IllegalArgumentException.class); + this.driver.setWebConnection(null); } - @Test(expected = IllegalArgumentException.class) - public void setWebConnectionNull() { - driver.setWebConnection(null); + @Test + public void setWebConnection() { + this.driver.setWebConnection(this.connection); + assertThat(this.driver.getWebConnection(), equalTo(this.connection)); + + this.exception.expect(InternalError.class); + this.driver.get("https://example.com"); } } \ No newline at end of file