Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.exparity.hamcrest.beans;
import static org.apache.commons.lang.StringUtils.substringAfterLast;
import static org.exparity.beans.Type.type;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.exparity.beans.Type;
import org.exparity.beans.core.ImmutableTypeProperty;
import org.exparity.beans.core.TypeProperty;
import org.exparity.beans.core.naming.CapitalizedNamingStrategy;
import org.exparity.hamcrest.beans.comparators.Excluded;
import org.exparity.hamcrest.beans.comparators.Matches;
import org.exparity.hamcrest.beans.comparators.IsComparable;
import org.exparity.hamcrest.beans.comparators.IsEqual;
import org.exparity.hamcrest.beans.comparators.IsEqualTimestamp;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of a {@link Matcher} for performing a deep comparison of two
* objects by testing getters which start with get, is, or
* has are the same on each instances.
*
* When comparing elements in a collection or an array the {@link Matcher}
* orders a copy of the collection. If there is a default comparator then one
* will be used, or alternatively one will built using
* {@link org.apache.commons.lang.builder.CompareToBuilder#reflectionCompare(Object, Object)}
*
*
* @author Stewart Bissett
*/
public class TheSameAs extends TypeSafeDiagnosingMatcher {
/**
* Enumeration of the types of properties the matcher will check.
*
* @author Stewart Bissett
*/
public static enum PropertyType {
/**
* Check properties which follow the Java beans standard i.e. have both
* a get or is and set pair
*/
BEAN,
/**
* Check all getter style properties i.e. no arguments, method return
* name starts with get or is, and returns non-void.
*
* This is the default option if not property type is supplied.
*/
ALL_GETTERS
};
/**
* Creates a matcher that matches the full object graph for the given
* instance against another instance by comparing all getter properties
*
* For example:
*
*
* MyObject instance = new MyObject();
* dao.save(instance); // Save instance to persistent store
* assertThat(dao.getById(instance.getId()), theSameAs(instance);
*
*
* @param object
* the instance to match against
*/
@Factory
public static TheSameAs theSameAs(final T object) {
return new TheSameAs(object, PropertyType.ALL_GETTERS);
}
/**
* Creates a matcher that matches the full object graph for the given
* instance against another instance by comparing bean properties i.e.
* properties with both a getter and a setter
*
* For example:
*
*
* MyObject instance = new MyObject();
* dao.save(instance); // Save instance to persistent store
* assertThat(dao.getById(instance.getId()), theSameBeanAs(instance);
*
*
* @param object
* the instance to match against
*/
@Factory
public static TheSameAs theSameBeanAs(final T object) {
return new TheSameAs(object, PropertyType.BEAN);
}
/**
* Creates a matcher that matches the full object graph for the given
* instance against another instance
*
* For example:
*
*
* MyObject instance = new MyObject();
* dao.save(instance); // Save instance to persistent store
* assertThat(dao.getById(instance.getId()), theSameAs(instance, "MyInstance");
*
*
* @param object
* the instance to match against
* @param name
* the name given to the root entity
*/
@Factory
public static TheSameAs theSameAs(final T object, final String name) {
return new TheSameAs(object, name, PropertyType.ALL_GETTERS);
}
/**
* Creates a matcher that matches the full object graph for the given
* instance against another instance by comparing bean properties i.e.
* properties with both a getter and a setter
*
* For example:
*
*
* MyObject instance = new MyObject();
* dao.save(instance); // Save instance to persistent store
* assertThat(dao.getById(instance.getId()), theSameBeanAs(instance, "MyInstance");
*
*
* @param object
* the instance to match against
* @param name
* the name given to the root entity
*/
@Factory
public static TheSameAs theSameBeanAs(final T object, final String name) {
return new TheSameAs(object, name, PropertyType.BEAN);
}
private static final Logger LOG = LoggerFactory.getLogger(TheSameAs.class);
/**
* Interface to be implemented by classes which can compare two property
* values to confirm if they're equivalent
*/
public interface PropertyComparator {
/**
* Return true if the actual value matches the expected
* value
*/
public boolean matches(final T lhs, final T rhs);
}
@SuppressWarnings("rawtypes")
private static Comparator DEFAULT_COMPARATOR = new Comparator() {
public int compare(final Object o1, final Object o2) {
return CompareToBuilder.reflectionCompare(o1, o2);
}
};
private final Map> paths = new HashMap<>();
private final Map> properties = new HashMap<>();
private final Map, PropertyComparator>> types = new HashMap<>();
private final T object;
private final String name;
private final PropertyType propertyTypes;
public TheSameAs(final T object) {
this(object, PropertyType.BEAN);
}
public TheSameAs(final T object, final PropertyType propertyTypes) {
this(object, object.getClass().getSimpleName(), propertyTypes);
}
public TheSameAs(final T object, final String name, final PropertyType propertyTypes) {
this.types.put(BigDecimal.class, new IsComparable());
this.types.put(String.class, new IsEqual());
this.types.put(Integer.class, new IsEqual());
this.types.put(Long.class, new IsEqual());
this.types.put(Double.class, new IsEqual());
this.types.put(Float.class, new IsEqual());
this.types.put(Character.class, new IsEqual());
this.types.put(Date.class, new IsEqualTimestamp());
this.types.put(Class.class, new Excluded>());
this.object = object;
this.name = name;
this.propertyTypes = propertyTypes;
}
/**
* Exclude a property path from the comparison. For example
*
*
*
* class Person [
* private String firstName, lastName;
* public Person(final String firstName, final String lastName) {
* this.firstname = firstName;
* this.lastName = lastName;
* }
* public String getFirstName() { return firstName;};
* public String getLastName() { return lastName;};
* }
*
* // Testing a simple object is the same except for a property
* Person expected = new Person("Jane", "Doe");
* MatcherAssert.assertThat(new Person("John", "Doe"), BeanMatchers.theSameAs(expected).excludePath("Person.LastName"));
*
*
* @param property
* the path to exclude from the comparison e.g Person.LastName
* @return the current matcher
*/
public TheSameAs excludePath(final String path) {
this.paths.put(path.toLowerCase(), new Excluded