Skip to content

Commit

Permalink
Add more test coverage for ObjectReader/ObjectWriter; fix 2 minor iss…
Browse files Browse the repository at this point in the history
…ues found
  • Loading branch information
cowtowncoder committed Oct 19, 2016
1 parent 6362e24 commit b9904fa
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,10 @@ public ObjectReader withValueToUpdate(Object value)
{
if (value == _valueToUpdate) return this;
if (value == null) {
throw new IllegalArgumentException("cat not update null value");
// 18-Oct-2016, tatu: Actually, should be allowed, to remove value
// to update, if any
return _new(this, _config, _valueType, _rootDeserializer, null,
_schema, _injectableValues, _dataFormatReaders);
}
JavaType t;

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected ObjectWriter(ObjectWriter base, JsonFactory f)

_serializerProvider = base._serializerProvider;
_serializerFactory = base._serializerFactory;
_generatorFactory = base._generatorFactory;
_generatorFactory = f;

_generatorSettings = base._generatorSettings;
_prefetch = base._prefetch;
Expand Down Expand Up @@ -868,10 +868,21 @@ public boolean isEnabled(MapperFeature f) {
return _config.isEnabled(f);
}

/**
* @since 2.8.5
*/
@Deprecated
public boolean isEnabled(JsonParser.Feature f) {
return _generatorFactory.isEnabled(f);
}

/**
* @since 2.8.5
*/
public boolean isEnabled(JsonGenerator.Feature f) {
return _generatorFactory.isEnabled(f);
}

/**
* @since 2.2
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,20 @@ public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> c
* instead.
*/
@Deprecated
public abstract Collection<NamedType> collectAndResolveSubtypes(AnnotatedMember property,
MapperConfig<?> config, AnnotationIntrospector ai, JavaType baseType);

public Collection<NamedType> collectAndResolveSubtypes(AnnotatedMember property,
MapperConfig<?> config, AnnotationIntrospector ai, JavaType baseType) {
return collectAndResolveSubtypesByClass(config, property, baseType);
}

/**
* @deprecated Since 2.6 Use either
* {@link #collectAndResolveSubtypesByClass(MapperConfig, AnnotatedClass)}
* or {@link #collectAndResolveSubtypesByTypeId(MapperConfig, AnnotatedClass)}
* instead.
*/
@Deprecated
public abstract Collection<NamedType> collectAndResolveSubtypes(AnnotatedClass baseType,
MapperConfig<?> config, AnnotationIntrospector ai);
public Collection<NamedType> collectAndResolveSubtypes(AnnotatedClass baseType,
MapperConfig<?> config, AnnotationIntrospector ai) {
return collectAndResolveSubtypesByClass(config, baseType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Collection<NamedType> collectAndResolveSubtypesByClass(MapperConfig<?> co
{
final AnnotationIntrospector ai = config.getAnnotationIntrospector();
HashMap<NamedType, NamedType> subtypes = new HashMap<NamedType, NamedType>();
// [JACKSON-257] then consider registered subtypes (which have precedence over annotations)
// then consider registered subtypes (which have precedence over annotations)
if (_registeredSubtypes != null) {
Class<?> rawBase = type.getRawType();
for (NamedType subtype : _registeredSubtypes) {
Expand Down Expand Up @@ -181,28 +181,6 @@ public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> c
return _combineNamedAndUnnamed(typesHandled, byName);
}

/*
/**********************************************************
/* Deprecated method overrides
/**********************************************************
*/

@Override
@Deprecated
public Collection<NamedType> collectAndResolveSubtypes(AnnotatedMember property,
MapperConfig<?> config, AnnotationIntrospector ai, JavaType baseType)
{
return collectAndResolveSubtypesByClass(config, property, baseType);
}

@Override
@Deprecated
public Collection<NamedType> collectAndResolveSubtypes(AnnotatedClass type,
MapperConfig<?> config, AnnotationIntrospector ai)
{
return collectAndResolveSubtypesByClass(config, type);
}

/*
/**********************************************************
/* Internal methods
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.FormatSchema;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -26,6 +27,13 @@ public abstract class BaseMapTest
/**********************************************************
*/

public static class BogusSchema implements FormatSchema {
@Override
public String getSchemaType() {
return "TestFormat";
}
}

/**
* Simple wrapper around boolean types, usually to test value
* conversions or wrapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testProps()
ObjectMapper m = new ObjectMapper();
// should have default factory
assertNotNull(m.getNodeFactory());
JsonNodeFactory nf = JsonNodeFactory.instance;
JsonNodeFactory nf = new JsonNodeFactory(true);
m.setNodeFactory(nf);
assertNull(m.getInjectableValues());
assertSame(nf, m.getNodeFactory());
Expand Down Expand Up @@ -328,8 +328,14 @@ public void testDataInputViaMapper() throws Exception
assertEquals(Integer.valueOf(1), map.get("a"));

input = new DataInputStream(new ByteArrayInputStream(src));
// and via ObjectReader
map = MAPPER.readerFor(Map.class)
.readValue(input);
assertEquals(Integer.valueOf(1), map.get("a"));

input = new DataInputStream(new ByteArrayInputStream(src));
JsonNode n = MAPPER.readerFor(Map.class)
.readTree(input);
assertNotNull(n);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ void setB(String b) {
}
}

private final ObjectMapper MAPPER = new ObjectMapper();

// [databind#318] (and Scala module issue #83]
public void testValueUpdateWithCreator() throws Exception
{
Bean bean = new Bean("abc", "def");
new ObjectMapper().readerFor(Bean.class).withValueToUpdate(bean).readValue("{\"a\":\"ghi\",\"b\":\"jkl\"}");
MAPPER.readerFor(Bean.class).withValueToUpdate(bean).readValue("{\"a\":\"ghi\",\"b\":\"jkl\"}");
assertEquals("ghi", bean.getA());
assertEquals("jkl", bean.getB());
}

public void testValueUpdateOther() throws Exception
{
Bean bean = new Bean("abc", "def");
ObjectReader r = MAPPER.reader().withValueToUpdate(bean);
// but, changed our minds, no update
r = r.withValueToUpdate(null);
}
}
106 changes: 100 additions & 6 deletions src/test/java/com/fasterxml/jackson/databind/seq/ObjectReaderTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.fasterxml.jackson.databind.seq;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.FormatSchema;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

public class ObjectReaderTest extends BaseMapTest
{
Expand All @@ -18,6 +21,16 @@ static class POJO {
public Map<String, Object> name;
}

public void testSimpleViaParser() throws Exception
{
final String JSON = "[1]";
JsonParser p = MAPPER.getFactory().createParser(JSON);
Object ob = MAPPER.readerFor(Object.class)
.readValue(p);
p.close();
assertTrue(ob instanceof List<?>);
}

public void testParserFeatures() throws Exception
{
final String JSON = "[ /* foo */ 7 ]";
Expand Down Expand Up @@ -62,7 +75,7 @@ public void testPointerLoading() throws Exception {
public void testPointerLoadingAsJsonNode() throws Exception {
final String source = "{\"foo\":{\"bar\":{\"caller\":{\"name\":{\"value\":1234}}}}}";

ObjectReader reader = MAPPER.readerFor(POJO.class).at("/foo/bar/caller");
ObjectReader reader = MAPPER.readerFor(POJO.class).at(JsonPointer.compile("/foo/bar/caller"));

JsonNode node = reader.readTree(source);
assertTrue(node.has("name"));
Expand Down Expand Up @@ -105,4 +118,85 @@ public void testPointerLoadingMappingIteratorMany() throws Exception {
assertFalse(itr.hasNext());
itr.close();
}

public void testNodeHandling() throws Exception
{
JsonNodeFactory nodes = new JsonNodeFactory(true);
ObjectReader r = MAPPER.reader().with(nodes);
assertTrue(r.createArrayNode().isArray());
assertTrue(r.createObjectNode().isObject());
}

public void testSettings() throws Exception
{
ObjectReader r = MAPPER.reader();
assertSame(MAPPER.getFactory(), r.getFactory());

JsonFactory f = new JsonFactory();
r = r.with(f);
assertSame(f, r.getFactory());

assertNotNull(r.getTypeFactory());
assertNull(r.getInjectableValues());

r = r.withAttributes(Collections.emptyMap());
ContextAttributes attrs = r.getAttributes();
assertNotNull(attrs);
assertNull(attrs.getAttribute("abc"));

r = r.forType(MAPPER.constructType(String.class));
r = r.withRootName(PropertyName.construct("foo"));

r = r.withoutFeatures(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
assertFalse(r.isEnabled(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES));
assertFalse(r.isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE));
r = r.withFeatures(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
assertTrue(r.isEnabled(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES));
assertTrue(r.isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE));
}

public void testNoPrefetch() throws Exception
{
ObjectReader r = MAPPER.reader()
.without(DeserializationFeature.EAGER_DESERIALIZER_FETCH);
Number n = r.forType(Integer.class).readValue("123 ");
assertEquals(Integer.valueOf(123), n);
}

/*
/**********************************************************
/* Test methods, failures
/**********************************************************
*/

public void testMissingType() throws Exception
{
ObjectReader r = MAPPER.reader();
try {
r.readValue("1");
fail("Should not pass");
} catch (JsonMappingException e) {
verifyException(e, "No value type configured");
}
}

public void testSchema() throws Exception
{
ObjectReader r = MAPPER.readerFor(String.class);

// Ok to try to set `null` schema, always:
r = r.with((FormatSchema) null);

try {
// but not schema that doesn't match format (no schema exists for json)
r = r.with(new BogusSchema())
.readValue(quote("foo"));

fail("Should not pass");
} catch (IllegalArgumentException e) {
verifyException(e, "Can not use FormatSchema");
}
}
}
Loading

0 comments on commit b9904fa

Please sign in to comment.