1 package com.google.code.beanmatchers; 2 3 import static com.google.code.beanmatchers.BeanOperations.properties; 4 import static java.util.Arrays.asList; 5 6 import java.util.List; 7 import org.hamcrest.Description; 8 9 public class HasValidBeanEqualsExcludingMatcher<T> extends AbstractBeanEqualsMatcher<T> { 10 private final List<String> excludedProperties; 11 12 HasValidBeanEqualsExcludingMatcher( 13 TypeBasedValueGenerator valueGenerator, String... excludedProperties) { 14 super(valueGenerator); 15 this.excludedProperties = asList(excludedProperties); 16 } 17 18 @Override 19 protected boolean matchesSafely(Class<T> beanType, Description mismatchDescription) { 20 List<String> properties = properties(beanType); 21 properties.removeAll(excludedProperties); 22 return isValidBeanEquals(beanType, properties, mismatchDescription); 23 } 24 25 @Override 26 public void describeTo(Description description) { 27 if (excludedProperties.isEmpty()) { 28 description.appendText("bean with all properties compared in equals"); 29 } else { 30 description.appendText("bean with all properties excluding "); 31 description.appendValue(excludedProperties); 32 description.appendText(" compared in equals"); 33 } 34 } 35 }