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 16 protected Matcher<?> createMatcher() { 17 return samePropertyValuesAs(expectedBean); 18 } 19 20 public void testReportsMatchWhenAllPropertiesMatch() { 21 assertMatches("matched properties", samePropertyValuesAs(expectedBean), actualBean); 22 } 23 24 public void testReportsMismatchWhenActualTypeIsNotAssignableToExpectedType() { 25 assertMismatchDescription("is incompatible type: ExampleBean", 26 samePropertyValuesAs((Object)aValue), actualBean); 27 } 28 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 38 public void testMatchesBeansWithInheritanceButNoExtraProperties() { 39 assertMatches("sub type with same properties", 40 samePropertyValuesAs(expectedBean), new SubBeanWithNoExtraProperties("same", 1, aValue)); 41 } 42 43 public void testRejectsSubTypeThatHasExtraProperties() { 44 assertMismatchDescription("has extra properties called [extra]", 45 samePropertyValuesAs(expectedBean), new SubBeanWithExtraProperty("same", 1, aValue)); 46 } 47 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 { 53 public Value(Object value) { 54 this.value = value; 55 } 56 57 public final Object value; 58 @Override 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 69 public ExampleBean(String stringProperty, int intProperty, Value valueProperty) { 70 this.stringProperty = stringProperty; 71 this.intProperty = intProperty; 72 this.valueProperty = valueProperty; 73 } 74 75 public String getString() { 76 return stringProperty; 77 } 78 public int getInt() { 79 return intProperty; 80 } 81 public Value getValue() { 82 return valueProperty; 83 } 84 } 85 86 public static class SubBeanWithNoExtraProperties extends ExampleBean { 87 public SubBeanWithNoExtraProperties(String stringProperty, int intProperty, Value valueProperty) { 88 super(stringProperty, intProperty, valueProperty); 89 } 90 } 91 92 public static class SubBeanWithExtraProperty extends ExampleBean { 93 public SubBeanWithExtraProperty(String stringProperty, int intProperty, Value valueProperty) { 94 super(stringProperty, intProperty, valueProperty); 95 } 96 public String getExtra() { return "extra"; } 97 } 98 } 99