diff --git a/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/HeaderMatcher.java b/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/HeaderMatcher.java index 6bef0a28e2..2f93520216 100644 --- a/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/HeaderMatcher.java +++ b/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/HeaderMatcher.java @@ -15,16 +15,15 @@ */ package org.springframework.integration.test.matcher; -import java.util.Map; - import org.hamcrest.Description; import org.hamcrest.Factory; import org.hamcrest.Matcher; import org.junit.Assert; -import org.junit.internal.matchers.TypeSafeMatcher; import org.springframework.integration.core.Message; import org.springframework.integration.core.MessageHeaders; +import java.util.Map; + /** *

Are the {@link MessageHeaders} of a {@link Message} containing any entry * or multiple that match?

diff --git a/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/MapContentMatchers.java b/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/MapContentMatchers.java index 2e0c17c532..68f7ccb928 100644 --- a/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/MapContentMatchers.java +++ b/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/MapContentMatchers.java @@ -26,7 +26,6 @@ import org.hamcrest.Factory; import org.hamcrest.Matcher; import org.hamcrest.core.AllOf; import org.hamcrest.core.IsEqual; -import org.junit.internal.matchers.TypeSafeMatcher; /** * Matchers that examine the contents of a {@link Map}. diff --git a/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/PayloadMatcher.java b/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/PayloadMatcher.java index 14352628d8..538de02d2b 100644 --- a/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/PayloadMatcher.java +++ b/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/PayloadMatcher.java @@ -20,7 +20,6 @@ import org.hamcrest.Factory; import org.hamcrest.Matcher; import org.hamcrest.core.IsEqual; import org.junit.Assert; -import org.junit.internal.matchers.TypeSafeMatcher; import org.springframework.integration.core.Message; /** diff --git a/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/TypeSafeMatcher.java b/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/TypeSafeMatcher.java new file mode 100644 index 0000000000..881564d694 --- /dev/null +++ b/org.springframework.integration.test/src/main/java/org/springframework/integration/test/matcher/TypeSafeMatcher.java @@ -0,0 +1,79 @@ +/* + * Copyright 2002-2008 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.integration.test.matcher; + +import org.hamcrest.BaseMatcher; + +import java.lang.reflect.Method; + +/** + * This class was copied from JUnit to avoid using it from org.junit.internal (causing a backwards compatibility issue). + * If you want to extend this class use a recent version of JUnit, and extend + * org.junit.matchers.TypeSafeMatcher + *

+ * Convenient base class for Matchers that require a non-null value of a specific type. + * This simply implements the null check, checks the type and then casts. + * + * @author Joe Walnes + */ +abstract class TypeSafeMatcher extends BaseMatcher { + + private Class expectedType; + + /** + * Subclasses should implement this. The item will already have been checked for + * the specific type and will never be null. + */ + public abstract boolean matchesSafely(T item); + + protected TypeSafeMatcher() { + expectedType = findExpectedType(getClass()); + } + + private static Class findExpectedType(Class fromClass) { + for (Class c = fromClass; c != Object.class; c = c.getSuperclass()) { + for (Method method : c.getDeclaredMethods()) { + if (isMatchesSafelyMethod(method)) { + return method.getParameterTypes()[0]; + } + } + } + + throw new Error("Cannot determine correct type for matchesSafely() method."); + } + + private static boolean isMatchesSafelyMethod(Method method) { + return method.getName().equals("matchesSafely") + && method.getParameterTypes().length == 1 + && !method.isSynthetic(); + } + + protected TypeSafeMatcher(Class expectedType) { + this.expectedType = expectedType; + } + + /** + * Method made final to prevent accidental override. + * If you need to override this, there's no point on extending TypeSafeMatcher. + * Instead, extend the {@link BaseMatcher}. + */ + @SuppressWarnings({"unchecked"}) + public final boolean matches(Object item) { + return item != null + && expectedType.isInstance(item) + && matchesSafely((T) item); + } +}