21
21
import com .fasterxml .jackson .annotation .JsonAnyGetter ;
22
22
import com .fasterxml .jackson .annotation .JsonProperty ;
23
23
import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
24
+ import com .fasterxml .jackson .dataformat .xml .annotation .JacksonXmlProperty ;
24
25
import org .inferred .freebuilder .processor .Analyser .CannotGenerateCodeException ;
25
26
import org .inferred .freebuilder .processor .Metadata .Property ;
26
27
import org .inferred .freebuilder .processor .util .Excerpt ;
34
35
import org .junit .runners .JUnit4 ;
35
36
36
37
import java .util .Map ;
38
+ import java .util .Optional ;
37
39
38
40
import javax .lang .model .element .TypeElement ;
39
41
@@ -85,7 +87,25 @@ public void jacksonAnnotationAddedWithExplicitName() throws CannotGenerateCodeEx
85
87
Metadata metadata = analyser .analyse (dataType );
86
88
87
89
Property property = getOnlyElement (metadata .getProperties ());
88
- assertPropertyHasJsonPropertyAnnotation (property , "bob" );
90
+ assertPropertyHasAnnotation (property , JsonProperty .class , "@JsonProperty(\" bob\" )" );
91
+ }
92
+ @ Test
93
+ public void jacksonXmlAnnotationAddedWithExplicitName () throws CannotGenerateCodeException {
94
+ // See also https://github.com/google/FreeBuilder/issues/68
95
+ TypeElement dataType = model .newType (
96
+ "package com.example;" ,
97
+ "import " + JacksonXmlProperty .class .getName () + ";" ,
98
+ "@" + JsonDeserialize .class .getName () + "(builder = DataType.Builder.class)" ,
99
+ "public interface DataType {" ,
100
+ " @JacksonXmlProperty(localName=\" b-ob\" ) int getFooBar();" ,
101
+ " class Builder extends DataType_Builder {}" ,
102
+ "}" );
103
+
104
+ Metadata metadata = analyser .analyse (dataType );
105
+
106
+ Property property = getOnlyElement (metadata .getProperties ());
107
+ assertPropertyHasAnnotation (property ,
108
+ JacksonXmlProperty .class , "@JacksonXmlProperty(localName = \" b-ob\" )" );
89
109
}
90
110
91
111
@ Test
@@ -102,7 +122,7 @@ public void jacksonAnnotationAddedWithImplicitName() throws CannotGenerateCodeEx
102
122
Metadata metadata = analyser .analyse (dataType );
103
123
104
124
Property property = getOnlyElement (metadata .getProperties ());
105
- assertPropertyHasJsonPropertyAnnotation (property , " fooBar" );
125
+ assertPropertyHasAnnotation (property , JsonProperty . class , "@JsonProperty( \" fooBar\" ) " );
106
126
}
107
127
108
128
@ Test
@@ -122,11 +142,15 @@ public void jsonAnyGetterAnnotationDisablesImplicitProperty() throws CannotGener
122
142
assertThat (property .getAccessorAnnotations ()).named ("property accessor annotations" ).isEmpty ();
123
143
}
124
144
125
- private static void assertPropertyHasJsonPropertyAnnotation (
126
- Property property , String propertyName ) {
127
- assertThat (property .getAccessorAnnotations ()).named ("property accessor annotations" ).hasSize (1 );
128
- Excerpt accessorAnnotation = getOnlyElement (property .getAccessorAnnotations ());
129
- assertThat (asString (accessorAnnotation )).isEqualTo ("@JsonProperty(\" " + propertyName + "\" )\n " );
145
+ private void assertPropertyHasAnnotation (Property property , Class annotationClass ,
146
+ String annotationString ) {
147
+ Optional <Excerpt > annotationExcerpt = property .getAccessorAnnotations ()
148
+ .stream ()
149
+ .filter (excerpt -> excerpt .toString ().contains (annotationClass .getCanonicalName ()))
150
+ .findFirst ();
151
+ assertThat (annotationExcerpt ).named ("property accessor annotations" ).isNotNull ();
152
+ assertThat (asString (annotationExcerpt .get ()))
153
+ .isEqualTo (String .format ("%s%n" , annotationString ));
130
154
}
131
155
132
156
private static String asString (Excerpt excerpt ) {
0 commit comments