Skip to content

Commit

Permalink
Working on tests for #2211
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 20, 2019
1 parent 9659d43 commit aea65f1
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2594,7 +2594,14 @@ public JsonNode readTree(String content) throws IOException {
public JsonNode readTree(byte[] content) throws IOException {
return _readTreeAndClose(_jsonFactory.createParser(content));
}


/**
* @since 2.10
*/
public JsonNode readTree(byte[] content, int offset, int len) throws IOException {
return _readTreeAndClose(_jsonFactory.createParser(content, offset, len));
}

/**
* Method to deserialize JSON content as tree expressed using set of {@link JsonNode} instances.
* Returns root of the resulting tree (where root can consist of just a single node if the current
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/ObjectReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,12 @@ public <T> T readValue(DataInput src) throws IOException
return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src), false));
}

/*
/**********************************************************
/* Deserialization methods; JsonNode ("tree")
/**********************************************************
*/

/**
* Method that reads content from given input source,
* using configuration of this reader, and binds it as JSON Tree.
Expand Down Expand Up @@ -1358,6 +1364,28 @@ public JsonNode readTree(String json) throws IOException
return _bindAndCloseAsTree(_considerFilter(_parserFactory.createParser(json), false));
}

/**
* @since 2.10
*/
public JsonNode readTree(byte[] json) throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(json);
}
return _bindAndCloseAsTree(_considerFilter(_parserFactory.createParser(json), false));
}

/**
* @since 2.10
*/
public JsonNode readTree(byte[] json, int offset, int len) throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(json);
}
return _bindAndCloseAsTree(_considerFilter(_parserFactory.createParser(json, offset, len), false));
}

public JsonNode readTree(DataInput src) throws IOException
{
if (_dataFormatReaders != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.*;

/**
Expand All @@ -12,35 +15,127 @@ public class EmptyContentAsTreeTest extends BaseMapTest
{
private final ObjectMapper MAPPER = objectMapper();

private final String EMPTY0 = "";
private final byte[] EMPTY0_BYTES = EMPTY0.getBytes(StandardCharsets.UTF_8);
private final String EMPTY1 = " \n\t ";
private final byte[] EMPTY1_BYTES = EMPTY1.getBytes(StandardCharsets.UTF_8);

// [databind#1406]: when passing `JsonParser`, indicate lack of content
// by returning `null`

public void testNullFromEOFWithParser() throws Exception
public void testNullFromEOFWithParserAndMapper() throws Exception
{
assertNull(MAPPER.readTree(new StringReader("")));
assertNull(MAPPER.readTree(new ByteArrayInputStream(new byte[0])));
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY0))) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY1))) {
_assertNullTree(MAPPER.readTree(p));
}

try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0_BYTES)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
_assertNullTree(MAPPER.readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
_assertNullTree(MAPPER.readTree(p));
}
}

// [databind#1406]
public void testNullFromEOFWithParserViaReader() throws Exception
public void testNullFromEOFWithParserAndReader() throws Exception
{
assertNull(MAPPER.readTree(new StringReader("")));
assertNull(MAPPER.readTree(new ByteArrayInputStream(new byte[0])));
assertNull(MAPPER.readerFor(JsonNode.class)
.readTree(new StringReader("")));
assertNull(MAPPER.readerFor(JsonNode.class)
.readTree(new ByteArrayInputStream(new byte[0])));
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0)) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1)) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY0))) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new StringReader(EMPTY1))) {
_assertNullTree(MAPPER.reader().readTree(p));
}

try (JsonParser p = MAPPER.getFactory().createParser(EMPTY0_BYTES)) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES)) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(EMPTY1_BYTES, 0, EMPTY1_BYTES.length)) {
_assertNullTree(MAPPER.reader().readTree(p));
}

try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY0_BYTES))) {
_assertNullTree(MAPPER.reader().readTree(p));
}
try (JsonParser p = MAPPER.getFactory().createParser(new ByteArrayInputStream(EMPTY1_BYTES))) {
_assertNullTree(MAPPER.reader().readTree(p));
}
}

// [databind#2211]: when passing content sources OTHER than `JsonParser`,
// return "missing node" instead of alternate (return `null`, throw exception).
public void testMissingNodeForEOFOther() throws Exception
public void testMissingNodeForEOFOtherMapper() throws Exception
{
_assertMissing(MAPPER.readTree(EMPTY0));
_assertMissing(MAPPER.readTree(EMPTY1));
_assertMissing(MAPPER.readTree(new StringReader(EMPTY0)));
_assertMissing(MAPPER.readTree(new StringReader(EMPTY1)));

_assertMissing(MAPPER.readTree(EMPTY0_BYTES));
_assertMissing(MAPPER.readTree(EMPTY0_BYTES, 0, EMPTY0_BYTES.length));
_assertMissing(MAPPER.readTree(new ByteArrayInputStream(EMPTY0_BYTES)));
_assertMissing(MAPPER.readTree(EMPTY1_BYTES));
_assertMissing(MAPPER.readTree(EMPTY1_BYTES, 0, EMPTY1_BYTES.length));
_assertMissing(MAPPER.readTree(new ByteArrayInputStream(EMPTY1_BYTES)));

// Assume File, URL, etc are fine. Note: `DataInput` probably can't be made to
// work since it can not easily/gracefully handle unexpected end-of-input
}

public void testMissingNodeForEOFOtherViaReader() throws Exception
public void testMissingNodeViaObjectReader() throws Exception
{
_assertMissing(MAPPER.reader().readTree(EMPTY0));
_assertMissing(MAPPER.reader().readTree(EMPTY1));
_assertMissing(MAPPER.reader().readTree(new StringReader(EMPTY0)));
_assertMissing(MAPPER.reader().readTree(new StringReader(EMPTY1)));

_assertMissing(MAPPER.reader().readTree(EMPTY0_BYTES));
_assertMissing(MAPPER.reader().readTree(EMPTY0_BYTES, 0, EMPTY0_BYTES.length));
_assertMissing(MAPPER.reader().readTree(new ByteArrayInputStream(EMPTY0_BYTES)));
_assertMissing(MAPPER.reader().readTree(EMPTY1_BYTES));
_assertMissing(MAPPER.reader().readTree(EMPTY1_BYTES, 0, EMPTY1_BYTES.length));
_assertMissing(MAPPER.reader().readTree(new ByteArrayInputStream(EMPTY1_BYTES)));
}

private void _assertNullTree(TreeNode n) {
if (n != null) {
fail("Should get `null` for reads with `JsonParser`, instead got: "+n.getClass().getName());
}
}

private void _assertMissing(JsonNode n) {
/*
assertNotNull("Should not get `null` but `MissingNode`", n);
if (!n.isMissingNode()) {
fail("Should get `MissingNode` but got: "+n.getClass().getName());
}
*/
}
}

0 comments on commit aea65f1

Please sign in to comment.