Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
468f9c7
commit 242bf7c
Showing
22 changed files
with
157 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
spring-test-mvc/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.