|
| 1 | +package com.fasterxml.jackson.dataformat.ion.sequence; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.MappingIterator; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.SequenceWriter; |
| 10 | +import com.fasterxml.jackson.dataformat.ion.IonObjectMapper; |
| 11 | +import java.io.ByteArrayInputStream; |
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +public class MappingIteratorTest { |
| 16 | + |
| 17 | + private static final ObjectMapper MAPPER = new IonObjectMapper(); |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testReadFromWrite() throws Exception { |
| 21 | + final Object[] values = new String[]{"1", "2", "3", "4"}; |
| 22 | + |
| 23 | + // write |
| 24 | + final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 25 | + try (SequenceWriter seq = MAPPER.writer().writeValues(out)) { |
| 26 | + for (Object value : values) { |
| 27 | + seq.write(value); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // read |
| 32 | + final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); |
| 33 | + try (MappingIterator<Object> it = MAPPER.readerFor(Object.class).readValues(in)) { |
| 34 | + for (Object value : values) { |
| 35 | + assertTrue(it.hasNext()); |
| 36 | + assertTrue(it.hasNext()); // should not alter the iterator state |
| 37 | + assertEquals(value, it.next()); |
| 38 | + } |
| 39 | + assertFalse(it.hasNext()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testReadFromEmpty() throws Exception { |
| 45 | + final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]); |
| 46 | + try (MappingIterator<Object> it = MAPPER.readerFor(Object.class).readValues(in)) { |
| 47 | + assertFalse(it.hasNext()); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments