Skip to content

Commit e305880

Browse files
committed
Adds integration test for new field annotation feature
I also added the change to the CHANGELOG and fixed some minor code format issues.
1 parent 5f91ec4 commit e305880

File tree

6 files changed

+125
-40
lines changed

6 files changed

+125
-40
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.92.1
4+
5+
- **Feature:** Allow for checking field level annotations via DiffNode (Thanks [@NagyGa1](https://github.com/NagyGa1)) [#134]
6+
37
## 0.92
48

59
### Improvements
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2015 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.ObjectDiffer
20+
import de.danielbechler.diff.ObjectDifferBuilder
21+
import spock.lang.Specification
22+
23+
class AccessFieldAnnotationIT extends Specification {
24+
25+
ObjectDiffer objectDiffer = ObjectDifferBuilder.buildDefault()
26+
27+
def 'access field annotation'() {
28+
given:
29+
def working = new Foo(value: 'working')
30+
def base = new Foo(value: 'base')
31+
when:
32+
DiffNode node = objectDiffer.compare(working, base)
33+
then:
34+
node.getChild('value').getFieldAnnotation(TestAnnotation).value() == "it works"
35+
}
36+
37+
class Foo {
38+
@TestAnnotation("it works")
39+
private String value
40+
41+
public String getValue() {
42+
return value
43+
}
44+
45+
public void setValue(String value) {
46+
this.value = value
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2015 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.Inherited;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
@Retention(RetentionPolicy.RUNTIME)
26+
@Target(ElementType.FIELD)
27+
@Inherited
28+
public @interface TestAnnotation
29+
{
30+
String value();
31+
}

Diff for: src/main/java/de/danielbechler/diff/introspection/PropertyAccessor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ private Set<Annotation> getFieldAnnotations(final Class<?> clazz)
9494
try
9595
{
9696
return new LinkedHashSet<Annotation>(asList(clazz.getDeclaredField(propertyName).getAnnotations()));
97-
} catch (NoSuchFieldException e)
97+
}
98+
catch (final NoSuchFieldException e)
9899
{
99100
if (clazz.getSuperclass() != null)
100101
{

Diff for: src/main/java/de/danielbechler/diff/node/DiffNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public final void visitParents(final Visitor visitor)
445445

446446
/**
447447
* If this node represents a bean property this method returns all annotations of its field.
448-
*
448+
* <p/>
449449
* Only works for fields having a name that matches the name derived from the getter.
450450
*
451451
* @return The annotations of the field, or an empty set if there is no field with the name derived from the getter.

Diff for: src/test/java/de/danielbechler/diff/node/DiffNodeFieldAnnotationsTest.groovy

+38-38
Original file line numberDiff line numberDiff line change
@@ -25,70 +25,70 @@ class DiffNodeFieldAnnotationsTest extends Specification {
2525

2626
def 'getFieldAnnotation(): returns null if not PropertyAwareAccessor'() {
2727
given:
28-
def accessor = Mock(Accessor) {
29-
getElementSelector() >> new BeanPropertyElementSelector('f')
30-
}
31-
def diffNode = new DiffNode(null, accessor, A)
28+
def accessor = Mock(Accessor) {
29+
getElementSelector() >> new BeanPropertyElementSelector('f')
30+
}
31+
def diffNode = new DiffNode(null, accessor, A)
3232
expect:
33-
diffNode.fieldAnnotations.size() == 0
34-
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
33+
diffNode.fieldAnnotations.size() == 0
34+
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
3535
}
3636

3737
def 'getFieldAnnotation(): class has the field and annotated'() {
3838
given:
39-
def accessor = new PropertyAccessor("f", A.getMethod("getF"), A.getMethod("setF", String))
40-
def diffNode = new DiffNode(null, accessor, A)
39+
def accessor = new PropertyAccessor("f", A.getMethod("getF"), A.getMethod("setF", String))
40+
def diffNode = new DiffNode(null, accessor, A)
4141
expect:
42-
diffNode.fieldAnnotations.size() == 1
43-
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
44-
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
42+
diffNode.fieldAnnotations.size() == 1
43+
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
44+
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
4545
}
4646

4747
def 'getFieldAnnotation(): class does not have the field, or different name'() {
4848
given:
49-
def accessor = new PropertyAccessor("F", ADiffName.getMethod("getF"), null)
50-
def diffNode = new DiffNode(null, accessor, ADiffName)
49+
def accessor = new PropertyAccessor("F", ADiffName.getMethod("getF"), null)
50+
def diffNode = new DiffNode(null, accessor, ADiffName)
5151
expect:
52-
diffNode.fieldAnnotations.size() == 0
53-
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
52+
diffNode.fieldAnnotations.size() == 0
53+
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
5454
}
5555

5656
def 'getFieldAnnotation(): inheritance'() {
5757
given:
58-
def accessor = new PropertyAccessor("f", AB.getMethod("getF"), AB.getMethod("setF", String))
59-
def diffNode = new DiffNode(null, accessor, AB)
58+
def accessor = new PropertyAccessor("f", AB.getMethod("getF"), AB.getMethod("setF", String))
59+
def diffNode = new DiffNode(null, accessor, AB)
6060
expect:
61-
diffNode.fieldAnnotations.size() == 1
62-
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
63-
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
61+
diffNode.fieldAnnotations.size() == 1
62+
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
63+
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
6464
}
6565

6666
def 'getFieldAnnotation(): inheritance, overridden getter'() {
6767
given:
68-
def accessor = new PropertyAccessor("f", ABGetter.getMethod("getF"), ABGetter.getMethod("setF", String))
69-
def diffNode = new DiffNode(null, accessor, ABGetter)
68+
def accessor = new PropertyAccessor("f", ABGetter.getMethod("getF"), ABGetter.getMethod("setF", String))
69+
def diffNode = new DiffNode(null, accessor, ABGetter)
7070
expect:
71-
diffNode.fieldAnnotations.size() == 1
72-
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
73-
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
71+
diffNode.fieldAnnotations.size() == 1
72+
diffNode.getFieldAnnotation(SomeFieldAnnotation) != null
73+
diffNode.getFieldAnnotation(SomeFieldAnnotation).annotationType() == SomeFieldAnnotation
7474
}
7575

7676
def 'getFieldAnnotation(): inheritance, not annotated'() {
7777
given:
78-
def accessor = new PropertyAccessor("f", NAB.getMethod("getF"), NAB.getMethod("setF", String))
79-
def diffNode = new DiffNode(null, accessor, NAB)
78+
def accessor = new PropertyAccessor("f", NAB.getMethod("getF"), NAB.getMethod("setF", String))
79+
def diffNode = new DiffNode(null, accessor, NAB)
8080
expect:
81-
diffNode.fieldAnnotations.size() == 0
82-
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
81+
diffNode.fieldAnnotations.size() == 0
82+
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
8383
}
8484

8585
def 'getFieldAnnotation(): inheritance, overridden getter, not annotated'() {
8686
given:
87-
def accessor = new PropertyAccessor("f", NABGetter.getMethod("getF"), NABGetter.getMethod("setF", String))
88-
def diffNode = new DiffNode(null, accessor, NABGetter)
87+
def accessor = new PropertyAccessor("f", NABGetter.getMethod("getF"), NABGetter.getMethod("setF", String))
88+
def diffNode = new DiffNode(null, accessor, NABGetter)
8989
expect:
90-
diffNode.fieldAnnotations.size() == 0
91-
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
90+
diffNode.fieldAnnotations.size() == 0
91+
diffNode.getFieldAnnotation(SomeFieldAnnotation) == null
9292
}
9393

9494
public static class A {
@@ -100,11 +100,11 @@ class DiffNodeFieldAnnotationsTest extends Specification {
100100
String f;
101101
}
102102

103-
public static class ADiffName {
104-
public String getF() {
105-
return null;
106-
}
107-
}
103+
public static class ADiffName {
104+
public String getF() {
105+
return null;
106+
}
107+
}
108108

109109
public static class AB extends A {
110110
}

0 commit comments

Comments
 (0)