1+ package org .hamcrest .beans ;
2+
3+ import static org .hamcrest .beans .PropertyUtil .NO_ARGUMENTS ;
4+ import static org .hamcrest .beans .PropertyUtil .propertyDescriptorsFor ;
5+
6+ import java .beans .PropertyDescriptor ;
7+ import java .lang .reflect .Method ;
8+ import java .util .ArrayList ;
9+ import java .util .HashSet ;
10+ import java .util .List ;
11+ import java .util .Set ;
12+
13+ import org .hamcrest .Description ;
14+ import org .hamcrest .DiagnosingMatcher ;
15+ import org .hamcrest .Factory ;
16+ import org .hamcrest .Matcher ;
17+ import org .hamcrest .TypeSafeDiagnosingMatcher ;
18+ import org .hamcrest .core .IsEqual ;
19+
20+ public class HasSamePropertyValuesAs <T > extends TypeSafeDiagnosingMatcher <T > {
21+ private final T expectedBean ;
22+ private final Set <String > propertyNames ;
23+ private final List <PropertyMatcher > propertyMatchers ;
24+
25+
26+ public HasSamePropertyValuesAs (T expectedBean ) {
27+ PropertyDescriptor [] descriptors = propertyDescriptorsFor (expectedBean , Object .class );
28+ this .expectedBean = expectedBean ;
29+ this .propertyNames = propertyNamesFrom (descriptors );
30+ this .propertyMatchers = propertyMatchersFor (expectedBean , descriptors );
31+ }
32+
33+ @ Override
34+ public boolean matchesSafely (T item , Description mismatchDescription ) {
35+ return isCompatibleType (item , mismatchDescription )
36+ && hasNoExtraProperties (item , mismatchDescription )
37+ && hasMatchingValues (item , mismatchDescription );
38+ }
39+
40+ private boolean isCompatibleType (T item , Description mismatchDescription ) {
41+ if (! expectedBean .getClass ().isAssignableFrom (item .getClass ())) {
42+ mismatchDescription .appendText ("is incompatible type: " + item .getClass ().getSimpleName ());
43+ return false ;
44+ }
45+ return true ;
46+ }
47+
48+ private boolean hasNoExtraProperties (T item , Description mismatchDescription ) {
49+ Set <String > actualPropertyNames = propertyNamesFrom (propertyDescriptorsFor (item , Object .class ));
50+ actualPropertyNames .removeAll (propertyNames );
51+ if (! actualPropertyNames .isEmpty ()) {
52+ mismatchDescription .appendText ("has extra properties called " + actualPropertyNames );
53+ return false ;
54+ }
55+ return true ;
56+ }
57+
58+ private boolean hasMatchingValues (T item , Description mismatchDescription ) {
59+ for (PropertyMatcher propertyMatcher : propertyMatchers ) {
60+ if (! propertyMatcher .matches (item )) {
61+ propertyMatcher .describeMismatch (item , mismatchDescription );
62+ return false ;
63+ }
64+ }
65+ return true ;
66+ }
67+
68+ public void describeTo (Description description ) {
69+ description .appendList ("has same property values: [" , ", " , "]" , propertyMatchers );
70+ }
71+
72+ private static <T > List <PropertyMatcher > propertyMatchersFor (T bean , PropertyDescriptor [] descriptors ) {
73+ List <PropertyMatcher > result = new ArrayList <PropertyMatcher >(descriptors .length );
74+ for (PropertyDescriptor propertyDescriptor : descriptors ) {
75+ result .add (new PropertyMatcher (propertyDescriptor , bean ));
76+ }
77+ return result ;
78+ }
79+
80+ private static Set <String > propertyNamesFrom (PropertyDescriptor [] descriptors ) {
81+ HashSet <String > result = new HashSet <String >();
82+ for (PropertyDescriptor propertyDescriptor : descriptors ) {
83+ result .add (propertyDescriptor .getDisplayName ());
84+ }
85+ return result ;
86+ }
87+
88+ public static class PropertyMatcher extends DiagnosingMatcher <Object > {
89+ private final Method readMethod ;
90+ private final Matcher <Object > matcher ;
91+ private final String propertyName ;
92+
93+ public PropertyMatcher (PropertyDescriptor descriptor , Object expectedObject ) {
94+ this .propertyName = descriptor .getDisplayName ();
95+ this .readMethod = descriptor .getReadMethod ();
96+ this .matcher = new IsEqual <Object >(readProperty (readMethod , expectedObject ));
97+ }
98+ @ Override
99+ public boolean matches (Object actual , Description mismatchDescription ) {
100+ Object actualValue = readProperty (readMethod , actual );
101+ if (! matcher .matches (actualValue )) {
102+ mismatchDescription .appendText (propertyName + " " );
103+ matcher .describeMismatch (actualValue , mismatchDescription );
104+ return false ;
105+ }
106+ return true ;
107+ }
108+
109+ public void describeTo (Description description ) {
110+ description .appendText (propertyName + ": " ).appendDescriptionOf (matcher );
111+ }
112+ }
113+
114+ private static Object readProperty (Method method , Object target ) {
115+ try {
116+ return method .invoke (target , NO_ARGUMENTS );
117+ } catch (Exception e ) {
118+ throw new IllegalArgumentException ("Could not invoke " + method + " on " + target , e );
119+ }
120+ }
121+
122+ @ Factory
123+ public static <T > Matcher <T > hasSamePropertyValuesAs (T expectedBean ) {
124+ return new HasSamePropertyValuesAs <T >(expectedBean );
125+ }
126+
127+ }
128+
0 commit comments