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