• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.hamcrest.beans;
2 
3 import org.hamcrest.AbstractMatcherTest;
4 import org.hamcrest.Matcher;
5 
6 import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs;
7 
8 @SuppressWarnings("UnusedDeclaration")
9 public class SamePropertyValuesAsTest extends AbstractMatcherTest {
10   private static final Value aValue = new Value("expected");
11   private static final ExampleBean expectedBean = new ExampleBean("same", 1, aValue);
12   private static final ExampleBean actualBean = new ExampleBean("same", 1, aValue);
13 
14 
15   @Override
createMatcher()16   protected Matcher<?> createMatcher() {
17     return samePropertyValuesAs(expectedBean);
18   }
19 
testReportsMatchWhenAllPropertiesMatch()20   public void testReportsMatchWhenAllPropertiesMatch() {
21     assertMatches("matched properties", samePropertyValuesAs(expectedBean), actualBean);
22   }
23 
testReportsMismatchWhenActualTypeIsNotAssignableToExpectedType()24   public void testReportsMismatchWhenActualTypeIsNotAssignableToExpectedType() {
25     assertMismatchDescription("is incompatible type: ExampleBean",
26                               samePropertyValuesAs((Object)aValue), actualBean);
27   }
28 
testReportsMismatchOnFirstPropertyDifference()29   public void testReportsMismatchOnFirstPropertyDifference() {
30     assertMismatchDescription("string was \"different\"",
31         samePropertyValuesAs(expectedBean), new ExampleBean("different", 1, aValue));
32     assertMismatchDescription("int was <2>",
33         samePropertyValuesAs(expectedBean), new ExampleBean("same", 2, aValue));
34     assertMismatchDescription("value was <Value other>",
35         samePropertyValuesAs(expectedBean), new ExampleBean("same", 1, new Value("other")));
36   }
37 
testMatchesBeansWithInheritanceButNoExtraProperties()38   public void testMatchesBeansWithInheritanceButNoExtraProperties() {
39     assertMatches("sub type with same properties",
40         samePropertyValuesAs(expectedBean), new SubBeanWithNoExtraProperties("same", 1, aValue));
41   }
42 
testRejectsSubTypeThatHasExtraProperties()43   public void testRejectsSubTypeThatHasExtraProperties() {
44     assertMismatchDescription("has extra properties called [extra]",
45         samePropertyValuesAs(expectedBean), new SubBeanWithExtraProperty("same", 1, aValue));
46   }
47 
testDescribesItself()48   public void testDescribesItself() {
49     assertDescription("same property values as ExampleBean [int: <1>, string: \"same\", value: <Value expected>]", samePropertyValuesAs(expectedBean));
50   }
51 
52   public static class Value {
Value(Object value)53     public Value(Object value) {
54       this.value = value;
55     }
56 
57     public final Object value;
58     @Override
toString()59     public String toString() {
60       return "Value " + value;
61     }
62   }
63 
64   public static class ExampleBean {
65     private String stringProperty;
66     private int intProperty;
67     private Value valueProperty;
68 
ExampleBean(String stringProperty, int intProperty, Value valueProperty)69     public ExampleBean(String stringProperty, int intProperty, Value valueProperty) {
70       this.stringProperty = stringProperty;
71       this.intProperty = intProperty;
72       this.valueProperty = valueProperty;
73     }
74 
getString()75     public String getString() {
76       return stringProperty;
77     }
getInt()78     public int getInt() {
79       return intProperty;
80     }
getValue()81     public Value getValue() {
82       return valueProperty;
83     }
84   }
85 
86   public static class SubBeanWithNoExtraProperties extends ExampleBean {
SubBeanWithNoExtraProperties(String stringProperty, int intProperty, Value valueProperty)87     public SubBeanWithNoExtraProperties(String stringProperty, int intProperty, Value valueProperty) {
88       super(stringProperty, intProperty, valueProperty);
89     }
90   }
91 
92   public static class SubBeanWithExtraProperty extends ExampleBean {
SubBeanWithExtraProperty(String stringProperty, int intProperty, Value valueProperty)93     public SubBeanWithExtraProperty(String stringProperty, int intProperty, Value valueProperty) {
94       super(stringProperty, intProperty, valueProperty);
95     }
getExtra()96     public String getExtra() { return "extra"; }
97   }
98 }
99