From 963f34dfa63970af8caf6dfe35893c09b5df4325 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 21 Aug 2020 13:17:13 -0700 Subject: [PATCH] test improvements --- .../jackson/databind/RecordTest.java | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/test-jdk14/java/com/fasterxml/jackson/databind/RecordTest.java b/src/test-jdk14/java/com/fasterxml/jackson/databind/RecordTest.java index d25991ff8e..b75e56db76 100644 --- a/src/test-jdk14/java/com/fasterxml/jackson/databind/RecordTest.java +++ b/src/test-jdk14/java/com/fasterxml/jackson/databind/RecordTest.java @@ -1,5 +1,6 @@ package com.fasterxml.jackson.databind; +import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -12,26 +13,29 @@ public class RecordTest extends BaseMapTest { - record SimpleRecord(int id, String name) { - } + record SimpleRecord(int id, String name) { } - record RecordOfRecord(SimpleRecord record) { - } + record RecordOfRecord(SimpleRecord record) { } - record RecordWithConstructor(int id, String name) { - public RecordWithConstructor(int id) { - this(id, "name"); + record RecordWithCanonicalCtorOverride(int id, String name) { + public RecordWithCanonicalCtorOverride(int id, String name) { + this.id = id; + this.name = "name"; } } - record JsonIgnoreRecord(int id, @JsonIgnore String name) { + record RecordWithAltCtor(int id, String name) { + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public RecordWithAltCtor(@JsonProperty("id") int id) { + this(id, "name2"); + } } - record JsonPropertyRenameRecord(int id, @JsonProperty("rename")String name) { - } + record JsonIgnoreRecord(int id, @JsonIgnore String name) { } - record EmptyRecord() { - } + record JsonPropertyRenameRecord(int id, @JsonProperty("rename")String name) { } + + record EmptyRecord() { } private final ObjectMapper MAPPER = newJsonMapper(); @@ -46,7 +50,6 @@ public void testClassUtil() { assertTrue(ClassUtil.isRecordType(SimpleRecord.class)); assertTrue(ClassUtil.isRecordType(RecordOfRecord.class)); - assertTrue(ClassUtil.isRecordType(RecordWithConstructor.class)); assertTrue(ClassUtil.isRecordType(JsonIgnoreRecord.class)); assertTrue(ClassUtil.isRecordType(JsonPropertyRenameRecord.class)); } @@ -56,7 +59,6 @@ public void testRecordJavaType() { assertTrue(MAPPER.constructType(SimpleRecord.class).isRecordType()); assertTrue(MAPPER.constructType(RecordOfRecord.class).isRecordType()); - assertTrue(MAPPER.constructType(RecordWithConstructor.class).isRecordType()); assertTrue(MAPPER.constructType(JsonIgnoreRecord.class).isRecordType()); assertTrue(MAPPER.constructType(JsonPropertyRenameRecord.class).isRecordType()); } @@ -135,10 +137,18 @@ public void testSerializeJsonIgnoreRecord() throws Exception { assertEquals("{\"id\":123}", json); } - public void testDeserializeRecordWithConstructor() throws Exception { - RecordWithConstructor value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", - RecordWithConstructor.class); - assertEquals(new RecordWithConstructor(123, "Bob"), value); + public void testDeserializeWithCanonicalCtorOverride() throws Exception { + RecordWithCanonicalCtorOverride value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", + RecordWithCanonicalCtorOverride.class); + assertEquals(123, value.id()); + assertEquals("name", value.name()); + } + + public void testDeserializeWithAltCtor() throws Exception { + RecordWithAltCtor value = MAPPER.readValue("{\"id\":2812}", + RecordWithAltCtor.class); + assertEquals(2812, value.id()); + assertEquals("name2", value.name()); } public void testSerializeJsonRenameRecord() throws Exception {