Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't close IonParser on EOF to be compatible with MappingIterator when source is an empty InputStream #487

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ public JsonToken nextToken() throws IOException
}
if (type == null) {
if (_parsingContext.inRoot()) { // EOF?
close();
_currToken = null;
} else {
_parsingContext = _parsingContext.getParent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.fasterxml.jackson.dataformat.ion.sequence;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.junit.Test;

public class MappingIteratorTest {

private static final ObjectMapper MAPPER = new IonObjectMapper();

@Test
public void testReadFromWrite() throws Exception {
final Object[] values = new String[]{"1", "2", "3", "4"};

// write
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (SequenceWriter seq = MAPPER.writer().writeValues(out)) {
for (Object value : values) {
seq.write(value);
}
}

// read
final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
try (MappingIterator<Object> it = MAPPER.readerFor(Object.class).readValues(in)) {
for (Object value : values) {
assertTrue(it.hasNext());
assertTrue(it.hasNext()); // should not alter the iterator state
assertEquals(value, it.next());
}
assertFalse(it.hasNext());
}
}

@Test
public void testReadFromEmpty() throws Exception {
final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
try (MappingIterator<Object> it = MAPPER.readerFor(Object.class).readValues(in)) {
assertFalse(it.hasNext());
}
}
}