Permalink
Browse files

Remove dependency on hamcrest-lib from spring-mvc-test

The change removes the use of concrete Matcher implementations and thus
the dependency on hamcrest-lib leaving hamcrest-core as the only
(optional) hamcrest dependency.

Issue: SPR-9961
  • Loading branch information...
1 parent 9a6ec1b commit 4812fcccc45094e838087451f3fee227f1023a6d @rstoyanchev rstoyanchev committed Nov 7, 2012
Showing with 311 additions and 147 deletions.
  1. +0 −1 build.gradle
  2. +19 −9 spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java
  3. +23 −15 spring-test-mvc/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java
  4. +8 −5 spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java
  5. +13 −7 spring-test-mvc/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java
  6. +28 −19 spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java
  7. +33 −9 spring-test-mvc/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java
  8. +7 −5 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java
  9. +44 −13 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java
  10. +8 −4 ...t-mvc/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java
  11. +15 −8 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java
  12. +5 −2 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
  13. +10 −6 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java
  14. +15 −7 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java
  15. +14 −7 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java
  16. +9 −5 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java
  17. +33 −9 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java
  18. +27 −16 spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java
View
@@ -558,7 +558,6 @@ project('spring-test-mvc') {
compile project(":spring-test").sourceSets.main.output
compile("org.apache.tomcat:tomcat-servlet-api:7.0.8", provided)
compile("org.hamcrest:hamcrest-core:1.3", optional)
- compile("org.hamcrest:hamcrest-library:1.3", optional)
compile("com.jayway.jsonpath:json-path:0.8.1", optional)
compile("xmlunit:xmlunit:1.2", optional)
testCompile("org.slf4j:jcl-over-slf4j:1.6.1")
@@ -16,14 +16,14 @@
package org.springframework.test.util;
-import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
+import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertTrue;
+import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
import java.text.ParseException;
import java.util.List;
import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
@@ -55,11 +55,13 @@ public JsonPathExpectationsHelper(String expression, Object ... args) {
/**
* Evaluate the JSONPath and assert the resulting value with the given {@code Matcher}.
+ * @param content the response content
+ * @param matcher the matcher to assert on the resulting json path
*/
@SuppressWarnings("unchecked")
public <T> void assertValue(String content, Matcher<T> matcher) throws ParseException {
T value = (T) evaluateJsonPath(content);
- assertThat("JSON path: " + this.expression, value, matcher);
+ assertThat("JSON path" + this.expression, value, matcher);
}
private Object evaluateJsonPath(String content) throws ParseException {
@@ -81,16 +83,27 @@ private Object evaluateJsonPath(String content) throws ParseException {
/**
* Apply the JSONPath and assert the resulting value.
*/
- public void assertValue(Object value) throws ParseException {
- assertValue(Matchers.equalTo(value));
+ public void assertValue(String responseContent, Object expectedValue) throws ParseException {
+ Object actualValue = evaluateJsonPath(responseContent);
+ assertEquals("JSON path" + this.expression, expectedValue, actualValue);
+ }
+
+ /**
+ * Apply the JSONPath and assert the resulting value is an array.
+ */
+ public void assertValueIsArray(String responseContent) throws ParseException {
+ Object actualValue = evaluateJsonPath(responseContent);
+ assertTrue("No value for JSON path " + this.expression, actualValue != null);
+ String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
+ assertTrue(reason, actualValue instanceof List);
}
/**
* Evaluate the JSON path and assert the resulting content exists.
*/
public void exists(String content) throws ParseException {
Object value = evaluateJsonPath(content);
- String reason = "No value for JSON path: " + this.expression;
+ String reason = "No value for JSON path " + this.expression;
assertTrue(reason, value != null);
if (List.class.isInstance(value)) {
assertTrue(reason, !((List<?>) value).isEmpty());
@@ -101,15 +114,13 @@ public void exists(String content) throws ParseException {
* Evaluate the JSON path and assert it doesn't point to any content.
*/
public void doesNotExist(String content) throws ParseException {
-
Object value;
try {
value = evaluateJsonPath(content);
}
catch (AssertionError ex) {
return;
}
-
String reason = String.format("Expected no value for JSON path: %s but found: %s", this.expression, value);
if (List.class.isInstance(value)) {
assertTrue(reason, ((List<?>) value).isEmpty());
@@ -118,5 +129,4 @@ public void doesNotExist(String content) throws ParseException {
assertTrue(reason, value == null);
}
}
-
}
@@ -16,8 +16,8 @@
package org.springframework.test.util;
-import static org.springframework.test.util.AssertionErrors.assertEquals;
-import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
+import static org.springframework.test.util.AssertionErrors.*;
+import static org.springframework.test.util.MatcherAssertionErrors.*;
import java.io.StringReader;
import java.util.Collections;
@@ -33,7 +33,6 @@
import javax.xml.xpath.XPathFactory;
import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
import org.springframework.util.xml.SimpleNamespaceContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
@@ -93,7 +92,7 @@ protected XPathExpression getXpathExpression() {
public void assertNode(String content, final Matcher<? super Node> matcher) throws Exception {
Document document = parseXmlString(content);
Node node = evaluateXpath(document, XPathConstants.NODE, Node.class);
- assertThat("Xpath: " + XpathExpectationsHelper.this.expression, node, matcher);
+ assertThat("XPath " + this.expression, node, matcher);
}
/**
@@ -128,15 +127,19 @@ protected Document parseXmlString(String xml) throws Exception {
* @throws Exception if content parsing or expression evaluation fails
*/
public void exists(String content) throws Exception {
- assertNode(content, Matchers.notNullValue());
+ Document document = parseXmlString(content);
+ Node node = evaluateXpath(document, XPathConstants.NODE, Node.class);
+ assertTrue("XPath " + this.expression + " does not exist", node != null);
}
/**
* Apply the XPath expression and assert the resulting content does not exist.
* @throws Exception if content parsing or expression evaluation fails
*/
public void doesNotExist(String content) throws Exception {
- assertNode(content, Matchers.nullValue());
+ Document document = parseXmlString(content);
+ Node node = evaluateXpath(document, XPathConstants.NODE, Node.class);
+ assertTrue("XPath " + this.expression + " exists", node == null);
}
/**
@@ -148,16 +151,17 @@ public void doesNotExist(String content) throws Exception {
public void assertNodeCount(String content, Matcher<Integer> matcher) throws Exception {
Document document = parseXmlString(content);
NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class);
- String reason = "nodeCount Xpath: " + XpathExpectationsHelper.this.expression;
- assertThat(reason, nodeList.getLength(), matcher);
+ assertThat("nodeCount for XPath " + this.expression, nodeList.getLength(), matcher);
}
/**
* Apply the XPath expression and assert the resulting content as an integer.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNodeCount(String content, int expectedCount) throws Exception {
- assertNodeCount(content, Matchers.equalTo(expectedCount));
+ Document document = parseXmlString(content);
+ NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class);
+ assertEquals("nodeCount for XPath " + this.expression, expectedCount, nodeList.getLength());
}
/**
@@ -169,15 +173,17 @@ public void assertNodeCount(String content, int expectedCount) throws Exception
public void assertString(String content, Matcher<? super String> matcher) throws Exception {
Document document = parseXmlString(content);
String result = evaluateXpath(document, XPathConstants.STRING, String.class);
- assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
+ assertThat("XPath " + this.expression, result, matcher);
}
/**
* Apply the XPath expression and assert the resulting content as a String.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertString(String content, String expectedValue) throws Exception {
- assertString(content, Matchers.equalTo(expectedValue));
+ Document document = parseXmlString(content);
+ String actual = evaluateXpath(document, XPathConstants.STRING, String.class);
+ assertEquals("XPath " + this.expression, expectedValue, actual);
}
/**
@@ -189,15 +195,17 @@ public void assertString(String content, String expectedValue) throws Exception
public void assertNumber(String content, Matcher<? super Double> matcher) throws Exception {
Document document = parseXmlString(content);
Double result = evaluateXpath(document, XPathConstants.NUMBER, Double.class);
- assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
+ assertThat("XPath " + this.expression, result, matcher);
}
/**
* Apply the XPath expression and assert the resulting content as a Double.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNumber(String content, Double expectedValue) throws Exception {
- assertNumber(content, Matchers.equalTo(expectedValue));
+ Document document = parseXmlString(content);
+ Double actual = evaluateXpath(document, XPathConstants.NUMBER, Double.class);
+ assertEquals("XPath " + this.expression, expectedValue, actual);
}
/**
@@ -206,8 +214,8 @@ public void assertNumber(String content, Double expectedValue) throws Exception
*/
public void assertBoolean(String content, boolean expectedValue) throws Exception {
Document document = parseXmlString(content);
- String result = evaluateXpath(document, XPathConstants.STRING, String.class);
- assertEquals("Xpath:", expectedValue, Boolean.parseBoolean(result));
+ String actual = evaluateXpath(document, XPathConstants.STRING, String.class);
+ assertEquals("XPath " + this.expression, expectedValue, Boolean.parseBoolean(actual));
}
}
@@ -25,7 +25,6 @@
import javax.xml.transform.dom.DOMSource;
import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpRequest;
@@ -88,8 +87,13 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
/**
* Get the body of the request as a UTF-8 string and compare it to the given String.
*/
- public RequestMatcher string(String expectedContent) {
- return string(Matchers.equalTo(expectedContent));
+ public RequestMatcher string(final String expectedContent) {
+ return new RequestMatcher() {
+ public void match(ClientHttpRequest request) throws IOException, AssertionError {
+ MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
+ assertEquals("Request content", expectedContent, mockRequest.getBodyAsString());
+ }
+ };
}
/**
@@ -99,8 +103,7 @@ public RequestMatcher bytes(final byte[] expectedContent) {
return new RequestMatcher() {
public void match(ClientHttpRequest request) throws IOException, AssertionError {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
- byte[] content = mockRequest.getBodyAsBytes();
- assertThat("Request content", content, Matchers.equalTo(expectedContent));
+ assertEquals("Request content", expectedContent, mockRequest.getBodyAsBytes());
}
};
}
@@ -15,12 +15,8 @@
*/
package org.springframework.test.web.client.match;
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.instanceOf;
-
import java.io.IOException;
import java.text.ParseException;
-import java.util.List;
import org.hamcrest.Matcher;
import org.springframework.http.client.ClientHttpRequest;
@@ -71,8 +67,13 @@ protected void matchInternal(MockClientHttpRequest request) throws IOException,
/**
* Apply the JSONPath and assert the resulting value.
*/
- public RequestMatcher value(Object expectedValue) {
- return value(equalTo(expectedValue));
+ public RequestMatcher value(final Object expectedValue) {
+ return new AbstractJsonPathRequestMatcher() {
+ @Override
+ protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
+ jsonPathHelper.assertValue(request.getBodyAsString(), expectedValue);
+ }
+ };
}
/**
@@ -103,7 +104,12 @@ protected void matchInternal(MockClientHttpRequest request) throws IOException,
* Assert the content at the given JSONPath is an array.
*/
public RequestMatcher isArray() {
- return value(instanceOf(List.class));
+ return new AbstractJsonPathRequestMatcher() {
+ @Override
+ protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
+ jsonPathHelper.assertValueIsArray(request.getBodyAsString());
+ }
+ };
}
@@ -16,6 +16,7 @@
package org.springframework.test.web.client.match;
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
+import static org.springframework.test.util.AssertionErrors.*;
import java.io.IOException;
import java.net.URI;
@@ -25,8 +26,6 @@
import javax.xml.xpath.XPathExpressionException;
import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
-import org.hamcrest.core.IsEqual;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
@@ -83,12 +82,16 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
/**
* Assert the request URI string.
*
- * @param uri the expected URI
+ * @param expectedUri the expected URI
* @return the request matcher
*/
- public static RequestMatcher requestTo(String uri) {
- Assert.notNull(uri, "'uri' must not be null");
- return requestTo(Matchers.equalTo(uri));
+ public static RequestMatcher requestTo(final String expectedUri) {
+ Assert.notNull(expectedUri, "'uri' must not be null");
+ return new RequestMatcher() {
+ public void match(ClientHttpRequest request) throws IOException, AssertionError {
+ assertEquals("Request URI", expectedUri, request.getURI().toString());
+ }
+ };
}
/**
@@ -127,13 +130,9 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
public static RequestMatcher header(final String name, final Matcher<? super String>... matchers) {
return new RequestMatcher() {
public void match(ClientHttpRequest request) {
- HttpHeaders headers = request.getHeaders();
- List<String> values = headers.get(name);
- AssertionErrors.assertTrue("Expected header <" + name + ">", values != null);
- AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + matchers.length
- + "> values but it has only <" + values.size() + ">", matchers.length <= values.size());
+ assertHeaderValueCount(name, request.getHeaders(), matchers.length);
for (int i = 0 ; i < matchers.length; i++) {
- assertThat("Request header", headers.get(name).get(i), matchers[i]);
+ assertThat("Request header", request.getHeaders().get(name).get(i), matchers[i]);
}
}
};
@@ -142,13 +141,23 @@ public void match(ClientHttpRequest request) {
/**
* Assert request header values.
*/
- public static RequestMatcher header(String name, String... values) {
- @SuppressWarnings("unchecked")
- Matcher<? super String>[] matchers = new IsEqual[values.length];
- for (int i = 0; i < values.length; i++) {
- matchers[i] = Matchers.equalTo(values[i]);
- }
- return header(name, matchers);
+ public static RequestMatcher header(final String name, final String... expectedValues) {
+ return new RequestMatcher() {
+ public void match(ClientHttpRequest request) {
+ assertHeaderValueCount(name, request.getHeaders(), expectedValues.length);
+ for (int i = 0 ; i < expectedValues.length; i++) {
+ assertEquals("Request header + [" + name + "]",
+ expectedValues[i], request.getHeaders().get(name).get(i));
+ }
+ }
+ };
+ }
+
+ private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) {
+ List<String> actualValues = headers.get(name);
+ AssertionErrors.assertTrue("Expected header <" + name + ">", actualValues != null);
+ AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount
+ + "> values but found " + actualValues, expectedCount <= actualValues.size());
}
/**
Oops, something went wrong.

0 comments on commit 4812fcc

Please sign in to comment.