diff options
author | Paul Duffin <paulduffin@google.com> | 2017-01-20 16:08:15 +0000 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2017-02-09 15:12:38 +0000 |
commit | e3f85a659ab92bd0b2366fd826b8eca28adffc77 (patch) | |
tree | 450490f7d8f43c6d7c99936e95f62358c950fe6d | |
parent | c1dbb44e71e47410ad5685aba3ef3fccb095a2b4 (diff) | |
download | hamcrest-e3f85a659ab92bd0b2366fd826b8eca28adffc77.tar.gz |
Remove org.hamcrest.beans and update Matchers
This package relies on java.beans. which is not supported on
Android.
Bug: 30946317
Test: make checkbuild
Change-Id: I87564d796292207de796c34760ec7e33e2aca3c3
7 files changed, 2 insertions, 445 deletions
diff --git a/README.version b/README.version index ac4f06f..660cee5 100644 --- a/README.version +++ b/README.version @@ -11,4 +11,5 @@ what the future holds for Hamcrest given that it has had no activity since June conflicts in future upgrades. Local Changes: - None + Remove hamcrest-library/src/main/java/org/hamcrest/beans/ as Android does not support java.beans. +
\ No newline at end of file diff --git a/hamcrest-library/src/main/java/org/hamcrest/Matchers.java b/hamcrest-library/src/main/java/org/hamcrest/Matchers.java index 691d15f..ca1238d 100644 --- a/hamcrest-library/src/main/java/org/hamcrest/Matchers.java +++ b/hamcrest-library/src/main/java/org/hamcrest/Matchers.java @@ -1506,48 +1506,6 @@ public class Matchers { } /** - * Creates a matcher that matches when the examined object has a JavaBean property - * with the specified name. - * For example: - * <pre>assertThat(myBean, hasProperty("foo"))</pre> - * - * @param propertyName - * the name of the JavaBean property that examined beans should possess - */ - public static <T> org.hamcrest.Matcher<T> hasProperty(java.lang.String propertyName) { - return org.hamcrest.beans.HasProperty.<T>hasProperty(propertyName); - } - - /** - * Creates a matcher that matches when the examined object has a JavaBean property - * with the specified name whose value satisfies the specified matcher. - * For example: - * <pre>assertThat(myBean, hasProperty("foo", equalTo("bar"))</pre> - * - * @param propertyName - * the name of the JavaBean property that examined beans should possess - * @param valueMatcher - * a matcher for the value of the specified property of the examined bean - */ - public static <T> org.hamcrest.Matcher<T> hasProperty(java.lang.String propertyName, org.hamcrest.Matcher<?> valueMatcher) { - return org.hamcrest.beans.HasPropertyWithValue.<T>hasProperty(propertyName, valueMatcher); - } - - /** - * Creates a matcher that matches when the examined object has values for all of - * its JavaBean properties that are equal to the corresponding values of the - * specified bean. - * For example: - * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre> - * - * @param expectedBean - * the bean against which examined beans are compared - */ - public static <T> org.hamcrest.Matcher<T> samePropertyValuesAs(T expectedBean) { - return org.hamcrest.beans.SamePropertyValuesAs.<T>samePropertyValuesAs(expectedBean); - } - - /** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the * specified <code>xPath</code> that satisfies the specified <code>valueMatcher</code>. * For example: diff --git a/hamcrest-library/src/main/java/org/hamcrest/beans/HasProperty.java b/hamcrest-library/src/main/java/org/hamcrest/beans/HasProperty.java deleted file mode 100644 index 320a49e..0000000 --- a/hamcrest-library/src/main/java/org/hamcrest/beans/HasProperty.java +++ /dev/null @@ -1,56 +0,0 @@ -package org.hamcrest.beans; - -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeMatcher; - -/** - * A Matcher that checks that an object has a JavaBean property - * with the specified name. If an error occurs during introspection - * of the object then this is treated as a mismatch. - * - * @author Iain McGinniss - * @author Nat Pryce - * @author Steve Freeman - */ -public class HasProperty<T> extends TypeSafeMatcher<T> { - - private final String propertyName; - - public HasProperty(String propertyName) { - this.propertyName = propertyName; - } - - @Override - public boolean matchesSafely(T obj) { - try { - return PropertyUtil.getPropertyDescriptor(propertyName, obj) != null; - } catch (IllegalArgumentException e) { - return false; - } - } - - @Override - public void describeMismatchSafely(T item, Description mismatchDescription) { - mismatchDescription.appendText("no ").appendValue(propertyName).appendText(" in ").appendValue(item); - } - - @Override - public void describeTo(Description description) { - description.appendText("hasProperty(").appendValue(propertyName).appendText(")"); - } - - /** - * Creates a matcher that matches when the examined object has a JavaBean property - * with the specified name. - * For example: - * <pre>assertThat(myBean, hasProperty("foo"))</pre> - * - * @param propertyName - * the name of the JavaBean property that examined beans should possess - */ - public static <T> Matcher<T> hasProperty(String propertyName) { - return new HasProperty<T>(propertyName); - } - -} diff --git a/hamcrest-library/src/main/java/org/hamcrest/beans/HasPropertyWithValue.java b/hamcrest-library/src/main/java/org/hamcrest/beans/HasPropertyWithValue.java deleted file mode 100644 index 326f9dc..0000000 --- a/hamcrest-library/src/main/java/org/hamcrest/beans/HasPropertyWithValue.java +++ /dev/null @@ -1,148 +0,0 @@ -package org.hamcrest.beans; - -import org.hamcrest.Condition; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeDiagnosingMatcher; - -import java.beans.PropertyDescriptor; -import java.lang.reflect.Method; - -import static org.hamcrest.Condition.matched; -import static org.hamcrest.Condition.notMatched; -import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS; - -/** - * <p>Matcher that asserts that a JavaBean property on an argument passed to the - * mock object meets the provided matcher. This is useful for when objects - * are created within code under test and passed to a mock object, and you wish - * to assert that the created object has certain properties. - * </p> - * - * <h2>Example Usage</h2> - * Consider the situation where we have a class representing a person, which - * follows the basic JavaBean convention of having get() and possibly set() - * methods for it's properties: - * <pre> - * public class Person { - * private String name; - * public Person(String person) { - * this.person = person; - * } - * public String getName() { - * return name; - * } - * }</pre> - * - * And that these person objects are generated within a piece of code under test - * (a class named PersonGenerator). This object is sent to one of our mock objects - * which overrides the PersonGenerationListener interface: - * <pre> - * public interface PersonGenerationListener { - * public void personGenerated(Person person); - * }</pre> - * - * In order to check that the code under test generates a person with name - * "Iain" we would do the following: - * <pre> - * Mock personGenListenerMock = mock(PersonGenerationListener.class); - * personGenListenerMock.expects(once()).method("personGenerated").with(and(isA(Person.class), hasProperty("Name", eq("Iain"))); - * PersonGenerationListener listener = (PersonGenerationListener)personGenListenerMock.proxy();</pre> - * - * <p>If an exception is thrown by the getter method for a property, the property - * does not exist, is not readable, or a reflection related exception is thrown - * when trying to invoke it then this is treated as an evaluation failure and - * the matches method will return false. - * </p> - * <p>This matcher class will also work with JavaBean objects that have explicit - * bean descriptions via an associated BeanInfo description class. See the - * JavaBeans specification for more information: - * http://java.sun.com/products/javabeans/docs/index.html - * </p> - * - * @author Iain McGinniss - * @author Nat Pryce - * @author Steve Freeman - */ -public class HasPropertyWithValue<T> extends TypeSafeDiagnosingMatcher<T> { - private static final Condition.Step<PropertyDescriptor,Method> WITH_READ_METHOD = withReadMethod(); - private final String propertyName; - private final Matcher<Object> valueMatcher; - - public HasPropertyWithValue(String propertyName, Matcher<?> valueMatcher) { - this.propertyName = propertyName; - this.valueMatcher = nastyGenericsWorkaround(valueMatcher); - } - - @Override - public boolean matchesSafely(T bean, Description mismatch) { - return propertyOn(bean, mismatch) - .and(WITH_READ_METHOD) - .and(withPropertyValue(bean)) - .matching(valueMatcher, "property '" + propertyName + "' "); - } - - @Override - public void describeTo(Description description) { - description.appendText("hasProperty(").appendValue(propertyName).appendText(", ") - .appendDescriptionOf(valueMatcher).appendText(")"); - } - - private Condition<PropertyDescriptor> propertyOn(T bean, Description mismatch) { - PropertyDescriptor property = PropertyUtil.getPropertyDescriptor(propertyName, bean); - if (property == null) { - mismatch.appendText("No property \"" + propertyName + "\""); - return notMatched(); - } - - return matched(property, mismatch); - } - - private Condition.Step<Method, Object> withPropertyValue(final T bean) { - return new Condition.Step<Method, Object>() { - @Override - public Condition<Object> apply(Method readMethod, Description mismatch) { - try { - return matched(readMethod.invoke(bean, NO_ARGUMENTS), mismatch); - } catch (Exception e) { - mismatch.appendText(e.getMessage()); - return notMatched(); - } - } - }; - } - - @SuppressWarnings("unchecked") - private static Matcher<Object> nastyGenericsWorkaround(Matcher<?> valueMatcher) { - return (Matcher<Object>) valueMatcher; - } - - private static Condition.Step<PropertyDescriptor,Method> withReadMethod() { - return new Condition.Step<PropertyDescriptor, java.lang.reflect.Method>() { - @Override - public Condition<Method> apply(PropertyDescriptor property, Description mismatch) { - final Method readMethod = property.getReadMethod(); - if (null == readMethod) { - mismatch.appendText("property \"" + property.getName() + "\" is not readable"); - return notMatched(); - } - return matched(readMethod, mismatch); - } - }; - } - - /** - * Creates a matcher that matches when the examined object has a JavaBean property - * with the specified name whose value satisfies the specified matcher. - * For example: - * <pre>assertThat(myBean, hasProperty("foo", equalTo("bar"))</pre> - * - * @param propertyName - * the name of the JavaBean property that examined beans should possess - * @param valueMatcher - * a matcher for the value of the specified property of the examined bean - */ - public static <T> Matcher<T> hasProperty(String propertyName, Matcher<?> valueMatcher) { - return new HasPropertyWithValue<T>(propertyName, valueMatcher); - } -} diff --git a/hamcrest-library/src/main/java/org/hamcrest/beans/PropertyUtil.java b/hamcrest-library/src/main/java/org/hamcrest/beans/PropertyUtil.java deleted file mode 100644 index 946c4f8..0000000 --- a/hamcrest-library/src/main/java/org/hamcrest/beans/PropertyUtil.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.hamcrest.beans; - -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; - -/** - * Utility class for accessing properties on JavaBean objects. - * See http://java.sun.com/products/javabeans/docs/index.html for - * more information on JavaBeans. - * - * @author Iain McGinniss - * @author Steve Freeman - * @since 1.1.0 - */ -public class PropertyUtil { - /** - * Returns the description of the property with the provided - * name on the provided object's interface. - * - * @return the descriptor of the property, or null if the property does not exist. - * @throws IllegalArgumentException if there's a introspection failure - */ - public static PropertyDescriptor getPropertyDescriptor(String propertyName, Object fromObj) throws IllegalArgumentException { - for (PropertyDescriptor property : propertyDescriptorsFor(fromObj, null)) { - if (property.getName().equals(propertyName)) { - return property; - } - } - - return null; - } - - /** - * Returns all the property descriptors for the class associated with the given object - * - * @param fromObj Use the class of this object - * @param stopClass Don't include any properties from this ancestor class upwards. - * @return Property descriptors - * @throws IllegalArgumentException if there's a introspection failure - */ - public static PropertyDescriptor[] propertyDescriptorsFor(Object fromObj, Class<Object> stopClass) throws IllegalArgumentException { - try { - return Introspector.getBeanInfo(fromObj.getClass(), stopClass).getPropertyDescriptors(); - } catch (IntrospectionException e) { - throw new IllegalArgumentException("Could not get property descriptors for " + fromObj.getClass(), e); - } - } - - public static final Object[] NO_ARGUMENTS = new Object[0]; -} diff --git a/hamcrest-library/src/main/java/org/hamcrest/beans/SamePropertyValuesAs.java b/hamcrest-library/src/main/java/org/hamcrest/beans/SamePropertyValuesAs.java deleted file mode 100644 index df7eab7..0000000 --- a/hamcrest-library/src/main/java/org/hamcrest/beans/SamePropertyValuesAs.java +++ /dev/null @@ -1,140 +0,0 @@ -package org.hamcrest.beans; - -import org.hamcrest.Description; -import org.hamcrest.DiagnosingMatcher; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeDiagnosingMatcher; - -import java.beans.PropertyDescriptor; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS; -import static org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor; -import static org.hamcrest.core.IsEqual.equalTo; - -public class SamePropertyValuesAs<T> extends TypeSafeDiagnosingMatcher<T> { - private final T expectedBean; - private final Set<String> propertyNames; - private final List<PropertyMatcher> propertyMatchers; - - - public SamePropertyValuesAs(T expectedBean) { - PropertyDescriptor[] descriptors = propertyDescriptorsFor(expectedBean, Object.class); - this.expectedBean = expectedBean; - this.propertyNames = propertyNamesFrom(descriptors); - this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors); - } - - @Override - public boolean matchesSafely(T bean, Description mismatch) { - return isCompatibleType(bean, mismatch) - && hasNoExtraProperties(bean, mismatch) - && hasMatchingValues(bean, mismatch); - } - - @Override - public void describeTo(Description description) { - description.appendText("same property values as " + expectedBean.getClass().getSimpleName()) - .appendList(" [", ", ", "]", propertyMatchers); - } - - - private boolean isCompatibleType(T item, Description mismatchDescription) { - if (!expectedBean.getClass().isAssignableFrom(item.getClass())) { - mismatchDescription.appendText("is incompatible type: " + item.getClass().getSimpleName()); - return false; - } - return true; - } - - private boolean hasNoExtraProperties(T item, Description mismatchDescription) { - Set<String> actualPropertyNames = propertyNamesFrom(propertyDescriptorsFor(item, Object.class)); - actualPropertyNames.removeAll(propertyNames); - if (!actualPropertyNames.isEmpty()) { - mismatchDescription.appendText("has extra properties called " + actualPropertyNames); - return false; - } - return true; - } - - private boolean hasMatchingValues(T item, Description mismatchDescription) { - for (PropertyMatcher propertyMatcher : propertyMatchers) { - if (!propertyMatcher.matches(item)) { - propertyMatcher.describeMismatch(item, mismatchDescription); - return false; - } - } - return true; - } - - private static <T> List<PropertyMatcher> propertyMatchersFor(T bean, PropertyDescriptor[] descriptors) { - List<PropertyMatcher> result = new ArrayList<PropertyMatcher>(descriptors.length); - for (PropertyDescriptor propertyDescriptor : descriptors) { - result.add(new PropertyMatcher(propertyDescriptor, bean)); - } - return result; - } - - private static Set<String> propertyNamesFrom(PropertyDescriptor[] descriptors) { - HashSet<String> result = new HashSet<String>(); - for (PropertyDescriptor propertyDescriptor : descriptors) { - result.add(propertyDescriptor.getDisplayName()); - } - return result; - } - - public static class PropertyMatcher extends DiagnosingMatcher<Object> { - private final Method readMethod; - private final Matcher<Object> matcher; - private final String propertyName; - - public PropertyMatcher(PropertyDescriptor descriptor, Object expectedObject) { - this.propertyName = descriptor.getDisplayName(); - this.readMethod = descriptor.getReadMethod(); - this.matcher = equalTo(readProperty(readMethod, expectedObject)); - } - - @Override - public boolean matches(Object actual, Description mismatch) { - final Object actualValue = readProperty(readMethod, actual); - if (!matcher.matches(actualValue)) { - mismatch.appendText(propertyName + " "); - matcher.describeMismatch(actualValue, mismatch); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText(propertyName + ": ").appendDescriptionOf(matcher); - } - } - - private static Object readProperty(Method method, Object target) { - try { - return method.invoke(target, NO_ARGUMENTS); - } catch (Exception e) { - throw new IllegalArgumentException("Could not invoke " + method + " on " + target, e); - } - } - - /** - * Creates a matcher that matches when the examined object has values for all of - * its JavaBean properties that are equal to the corresponding values of the - * specified bean. - * For example: - * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre> - * - * @param expectedBean - * the bean against which examined beans are compared - */ - public static <T> Matcher<T> samePropertyValuesAs(T expectedBean) { - return new SamePropertyValuesAs<T>(expectedBean); - } - -} diff --git a/hamcrest-library/src/main/java/org/hamcrest/beans/package.html b/hamcrest-library/src/main/java/org/hamcrest/beans/package.html deleted file mode 100644 index 0dcc555..0000000 --- a/hamcrest-library/src/main/java/org/hamcrest/beans/package.html +++ /dev/null @@ -1,7 +0,0 @@ -<html> -<head> -</head> -<body> - <p>Matchers of Java Bean properties and their values.</p> -</body> -</html> |