1717package de.danielbechler.diff.introspection
1818
1919import de.danielbechler.diff.access.PropertyAwareAccessor
20- import de.danielbechler.diff.mock.ObjectWithInheritedPropertyAnnotation
21- import de.danielbechler.diff.mock.ObjectWithPropertyAnnotations
2220import de.danielbechler.diff.mock.ObjectWithString
23- import de.danielbechler.diff.selector.BeanPropertyElementSelector
2421import spock.lang.Specification
2522
2623import java.beans.BeanInfo
@@ -30,92 +27,74 @@ import java.beans.IntrospectionException
3027 * @author Daniel Bechler
3128 */
3229public class StandardBeanIntrospectorTest extends Specification {
33- StandardBeanIntrospector introspector = new StandardBeanIntrospector ()
34-
35- // @Test(enabled = false)
36- // public void testIntrospectWithEqualsOnlyPropertyType() throws Exception {
37- // final Iterable<PropertyAwareAccessor> accessors = introspector.introspect(ObjectWithEqualsOnlyPropertyType.class);
38- // assertThat(accessors.iterator().hasNext(), is(true));
39- // // final PropertyAwareAccessor propertyAwareAccessor = accessors.iterator().next();
40- // // assertThat(propertyAwareAccessor.getComparisonStrategy(), instanceOf(EqualsOnlyComparisonStrategy.class));
41- // }
42- //
43- // @Test
44- // public void testIntrospectWithEqualsOnlyPropertyTypeAndValueProviderMethod() throws Exception {
45- // final Object object = new Object()
46- // {
47- // public ObjectWithObjectDiffEqualsOnlyTypeAnnotationAndValueProviderMethod getValue() {
48- // return null;
49- // }
50- // };
51- //
52- // final Iterable<PropertyAwareAccessor> accessors = introspector.introspect(object.getClass());
53- // assertThat(accessors.iterator().hasNext(), is(true));
54- //
55- // final PropertyAwareAccessor propertyAwareAccessor = accessors.iterator().next();
56- //
57- // // final ComparisonStrategy comparisonStrategy = propertyAwareAccessor.getComparisonStrategy();
58- // // assertThat(comparisonStrategy, is(instanceOf(EqualsOnlyComparisonStrategy.class)));
59- //
60- // // final EqualsOnlyComparisonStrategy equalsOnlyComparisonStrategy = (EqualsOnlyComparisonStrategy) comparisonStrategy;
61- // // assertThat(equalsOnlyComparisonStrategy.getEqualsValueProviderMethod(), is(IsEqual.equalTo("foo")));
62- // }
63-
64- class ObjectWithIgnoredProperty {
65- @ObjectDiffProperty (excluded = true )
66- def getProperty () {
30+
31+ def introspector = new StandardBeanIntrospector ()
32+
33+ private Map<String , PropertyAwareAccessor > introspect (Class<?> type ) {
34+ introspector. introspect(type). collectEntries {
35+ accessor -> [accessor. propertyName, accessor]
6736 }
6837 }
6938
70- def ' excluded property' () {
39+ def ' should return proper accessor for property' () {
7140 when :
72- PropertyAwareAccessor propertyAccessor = introspector. introspect(ObjectWithIgnoredProperty ). find({
73- it. propertyName == ' property' ? it : null
74- }) as PropertyAwareAccessor
41+ def accessor = introspect(TypeWithOnlyOneProperty ). get(' value' )
7542 then :
76- propertyAccessor. isExcluded()
43+ accessor. propertyName == ' value'
44+ and :
45+ def target = new TypeWithOnlyOneProperty ()
46+ accessor. get(target) == null
47+ and :
48+ accessor. set(target, ' bar' )
49+ accessor. get(target) == ' bar'
50+ and :
51+ accessor. excluded == false
52+ and :
53+ accessor. categories. isEmpty()
7754 }
7855
79- def ' Introspect With Property Annotations ' () {
56+ def ' should return PropertyAwareAccessors for each property of the given class ' () {
8057 when :
81- Iterable<PropertyAwareAccessor > accessors = introspector. introspect(ObjectWithPropertyAnnotations . class);
58+ def accessors = introspect(TypeWithTwoProperties )
59+ then :
60+ accessors. size() == 2
61+ accessors. get(' foo' ) != null
62+ accessors. get(' bar' ) != null
63+ }
8264
65+ def ' should apply categories of ObjectDiffProperty annotation to accessor' () {
66+ when :
67+ def accessor = introspect(TypeWithPropertyAnnotation ). get(' value' )
8368 then :
84- for (final PropertyAwareAccessor accessor : accessors) {
85- if (accessor. getElementSelector(). equals(new BeanPropertyElementSelector (" ignored" ))) {
86- assert accessor. isExcluded()
87- } else if (accessor. getElementSelector(). equals(new BeanPropertyElementSelector (" categorized" ))) {
88- assert accessor. getCategories(). size() == 1
89- assert accessor. getCategories(). containsAll([' foo' ])
90- } else if (accessor. getElementSelector(). equals(new BeanPropertyElementSelector (" item" ))) {
91- assert accessor. isExcluded() == false
92- assert accessor. getCategories(). isEmpty()
93- } else if (accessor. getElementSelector(). equals(new BeanPropertyElementSelector (" key" ))) {
94- // no op
95- } else if (accessor. getElementSelector(). equals(new BeanPropertyElementSelector (" value" ))) {
96- // no op
97- } else {
98- assert false : " Unexpected accessor: " + accessor. getElementSelector()
99- }
100- }
69+ accessor. categories. size() == 2
70+ accessor. categories. containsAll([' category1' , ' category2' ])
10171 }
10272
103- def IntrospectWithInheritedPropertyAnnotations () {
73+ def ' should apply exclusion of ObjectDiffProperty annotation to accessor ' () {
10474 when :
105- PropertyAwareAccessor accessor = introspector . introspect(ObjectWithInheritedPropertyAnnotation ) . first();
75+ def accessor = introspect(TypeWithPropertyAnnotation ) . get( ' value ' )
10676 then :
107- accessor. getElementSelector() == new BeanPropertyElementSelector (" value" )
108- accessor. isExcluded()
77+ accessor. excluded == true
10978 }
11079
111- def IntrospectWithNullType () {
80+ def ' should throw exception when invoked without type ' () {
11281 when :
113- introspector. introspect(null );
82+ introspector. introspect(null )
11483 then :
11584 thrown(IllegalArgumentException )
11685 }
11786
118- def IntrospectWithSimulatedIntrospectionException () {
87+ def ' should skip default class properties' () {
88+ expect :
89+ introspect(TypeWithNothingButDefaultProperties ). isEmpty()
90+ }
91+
92+ def ' should skip properties without getter' () {
93+ expect :
94+ introspect(TypeWithPropertyWithoutGetter ). isEmpty()
95+ }
96+
97+ def ' should wrap IntrospectionException with RuntimeException' () {
11998 given :
12099 introspector = new StandardBeanIntrospector () {
121100 @Override
@@ -128,4 +107,37 @@ public class StandardBeanIntrospectorTest extends Specification {
128107 then :
129108 thrown(RuntimeException )
130109 }
110+
111+ private class TypeWithNothingButDefaultProperties {
112+ }
113+
114+ private class TypeWithPropertyWithoutGetter {
115+ private String value
116+
117+ void setValue (String value ) {
118+ this . value = value
119+ }
120+ }
121+
122+ private class TypeWithPropertyAnnotation {
123+ private String value
124+
125+ @ObjectDiffProperty (excluded = true , categories = [' category1' , ' category2' ])
126+ String getValue () {
127+ return value
128+ }
129+
130+ void setValue (String value ) {
131+ this . value = value
132+ }
133+ }
134+
135+ private class TypeWithOnlyOneProperty {
136+ def value
137+ }
138+
139+ private class TypeWithTwoProperties {
140+ def foo
141+ def bar
142+ }
131143}
0 commit comments