Skip to content

Commit

Permalink
Refine hamcrest dependency in spring-test-mvc project
Browse files Browse the repository at this point in the history
1) removed the hamcrest-all dependency requirement and replaced it with
the more focused hamcrest-library dependency

2) added MatcherAssertionErrors as a replacement of
org.hamcrest.MatcherAssert, which in hamcrest 1.1 is only available
through the hamcrest-all dependency (and not in hamcrest-core nor in
the hamcrest embedded in JUnit 4.4 through 4.8)

3) changed the required hamcrest version from 1.1 to 1.3 and made sure
the spring-test-mvc project does not rely on newer hamcrest
functionality without checking if it is available first

Applications that already depend on older versions of hamcrest
(in particular 1.1) via hamcrest-library, hamcrest-all or as part of
junit 4.4 through 4.8 should not be disrupted if they add spring-test
but may wish to exclude the hamcrest-library transitive dependency
from spring-test in order to avoid extra jars in the classpath

Applications that depend on hamcrest 1.3 should not have to do anything

Issue: SPR-9940
  • Loading branch information
rstoyanchev committed Nov 2, 2012
1 parent 468f9c7 commit 242bf7c
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 79 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -557,7 +557,7 @@ project('spring-test-mvc') {
compile project(":spring-webmvc")
compile project(":spring-test").sourceSets.main.output
compile("org.apache.tomcat:tomcat-servlet-api:7.0.8", provided)
compile "org.hamcrest:hamcrest-all:1.1"
compile "org.hamcrest:hamcrest-library:1.3"
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")
Expand Down
Expand Up @@ -16,13 +16,13 @@

package org.springframework.test.util;

import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
import static org.springframework.test.util.AssertionErrors.assertTrue;

import java.text.ParseException;
import java.util.List;

import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;

import com.jayway.jsonpath.InvalidPathException;
Expand Down Expand Up @@ -59,7 +59,7 @@ public JsonPathExpectationsHelper(String expression, Object ... args) {
@SuppressWarnings("unchecked")
public <T> void assertValue(String content, Matcher<T> matcher) throws ParseException {
T value = (T) evaluateJsonPath(content);
MatcherAssert.assertThat("JSON path: " + this.expression, value, matcher);
assertThat("JSON path: " + this.expression, value, matcher);
}

private Object evaluateJsonPath(String content) throws ParseException {
Expand Down
@@ -0,0 +1,80 @@
/*
* Copyright 2002-2012 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.util;

import java.lang.reflect.Method;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.springframework.util.ClassUtils;

/**
* 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
* compatibility with Hamcrest 1.1 (also embedded in JUnit 4.4 through 4.8).
*
* @author Rossen Stoyanchev
* @since 3.2
*/
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.
*
* @param <T> the static type accepted by the matcher
* @param actual the value to match against
* @param matcher the matcher
*/
public static <T> void assertThat(T actual, Matcher<T> matcher) {
assertThat("", actual, matcher);
}

/**
* Asserts that the given matcher matches the actual value.
*
* @param <T> the static type accepted by the matcher
* @param reason additional information about the error
* @param actual the value to match against
* @param matcher the matcher
*/
public static <T> void assertThat(String reason, T actual, Matcher<T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description.appendText(reason);
description.appendText("\nExpected: ");
description.appendDescriptionOf(matcher);
if (describeMismatchMethod != null) {
description.appendText("\n but: ");
matcher.describeMismatch(actual, description);
}
else {
description.appendText("\n got: ");
description.appendValue(actual);
description.appendText("\n");
}
throw new AssertionError(description.toString());
}
}

}
Expand Up @@ -16,6 +16,8 @@

package org.springframework.test.util;

import static org.springframework.test.util.MatcherAssertionErrors.assertThat;

import java.io.StringReader;
import java.util.Map;

Expand All @@ -27,8 +29,6 @@
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.springframework.test.util.AssertionErrors;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
Expand All @@ -49,7 +49,7 @@ public class XmlExpectationsHelper {
*/
public void assertNode(String content, Matcher<? super Node> matcher) throws Exception {
Document document = parseXmlString(content);
MatcherAssert.assertThat("Body content", document, matcher);
assertThat("Body content", document, matcher);
}

private Document parseXmlString(String xml) throws Exception {
Expand All @@ -67,7 +67,7 @@ private Document parseXmlString(String xml) throws Exception {
*/
public void assertSource(String content, Matcher<? super Source> matcher) throws Exception {
Document document = parseXmlString(content);
MatcherAssert.assertThat("Body content", new DOMSource(document), matcher);
assertThat("Body content", new DOMSource(document), matcher);
}

/**
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.springframework.test.util;

import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;

import java.io.StringReader;
import java.util.Collections;
Expand All @@ -32,7 +33,6 @@
import javax.xml.xpath.XPathFactory;

import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.springframework.util.xml.SimpleNamespaceContext;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -93,7 +93,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);
MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, node, matcher);
assertThat("Xpath: " + XpathExpectationsHelper.this.expression, node, matcher);
}

/**
Expand Down Expand Up @@ -149,7 +149,7 @@ public void assertNodeCount(String content, Matcher<Integer> matcher) throws Exc
Document document = parseXmlString(content);
NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class);
String reason = "nodeCount Xpath: " + XpathExpectationsHelper.this.expression;
MatcherAssert.assertThat(reason, nodeList.getLength(), matcher);
assertThat(reason, nodeList.getLength(), matcher);
}

/**
Expand All @@ -169,7 +169,7 @@ 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);
MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
}

/**
Expand All @@ -189,7 +189,7 @@ 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);
MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
}

/**
Expand Down
Expand Up @@ -16,6 +16,7 @@
package org.springframework.test.web.client.match;

import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
import static org.springframework.test.util.AssertionErrors.assertTrue;

import java.io.IOException;
Expand All @@ -24,7 +25,6 @@
import javax.xml.transform.dom.DOMSource;

import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
Expand Down Expand Up @@ -80,7 +80,7 @@ public RequestMatcher string(final Matcher<? super String> matcher) {
return new RequestMatcher() {
public void match(ClientHttpRequest request) throws IOException, AssertionError {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
MatcherAssert.assertThat("Request content", mockRequest.getBodyAsString(), matcher);
assertThat("Request content", mockRequest.getBodyAsString(), matcher);
}
};
}
Expand All @@ -100,7 +100,7 @@ public RequestMatcher bytes(final byte[] expectedContent) {
public void match(ClientHttpRequest request) throws IOException, AssertionError {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
byte[] content = mockRequest.getBodyAsBytes();
MatcherAssert.assertThat("Request content", content, Matchers.equalTo(expectedContent));
assertThat("Request content", content, Matchers.equalTo(expectedContent));
}
};
}
Expand Down
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.test.web.client.match;

import static org.springframework.test.util.MatcherAssertionErrors.assertThat;

import java.io.IOException;
import java.net.URI;
import java.util.List;
Expand All @@ -23,13 +25,11 @@
import javax.xml.xpath.XPathExpressionException;

import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
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;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.test.util.AssertionErrors;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.RequestMatcher;
Expand Down Expand Up @@ -75,7 +75,7 @@ public static RequestMatcher requestTo(final Matcher<String> matcher) {
Assert.notNull(matcher, "'matcher' must not be null");
return new RequestMatcher() {
public void match(ClientHttpRequest request) throws IOException, AssertionError {
MatcherAssert.assertThat("Request URI", request.getURI().toString(), matcher);
assertThat("Request URI", request.getURI().toString(), matcher);
}
};
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public void match(ClientHttpRequest request) {
AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + matchers.length
+ "> values but it has only <" + values.size() + ">", matchers.length <= values.size());
for (int i = 0 ; i < matchers.length; i++) {
MatcherAssert.assertThat("Request header", headers.get(name).get(i), matchers[i]);
assertThat("Request header", headers.get(name).get(i), matchers[i]);
}
}
};
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.springframework.test.web.servlet.result;

import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
import static org.springframework.test.util.AssertionErrors.assertTrue;

import java.util.Map;
Expand All @@ -26,7 +27,6 @@
import javax.xml.transform.dom.DOMSource;

import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.springframework.http.MediaType;
import org.springframework.test.util.XmlExpectationsHelper;
Expand Down Expand Up @@ -97,7 +97,7 @@ public void match(MvcResult result) {
public ResultMatcher string(final Matcher<? super String> matcher) {
return new ResultMatcher() {
public void match(MvcResult result) throws Exception {
MatcherAssert.assertThat("Response content", result.getResponse().getContentAsString(), matcher);
assertThat("Response content", result.getResponse().getContentAsString(), matcher);
}
};
}
Expand All @@ -116,7 +116,7 @@ public ResultMatcher bytes(final byte[] expectedContent) {
return new ResultMatcher() {
public void match(MvcResult result) throws Exception {
byte[] content = result.getResponse().getContentAsByteArray();
MatcherAssert.assertThat("Response content", content, Matchers.equalTo(expectedContent));
assertThat("Response content", content, Matchers.equalTo(expectedContent));
}
};
}
Expand Down
Expand Up @@ -17,12 +17,12 @@
package org.springframework.test.web.servlet.result;

import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
import static org.springframework.test.util.AssertionErrors.assertTrue;

import javax.servlet.http.Cookie;

import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
Expand Down Expand Up @@ -53,7 +53,7 @@ public ResultMatcher value(final String name, final Matcher<? super String> matc
public void match(MvcResult result) {
Cookie cookie = result.getResponse().getCookie(name);
assertTrue("Response cookie not found: " + name, cookie != null);
MatcherAssert.assertThat("Response cookie", cookie.getValue(), matcher);
assertThat("Response cookie", cookie.getValue(), matcher);
}
};
}
Expand Down Expand Up @@ -99,7 +99,7 @@ public ResultMatcher maxAge(final String name, final Matcher<? super Integer> ma
public void match(MvcResult result) {
Cookie cookie = result.getResponse().getCookie(name);
assertTrue("No cookie with name: " + name, cookie != null);
MatcherAssert.assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher);
assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher);
}
};
}
Expand All @@ -118,7 +118,7 @@ public ResultMatcher path(final String name, final Matcher<? super String> match
return new ResultMatcher() {
public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name);
MatcherAssert.assertThat("Response cookie path", cookie.getPath(), matcher);
assertThat("Response cookie path", cookie.getPath(), matcher);
}
};
}
Expand All @@ -134,7 +134,7 @@ public ResultMatcher domain(final String name, final Matcher<? super String> mat
return new ResultMatcher() {
public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name);
MatcherAssert.assertThat("Response cookie domain", cookie.getDomain(), matcher);
assertThat("Response cookie domain", cookie.getDomain(), matcher);
}
};
}
Expand All @@ -153,7 +153,7 @@ public ResultMatcher comment(final String name, final Matcher<? super String> ma
return new ResultMatcher() {
public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name);
MatcherAssert.assertThat("Response cookie comment", cookie.getComment(), matcher);
assertThat("Response cookie comment", cookie.getComment(), matcher);
}
};
}
Expand All @@ -172,7 +172,7 @@ public ResultMatcher version(final String name, final Matcher<? super Integer> m
return new ResultMatcher() {
public void match(MvcResult result) throws Exception {
Cookie cookie = result.getResponse().getCookie(name);
MatcherAssert.assertThat("Response cookie version", cookie.getVersion(), matcher);
assertThat("Response cookie version", cookie.getVersion(), matcher);
}
};
}
Expand Down

0 comments on commit 242bf7c

Please sign in to comment.