extends SelfDescribing {
/**
* Evaluates the matcher for argument item.
*
* This method matches against Object, instead of the generic type T. This is
* because the caller of the Matcher does not know at runtime what the type is
* (because of type erasure with Java generics). It is down to the implementations
* to check the correct type.
*
* @param item the object against which the matcher is evaluated.
* @return true
if item matches, otherwise false
.
*
* @see BaseMatcher
*/
boolean matches(Object item);
/**
* Generate a description of why the matcher has not accepted the item.
* The description will be part of a larger description of why a matching
* failed, so it should be concise.
* This method assumes that matches(item)
is false, but
* will not check this.
*
* @param item The item that the Matcher has rejected.
* @param mismatchDescription
* The description to be built or appended to.
*/
void describeMismatch(Object item, Description mismatchDescription);
/**
* This method simply acts a friendly reminder not to implement Matcher directly and
* instead extend BaseMatcher. It's easy to ignore JavaDoc, but a bit harder to ignore
* compile errors .
*
* @see Matcher for reasons why.
* @see BaseMatcher
* @deprecated to make
*/
@Deprecated
void _dont_implement_Matcher___instead_extend_BaseMatcher_();
}
libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/CustomMatcher.java 0000644 0001750 0001750 00000002247 11753011773 027341 0 ustar ebourg ebourg package org.hamcrest;
/**
* Utility class for writing one off matchers.
* For example:
*
* Matcher<String> aNonEmptyString = new CustomMatcher<String>("a non empty string") {
* public boolean matches(Object object) {
* return ((object instanceof String) && !((String) object).isEmpty();
* }
* };
*
*
* This class is designed for scenarios where an anonymous inner class
* matcher makes sense. It should not be used by API designers implementing
* matchers.
*
* @author Neil Dunn
* @see CustomTypeSafeMatcher for a type safe variant of this class that you probably
* want to use.
* @param The type of object being matched.
*/
public abstract class CustomMatcher extends BaseMatcher {
private final String fixedDescription;
public CustomMatcher(String description) {
if (description == null) {
throw new IllegalArgumentException("Description should be non null!");
}
this.fixedDescription = description;
}
@Override
public final void describeTo(Description description) {
description.appendText(fixedDescription);
}
}
libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/TypeSafeDiagnosingMatcher.java 0000644 0001750 0001750 00000004605 11753011773 031612 0 ustar ebourg ebourg package org.hamcrest;
import org.hamcrest.internal.ReflectiveTypeFinder;
/**
* Convenient base class for Matchers that require a non-null value of a specific type
* and that will report why the received value has been rejected.
* This implements the null check, checks the type and then casts.
* To use, implement matchesSafely()
.
*
* @param
* @author Neil Dunn
* @author Nat Pryce
* @author Steve Freeman
*/
public abstract class TypeSafeDiagnosingMatcher extends BaseMatcher {
private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder("matchesSafely", 2, 0);
private final Class> expectedType;
/**
* Subclasses should implement this. The item will already have been checked
* for the specific type and will never be null.
*/
protected abstract boolean matchesSafely(T item, Description mismatchDescription);
/**
* Use this constructor if the subclass that implements matchesSafely
* is not the class that binds <T> to a type.
* @param expectedType The expectedType of the actual value.
*/
protected TypeSafeDiagnosingMatcher(Class> expectedType) {
this.expectedType = expectedType;
}
/**
* Use this constructor if the subclass that implements matchesSafely
* is not the class that binds <T> to a type.
* @param typeFinder A type finder to extract the type
*/
protected TypeSafeDiagnosingMatcher(ReflectiveTypeFinder typeFinder) {
this.expectedType = typeFinder.findExpectedType(getClass());
}
/**
* The default constructor for simple sub types
*/
protected TypeSafeDiagnosingMatcher() {
this(TYPE_FINDER);
}
@Override
@SuppressWarnings("unchecked")
public final boolean matches(Object item) {
return item != null
&& expectedType.isInstance(item)
&& matchesSafely((T) item, new Description.NullDescription());
}
@SuppressWarnings("unchecked")
@Override
public final void describeMismatch(Object item, Description mismatchDescription) {
if (item == null || !expectedType.isInstance(item)) {
super.describeMismatch(item, mismatchDescription);
} else {
matchesSafely((T) item, mismatchDescription);
}
}
}
libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/FeatureMatcher.java 0000644 0001750 0001750 00000003607 11753011773 027463 0 ustar ebourg ebourg package org.hamcrest;
import org.hamcrest.internal.ReflectiveTypeFinder;
/**
* Supporting class for matching a feature of an object. Implement featureValueOf()
* in a subclass to pull out the feature to be matched against.
*
* @param The type of the object to be matched
* @param The type of the feature to be matched
*/
public abstract class FeatureMatcher extends TypeSafeDiagnosingMatcher {
private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder("featureValueOf", 1, 0);
private final Matcher super U> subMatcher;
private final String featureDescription;
private final String featureName;
/**
* Constructor
* @param subMatcher The matcher to apply to the feature
* @param featureDescription Descriptive text to use in describeTo
* @param featureName Identifying text for mismatch message
*/
public FeatureMatcher(Matcher super U> subMatcher, String featureDescription, String featureName) {
super(TYPE_FINDER);
this.subMatcher = subMatcher;
this.featureDescription = featureDescription;
this.featureName = featureName;
}
/**
* Implement this to extract the interesting feature.
* @param actual the target object
* @return the feature to be matched
*/
protected abstract U featureValueOf(T actual);
@Override
protected boolean matchesSafely(T actual, Description mismatch) {
final U featureValue = featureValueOf(actual);
if (!subMatcher.matches(featureValue)) {
mismatch.appendText(featureName).appendText(" ");
subMatcher.describeMismatch(featureValue, mismatch);
return false;
}
return true;
};
@Override
public final void describeTo(Description description) {
description.appendText(featureDescription).appendText(" ")
.appendDescriptionOf(subMatcher);
}
}
libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/Description.java 0000644 0001750 0001750 00000004645 11753011773 027052 0 ustar ebourg ebourg package org.hamcrest;
/**
* A description of a Matcher. A Matcher will describe itself to a description
* which can later be used for reporting.
*
* @see Matcher#describeTo(Description)
*/
public interface Description {
/**
* A description that consumes input but does nothing.
*/
static final Description NONE = new NullDescription();
/**
* Appends some plain text to the description.
*/
Description appendText(String text);
/**
* Appends the description of a {@link SelfDescribing} value to this description.
*/
Description appendDescriptionOf(SelfDescribing value);
/**
* Appends an arbitary value to the description.
*/
Description appendValue(Object value);
/**
* Appends a list of values to the description.
*/
Description appendValueList(String start, String separator, String end,
T... values);
/**
* Appends a list of values to the description.
*/
Description appendValueList(String start, String separator, String end,
Iterable values);
/**
* Appends a list of {@link org.hamcrest.SelfDescribing} objects
* to the description.
*/
Description appendList(String start, String separator, String end,
Iterable extends SelfDescribing> values);
public static final class NullDescription implements Description {
@Override
public Description appendDescriptionOf(SelfDescribing value) {
return this;
}
@Override
public Description appendList(String start, String separator,
String end, Iterable extends SelfDescribing> values) {
return this;
}
@Override
public Description appendText(String text) {
return this;
}
@Override
public Description appendValue(Object value) {
return this;
}
@Override
public Description appendValueList(String start, String separator,
String end, T... values) {
return this;
}
@Override
public Description appendValueList(String start, String separator,
String end, Iterable values) {
return this;
}
@Override
public String toString() {
return "";
}
}
}
libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/TypeSafeMatcher.java 0000644 0001750 0001750 00000005706 11753011773 027612 0 ustar ebourg ebourg package org.hamcrest;
import org.hamcrest.internal.ReflectiveTypeFinder;
/**
* 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
* @author Steve Freeman
* @author Nat Pryce
*/
public abstract class TypeSafeMatcher extends BaseMatcher {
private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder("matchesSafely", 1, 0);
final private Class> expectedType;
/**
* The default constructor for simple sub types
*/
protected TypeSafeMatcher() {
this(TYPE_FINDER);
}
/**
* Use this constructor if the subclass that implements matchesSafely
* is not the class that binds <T> to a type.
* @param expectedType The expectedType of the actual value.
*/
protected TypeSafeMatcher(Class> expectedType) {
this.expectedType = expectedType;
}
/**
* Use this constructor if the subclass that implements matchesSafely
* is not the class that binds <T> to a type.
* @param typeFinder A type finder to extract the type
*/
protected TypeSafeMatcher(ReflectiveTypeFinder typeFinder) {
this.expectedType = typeFinder.findExpectedType(getClass());
}
/**
* Subclasses should implement this. The item will already have been checked for
* the specific type and will never be null.
*/
protected abstract boolean matchesSafely(T item);
/**
* Subclasses should override this. The item will already have been checked for
* the specific type and will never be null.
*/
protected void describeMismatchSafely(T item, Description mismatchDescription) {
super.describeMismatch(item, mismatchDescription);
}
/**
* Methods made final to prevent accidental override.
* If you need to override this, there's no point on extending TypeSafeMatcher.
* Instead, extend the {@link BaseMatcher}.
*/
@Override
@SuppressWarnings({"unchecked"})
public final boolean matches(Object item) {
return item != null
&& expectedType.isInstance(item)
&& matchesSafely((T) item);
}
@SuppressWarnings("unchecked")
@Override
final public void describeMismatch(Object item, Description description) {
if (item == null) {
super.describeMismatch(item, description);
} else if (! expectedType.isInstance(item)) {
description.appendText("was a ")
.appendText(item.getClass().getName())
.appendText(" (")
.appendValue(item)
.appendText(")");
} else {
describeMismatchSafely((T)item, description);
}
}
}
libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/internal/ 0000755 0001750 0001750 00000000000 12131006721 025513 5 ustar ebourg ebourg libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/internal/ReflectiveTypeFinder.java 0000644 0001750 0001750 00000006160 11753011773 032457 0 ustar ebourg ebourg /**
* The TypeSafe classes, and their descendants, need a mechanism to find out what type has been used as a parameter
* for the concrete matcher. Unfortunately, this type is lost during type erasure so we need to use reflection
* to get it back, by picking out the type of a known parameter to a known method.
* The catch is that, with bridging methods, this type is only visible in the class that actually implements
* the expected method, so the ReflectiveTypeFinder needs to be applied to that class or a subtype.
*
* For example, the abstract TypeSafeDiagnosingMatcher<T>
defines an abstract method
* protected abstract boolean matchesSafely(T item, Description mismatchDescription);
* By default it uses new ReflectiveTypeFinder("matchesSafely", 2, 0);
to find the
* parameterised type. If we create a TypeSafeDiagnosingMatcher<String>
, the type
* finder will return String.class
.
*
* A FeatureMatcher
is an abstract subclass of TypeSafeDiagnosingMatcher
.
* Although it has a templated implementation of matchesSafely(<T>, Decription);
, the
* actualy run-time signature of this is matchesSafely(Object, Description);
. Instead,
* we must find the type by reflecting on the concrete implementation of
* protected abstract U featureValueOf(T actual);
* a method which is declared in FeatureMatcher
.
*
* In short, use this to extract a type from a method in the leaf class of a templated class hierarchy.
*
* @author Steve Freeman
* @author Nat Pryce
*/
package org.hamcrest.internal;
import java.lang.reflect.Method;
public class ReflectiveTypeFinder {
private final String methodName;
private final int expectedNumberOfParameters;
private final int typedParameter;
public ReflectiveTypeFinder(String methodName, int expectedNumberOfParameters, int typedParameter) {
this.methodName = methodName;
this.expectedNumberOfParameters = expectedNumberOfParameters;
this.typedParameter = typedParameter;
}
public Class> findExpectedType(Class> fromClass) {
for (Class> c = fromClass; c != Object.class; c = c.getSuperclass()) {
for (Method method : c.getDeclaredMethods()) {
if (canObtainExpectedTypeFrom(method)) {
return expectedTypeFrom(method);
}
}
}
throw new Error("Cannot determine correct type for " + methodName + "() method.");
}
/**
* @param method The method to examine.
* @return true if this method references the relevant type
*/
protected boolean canObtainExpectedTypeFrom(Method method) {
return method.getName().equals(methodName)
&& method.getParameterTypes().length == expectedNumberOfParameters
&& !method.isSynthetic();
}
/**
* @param method The method from which to extract
* @return The type we're looking for
*/
protected Class> expectedTypeFrom(Method method) {
return method.getParameterTypes()[typedParameter];
}
} ././@LongLink 0000000 0000000 0000000 00000000150 00000000000 011561 L ustar root root libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValueIterator.java libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/internal/SelfDescribingValueIterator.j0000644 0001750 0001750 00000001153 11753011773 033274 0 ustar ebourg ebourg package org.hamcrest.internal;
import java.util.Iterator;
import org.hamcrest.SelfDescribing;
public class SelfDescribingValueIterator implements Iterator {
private Iterator values;
public SelfDescribingValueIterator(Iterator values) {
this.values = values;
}
@Override
public boolean hasNext() {
return values.hasNext();
}
@Override
public SelfDescribing next() {
return new SelfDescribingValue(values.next());
}
@Override
public void remove() {
values.remove();
}
}
libhamcrest-java-1.3/hamcrest-core/src/main/java/org/hamcrest/internal/ArrayIterator.java 0000644 0001750 0001750 00000001426 11753011773 031165 0 ustar ebourg ebourg package org.hamcrest.internal;
import java.lang.reflect.Array;
import java.util.Iterator;
public class ArrayIterator implements Iterator