Skip to content

Commit

Permalink
test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 21, 2020
1 parent 4dff70b commit 963f34d
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/test-jdk14/java/com/fasterxml/jackson/databind/RecordTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();

Expand All @@ -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));
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 963f34d

Please sign in to comment.