From 4c941e2eab7ee819850d5448355777cec868be64 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Tue, 4 Apr 2023 14:14:01 -0400 Subject: [PATCH] Implement a failing test for InternCache churn on map keys --- .../MapDeserializationInternStringsTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/java/com/fasterxml/jackson/failing/MapDeserializationInternStringsTest.java diff --git a/src/test/java/com/fasterxml/jackson/failing/MapDeserializationInternStringsTest.java b/src/test/java/com/fasterxml/jackson/failing/MapDeserializationInternStringsTest.java new file mode 100644 index 0000000000..24770facdf --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/MapDeserializationInternStringsTest.java @@ -0,0 +1,33 @@ +package com.fasterxml.jackson.failing; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.core.util.InternCache; +import com.fasterxml.jackson.databind.BaseMapTest; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +// for [core#946] +public class MapDeserializationInternStringsTest extends BaseMapTest +{ + public void testArbitraryKeysDontChurnCache() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + String json = a2q("{'1st':'onedata','2nd':'twodata'}"); + Map expected = new HashMap<>(); + expected.put("1st", "onedata"); + expected.put("2nd", "twodata"); + // Clear the InternCache before deserialization to avoid impact from other tests. Note that the + // cache is static state, and this test will not be meaningful in an environment where tests are + // executed in parallel. + InternCache.instance.clear(); + Map deserialized = mapper.readValue(json, new TypeReference>() {}); + // Verify our test has done what we expect before asserting state on the InternCache. + assertEquals(expected, deserialized); + assertEquals("Arbitrary map key values should not be added to the InternCache because " + + "map keys may have greater cardinality than the InternCache expects", + Collections.emptyMap(), InternCache.instance); + } +}