15
15
package com .google .firebase .encoders .json ;
16
16
17
17
import static com .google .common .truth .Truth .assertThat ;
18
+ import static org .junit .Assert .assertThrows ;
18
19
import static org .mockito .ArgumentMatchers .any ;
19
20
import static org .mockito .Mockito .doThrow ;
20
21
import static org .mockito .Mockito .mock ;
29
30
import java .io .Writer ;
30
31
import java .nio .charset .Charset ;
31
32
import java .util .Calendar ;
33
+ import java .util .Collections ;
32
34
import java .util .TimeZone ;
33
- import org .junit .Assert ;
34
35
import org .junit .Test ;
35
36
import org .junit .runner .RunWith ;
36
37
@@ -204,6 +205,38 @@ public void testEncodingCollection() throws IOException, EncodingException {
204
205
"[{\" Name\" :\" innerClass\" },{\" Name\" :\" innerClass\" }]" ));
205
206
}
206
207
208
+ @ Test
209
+ public void testEncodingMap_withStringKey_shouldSucceed () throws EncodingException {
210
+ ObjectEncoder <DummyClass > objectEncoder =
211
+ (o , ctx ) -> {
212
+ ctx .add ("map" , Collections .singletonMap ("key" , true ));
213
+ };
214
+
215
+ String result =
216
+ new JsonDataEncoderBuilder ()
217
+ .registerEncoder (DummyClass .class , objectEncoder )
218
+ .build ()
219
+ .encode (DummyClass .INSTANCE );
220
+
221
+ assertThat (result ).isEqualTo (String .format ("{\" map\" :%s}" , "{\" key\" :true}" ));
222
+ }
223
+
224
+ @ Test
225
+ public void testEncodingMap_withNonStringKey_shouldFail () {
226
+ ObjectEncoder <DummyClass > objectEncoder =
227
+ (o , ctx ) -> {
228
+ ctx .add ("map" , Collections .singletonMap (1L , true ));
229
+ };
230
+
231
+ DataEncoder encoder =
232
+ new JsonDataEncoderBuilder ().registerEncoder (DummyClass .class , objectEncoder ).build ();
233
+
234
+ assertThrows (
235
+ "Only String keys are currently supported in maps" ,
236
+ EncodingException .class ,
237
+ () -> encoder .encode (DummyClass .INSTANCE ));
238
+ }
239
+
207
240
@ Test
208
241
public void testEncodingBytes () throws IOException , EncodingException {
209
242
ObjectEncoder <DummyClass > objectEncoder =
@@ -336,7 +369,7 @@ public void testEncodingComplexTypes_InnerExtendedEncoder()
336
369
@ Test
337
370
public void testMissingEncoder () throws IOException , EncodingException {
338
371
DataEncoder dataEncoder = new JsonDataEncoderBuilder ().build ();
339
- Assert . assertThrows (EncodingException .class , () -> dataEncoder .encode (DummyClass .INSTANCE ));
372
+ assertThrows (EncodingException .class , () -> dataEncoder .encode (DummyClass .INSTANCE ));
340
373
}
341
374
342
375
@ Test
@@ -345,7 +378,7 @@ public void testEncoderError() throws IOException, EncodingException {
345
378
Writer mockWriter = mock (Writer .class );
346
379
doThrow (IOException .class ).when (mockWriter ).write (any (String .class ));
347
380
348
- Assert . assertThrows (
381
+ assertThrows (
349
382
IOException .class ,
350
383
() ->
351
384
new JsonDataEncoderBuilder ()
0 commit comments