1616
1717package org .springframework .boot .jackson ;
1818
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 ;
1925import com .fasterxml .jackson .databind .Module ;
2026import com .fasterxml .jackson .databind .ObjectMapper ;
2127import org .junit .After ;
2430import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2531
2632import static org .assertj .core .api .Assertions .assertThat ;
33+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
2734
2835/**
2936 * Tests for {@link JsonComponentModule}.
3037 *
3138 * @author Phillip Webb
3239 * @author Vladimir Tsanev
40+ * @author Paul Aly
3341 */
3442public class JsonComponentModuleTests {
3543
@@ -73,6 +81,38 @@ public void moduleShouldAllowInnerAbstractClasses() throws Exception {
7381 context .close ();
7482 }
7583
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+
76116 private void load (Class <?>... configs ) {
77117 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
78118 context .register (configs );
@@ -81,11 +121,17 @@ private void load(Class<?>... configs) {
81121 this .context = context ;
82122 }
83123
84- private void assertSerialize (Module module ) throws Exception {
124+ private void assertSerialize (Module module , Name value , String expectedJson )
125+ throws Exception {
85126 ObjectMapper mapper = new ObjectMapper ();
86127 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}" );
89135 }
90136
91137 private void assertDeserialize (Module module ) throws Exception {
@@ -97,6 +143,37 @@ private void assertDeserialize(Module module) throws Exception {
97143 assertThat (nameAndAge .getAge ()).isEqualTo (100 );
98144 }
99145
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+
100177 @ JsonComponent
101178 static class OnlySerializer extends NameAndAgeJsonComponent .Serializer {
102179
@@ -121,4 +198,14 @@ static class ConcreteSerializer extends AbstractSerializer {
121198
122199 }
123200
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+
124211}
0 commit comments