|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.util.Currency; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 11 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 12 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +// [databind#4773] Test to verify `SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS` behavior |
| 18 | +// when serializing `Map` instances with un-comparable keys. |
| 19 | +public class OrderMapEntriesByKeysSerializationFeature4773Test |
| 20 | + extends DatabindTestUtil |
| 21 | +{ |
| 22 | + |
| 23 | + public static class IncomparableContainer4773 { |
| 24 | + public Map<Currency, String> exampleMap = new HashMap<>(); |
| 25 | + } |
| 26 | + |
| 27 | + public static class ObjectContainer4773 { |
| 28 | + public Map<Object, String> exampleMap = new HashMap<>(); |
| 29 | + } |
| 30 | + |
| 31 | + private final ObjectMapper objectMapper = jsonMapperBuilder() |
| 32 | + .configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true) |
| 33 | + .build(); |
| 34 | + |
| 35 | + @JacksonTestFailureExpected |
| 36 | + @Test |
| 37 | + void testSerializationWithIncomparableKeys() |
| 38 | + throws Exception |
| 39 | + { |
| 40 | + // Given |
| 41 | + IncomparableContainer4773 entity = new IncomparableContainer4773(); |
| 42 | + entity.exampleMap.put(Currency.getInstance("GBP"), "GBP_TEXT"); |
| 43 | + entity.exampleMap.put(Currency.getInstance("AUD"), "AUD_TEXT"); |
| 44 | + |
| 45 | + // When : Throws exception |
| 46 | + // com.fasterxml.jackson.databind.JsonMappingException: class java.util.Currency cannot be cast to class java.lang.Comparable |
| 47 | + String jsonResult = objectMapper.writeValueAsString(entity); |
| 48 | + |
| 49 | + // Then : Order should not matter, just plain old serialize |
| 50 | + assertTrue(jsonResult.contains("GBP")); |
| 51 | + assertTrue(jsonResult.contains("AUD")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testSerializationWithGenericObjectKeys() |
| 56 | + throws Exception |
| 57 | + { |
| 58 | + // Given |
| 59 | + ObjectContainer4773 entity = new ObjectContainer4773(); |
| 60 | + entity.exampleMap.put(5, "N_TEXT"); |
| 61 | + entity.exampleMap.put(1, "GBP_TEXT"); |
| 62 | + entity.exampleMap.put(3, "T_TEXT"); |
| 63 | + entity.exampleMap.put(4, "AUD_TEXT"); |
| 64 | + entity.exampleMap.put(2, "KRW_TEXT"); |
| 65 | + |
| 66 | + // When |
| 67 | + String jsonResult = objectMapper.writeValueAsString(entity); |
| 68 | + |
| 69 | + // Then |
| 70 | + assertEquals(a2q("{'exampleMap':{" + |
| 71 | + "'1':'GBP_TEXT'," + |
| 72 | + "'2':'KRW_TEXT'," + |
| 73 | + "'3':'T_TEXT'," + |
| 74 | + "'4':'AUD_TEXT'," + |
| 75 | + "'5':'N_TEXT'}}"), jsonResult); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments