-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb79cca
commit 0c8642c
Showing
5 changed files
with
203 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
src/test/java/com/fasterxml/jackson/databind/deser/merge/ArrayMergeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package com.fasterxml.jackson.databind.deser.merge; | ||
|
||
import org.junit.Assert; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSetter; | ||
import com.fasterxml.jackson.annotation.OptBoolean; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
|
||
public class ArrayMergeTest extends BaseMapTest | ||
{ | ||
static class MergedX<T> | ||
{ | ||
@JsonSetter(merge=OptBoolean.TRUE) | ||
public T value; | ||
|
||
public MergedX(T v) { value = v; } | ||
protected MergedX() { } | ||
} | ||
|
||
/* | ||
/******************************************************** | ||
/* Test methods | ||
/******************************************************** | ||
*/ | ||
|
||
private final ObjectMapper MAPPER = new ObjectMapper() | ||
// 26-Oct-2016, tatu: Make sure we'll report merge problems by default | ||
.disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE) | ||
; | ||
|
||
public void testObjectArrayMerging() throws Exception | ||
{ | ||
MergedX<Object[]> input = new MergedX<Object[]>(new Object[] { | ||
"foo" | ||
}); | ||
final JavaType type = MAPPER.getTypeFactory().constructType(new TypeReference<MergedX<Object[]>>() {}); | ||
MergedX<Object[]> result = MAPPER.readerFor(type) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':['bar']}")); | ||
assertSame(input, result); | ||
assertEquals(2, result.value.length); | ||
assertEquals("foo", result.value[0]); | ||
assertEquals("bar", result.value[1]); | ||
|
||
// and with one trick | ||
result = MAPPER.readerFor(type) | ||
.with(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':'zap'}")); | ||
assertSame(input, result); | ||
assertEquals(3, result.value.length); | ||
assertEquals("foo", result.value[0]); | ||
assertEquals("bar", result.value[1]); | ||
assertEquals("zap", result.value[2]); | ||
} | ||
|
||
public void testStringArrayMerging() throws Exception | ||
{ | ||
MergedX<String[]> input = new MergedX<String[]>(new String[] { "foo" }); | ||
MergedX<String[]> result = MAPPER | ||
.readerFor(new TypeReference<MergedX<String[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':['bar']}")); | ||
assertSame(input, result); | ||
assertEquals(2, result.value.length); | ||
assertEquals("foo", result.value[0]); | ||
assertEquals("bar", result.value[1]); | ||
} | ||
|
||
public void testBooleanArrayMerging() throws Exception | ||
{ | ||
MergedX<boolean[]> input = new MergedX<boolean[]>(new boolean[] { true, false }); | ||
MergedX<boolean[]> result = MAPPER | ||
.readerFor(new TypeReference<MergedX<boolean[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':[true]}")); | ||
assertSame(input, result); | ||
assertEquals(3, result.value.length); | ||
Assert.assertArrayEquals(new boolean[] { true, false, true }, result.value); | ||
} | ||
|
||
public void testByteArrayMerging() throws Exception | ||
{ | ||
MergedX<byte[]> input = new MergedX<byte[]>(new byte[] { 1, 2 }); | ||
MergedX<byte[]> result = MAPPER | ||
.readerFor(new TypeReference<MergedX<byte[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':[4, 6.0, null]}")); | ||
assertSame(input, result); | ||
assertEquals(5, result.value.length); | ||
Assert.assertArrayEquals(new byte[] { 1, 2, 4, 6, 0 }, result.value); | ||
} | ||
|
||
public void testShortArrayMerging() throws Exception | ||
{ | ||
MergedX<short[]> input = new MergedX<short[]>(new short[] { 1, 2 }); | ||
MergedX<short[]> result = MAPPER | ||
.readerFor(new TypeReference<MergedX<short[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':[4, 6]}")); | ||
assertSame(input, result); | ||
assertEquals(4, result.value.length); | ||
Assert.assertArrayEquals(new short[] { 1, 2, 4, 6 }, result.value); | ||
} | ||
|
||
public void testCharArrayMerging() throws Exception | ||
{ | ||
MergedX<char[]> input = new MergedX<char[]>(new char[] { 'a', 'b' }); | ||
MergedX<char[]> result = MAPPER | ||
.readerFor(new TypeReference<MergedX<char[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':['c']}")); | ||
assertSame(input, result); | ||
Assert.assertArrayEquals(new char[] { 'a', 'b', 'c' }, result.value); | ||
|
||
// also some variation | ||
input = new MergedX<char[]>(new char[] { }); | ||
result = MAPPER | ||
.readerFor(new TypeReference<MergedX<char[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':['c']}")); | ||
assertSame(input, result); | ||
Assert.assertArrayEquals(new char[] { 'c' }, result.value); | ||
} | ||
|
||
public void testIntArrayMerging() throws Exception | ||
{ | ||
MergedX<int[]> input = new MergedX<int[]>(new int[] { 1, 2 }); | ||
MergedX<int[]> result = MAPPER | ||
.readerFor(new TypeReference<MergedX<int[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':[4, 6]}")); | ||
assertSame(input, result); | ||
assertEquals(4, result.value.length); | ||
Assert.assertArrayEquals(new int[] { 1, 2, 4, 6 }, result.value); | ||
|
||
// also some variation | ||
input = new MergedX<int[]>(new int[] { 3, 4, 6 }); | ||
result = MAPPER | ||
.readerFor(new TypeReference<MergedX<int[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':[ ]}")); | ||
assertSame(input, result); | ||
Assert.assertArrayEquals(new int[] { 3, 4, 6 }, result.value); | ||
} | ||
|
||
public void testLongArrayMerging() throws Exception | ||
{ | ||
MergedX<long[]> input = new MergedX<long[]>(new long[] { 1, 2 }); | ||
MergedX<long[]> result = MAPPER | ||
.readerFor(new TypeReference<MergedX<long[]>>() {}) | ||
.withValueToUpdate(input) | ||
.readValue(aposToQuotes("{'value':[4, 6]}")); | ||
assertSame(input, result); | ||
assertEquals(4, result.value.length); | ||
Assert.assertArrayEquals(new long[] { 1, 2, 4, 6 }, result.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.