From b61e047509cb57eb7988e83b52c5f4bb41987ebc Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 13 Feb 2025 08:57:44 -0800 Subject: [PATCH] Unit test changes for improved code coverage --- .../databind/module/SimpleModuleTest.java | 59 ++++++++++++++++--- .../jackson/databind/node/ObjectNodeTest.java | 9 ++- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java b/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java index baef8550c7..58cd7418a6 100644 --- a/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java @@ -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.*; @@ -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"); } } diff --git a/src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java b/src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java index 5dc69962fe..5d6654b1fe 100644 --- a/src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java @@ -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; @@ -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: @@ -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) {