1
1
package com .fasterxml .jackson .module .androidrecord ;
2
2
3
+ import java .util .*;
4
+
5
+ import org .junit .jupiter .api .Test ;
6
+
3
7
import com .android .tools .r8 .RecordTag ;
8
+
4
9
import com .fasterxml .jackson .annotation .JacksonInject ;
5
10
import com .fasterxml .jackson .annotation .JsonProperty ;
11
+
6
12
import com .fasterxml .jackson .databind .*;
7
13
import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
8
14
import com .fasterxml .jackson .databind .annotation .JsonNaming ;
9
15
import com .fasterxml .jackson .databind .exc .InvalidDefinitionException ;
10
16
import com .fasterxml .jackson .databind .json .JsonMapper ;
11
17
import com .fasterxml .jackson .databind .type .TypeFactory ;
12
18
import com .fasterxml .jackson .databind .util .Converter ;
13
- import org .junit .Assert ;
14
19
15
- import java .util .Collections ;
16
- import java .util .LinkedHashMap ;
17
- import java .util .Map ;
18
- import java .util .Objects ;
20
+ import static org .junit .jupiter .api .Assertions .*;
19
21
20
- public class RecordBasicsTest extends BaseMapTest {
22
+ public class RecordBasicsTest extends BaseMapTest
23
+ {
21
24
static final class EmptyRecord extends RecordTag {
22
25
@ Override
23
26
public boolean equals (Object o ) {
@@ -327,13 +330,15 @@ public String toString() {
327
330
/**********************************************************************
328
331
*/
329
332
333
+ @ Test
330
334
public void testClassUtil () {
331
335
assertFalse (AndroidRecordModule .isDesugaredRecordClass (getClass ()));
332
336
assertTrue (AndroidRecordModule .isDesugaredRecordClass (SimpleRecord .class ));
333
337
assertTrue (AndroidRecordModule .isDesugaredRecordClass (RecordOfRecord .class ));
334
338
assertTrue (AndroidRecordModule .isDesugaredRecordClass (RecordWithRename .class ));
335
339
}
336
340
341
+ @ Test
337
342
public void testRecordJavaType () {
338
343
assertFalse (AndroidRecordModule .isDesugaredRecordClass (MAPPER .constructType (getClass ()).getRawClass ()));
339
344
assertTrue (AndroidRecordModule .isDesugaredRecordClass (MAPPER .constructType (SimpleRecord .class ).getRawClass ()));
@@ -347,26 +352,31 @@ public void testRecordJavaType() {
347
352
/**********************************************************************
348
353
*/
349
354
355
+ @ Test
350
356
public void testSerializeSimpleRecord () throws Exception {
351
357
String json = MAPPER .writeValueAsString (new SimpleRecord (123 , "Bob" ));
352
358
final Object EXP = map ("id" , Integer .valueOf (123 ), "name" , "Bob" );
353
359
assertEquals (EXP , MAPPER .readValue (json , Object .class ));
354
360
}
355
361
362
+ @ Test
356
363
public void testDeserializeSimpleRecord () throws Exception {
357
364
assertEquals (new SimpleRecord (123 , "Bob" ),
358
365
MAPPER .readValue ("{\" id\" :123,\" name\" :\" Bob\" }" , SimpleRecord .class ));
359
366
}
360
367
368
+ @ Test
361
369
public void testSerializeEmptyRecord () throws Exception {
362
370
assertEquals ("{}" , MAPPER .writeValueAsString (new EmptyRecord ()));
363
371
}
364
372
373
+ @ Test
365
374
public void testDeserializeEmptyRecord () throws Exception {
366
375
assertEquals (new EmptyRecord (),
367
376
MAPPER .readValue ("{}" , EmptyRecord .class ));
368
377
}
369
378
379
+ @ Test
370
380
public void testSerializeRecordOfRecord () throws Exception {
371
381
RecordOfRecord record = new RecordOfRecord (new SimpleRecord (123 , "Bob" ));
372
382
String json = MAPPER .writeValueAsString (record );
@@ -375,6 +385,7 @@ public void testSerializeRecordOfRecord() throws Exception {
375
385
assertEquals (EXP , MAPPER .readValue (json , Object .class ));
376
386
}
377
387
388
+ @ Test
378
389
public void testDeserializeRecordOfRecord () throws Exception {
379
390
assertEquals (new RecordOfRecord (new SimpleRecord (123 , "Bob" )),
380
391
MAPPER .readValue ("{\" record\" :{\" id\" :123,\" name\" :\" Bob\" }}" ,
@@ -387,6 +398,7 @@ public void testDeserializeRecordOfRecord() throws Exception {
387
398
/**********************************************************************
388
399
*/
389
400
401
+ @ Test
390
402
public void testSerializeSimpleRecord_DisableAnnotationIntrospector () throws Exception {
391
403
SimpleRecord record = new SimpleRecord (123 , "Bob" );
392
404
@@ -398,12 +410,14 @@ public void testSerializeSimpleRecord_DisableAnnotationIntrospector() throws Exc
398
410
assertEquals ("{\" id\" :123,\" name\" :\" Bob\" }" , json );
399
411
}
400
412
413
+ @ Test
401
414
public void testDeserializeSimpleRecord_DisableAnnotationIntrospector () throws Exception {
402
415
JsonMapper mapper = JsonMapper .builder ().addModule (new AndroidRecordModule ())
403
416
.configure (MapperFeature .USE_ANNOTATIONS , false )
404
417
.build ();
405
418
406
- Assert .assertThrows (InvalidDefinitionException .class , () -> mapper .readValue ("{\" id\" :123,\" name\" :\" Bob\" }" , SimpleRecord .class ));
419
+ assertThrows (InvalidDefinitionException .class ,
420
+ () -> mapper .readValue ("{\" id\" :123,\" name\" :\" Bob\" }" , SimpleRecord .class ));
407
421
}
408
422
409
423
/*
@@ -412,18 +426,21 @@ public void testDeserializeSimpleRecord_DisableAnnotationIntrospector() throws E
412
426
/**********************************************************************
413
427
*/
414
428
429
+ @ Test
415
430
public void testSerializeJsonRename () throws Exception {
416
431
String json = MAPPER .writeValueAsString (new RecordWithRename (123 , "Bob" ));
417
432
final Object EXP = map ("id" , Integer .valueOf (123 ), "rename" , "Bob" );
418
433
assertEquals (EXP , MAPPER .readValue (json , Object .class ));
419
434
}
420
435
436
+ @ Test
421
437
public void testDeserializeJsonRename () throws Exception {
422
438
RecordWithRename value = MAPPER .readValue ("{\" id\" :123,\" rename\" :\" Bob\" }" ,
423
439
RecordWithRename .class );
424
440
assertEquals (new RecordWithRename (123 , "Bob" ), value );
425
441
}
426
442
443
+ @ Test
427
444
public void testDeserializeConstructorInjectRecord () throws Exception {
428
445
MAPPER .setInjectableValues (new InjectableValues .Std ().addValue (String .class , "Bob" ));
429
446
@@ -438,6 +455,7 @@ public void testDeserializeConstructorInjectRecord() throws Exception {
438
455
*/
439
456
440
457
// [databind#2992]
458
+ @ Test
441
459
public void testNamingStrategy () throws Exception {
442
460
SnakeRecord input = new SnakeRecord ("123" , "value" );
443
461
@@ -454,25 +472,29 @@ public void testNamingStrategy() throws Exception {
454
472
/**********************************************************************
455
473
*/
456
474
475
+ @ Test
457
476
public void testSerialize_SingleWriteOnlyParameter () throws Exception {
458
477
String json = MAPPER .writeValueAsString (new RecordSingleWriteOnly (123 ));
459
478
460
479
assertEquals ("{}" , json );
461
480
}
462
481
463
482
// [databind#3897]
483
+ @ Test
464
484
public void testDeserialize_SingleWriteOnlyParameter () throws Exception {
465
485
RecordSingleWriteOnly value = MAPPER .readValue ("{\" id\" :123}" , RecordSingleWriteOnly .class );
466
486
467
487
assertEquals (new RecordSingleWriteOnly (123 ), value );
468
488
}
469
489
490
+ @ Test
470
491
public void testSerialize_SomeWriteOnlyParameter () throws Exception {
471
492
String json =
MAPPER .
writeValueAsString (
new RecordSomeWriteOnly (
123 ,
"Bob" ,
"[email protected] " ));
472
493
473
494
assertEquals (
"{\" email\" :\" [email protected] \" }" ,
json );
474
495
}
475
496
497
+ @ Test
476
498
public void testDeserialize_SomeWriteOnlyParameter () throws Exception {
477
499
RecordSomeWriteOnly value = MAPPER .readValue (
478
500
"{\" id\" :123,\" name\" :\" Bob\" ,\" email\" :\" [email protected] \" }" ,
@@ -481,12 +503,14 @@ public void testDeserialize_SomeWriteOnlyParameter() throws Exception {
481
503
assertEquals (
new RecordSomeWriteOnly (
123 ,
"Bob" ,
"[email protected] " ),
value );
482
504
}
483
505
506
+ @ Test
484
507
public void testSerialize_AllWriteOnlyParameter () throws Exception {
485
508
String json =
MAPPER .
writeValueAsString (
new RecordAllWriteOnly (
123 ,
"Bob" ,
"[email protected] " ));
486
509
487
510
assertEquals ("{}" , json );
488
511
}
489
512
513
+ @ Test
490
514
public void testDeserialize_AllWriteOnlyParameter () throws Exception {
491
515
RecordAllWriteOnly value = MAPPER .readValue (
492
516
"{\" id\" :123,\" name\" :\" Bob\" ,\" email\" :\" [email protected] \" }" ,
@@ -502,6 +526,7 @@ public void testDeserialize_AllWriteOnlyParameter() throws Exception {
502
526
*/
503
527
504
528
// Fails: converter not applied
529
+ @ Test
505
530
public void testDeserializeJsonDeserializeRecord () throws Exception {
506
531
RecordWithJsonDeserialize value = MAPPER .readValue ("{\" id\" :123,\" name\" :\" Bob \" }" ,
507
532
RecordWithJsonDeserialize .class );
0 commit comments