|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 6 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | + |
| 9 | +// Unit tests for [databind#1868], related |
| 10 | +public class TestDefaultForUtilCollections1868 extends BaseMapTest |
| 11 | +{ |
| 12 | + private final ObjectMapper DEFAULT_MAPPER = new ObjectMapper(); |
| 13 | + { |
| 14 | + DEFAULT_MAPPER.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); |
| 15 | + } |
| 16 | + |
| 17 | + /* |
| 18 | + /********************************************************** |
| 19 | + /* Unit tests, "empty" types |
| 20 | + /********************************************************** |
| 21 | + */ |
| 22 | + |
| 23 | + public void testEmptyList() throws Exception { |
| 24 | + _verifyCollection(Collections.emptyList()); |
| 25 | + } |
| 26 | + |
| 27 | + public void testEmptySet() throws Exception { |
| 28 | + _verifyCollection(Collections.emptySet()); |
| 29 | + } |
| 30 | + |
| 31 | + public void testEmptyMap() throws Exception { |
| 32 | + _verifyMap(Collections.emptyMap()); |
| 33 | + } |
| 34 | + |
| 35 | + /* |
| 36 | + /********************************************************** |
| 37 | + /* Unit tests, "singleton" types |
| 38 | + /********************************************************** |
| 39 | + */ |
| 40 | + |
| 41 | + public void testSingletonList() throws Exception { |
| 42 | + _verifyCollection(Collections.singletonList(Arrays.asList("TheOne"))); |
| 43 | + } |
| 44 | + |
| 45 | + public void testSingletonSet() throws Exception { |
| 46 | + _verifyCollection(Collections.singleton(Arrays.asList("TheOne"))); |
| 47 | + } |
| 48 | + |
| 49 | + public void testSingletonMap() throws Exception { |
| 50 | + _verifyMap(Collections.singletonMap("foo", "bar")); |
| 51 | + } |
| 52 | + |
| 53 | + /* |
| 54 | + /********************************************************** |
| 55 | + /* Unit tests, "unmodifiable" types |
| 56 | + /********************************************************** |
| 57 | + */ |
| 58 | + |
| 59 | + public void testUnmodifiableList() throws Exception { |
| 60 | + _verifyCollection(Collections.unmodifiableList(Arrays.asList("first", "second"))); |
| 61 | + } |
| 62 | + |
| 63 | + public void testUnmodifiableSet() throws Exception |
| 64 | + { |
| 65 | + Set<String> input = new LinkedHashSet<>(Arrays.asList("first", "second")); |
| 66 | + _verifyCollection(Collections.unmodifiableSet(input)); |
| 67 | + } |
| 68 | + |
| 69 | + public void testUnmodifiableMap() throws Exception |
| 70 | + { |
| 71 | + Map<String,String> input = new LinkedHashMap<>(); |
| 72 | + input.put("a", "b"); |
| 73 | + input.put("c", "d"); |
| 74 | + _verifyMap(Collections.unmodifiableMap(input)); |
| 75 | + } |
| 76 | + |
| 77 | + /* |
| 78 | + /********************************************************** |
| 79 | + /* Helper methods |
| 80 | + /********************************************************** |
| 81 | + */ |
| 82 | + |
| 83 | + protected void _verifyCollection(Collection<?> exp) throws Exception |
| 84 | + { |
| 85 | + String json = DEFAULT_MAPPER.writeValueAsString(exp); |
| 86 | + Collection<?> act = DEFAULT_MAPPER.readValue(json, Collection.class); |
| 87 | + |
| 88 | + assertEquals(exp, act); |
| 89 | + assertEquals(exp.getClass(), act.getClass()); |
| 90 | + } |
| 91 | + |
| 92 | + protected void _verifyMap(Map<?,?> exp) throws Exception |
| 93 | + { |
| 94 | + String json = DEFAULT_MAPPER.writeValueAsString(exp); |
| 95 | + Map<?,?> act = DEFAULT_MAPPER.readValue(json, Map.class); |
| 96 | + |
| 97 | + assertEquals(exp, act); |
| 98 | + assertEquals(exp.getClass(), act.getClass()); |
| 99 | + } |
| 100 | +} |
0 commit comments