|
| 1 | +package com.fasterxml.jackson.databind.deser.merge; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.EnumSet; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.List; |
| 8 | +import java.util.TreeSet; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.annotation.JsonSetter; |
| 11 | +import com.fasterxml.jackson.annotation.OptBoolean; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 14 | + |
| 15 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 16 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | + |
| 19 | +public class CollectionMergeTest extends BaseMapTest |
| 20 | +{ |
| 21 | + static class CollectionWrapper { |
| 22 | + @JsonSetter(merge=OptBoolean.TRUE) |
| 23 | + public Collection<String> bag = new TreeSet<String>(); |
| 24 | + { |
| 25 | + bag.add("a"); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + static class MergedList |
| 30 | + { |
| 31 | + @JsonSetter(merge=OptBoolean.TRUE) |
| 32 | + public List<String> values = new ArrayList<>(); |
| 33 | + { |
| 34 | + values.add("a"); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + static class MergedEnumSet |
| 39 | + { |
| 40 | + @JsonSetter(merge=OptBoolean.TRUE) |
| 41 | + public EnumSet<ABC> abc = EnumSet.of(ABC.B); |
| 42 | + } |
| 43 | + |
| 44 | + static class MergedX<T> |
| 45 | + { |
| 46 | + @JsonSetter(merge=OptBoolean.TRUE) |
| 47 | + public T value; |
| 48 | + |
| 49 | + public MergedX(T v) { value = v; } |
| 50 | + protected MergedX() { } |
| 51 | + } |
| 52 | + |
| 53 | + /* |
| 54 | + /******************************************************** |
| 55 | + /* Test methods |
| 56 | + /******************************************************** |
| 57 | + */ |
| 58 | + |
| 59 | + private final ObjectMapper MAPPER = new ObjectMapper() |
| 60 | + // 26-Oct-2016, tatu: Make sure we'll report merge problems by default |
| 61 | + .disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE) |
| 62 | + ; |
| 63 | + |
| 64 | + public void testCollectionMerging() throws Exception |
| 65 | + { |
| 66 | + CollectionWrapper w = MAPPER.readValue(aposToQuotes("{'bag':['b']}"), CollectionWrapper.class); |
| 67 | + assertEquals(2, w.bag.size()); |
| 68 | + assertTrue(w.bag.contains("a")); |
| 69 | + assertTrue(w.bag.contains("b")); |
| 70 | + } |
| 71 | + |
| 72 | + public void testListMerging() throws Exception |
| 73 | + { |
| 74 | + MergedList w = MAPPER.readValue(aposToQuotes("{'values':['x']}"), MergedList.class); |
| 75 | + assertEquals(2, w.values.size()); |
| 76 | + assertTrue(w.values.contains("a")); |
| 77 | + assertTrue(w.values.contains("x")); |
| 78 | + } |
| 79 | + |
| 80 | + // Test that uses generic type |
| 81 | + public void testGenericListMerging() throws Exception |
| 82 | + { |
| 83 | + Collection<String> l = new ArrayList<>(); |
| 84 | + l.add("foo"); |
| 85 | + MergedX<Collection<String>> input = new MergedX<Collection<String>>(l); |
| 86 | + |
| 87 | + MergedX<Collection<String>> result = MAPPER |
| 88 | + .readerFor(new TypeReference<MergedX<Collection<String>>>() {}) |
| 89 | + .withValueToUpdate(input) |
| 90 | + .readValue(aposToQuotes("{'value':['bar']}")); |
| 91 | + assertSame(input, result); |
| 92 | + assertEquals(2, result.value.size()); |
| 93 | + Iterator<String> it = result.value.iterator(); |
| 94 | + assertEquals("foo", it.next()); |
| 95 | + assertEquals("bar", it.next()); |
| 96 | + } |
| 97 | + |
| 98 | + public void testEnumSetMerging() throws Exception |
| 99 | + { |
| 100 | + MergedEnumSet result = MAPPER.readValue(aposToQuotes("{'abc':['A']}"), MergedEnumSet.class); |
| 101 | + assertEquals(2, result.abc.size()); |
| 102 | + assertTrue(result.abc.contains(ABC.B)); // original |
| 103 | + assertTrue(result.abc.contains(ABC.A)); // added |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments