Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <widdis@gmail.com>
  • Loading branch information
dbwiddis committed Oct 30, 2022
1 parent c7ab849 commit c8fded2
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,29 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class JsonpMapperTest extends Assert {

String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," +
"\"stringValue\":\"foo\"}";

@Test
public void testJsonb() {
String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1," +
"\"instantValue\":\"1970-01-01T00:00:00Z\",\"intValue\":1,\"stringValue\":\"foo\"}";

JsonpMapper mapper = new JsonbJsonpMapper();
testSerialize(mapper, json);
testDeserialize(mapper, json);
}

@Test
public void testJackson() {
String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," +
"\"stringValue\":\"foo\",\"instantValue\":0.0}";

JacksonJsonpMapper mapper = new JacksonJsonpMapper();
testSerialize(mapper, json);
testDeserialize(mapper, json);
Expand All @@ -87,7 +91,7 @@ public void testJacksonCustomMapper() {
JacksonJsonpMapper mapper = new JacksonJsonpMapper(jkMapper);

String json = "{\"children\":[{\"double_value\":3.2,\"int_value\":2}],\"double_value\":2.1,\"int_value\":1," +
"\"string_value\":\"foo\"}";
"\"string_value\":\"foo\",\"instant_value\":0.0}";

testSerialize(mapper, json);
testDeserialize(mapper, json);
Expand All @@ -108,6 +112,7 @@ public void testJacksonCustomJsonFactory() {
.add("doubleValue", "2.1")
.add("intValue", 1)
.add("stringValue", "foo")
.add("instantValue", "1970-01-01T00:00:00Z")
.build();

final Writer writer = new StringWriter();
Expand Down Expand Up @@ -179,6 +184,7 @@ private void testSerialize(JsonpMapper mapper, String expected) {
something.setIntValue(1);
something.setDoubleValue(2.1);
something.setStringValue("foo");
something.setInstantValue(Instant.EPOCH);

SomeClass other = new SomeClass();
other.setIntValue(2);
Expand All @@ -203,6 +209,7 @@ private void testDeserialize(JsonpMapper mapper, String json) {
assertEquals(1, parsed.getIntValue());
assertEquals(2.1, parsed.getDoubleValue(), 0.0);
assertEquals("foo", parsed.getStringValue());
assertEquals(Instant.EPOCH, parsed.getInstantValue());

List<SomeClass> children = parsed.getChildren();
assertEquals(1, children.size());
Expand All @@ -220,6 +227,7 @@ public static class SomeClass {
private double doubleValue;
private int intValue;
private String stringValue;
private Instant instantValue;

public int getIntValue() {
return intValue;
Expand All @@ -241,6 +249,14 @@ public double getDoubleValue() {
return doubleValue;
}

public void setInstantValue(Instant instantValue) {
this.instantValue = instantValue;
}

public Instant getInstantValue() {
return instantValue;
}

public void setDoubleValue(double doubleValue) {
this.doubleValue = doubleValue;
}
Expand Down

0 comments on commit c8fded2

Please sign in to comment.