Skip to content

Commit

Permalink
test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 9, 2016
1 parent bb79cca commit 0c8642c
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,7 @@ protected final JsonNode updateObject(JsonParser p, DeserializationContext ctxt,
if (p.isExpectedStartObjectToken()) {
key = p.nextFieldName();
} else {
JsonToken t = p.getCurrentToken();
if (t == JsonToken.END_OBJECT) {
return node;
}
// No, we don't have a JSON Object but some other type; deserialize normally
if (t != JsonToken.FIELD_NAME) {
if (!p.hasToken(JsonToken.FIELD_NAME)) {
return deserialize(p, ctxt);
}
key = p.getCurrentName();
Expand Down Expand Up @@ -516,14 +511,10 @@ protected final JsonNode deserializeAny(JsonParser p, DeserializationContext ctx
final JsonNodeFactory nodeFactory) throws IOException
{
switch (p.getCurrentTokenId()) {
case JsonTokenId.ID_START_OBJECT:
return deserializeObject(p, ctxt, nodeFactory);
case JsonTokenId.ID_END_OBJECT: // for empty JSON Objects we may point to this?
return nodeFactory.objectNode();
case JsonTokenId.ID_FIELD_NAME:
return deserializeObjectAtName(p, ctxt, nodeFactory);
case JsonTokenId.ID_START_ARRAY:
return deserializeArray(p, ctxt, nodeFactory);
case JsonTokenId.ID_EMBEDDED_OBJECT:
return _fromEmbedded(p, ctxt, nodeFactory);
case JsonTokenId.ID_STRING:
Expand All @@ -538,6 +529,14 @@ protected final JsonNode deserializeAny(JsonParser p, DeserializationContext ctx
return nodeFactory.booleanNode(false);
case JsonTokenId.ID_NULL:
return nodeFactory.nullNode();

/* Caller checks for these, should not get here ever
case JsonTokenId.ID_START_OBJECT:
return deserializeObject(p, ctxt, nodeFactory);
case JsonTokenId.ID_START_ARRAY:
return deserializeArray(p, ctxt, nodeFactory);
*/


// These states can not be mapped; input stream is
// off by an event or two
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer) throws IOException
{
/* Should there be separate handling for base64 stuff?
* for now this should be enough:
*/
// Should there be separate handling for base64 stuff?
// for now this should be enough:
return typeDeserializer.deserializeTypedFromArray(p, ctxt);
}

Expand All @@ -125,12 +124,11 @@ public T deserialize(JsonParser p, DeserializationContext ctxt, T existing) thro
*/
protected abstract T _concat(T oldValue, T newValue);

/**
/*
* Convenience method that constructs a concatenation of two arrays,
* with the type they have.
*
* @since 2.9
*/
@SuppressWarnings("unchecked")
public static <T> T concatArrays(T array1, T array2)
{
Expand All @@ -146,6 +144,7 @@ public static <T> T concatArrays(T array1, T array2)
System.arraycopy(array2, 0, result, len1, len2);
return (T) result;
}
*/

@SuppressWarnings("unchecked")
protected T handleNonArray(JsonParser p, DeserializationContext ctxt) throws IOException
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,44 @@ public void testObjectNodeMerge() throws Exception
assertEquals("xyz", w.props.path("stuff").asText());
}

public void testObjectDeepUpdate() throws Exception
{
ObjectNode base = MAPPER.createObjectNode();
ObjectNode props = base.putObject("props");
props.put("base", 123);
props.put("value", 456);
ArrayNode a = props.putArray("array");
a.add(true);
base.putNull("misc");
assertSame(base,
MAPPER.readerForUpdating(base)
.readValue(aposToQuotes(
"{'props':{'value':true, 'extra':25.5, 'array' : [ 3 ]}}")));
assertEquals(2, base.size());
ObjectNode resultProps = (ObjectNode) base.get("props");
assertEquals(4, resultProps.size());

assertEquals(123, resultProps.path("base").asInt());
assertTrue(resultProps.path("value").asBoolean());
assertEquals(25.5, resultProps.path("extra").asDouble());
JsonNode n = resultProps.get("array");
assertEquals(ArrayNode.class, n.getClass());
assertEquals(2, n.size());
assertEquals(3, n.get(1).asInt());
}

public void testArrayNodeUpdateValue() throws Exception
{
ArrayNode base = MAPPER.createArrayNode();
base.add("first");
assertSame(base,
MAPPER.readerForUpdating(base)
.readValue(aposToQuotes("['second','third']")));
assertEquals(3, base.size());
.readValue(aposToQuotes("['second',false,null]")));
assertEquals(4, base.size());
assertEquals("first", base.path(0).asText());
assertEquals("second", base.path(1).asText());
assertEquals("third", base.path(2).asText());
assertFalse(base.path(2).asBoolean());
assertTrue(base.path(3).isNull());
}

public void testArrayNodeMerge() throws Exception
Expand Down
Loading

0 comments on commit 0c8642c

Please sign in to comment.