Skip to content

Commit

Permalink
Unit test changes for improved code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 13, 2025
1 parent cfef132 commit b61e047
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fasterxml.jackson.databind.module;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.*;

Expand Down Expand Up @@ -239,29 +241,68 @@ public void serialize(Test3787Bean value, JsonGenerator gen, SerializerProvider
/**********************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testDeserializationWithoutModule() throws Exception
{
final String DOC = "{\"str\":\"ab\",\"num\":2}";

try {
MAPPER.readValue(DOC, CustomBean.class);
fail("Should have caused an exception");
} catch (DatabindException e) {
verifyException(e, "Cannot construct");
verifyException(e, "no creators");
}

// And then other variations
try {
MAPPER.readValue(new StringReader(DOC), CustomBean.class);
fail("Should have caused an exception");
} catch (DatabindException e) {
verifyException(e, "Cannot construct");
verifyException(e, "no creators");
}

try {
MAPPER.readValue(utf8Bytes(DOC), CustomBean.class);
fail("Should have caused an exception");
} catch (DatabindException e) {
verifyException(e, "Cannot construct");
verifyException(e, "no creators");
}

try {
MAPPER.readValue(new ByteArrayInputStream(utf8Bytes(DOC)), CustomBean.class);
fail("Should have caused an exception");
} catch (DatabindException e) {
verifyException(e, "Cannot construct");
verifyException(e, "no creators");
}
}

/**
* Basic test to ensure we do not have functioning default
* serializers for custom types used in tests.
*/
@Test
public void testWithoutModule()
public void testSerializationWithoutModule() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
// first: serialization failure:
try {
mapper.writeValueAsString(new CustomBean("foo", 3));
MAPPER.writeValueAsString(new CustomBean("foo", 3));
fail("Should have caused an exception");
} catch (IOException e) {
} catch (DatabindException e) {
verifyException(e, "No serializer found");
}

// then deserialization
// and with another write call for test coverage
try {
mapper.readValue("{\"str\":\"ab\",\"num\":2}", CustomBean.class);
MAPPER.writeValueAsBytes(new CustomBean("foo", 3));
fail("Should have caused an exception");
} catch (IOException e) {
verifyException(e, "Cannot construct");
verifyException(e, "no creators");
} catch (DatabindException e) {
verifyException(e, "No serializer found");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonValue;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
Expand Down Expand Up @@ -441,7 +441,7 @@ public void testFailOnDupKeys() throws Exception

// first: verify defaults:
assertFalse(MAPPER.isEnabled(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY));
ObjectNode root = (ObjectNode) MAPPER.readTree(DUP_JSON);
ObjectNode root = _objNode(DUP_JSON);
assertEquals(2, root.path("a").asInt());

// and then enable checks:
Expand Down Expand Up @@ -609,7 +609,10 @@ public void testRemoveNulls() throws Exception
}

private ObjectNode _objNode(String json) throws Exception {
return (ObjectNode) MAPPER.readTree(a2q(json));
// Use different read method for better code coverage
try (JsonParser p = MAPPER.createParser(a2q(json))) {
return (ObjectNode) MAPPER.reader().readTree(p);
}
}

private String _toString(JsonNode n) {
Expand Down

0 comments on commit b61e047

Please sign in to comment.