Skip to content

Commit 242bf7c

Browse files
committed
Refine hamcrest dependency in spring-test-mvc project
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
1 parent 468f9c7 commit 242bf7c

22 files changed

+157
-79
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ project('spring-test-mvc') {
557557
compile project(":spring-webmvc")
558558
compile project(":spring-test").sourceSets.main.output
559559
compile("org.apache.tomcat:tomcat-servlet-api:7.0.8", provided)
560-
compile "org.hamcrest:hamcrest-all:1.1"
560+
compile "org.hamcrest:hamcrest-library:1.3"
561561
compile("com.jayway.jsonpath:json-path:0.8.1", optional)
562562
compile("xmlunit:xmlunit:1.2", optional)
563563
testCompile("org.slf4j:jcl-over-slf4j:1.6.1")

spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package org.springframework.test.util;
1818

19+
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
1920
import static org.springframework.test.util.AssertionErrors.assertTrue;
2021

2122
import java.text.ParseException;
2223
import java.util.List;
2324

2425
import org.hamcrest.Matcher;
25-
import org.hamcrest.MatcherAssert;
2626
import org.hamcrest.Matchers;
2727

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

6565
private Object evaluateJsonPath(String content) throws ParseException {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.test.util;
17+
18+
import java.lang.reflect.Method;
19+
20+
import org.hamcrest.Description;
21+
import org.hamcrest.Matcher;
22+
import org.hamcrest.StringDescription;
23+
import org.springframework.util.ClassUtils;
24+
25+
/**
26+
* A replacement of {@link org.hamcrest.MatcherAssert} that removes the need to
27+
* depend on "hamcrest-all" when using Hamcrest 1.1 and also maintains backward
28+
* compatibility with Hamcrest 1.1 (also embedded in JUnit 4.4 through 4.8).
29+
*
30+
* @author Rossen Stoyanchev
31+
* @since 3.2
32+
*/
33+
public abstract class MatcherAssertionErrors {
34+
35+
private static final Method describeMismatchMethod =
36+
ClassUtils.getMethodIfAvailable(Matcher.class, "describeMismatch", Object.class, Description.class);
37+
38+
39+
private MatcherAssertionErrors() {
40+
}
41+
42+
/**
43+
* Asserts that the given matcher matches the actual value.
44+
*
45+
* @param <T> the static type accepted by the matcher
46+
* @param actual the value to match against
47+
* @param matcher the matcher
48+
*/
49+
public static <T> void assertThat(T actual, Matcher<T> matcher) {
50+
assertThat("", actual, matcher);
51+
}
52+
53+
/**
54+
* Asserts that the given matcher matches the actual value.
55+
*
56+
* @param <T> the static type accepted by the matcher
57+
* @param reason additional information about the error
58+
* @param actual the value to match against
59+
* @param matcher the matcher
60+
*/
61+
public static <T> void assertThat(String reason, T actual, Matcher<T> matcher) {
62+
if (!matcher.matches(actual)) {
63+
Description description = new StringDescription();
64+
description.appendText(reason);
65+
description.appendText("\nExpected: ");
66+
description.appendDescriptionOf(matcher);
67+
if (describeMismatchMethod != null) {
68+
description.appendText("\n but: ");
69+
matcher.describeMismatch(actual, description);
70+
}
71+
else {
72+
description.appendText("\n got: ");
73+
description.appendValue(actual);
74+
description.appendText("\n");
75+
}
76+
throw new AssertionError(description.toString());
77+
}
78+
}
79+
80+
}

spring-test-mvc/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.test.util;
1818

19+
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
20+
1921
import java.io.StringReader;
2022
import java.util.Map;
2123

@@ -27,8 +29,6 @@
2729
import org.custommonkey.xmlunit.Diff;
2830
import org.custommonkey.xmlunit.XMLUnit;
2931
import org.hamcrest.Matcher;
30-
import org.hamcrest.MatcherAssert;
31-
import org.springframework.test.util.AssertionErrors;
3232
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
3333
import org.w3c.dom.Document;
3434
import org.w3c.dom.Node;
@@ -49,7 +49,7 @@ public class XmlExpectationsHelper {
4949
*/
5050
public void assertNode(String content, Matcher<? super Node> matcher) throws Exception {
5151
Document document = parseXmlString(content);
52-
MatcherAssert.assertThat("Body content", document, matcher);
52+
assertThat("Body content", document, matcher);
5353
}
5454

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

7373
/**

spring-test-mvc/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.test.util;
1818

1919
import static org.springframework.test.util.AssertionErrors.assertEquals;
20+
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
2021

2122
import java.io.StringReader;
2223
import java.util.Collections;
@@ -32,7 +33,6 @@
3233
import javax.xml.xpath.XPathFactory;
3334

3435
import org.hamcrest.Matcher;
35-
import org.hamcrest.MatcherAssert;
3636
import org.hamcrest.Matchers;
3737
import org.springframework.util.xml.SimpleNamespaceContext;
3838
import org.w3c.dom.Document;
@@ -93,7 +93,7 @@ protected XPathExpression getXpathExpression() {
9393
public void assertNode(String content, final Matcher<? super Node> matcher) throws Exception {
9494
Document document = parseXmlString(content);
9595
Node node = evaluateXpath(document, XPathConstants.NODE, Node.class);
96-
MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, node, matcher);
96+
assertThat("Xpath: " + XpathExpectationsHelper.this.expression, node, matcher);
9797
}
9898

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

155155
/**
@@ -169,7 +169,7 @@ public void assertNodeCount(String content, int expectedCount) throws Exception
169169
public void assertString(String content, Matcher<? super String> matcher) throws Exception {
170170
Document document = parseXmlString(content);
171171
String result = evaluateXpath(document, XPathConstants.STRING, String.class);
172-
MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
172+
assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
173173
}
174174

175175
/**
@@ -189,7 +189,7 @@ public void assertString(String content, String expectedValue) throws Exception
189189
public void assertNumber(String content, Matcher<? super Double> matcher) throws Exception {
190190
Document document = parseXmlString(content);
191191
Double result = evaluateXpath(document, XPathConstants.NUMBER, Double.class);
192-
MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
192+
assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher);
193193
}
194194

195195
/**

spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.test.web.client.match;
1717

1818
import static org.springframework.test.util.AssertionErrors.assertEquals;
19+
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
1920
import static org.springframework.test.util.AssertionErrors.assertTrue;
2021

2122
import java.io.IOException;
@@ -24,7 +25,6 @@
2425
import javax.xml.transform.dom.DOMSource;
2526

2627
import org.hamcrest.Matcher;
27-
import org.hamcrest.MatcherAssert;
2828
import org.hamcrest.Matchers;
2929
import org.springframework.http.MediaType;
3030
import org.springframework.http.client.ClientHttpRequest;
@@ -80,7 +80,7 @@ public RequestMatcher string(final Matcher<? super String> matcher) {
8080
return new RequestMatcher() {
8181
public void match(ClientHttpRequest request) throws IOException, AssertionError {
8282
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
83-
MatcherAssert.assertThat("Request content", mockRequest.getBodyAsString(), matcher);
83+
assertThat("Request content", mockRequest.getBodyAsString(), matcher);
8484
}
8585
};
8686
}
@@ -100,7 +100,7 @@ public RequestMatcher bytes(final byte[] expectedContent) {
100100
public void match(ClientHttpRequest request) throws IOException, AssertionError {
101101
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
102102
byte[] content = mockRequest.getBodyAsBytes();
103-
MatcherAssert.assertThat("Request content", content, Matchers.equalTo(expectedContent));
103+
assertThat("Request content", content, Matchers.equalTo(expectedContent));
104104
}
105105
};
106106
}

spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.test.web.client.match;
1717

18+
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
19+
1820
import java.io.IOException;
1921
import java.net.URI;
2022
import java.util.List;
@@ -23,13 +25,11 @@
2325
import javax.xml.xpath.XPathExpressionException;
2426

2527
import org.hamcrest.Matcher;
26-
import org.hamcrest.MatcherAssert;
2728
import org.hamcrest.Matchers;
2829
import org.hamcrest.core.IsEqual;
2930
import org.springframework.http.HttpHeaders;
3031
import org.springframework.http.HttpMethod;
3132
import org.springframework.http.client.ClientHttpRequest;
32-
import org.springframework.mock.http.client.MockClientHttpRequest;
3333
import org.springframework.test.util.AssertionErrors;
3434
import org.springframework.test.web.client.MockRestServiceServer;
3535
import org.springframework.test.web.client.RequestMatcher;
@@ -75,7 +75,7 @@ public static RequestMatcher requestTo(final Matcher<String> matcher) {
7575
Assert.notNull(matcher, "'matcher' must not be null");
7676
return new RequestMatcher() {
7777
public void match(ClientHttpRequest request) throws IOException, AssertionError {
78-
MatcherAssert.assertThat("Request URI", request.getURI().toString(), matcher);
78+
assertThat("Request URI", request.getURI().toString(), matcher);
7979
}
8080
};
8181
}
@@ -133,7 +133,7 @@ public void match(ClientHttpRequest request) {
133133
AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + matchers.length
134134
+ "> values but it has only <" + values.size() + ">", matchers.length <= values.size());
135135
for (int i = 0 ; i < matchers.length; i++) {
136-
MatcherAssert.assertThat("Request header", headers.get(name).get(i), matchers[i]);
136+
assertThat("Request header", headers.get(name).get(i), matchers[i]);
137137
}
138138
}
139139
};

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.test.web.servlet.result;
1818

1919
import static org.springframework.test.util.AssertionErrors.assertEquals;
20+
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
2021
import static org.springframework.test.util.AssertionErrors.assertTrue;
2122

2223
import java.util.Map;
@@ -26,7 +27,6 @@
2627
import javax.xml.transform.dom.DOMSource;
2728

2829
import org.hamcrest.Matcher;
29-
import org.hamcrest.MatcherAssert;
3030
import org.hamcrest.Matchers;
3131
import org.springframework.http.MediaType;
3232
import org.springframework.test.util.XmlExpectationsHelper;
@@ -97,7 +97,7 @@ public void match(MvcResult result) {
9797
public ResultMatcher string(final Matcher<? super String> matcher) {
9898
return new ResultMatcher() {
9999
public void match(MvcResult result) throws Exception {
100-
MatcherAssert.assertThat("Response content", result.getResponse().getContentAsString(), matcher);
100+
assertThat("Response content", result.getResponse().getContentAsString(), matcher);
101101
}
102102
};
103103
}
@@ -116,7 +116,7 @@ public ResultMatcher bytes(final byte[] expectedContent) {
116116
return new ResultMatcher() {
117117
public void match(MvcResult result) throws Exception {
118118
byte[] content = result.getResponse().getContentAsByteArray();
119-
MatcherAssert.assertThat("Response content", content, Matchers.equalTo(expectedContent));
119+
assertThat("Response content", content, Matchers.equalTo(expectedContent));
120120
}
121121
};
122122
}

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package org.springframework.test.web.servlet.result;
1818

1919
import static org.springframework.test.util.AssertionErrors.assertEquals;
20+
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
2021
import static org.springframework.test.util.AssertionErrors.assertTrue;
2122

2223
import javax.servlet.http.Cookie;
2324

2425
import org.hamcrest.Matcher;
25-
import org.hamcrest.MatcherAssert;
2626
import org.hamcrest.Matchers;
2727
import org.springframework.test.web.servlet.MvcResult;
2828
import org.springframework.test.web.servlet.ResultMatcher;
@@ -53,7 +53,7 @@ public ResultMatcher value(final String name, final Matcher<? super String> matc
5353
public void match(MvcResult result) {
5454
Cookie cookie = result.getResponse().getCookie(name);
5555
assertTrue("Response cookie not found: " + name, cookie != null);
56-
MatcherAssert.assertThat("Response cookie", cookie.getValue(), matcher);
56+
assertThat("Response cookie", cookie.getValue(), matcher);
5757
}
5858
};
5959
}
@@ -99,7 +99,7 @@ public ResultMatcher maxAge(final String name, final Matcher<? super Integer> ma
9999
public void match(MvcResult result) {
100100
Cookie cookie = result.getResponse().getCookie(name);
101101
assertTrue("No cookie with name: " + name, cookie != null);
102-
MatcherAssert.assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher);
102+
assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher);
103103
}
104104
};
105105
}
@@ -118,7 +118,7 @@ public ResultMatcher path(final String name, final Matcher<? super String> match
118118
return new ResultMatcher() {
119119
public void match(MvcResult result) throws Exception {
120120
Cookie cookie = result.getResponse().getCookie(name);
121-
MatcherAssert.assertThat("Response cookie path", cookie.getPath(), matcher);
121+
assertThat("Response cookie path", cookie.getPath(), matcher);
122122
}
123123
};
124124
}
@@ -134,7 +134,7 @@ public ResultMatcher domain(final String name, final Matcher<? super String> mat
134134
return new ResultMatcher() {
135135
public void match(MvcResult result) throws Exception {
136136
Cookie cookie = result.getResponse().getCookie(name);
137-
MatcherAssert.assertThat("Response cookie domain", cookie.getDomain(), matcher);
137+
assertThat("Response cookie domain", cookie.getDomain(), matcher);
138138
}
139139
};
140140
}
@@ -153,7 +153,7 @@ public ResultMatcher comment(final String name, final Matcher<? super String> ma
153153
return new ResultMatcher() {
154154
public void match(MvcResult result) throws Exception {
155155
Cookie cookie = result.getResponse().getCookie(name);
156-
MatcherAssert.assertThat("Response cookie comment", cookie.getComment(), matcher);
156+
assertThat("Response cookie comment", cookie.getComment(), matcher);
157157
}
158158
};
159159
}
@@ -172,7 +172,7 @@ public ResultMatcher version(final String name, final Matcher<? super Integer> m
172172
return new ResultMatcher() {
173173
public void match(MvcResult result) throws Exception {
174174
Cookie cookie = result.getResponse().getCookie(name);
175-
MatcherAssert.assertThat("Response cookie version", cookie.getVersion(), matcher);
175+
assertThat("Response cookie version", cookie.getVersion(), matcher);
176176
}
177177
};
178178
}

0 commit comments

Comments
 (0)