16
16
17
17
package org .springframework .boot .jackson ;
18
18
19
+ import java .io .IOException ;
20
+ import java .util .HashMap ;
21
+ import java .util .Map ;
22
+
23
+ import com .fasterxml .jackson .core .type .TypeReference ;
24
+ import com .fasterxml .jackson .databind .JsonMappingException ;
19
25
import com .fasterxml .jackson .databind .Module ;
20
26
import com .fasterxml .jackson .databind .ObjectMapper ;
21
27
import org .junit .After ;
24
30
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
25
31
26
32
import static org .assertj .core .api .Assertions .assertThat ;
33
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
27
34
28
35
/**
29
36
* Tests for {@link JsonComponentModule}.
30
37
*
31
38
* @author Phillip Webb
32
39
* @author Vladimir Tsanev
40
+ * @author Paul Aly
33
41
*/
34
42
public class JsonComponentModuleTests {
35
43
@@ -73,6 +81,38 @@ public void moduleShouldAllowInnerAbstractClasses() throws Exception {
73
81
context .close ();
74
82
}
75
83
84
+ @ Test
85
+ public void moduleShouldRegisterKeySerializers () throws Exception {
86
+ load (OnlyKeySerializer .class );
87
+ JsonComponentModule module = this .context .getBean (JsonComponentModule .class );
88
+ assertKeySerialize (module );
89
+ }
90
+
91
+ @ Test
92
+ public void moduleShouldRegisterKeyDeserializers () throws Exception {
93
+ load (OnlyKeyDeserializer .class );
94
+ JsonComponentModule module = this .context .getBean (JsonComponentModule .class );
95
+ assertKeyDeserialize (module );
96
+ }
97
+
98
+ @ Test
99
+ public void moduleShouldRegisterInnerClassesForKeyHandlers () throws Exception {
100
+ load (NameAndAgeJsonKeyComponent .class );
101
+ JsonComponentModule module = this .context .getBean (JsonComponentModule .class );
102
+ assertKeySerialize (module );
103
+ assertKeyDeserialize (module );
104
+ }
105
+
106
+ @ Test
107
+ public void moduleShouldRegisterOnlyForSpecifiedClasses () throws Exception {
108
+ load (NameAndCareerJsonComponent .class );
109
+ JsonComponentModule module = this .context .getBean (JsonComponentModule .class );
110
+ assertSerialize (module , new NameAndCareer ("spring" , "developer" ),
111
+ "{\" name\" :\" spring\" }" );
112
+ assertSerialize (module );
113
+ assertDeserializeForSpecifiedClasses (module );
114
+ }
115
+
76
116
private void load (Class <?>... configs ) {
77
117
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
78
118
context .register (configs );
@@ -81,11 +121,17 @@ private void load(Class<?>... configs) {
81
121
this .context = context ;
82
122
}
83
123
84
- private void assertSerialize (Module module ) throws Exception {
124
+ private void assertSerialize (Module module , Name value , String expectedJson )
125
+ throws Exception {
85
126
ObjectMapper mapper = new ObjectMapper ();
86
127
mapper .registerModule (module );
87
- String json = mapper .writeValueAsString (new NameAndAge ("spring" , 100 ));
88
- assertThat (json ).isEqualToIgnoringWhitespace ("{\" name\" :\" spring\" ,\" age\" :100}" );
128
+ String json = mapper .writeValueAsString (value );
129
+ assertThat (json ).isEqualToIgnoringWhitespace (expectedJson );
130
+ }
131
+
132
+ private void assertSerialize (Module module ) throws Exception {
133
+ assertSerialize (module , new NameAndAge ("spring" , 100 ),
134
+ "{\" name\" :\" spring\" ,\" age\" :100}" );
89
135
}
90
136
91
137
private void assertDeserialize (Module module ) throws Exception {
@@ -97,6 +143,37 @@ private void assertDeserialize(Module module) throws Exception {
97
143
assertThat (nameAndAge .getAge ()).isEqualTo (100 );
98
144
}
99
145
146
+ private void assertDeserializeForSpecifiedClasses (JsonComponentModule module )
147
+ throws IOException {
148
+ ObjectMapper mapper = new ObjectMapper ();
149
+ mapper .registerModule (module );
150
+ assertThatExceptionOfType (JsonMappingException .class ).isThrownBy (() -> mapper
151
+ .readValue ("{\" name\" :\" spring\" ,\" age\" :100}" , NameAndAge .class ));
152
+ NameAndCareer nameAndCareer = mapper .readValue (
153
+ "{\" name\" :\" spring\" ,\" career\" :\" developer\" }" , NameAndCareer .class );
154
+ assertThat (nameAndCareer .getName ()).isEqualTo ("spring" );
155
+ assertThat (nameAndCareer .getCareer ()).isEqualTo ("developer" );
156
+ }
157
+
158
+ private void assertKeySerialize (Module module ) throws Exception {
159
+ ObjectMapper mapper = new ObjectMapper ();
160
+ mapper .registerModule (module );
161
+ Map <NameAndAge , Boolean > map = new HashMap <>();
162
+ map .put (new NameAndAge ("spring" , 100 ), true );
163
+ String json = mapper .writeValueAsString (map );
164
+ assertThat (json ).isEqualToIgnoringWhitespace ("{\" spring is 100\" : true}" );
165
+ }
166
+
167
+ private void assertKeyDeserialize (Module module ) throws IOException {
168
+ ObjectMapper mapper = new ObjectMapper ();
169
+ mapper .registerModule (module );
170
+ TypeReference <Map <NameAndAge , Boolean >> typeRef = new TypeReference <Map <NameAndAge , Boolean >>() {
171
+ };
172
+ Map <NameAndAge , Boolean > map = mapper .readValue ("{\" spring is 100\" : true}" ,
173
+ typeRef );
174
+ assertThat (map ).containsEntry (new NameAndAge ("spring" , 100 ), true );
175
+ }
176
+
100
177
@ JsonComponent
101
178
static class OnlySerializer extends NameAndAgeJsonComponent .Serializer {
102
179
@@ -121,4 +198,14 @@ static class ConcreteSerializer extends AbstractSerializer {
121
198
122
199
}
123
200
201
+ @ JsonComponent (handle = JsonComponent .Handle .KEYS )
202
+ static class OnlyKeySerializer extends NameAndAgeJsonKeyComponent .Serializer {
203
+
204
+ }
205
+
206
+ @ JsonComponent (handle = JsonComponent .Handle .KEYS , handleClasses = NameAndAge .class )
207
+ static class OnlyKeyDeserializer extends NameAndAgeJsonKeyComponent .Deserializer {
208
+
209
+ }
210
+
124
211
}
0 commit comments