Skip to content

Commit 64180b9

Browse files
author
Gabor Nagy
committed
DiffNodeFieldAnnotationsTest.groovy unit tests
1 parent e156e69 commit 64180b9

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed

src/main/java/de/danielbechler/diff/introspection/PropertyAccessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,16 @@ private Set<Annotation> getFieldAnnotations(final Class<?> clazz) {
103103
}
104104

105105
/**
106-
* @return The annotations of the field.
106+
* @return The annotations of the field, or an empty set if there is no field with the name derived from the getter.
107107
*/
108108
public Set<Annotation> getFieldAnnotations() {
109109
return getFieldAnnotations(readMethod.getDeclaringClass());
110110
}
111111

112+
/**
113+
* @return The given annotation of the field, or null if not annotated or if there is no field with the name derived
114+
* from the getter.
115+
*/
112116
public <T extends Annotation> T getFieldAnnotation(final Class<T> annotationClass) {
113117
final Set<? extends Annotation> annotations = getFieldAnnotations();
114118
assert (annotations != null) : "Something is wrong here. " +

src/main/java/de/danielbechler/diff/node/DiffNode.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public final void visitParents(final Visitor visitor)
448448
*
449449
* Only works for fields having a name that matches the name derived from the getter.
450450
*
451-
* @return A set of annotations of this nodes property field or an empty set.
451+
* @return The annotations of the field, or an empty set if there is no field with the name derived from the getter.
452452
*/
453453
public Set<Annotation> getFieldAnnotations()
454454
{
@@ -459,6 +459,12 @@ public Set<Annotation> getFieldAnnotations()
459459
return unmodifiableSet(Collections.<Annotation>emptySet());
460460
}
461461

462+
/**
463+
* @param annotationClass the annotation we are looking for
464+
* @param <T>
465+
* @return The given annotation of the field, or null if not annotated or if there is no field with the name derived
466+
* from the getter.
467+
*/
462468
public <T extends Annotation> T getFieldAnnotation(final Class<T> annotationClass)
463469
{
464470
if (accessor instanceof PropertyAwareAccessor)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2014 Daniel Bechler
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package de.danielbechler.diff.node
18+
19+
import de.danielbechler.diff.access.Accessor
20+
import de.danielbechler.diff.introspection.PropertyAccessor
21+
import de.danielbechler.diff.selector.BeanPropertyElementSelector
22+
import spock.lang.Specification
23+
24+
class DiffNodeFieldAnnotationsTest extends Specification {
25+
26+
def 'getFieldAnnotation(): returns null if not PropertyAwareAccessor'() {
27+
given:
28+
def accessor = Mock(Accessor) {
29+
getElementSelector() >> new BeanPropertyElementSelector('f')
30+
}
31+
def diffNode = new DiffNode(null, accessor, A)
32+
expect:
33+
diffNode.fieldAnnotations.size() == 0
34+
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
35+
}
36+
37+
def 'getFieldAnnotation(): class has the field and annotated'() {
38+
given:
39+
def accessor = new PropertyAccessor("f", A.getMethod("getF"), A.getMethod("setF", String))
40+
def diffNode = new DiffNode(null, accessor, A)
41+
expect:
42+
diffNode.fieldAnnotations.size() == 1
43+
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
44+
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
45+
}
46+
47+
def 'getFieldAnnotation(): class does not have the field, or different name'() {
48+
given:
49+
def accessor = new PropertyAccessor("F", ADiffName.getMethod("getF"), null)
50+
def diffNode = new DiffNode(null, accessor, ADiffName)
51+
expect:
52+
diffNode.fieldAnnotations.size() == 0
53+
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
54+
}
55+
56+
def 'getFieldAnnotation(): inheritance'() {
57+
given:
58+
def accessor = new PropertyAccessor("f", AB.getMethod("getF"), AB.getMethod("setF", String))
59+
def diffNode = new DiffNode(null, accessor, AB)
60+
expect:
61+
diffNode.fieldAnnotations.size() == 1
62+
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
63+
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
64+
}
65+
66+
def 'getFieldAnnotation(): inheritance, overridden getter'() {
67+
given:
68+
def accessor = new PropertyAccessor("f", ABGetter.getMethod("getF"), ABGetter.getMethod("setF", String))
69+
def diffNode = new DiffNode(null, accessor, ABGetter)
70+
expect:
71+
diffNode.fieldAnnotations.size() == 1
72+
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
73+
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
74+
}
75+
76+
def 'getFieldAnnotation(): inheritance, not annotated'() {
77+
given:
78+
def accessor = new PropertyAccessor("f", NAB.getMethod("getF"), NAB.getMethod("setF", String))
79+
def diffNode = new DiffNode(null, accessor, NAB)
80+
expect:
81+
diffNode.fieldAnnotations.size() == 0
82+
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
83+
}
84+
85+
def 'getFieldAnnotation(): inheritance, overridden getter, not annotated'() {
86+
given:
87+
def accessor = new PropertyAccessor("f", NABGetter.getMethod("getF"), NABGetter.getMethod("setF", String))
88+
def diffNode = new DiffNode(null, accessor, NABGetter)
89+
expect:
90+
diffNode.fieldAnnotations.size() == 0
91+
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
92+
}
93+
94+
public static class A {
95+
@SomeFieldAnnotation
96+
String f;
97+
}
98+
99+
public static class NA {
100+
String f;
101+
}
102+
103+
public static class ADiffName {
104+
public String getF() {
105+
return null;
106+
}
107+
}
108+
109+
public static class AB extends A {
110+
}
111+
112+
public static class ABGetter extends A {
113+
@Override
114+
public String getF() {
115+
return null;
116+
}
117+
}
118+
119+
public static class NAB extends NA {
120+
}
121+
122+
public static class NABGetter extends NA {
123+
@Override
124+
public String getF() {
125+
return null;
126+
}
127+
}
128+
129+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2013 Daniel Bechler
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package de.danielbechler.diff.node;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
@Retention(RetentionPolicy.RUNTIME)
25+
@Target(ElementType.FIELD)
26+
public @interface SomeFieldAnnotation {
27+
}

0 commit comments

Comments
 (0)