View Javadoc
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 HasToStringDescribingPropertiesExcludingMatcher<T>
10      extends AbstractBeanToStringMatcher<T> {
11  
12    private final List<String> excludedProperties;
13  
14    public HasToStringDescribingPropertiesExcludingMatcher(
15        TypeBasedValueGenerator valueGenerator, String... excludedProperties) {
16      super(valueGenerator);
17      this.excludedProperties = asList(excludedProperties);
18    }
19  
20    @Override
21    protected boolean matchesSafely(Class beanType, Description mismatchDescription) {
22      List<String> properties = properties(beanType);
23      properties.removeAll(excludedProperties);
24      return super.toStringDescribesProperties(beanType, properties, mismatchDescription);
25    }
26  
27    @Override
28    public void describeTo(Description description) {
29      if (excludedProperties.isEmpty()) {
30        description.appendText("bean with toString() describing class name and all properties");
31      } else {
32        description.appendText(
33            "bean with toString() describing class name and all properties excluding ");
34        description.appendValue(excludedProperties);
35      }
36    }
37  }