From 4e1af7d19518a783cf9a063e231b2991ca7ca1a6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 25 Mar 2015 00:04:47 +0100 Subject: [PATCH] Deprecated MatcherAssertionErrors in favor of the original org.hamcrest.MatcherAssert (v1.3) As of JUnit 4.9 / Hamcrest 1.3, there is no real need for a custom copy of that class anymore. --- .../test/util/JsonPathExpectationsHelper.java | 4 +-- .../test/util/MatcherAssertionErrors.java | 36 +++++-------------- .../test/util/XmlExpectationsHelper.java | 4 +-- .../test/util/XpathExpectationsHelper.java | 19 ++++------ .../client/match/ContentRequestMatchers.java | 5 +-- .../client/match/MockRestRequestMatchers.java | 22 +++--------- .../servlet/result/ContentResultMatchers.java | 11 ++---- .../servlet/result/CookieResultMatchers.java | 7 ++-- .../result/FlashAttributeResultMatchers.java | 6 ++-- .../servlet/result/HandlerResultMatchers.java | 9 ++--- .../servlet/result/HeaderResultMatchers.java | 10 ++---- .../result/JsonPathResultMatchers.java | 11 +++--- .../servlet/result/MockMvcResultHandlers.java | 13 +++---- .../servlet/result/MockMvcResultMatchers.java | 15 ++------ .../servlet/result/ModelResultMatchers.java | 5 +-- .../servlet/result/PrintingResultHandler.java | 26 +++++++++----- .../servlet/result/RequestResultMatchers.java | 6 ++-- .../servlet/result/StatusResultMatchers.java | 7 ++-- .../servlet/result/ViewResultMatchers.java | 9 +++-- .../servlet/result/XpathResultMatchers.java | 5 +-- ...ConditionalDelegatingFilterProxyTests.java | 21 ++++++++--- 21 files changed, 107 insertions(+), 144 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java index 827a3287ae..2364b13f9e 100644 --- a/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -28,8 +28,8 @@ import org.hamcrest.Matcher; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * A helper class for applying assertions via JSON path expressions. diff --git a/spring-test/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java b/spring-test/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java index 22bab2ede1..6301c7bb71 100644 --- a/spring-test/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java +++ b/spring-test/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -13,17 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.test.util; -import java.lang.reflect.Method; +package org.springframework.test.util; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.StringDescription; -import org.springframework.util.ClassUtils; -import org.springframework.util.ReflectionUtils; - /** * A replacement of {@link org.hamcrest.MatcherAssert} that removes the need to * depend on "hamcrest-all" when using Hamcrest 1.1 and also maintains backward @@ -32,19 +28,14 @@ import org.springframework.util.ReflectionUtils; * @author Rossen Stoyanchev * @author Sam Brannen * @since 3.2 + * @deprecated as of Spring 4.2, in favor of the original + * {@link org.hamcrest.MatcherAssert} class with JUnit 4.9 / Hamcrest 1.3 */ +@Deprecated public abstract class MatcherAssertionErrors { - private static final Method describeMismatchMethod = - ClassUtils.getMethodIfAvailable(Matcher.class, "describeMismatch", Object.class, Description.class); - - - private MatcherAssertionErrors() { - } - /** - * Asserts that the given matcher matches the actual value. - * + * Assert that the given matcher matches the actual value. * @param the static type accepted by the matcher * @param actual the value to match against * @param matcher the matcher @@ -54,8 +45,7 @@ public abstract class MatcherAssertionErrors { } /** - * Asserts that the given matcher matches the actual value. - * + * Assert that the given matcher matches the actual value. * @param the static type accepted by the matcher * @param reason additional information about the error * @param actual the value to match against @@ -67,16 +57,8 @@ public abstract class MatcherAssertionErrors { description.appendText(reason); description.appendText("\nExpected: "); description.appendDescriptionOf(matcher); - if (describeMismatchMethod != null) { - description.appendText("\n but: "); - // matcher.describeMismatch(actual, description); - ReflectionUtils.invokeMethod(describeMismatchMethod, matcher, actual, description); - } - else { - description.appendText("\n got: "); - description.appendValue(actual); - description.appendText("\n"); - } + description.appendText("\n but: "); + matcher.describeMismatch(actual, description); throw new AssertionError(description.toString()); } } diff --git a/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java index 8806a430ee..11fea6225e 100644 --- a/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 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. @@ -30,7 +30,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.InputSource; -import static org.springframework.test.util.MatcherAssertionErrors.*; +import static org.hamcrest.MatcherAssert.*; /** * A helper class for assertions on XML content. diff --git a/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java index f7fac1a17d..f506a9accb 100644 --- a/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -37,8 +37,8 @@ import org.xml.sax.InputSource; import org.springframework.util.CollectionUtils; import org.springframework.util.xml.SimpleNamespaceContext; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * A helper class for applying assertions via XPath expressions. @@ -56,8 +56,7 @@ public class XpathExpectationsHelper { /** - * Class constructor. - * + * XpathExpectationsHelper constructor. * @param expression the XPath expression * @param namespaces XML namespaces referenced in the XPath expression, or {@code null} * @param args arguments to parameterize the XPath expression with using the @@ -72,6 +71,7 @@ public class XpathExpectationsHelper { this.hasNamespaces = !CollectionUtils.isEmpty(namespaces); } + private XPathExpression compileXpathExpression(String expression, Map namespaces) throws XPathExpressionException { @@ -83,7 +83,7 @@ public class XpathExpectationsHelper { } /** - * @return the compiled XPath expression. + * Return the compiled XPath expression. */ protected XPathExpression getXpathExpression() { return this.xpathExpression; @@ -101,7 +101,6 @@ public class XpathExpectationsHelper { /** * Parse the given XML content to a {@link Document}. - * * @param xml the content to parse * @return the parsed document * @throws Exception in case of errors @@ -111,8 +110,7 @@ public class XpathExpectationsHelper { factory.setNamespaceAware(this.hasNamespaces); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xml)); - Document document = documentBuilder.parse(inputSource); - return document; + return documentBuilder.parse(inputSource); } /** @@ -149,7 +147,6 @@ public class XpathExpectationsHelper { /** * Apply the XPath expression and assert the resulting content with the * given Hamcrest matcher. - * * @throws Exception if content parsing or expression evaluation fails */ public void assertNodeCount(String content, Matcher matcher) throws Exception { @@ -171,7 +168,6 @@ public class XpathExpectationsHelper { /** * Apply the XPath expression and assert the resulting content with the * given Hamcrest matcher. - * * @throws Exception if content parsing or expression evaluation fails */ public void assertString(String content, Matcher matcher) throws Exception { @@ -193,7 +189,6 @@ public class XpathExpectationsHelper { /** * Apply the XPath expression and assert the resulting content with the * given Hamcrest matcher. - * * @throws Exception if content parsing or expression evaluation fails */ public void assertNumber(String content, Matcher matcher) throws Exception { @@ -222,4 +217,4 @@ public class XpathExpectationsHelper { assertEquals("XPath " + this.expression, expectedValue, Boolean.parseBoolean(actual)); } -} \ No newline at end of file +} diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java index 2f506e9b24..4acbf82e81 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.client.match; import java.io.IOException; @@ -28,8 +29,8 @@ import org.springframework.mock.http.client.MockClientHttpRequest; import org.springframework.test.util.XmlExpectationsHelper; import org.springframework.test.web.client.RequestMatcher; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for request content {@code RequestMatcher}'s. An instance of this diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java index 82431c1b9a..5a80c93859 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.client.match; import java.io.IOException; @@ -31,14 +32,14 @@ import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.test.web.client.RequestMatcher; import org.springframework.util.Assert; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Static, factory methods for {@link RequestMatcher} classes. Typically used to * provide input for {@link MockRestServiceServer#expect(RequestMatcher)}. * - *

Eclipse users: consider adding this class as a Java editor + *

Eclipse users: Consider adding this class as a Java editor * favorite. To navigate, open the Preferences and type "favorites". * * @author Craig Walls @@ -47,13 +48,6 @@ import static org.springframework.test.util.MatcherAssertionErrors.*; */ public abstract class MockRestRequestMatchers { - - /** - * Private class constructor. - */ - private MockRestRequestMatchers() { - } - /** * Match to any request. */ @@ -67,7 +61,6 @@ public abstract class MockRestRequestMatchers { /** * Assert the request URI string with the given matcher. - * * @param matcher String matcher for the expected URI * @return the request matcher */ @@ -83,7 +76,6 @@ public abstract class MockRestRequestMatchers { /** * Assert the request URI string. - * * @param expectedUri the expected URI * @return the request matcher */ @@ -99,7 +91,6 @@ public abstract class MockRestRequestMatchers { /** * Assert the {@link HttpMethod} of the request. - * * @param method the HTTP method * @return the request matcher */ @@ -115,7 +106,6 @@ public abstract class MockRestRequestMatchers { /** * Expect a request to the given URI. - * * @param uri the expected URI * @return the request matcher */ @@ -181,7 +171,6 @@ public abstract class MockRestRequestMatchers { * inspect a specific subset of the body. The JSON path expression can be a * parameterized string using formatting specifiers as defined in * {@link String#format(String, Object...)}. - * * @param expression the JSON path optionally parameterized with arguments * @param args arguments to parameterize the JSON path expression with */ @@ -194,7 +183,6 @@ public abstract class MockRestRequestMatchers { * href="http://goessner.net/articles/JsonPath/">JSONPath expression to * inspect a specific subset of the body and a Hamcrest match for asserting * the value found at the JSON path. - * * @param expression the JSON path expression * @param matcher a matcher for the value expected at the JSON path */ @@ -207,7 +195,6 @@ public abstract class MockRestRequestMatchers { * subset of the body. The XPath expression can be a parameterized string * using formatting specifiers as defined in * {@link String#format(String, Object...)}. - * * @param expression the XPath optionally parameterized with arguments * @param args arguments to parameterize the XPath expression with */ @@ -220,7 +207,6 @@ public abstract class MockRestRequestMatchers { * subset of the body. The XPath expression can be a parameterized string * using formatting specifiers as defined in * {@link String#format(String, Object...)}. - * * @param expression the XPath optionally parameterized with arguments * @param namespaces namespaces referenced in the XPath expression * @param args arguments to parameterize the XPath expression with diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java index d1d1084d07..123802c0ed 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -30,8 +30,8 @@ import org.springframework.test.util.XmlExpectationsHelper; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for response content assertions. An instance of this class is @@ -56,6 +56,7 @@ public class ContentResultMatchers { this.jsonHelper = new JsonExpectationsHelper(); } + /** * Assert the ServletResponse content type. The given content type must * fully match including type, sub-type, and parameters. For checking @@ -165,10 +166,8 @@ public class ContentResultMatchers { * Parse the response content and the given string as XML and assert the two * are "similar" - i.e. they contain the same elements and attributes * regardless of order. - * *

Use of this matcher requires the XMLUnit library. - * * @param xmlContent the expected XML content * @see MockMvcResultMatchers#xpath(String, Object...) * @see MockMvcResultMatchers#xpath(String, Map, Object...) @@ -200,7 +199,6 @@ public class ContentResultMatchers { /** * Parse the response content as {@link DOMSource} and apply the given * Hamcrest {@link Matcher}. - * * @see xml-matchers */ public ResultMatcher source(final Matcher matcher) { @@ -217,16 +215,13 @@ public class ContentResultMatchers { * Parse the response content and the given string as JSON and assert the two * are "similar" - i.e. they contain the same attribute-value pairs * regardless of order and formatting. - * *

Use of this matcher requires the JSONassert library. - * * @param jsonContent the expected JSON content * @since 4.1 */ public ResultMatcher json(final String jsonContent) { return new ResultMatcher() { - @Override public void match(MvcResult result) throws Exception { String content = result.getResponse().getContentAsString(); diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java index 9483c40ddf..ef17a562b2 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -23,8 +23,8 @@ import org.hamcrest.Matcher; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for response cookie assertions. An instance of this class is @@ -36,7 +36,6 @@ import static org.springframework.test.util.MatcherAssertionErrors.*; */ public class CookieResultMatchers { - /** * Protected constructor. * Use {@link MockMvcResultMatchers#cookie()}. @@ -44,6 +43,7 @@ public class CookieResultMatchers { protected CookieResultMatchers() { } + /** * Assert a cookie value with the given Hamcrest {@link Matcher}. */ @@ -241,4 +241,5 @@ public class CookieResultMatchers { } }; } + } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java index 91a048c6f9..3fca6b9518 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -21,8 +21,8 @@ import org.hamcrest.Matcher; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for "output" flash attribute assertions. An instance of this class is @@ -33,7 +33,6 @@ import static org.springframework.test.util.MatcherAssertionErrors.*; */ public class FlashAttributeResultMatchers { - /** * Protected constructor. * Use {@link MockMvcResultMatchers#flash()}. @@ -41,6 +40,7 @@ public class FlashAttributeResultMatchers { protected FlashAttributeResultMatchers() { } + /** * Assert a flash attribute's value with the given Hamcrest {@link Matcher}. */ diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java index cd24cd51b5..2095dc9a5f 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -27,8 +27,8 @@ import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for assertions on the selected handler. An instance of this class is @@ -39,7 +39,6 @@ import static org.springframework.test.util.MatcherAssertionErrors.*; */ public class HandlerResultMatchers { - /** * Protected constructor. * Use {@link MockMvcResultMatchers#handler()}. @@ -47,6 +46,7 @@ public class HandlerResultMatchers { protected HandlerResultMatchers() { } + /** * Assert the type of the handler that processed the request. */ @@ -68,7 +68,6 @@ public class HandlerResultMatchers { /** * Assert the name of the controller method that processed the request with * the given Hamcrest {@link Matcher}. - * *

Use of this method implies annotated controllers are processed with * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. */ @@ -84,7 +83,6 @@ public class HandlerResultMatchers { /** * Assert the name of the controller method that processed the request. - * *

Use of this method implies annotated controllers are processed with * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. */ @@ -100,7 +98,6 @@ public class HandlerResultMatchers { /** * Assert the controller method that processed the request. - * *

Use of this method implies annotated controllers are processed with * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. */ diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java index 38aada1b09..8eeb9bbe56 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 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. @@ -21,8 +21,8 @@ import org.hamcrest.Matcher; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for response header assertions. An instance of this @@ -34,7 +34,6 @@ import static org.springframework.test.util.MatcherAssertionErrors.*; */ public class HeaderResultMatchers { - /** * Protected constructor. * Use {@link MockMvcResultMatchers#header()}. @@ -48,7 +47,6 @@ public class HeaderResultMatchers { */ public ResultMatcher string(final String name, final Matcher matcher) { return new ResultMatcher() { - @Override public void match(MvcResult result) { assertThat("Response header " + name, result.getResponse().getHeader(name), matcher); @@ -61,7 +59,6 @@ public class HeaderResultMatchers { */ public ResultMatcher string(final String name, final String value) { return new ResultMatcher() { - @Override public void match(MvcResult result) { assertEquals("Response header " + name, value, result.getResponse().getHeader(name)); @@ -75,7 +72,6 @@ public class HeaderResultMatchers { */ public ResultMatcher doesNotExist(final String name) { return new ResultMatcher() { - @Override public void match(MvcResult result) { assertTrue("Response should not contain header " + name, !result.getResponse().containsHeader(name)); @@ -85,14 +81,12 @@ public class HeaderResultMatchers { /** * Assert the primary value of the named response header as a {@code long}. - * *

The {@link ResultMatcher} returned by this method throws an {@link AssertionError} * if the response does not contain the specified header, or if the supplied * {@code value} does not match the primary value. */ public ResultMatcher longValue(final String name, final long value) { return new ResultMatcher() { - @Override public void match(MvcResult result) { assertTrue("Response does not contain header " + name, result.getResponse().containsHeader(name)); diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/JsonPathResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/JsonPathResultMatchers.java index 5e6459c4fb..d18719790c 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/JsonPathResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/JsonPathResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -23,8 +23,8 @@ import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; /** - * Factory for assertions on the response content using JSONPath expressions. + * Factory for assertions on the response content using + * JSONPath expressions. * An instance of this class is typically accessed via * {@link MockMvcResultMatchers#jsonPath}. * @@ -33,7 +33,8 @@ import org.springframework.test.web.servlet.ResultMatcher; */ public class JsonPathResultMatchers { - private JsonPathExpectationsHelper jsonPathHelper; + private final JsonPathExpectationsHelper jsonPathHelper; + /** * Protected constructor. Use @@ -44,6 +45,7 @@ public class JsonPathResultMatchers { this.jsonPathHelper = new JsonPathExpectationsHelper(expression, args); } + /** * Evaluate the JSONPath and assert the value of the content found with the * given Hamcrest {@code Matcher}. @@ -108,4 +110,5 @@ public class JsonPathResultMatchers { } }; } + } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java index cfe96ca518..beb832ae94 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -31,10 +31,6 @@ import org.springframework.util.CollectionUtils; */ public abstract class MockMvcResultHandlers { - - private MockMvcResultHandlers() { - } - /** * Print {@link MvcResult} details to the "standard" output stream. */ @@ -43,18 +39,18 @@ public abstract class MockMvcResultHandlers { } - /** An {@link PrintingResultHandler} that writes to the "standard" output stream */ + /** + * An {@link PrintingResultHandler} that writes to the "standard" output stream + */ private static class ConsolePrintingResultHandler extends PrintingResultHandler { public ConsolePrintingResultHandler() { super(new ResultValuePrinter() { - @Override public void printHeading(String heading) { System.out.println(); System.out.println(String.format("%20s:", heading)); } - @Override public void printValue(String label, Object value) { if (value != null && value.getClass().isArray()) { @@ -65,4 +61,5 @@ public abstract class MockMvcResultHandlers { }); } } + } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java index eea428dd99..c647f6595c 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 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. @@ -42,9 +42,6 @@ public abstract class MockMvcResultMatchers { private static final AntPathMatcher pathMatcher = new AntPathMatcher(); - private MockMvcResultMatchers() { - } - /** * Access to request-related assertions. */ @@ -87,7 +84,6 @@ public abstract class MockMvcResultMatchers { */ public static ResultMatcher forwardedUrl(final String expectedUrl) { return new ResultMatcher() { - @Override public void match(MvcResult result) { assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl()); @@ -99,12 +95,11 @@ public abstract class MockMvcResultMatchers { * Asserts the request was forwarded to the given URL. * This methods accepts {@link org.springframework.util.AntPathMatcher} expressions. * @param urlPattern an AntPath expression to match against - * @see org.springframework.util.AntPathMatcher * @since 4.0 + * @see org.springframework.util.AntPathMatcher */ public static ResultMatcher forwardedUrlPattern(final String urlPattern) { return new ResultMatcher() { - @Override public void match(MvcResult result) { assertTrue("AntPath expression", pathMatcher.isPattern(urlPattern)); @@ -121,7 +116,6 @@ public abstract class MockMvcResultMatchers { */ public static ResultMatcher redirectedUrl(final String expectedUrl) { return new ResultMatcher() { - @Override public void match(MvcResult result) { assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl()); @@ -138,7 +132,6 @@ public abstract class MockMvcResultMatchers { */ public static ResultMatcher redirectedUrlPattern(final String expectedUrl) { return new ResultMatcher() { - @Override public void match(MvcResult result) { assertTrue("AntPath expression",pathMatcher.isPattern(expectedUrl)); @@ -175,7 +168,6 @@ public abstract class MockMvcResultMatchers { * inspect a specific subset of the body. The JSON path expression can be a * parameterized string using formatting specifiers as defined in * {@link String#format(String, Object...)}. - * * @param expression the JSON path optionally parameterized with arguments * @param args arguments to parameterize the JSON path expression with */ @@ -188,7 +180,6 @@ public abstract class MockMvcResultMatchers { * href="http://goessner.net/articles/JsonPath/">JSONPath expression to * inspect a specific subset of the body and a Hamcrest match for asserting * the value found at the JSON path. - * * @param expression the JSON path expression * @param matcher a matcher for the value expected at the JSON path */ @@ -201,7 +192,6 @@ public abstract class MockMvcResultMatchers { * subset of the body. The XPath expression can be a parameterized string * using formatting specifiers as defined in * {@link String#format(String, Object...)}. - * * @param expression the XPath optionally parameterized with arguments * @param args arguments to parameterize the XPath expression with */ @@ -214,7 +204,6 @@ public abstract class MockMvcResultMatchers { * subset of the body. The XPath expression can be a parameterized string * using formatting specifiers as defined in * {@link String#format(String, Object...)}. - * * @param expression the XPath optionally parameterized with arguments * @param namespaces namespaces referenced in the XPath expression * @param args arguments to parameterize the XPath expression with diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java index d9da8934ca..a454f5b4ee 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -25,8 +25,8 @@ import org.springframework.validation.BindingResult; import org.springframework.validation.Errors; import org.springframework.web.servlet.ModelAndView; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for assertions on the model. An instance of this class is @@ -44,6 +44,7 @@ public class ModelResultMatchers { protected ModelResultMatchers() { } + /** * Assert a model attribute value with the given Hamcrest {@link Matcher}. */ diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java index 214600310d..9dba5b1c47 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java @@ -18,7 +18,6 @@ package org.springframework.test.web.servlet.result; import java.util.Enumeration; import java.util.Map; - import javax.servlet.http.HttpServletRequest; import org.springframework.http.HttpHeaders; @@ -69,7 +68,6 @@ public class PrintingResultHandler implements ResultHandler { */ @Override public final void handle(MvcResult result) throws Exception { - this.printer.printHeading("MockHttpServletRequest"); printRequest(result.getRequest()); @@ -92,7 +90,9 @@ public class PrintingResultHandler implements ResultHandler { printResponse(result.getResponse()); } - /** Print the request */ + /** + * Print the request. + */ protected void printRequest(MockHttpServletRequest request) throws Exception { this.printer.printValue("HTTP Method", request.getMethod()); this.printer.printValue("Request URI", request.getRequestURI()); @@ -139,7 +139,9 @@ public class PrintingResultHandler implements ResultHandler { this.printer.printValue("Async result", asyncResult); } - /** Print the handler */ + /** + * Print the handler. + */ protected void printHandler(Object handler, HandlerInterceptor[] interceptors) throws Exception { if (handler == null) { this.printer.printValue("Type", null); @@ -156,7 +158,9 @@ public class PrintingResultHandler implements ResultHandler { } } - /** Print exceptions resolved through a HandlerExceptionResolver */ + /** + * Print exceptions resolved through a HandlerExceptionResolver. + */ protected void printResolvedException(Exception resolvedException) throws Exception { if (resolvedException == null) { this.printer.printValue("Type", null); @@ -166,7 +170,9 @@ public class PrintingResultHandler implements ResultHandler { } } - /** Print the ModelAndView */ + /** + * Print the ModelAndView. + */ protected void printModelAndView(ModelAndView mav) throws Exception { this.printer.printValue("View name", (mav != null) ? mav.getViewName() : null); this.printer.printValue("View", (mav != null) ? mav.getView() : null); @@ -188,7 +194,9 @@ public class PrintingResultHandler implements ResultHandler { } } - /** Print "output" flash attributes */ + /** + * Print "output" flash attributes. + */ protected void printFlashMap(FlashMap flashMap) throws Exception { if (flashMap == null) { this.printer.printValue("Attributes", null); @@ -201,7 +209,9 @@ public class PrintingResultHandler implements ResultHandler { } } - /** Print the response */ + /** + * Print the response. + */ protected void printResponse(MockHttpServletResponse response) throws Exception { this.printer.printValue("Status", response.getStatus()); this.printer.printValue("Error message", response.getErrorMessage()); diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java index 6125985c48..c378e4a472 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -27,8 +27,8 @@ import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.WebAsyncTask; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for assertions on the request. An instance of this class is @@ -39,7 +39,6 @@ import static org.springframework.test.util.MatcherAssertionErrors.*; */ public class RequestResultMatchers { - /** * Protected constructor. * Use {@link MockMvcResultMatchers#request()}. @@ -47,6 +46,7 @@ public class RequestResultMatchers { protected RequestResultMatchers() { } + /** * Assert a request attribute value with the given Hamcrest {@link Matcher}. * Whether asynchronous processing started, usually as a result of a diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java index 1695cad1a2..3ec7e5f04e 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.servlet.result; import org.hamcrest.Matcher; @@ -21,8 +22,8 @@ import org.springframework.http.HttpStatus; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for assertions on the response status. An instance of this class is @@ -35,7 +36,6 @@ import static org.springframework.test.util.MatcherAssertionErrors.*; */ public class StatusResultMatchers { - /** * Protected constructor. * Use {@link MockMvcResultMatchers#status()}. @@ -43,6 +43,7 @@ public class StatusResultMatchers { protected StatusResultMatchers() { } + /** * Assert the response status code with the given Hamcrest {@link Matcher}. */ diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java index 3c96e3c371..0548c664fb 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -22,17 +22,18 @@ import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.web.servlet.ModelAndView; +import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** * Factory for assertions on the selected view. An instance of this class is * typically accessed via {@link MockMvcResultMatchers#view()}. + * + * @author Rossen Stoyanchev * @since 3.2 */ public class ViewResultMatchers { - /** * Protected constructor. * Use {@link MockMvcResultMatchers#view()}. @@ -40,6 +41,7 @@ public class ViewResultMatchers { protected ViewResultMatchers() { } + /** * Assert the selected view name with the given Hamcrest {@link Matcher}. */ @@ -67,4 +69,5 @@ public class ViewResultMatchers { } }; } + } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java index e62313c198..922f2b8283 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-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. @@ -43,13 +43,10 @@ public class XpathResultMatchers { * Protected constructor, not for direct instantiation. Use * {@link MockMvcResultMatchers#xpath(String, Object...)} or * {@link MockMvcResultMatchers#xpath(String, Map, Object...)}. - * * @param expression the XPath expression * @param namespaces XML namespaces referenced in the XPath expression, or {@code null} * @param args arguments to parameterize the XPath expression with using the * formatting specifiers defined in {@link String#format(String, Object...)} - * - * @throws XPathExpressionException */ protected XpathResultMatchers(String expression, Map namespaces, Object ... args) throws XPathExpressionException { diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/setup/ConditionalDelegatingFilterProxyTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/setup/ConditionalDelegatingFilterProxyTests.java index 212c410f26..fa3857e860 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/setup/ConditionalDelegatingFilterProxyTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/setup/ConditionalDelegatingFilterProxyTests.java @@ -12,7 +12,6 @@ */ package org.springframework.test.web.servlet.setup; -import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -28,20 +27,25 @@ import org.springframework.mock.web.MockFilterConfig; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; +import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; -import static org.springframework.test.util.MatcherAssertionErrors.*; /** - * * @author Rob Winch */ public class ConditionalDelegatingFilterProxyTests { + private MockHttpServletRequest request; + private MockHttpServletResponse response; + private MockFilterChain filterChain; + private MockFilter delegate; + private PatternMappingFilterProxy filter; + @Before public void setup() { request = new MockHttpServletRequest(); @@ -51,6 +55,7 @@ public class ConditionalDelegatingFilterProxyTests { delegate = new MockFilter(); } + @Test public void init() throws Exception { FilterConfig config = new MockFilterConfig(); @@ -245,11 +250,17 @@ public class ConditionalDelegatingFilterProxyTests { delegate = new MockFilter(); } + private static class MockFilter implements Filter { + private FilterConfig filterConfig; + private ServletRequest request; + private ServletResponse response; + private FilterChain chain; + private boolean destroy; @Override @@ -258,8 +269,7 @@ public class ConditionalDelegatingFilterProxyTests { } @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, - ServletException { + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { this.request = request; this.response = response; this.chain = chain; @@ -270,4 +280,5 @@ public class ConditionalDelegatingFilterProxyTests { this.destroy = true; } } + }