|
| 1 | +package com.fasterxml.jackson.dataformat.ion; |
| 2 | + |
| 3 | +import com.amazon.ion.IonReader; |
| 4 | +import com.fasterxml.jackson.core.JsonParser; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.StringReader; |
| 9 | + |
| 10 | +import static org.junit.Assert.assertEquals; |
| 11 | +import static org.junit.Assert.assertTrue; |
| 12 | + |
| 13 | +public class IonFactoryTest { |
| 14 | + |
| 15 | + // 4-byte Ion 1.0 IVM followed by int 0. |
| 16 | + private static final byte[] BINARY_INT_0 = new byte[] {(byte) 0xE0, 0x01, 0x00, (byte) 0xEA, 0x20}; |
| 17 | + private static final String TEXT_INT_0 = "0"; |
| 18 | + |
| 19 | + @Test |
| 20 | + public void byteArrayIsManaged() throws Throwable { |
| 21 | + assertResourceManaged(true, parser(f -> f.createParser(BINARY_INT_0))); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void charArrayIsManaged() throws Throwable { |
| 26 | + assertResourceManaged(true, parser(f -> f.createParser(TEXT_INT_0.toCharArray()))); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void readerIsManaged() throws Throwable { |
| 31 | + assertResourceManaged(true, parser(f -> f.createParser(new StringReader(TEXT_INT_0)))); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void inputStreamIsManaged() throws Throwable { |
| 36 | + assertResourceManaged(true, parser(f -> f.createParser(new ByteArrayInputStream(BINARY_INT_0)))); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void ionValueIsManaged() throws Throwable { |
| 41 | + assertResourceManaged(true, parser(f -> f.createParser(f.getIonSystem().newInt(0)))); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void ionReaderIsNotManaged() throws Throwable { |
| 46 | + // When the user provides an IonReader, it is not resource-managed, meaning that the user retains the |
| 47 | + // responsibility to close it. In all other cases, the IonReader is created internally, is resource-managed, |
| 48 | + // and is closed automatically in IonParser.close(). |
| 49 | + assertResourceManaged(false, parser(f -> f.createParser(f.getIonSystem().newReader(BINARY_INT_0)))); |
| 50 | + } |
| 51 | + |
| 52 | + private void assertResourceManaged(boolean expectResourceManaged, ThrowingSupplier<IonParser> supplier) |
| 53 | + throws Throwable { |
| 54 | + IonParser parser = supplier.get(); |
| 55 | + assertEquals(expectResourceManaged, parser._ioContext.isResourceManaged()); |
| 56 | + assertTrue(IonReader.class.isAssignableFrom(parser._ioContext.contentReference().getRawContent().getClass())); |
| 57 | + parser.close(); |
| 58 | + } |
| 59 | + |
| 60 | + private interface ThrowingFunction<T, R> { |
| 61 | + R apply(T t) throws Throwable; |
| 62 | + } |
| 63 | + |
| 64 | + private interface ThrowingSupplier<T> { |
| 65 | + T get() throws Throwable; |
| 66 | + } |
| 67 | + |
| 68 | + private static ThrowingSupplier<IonParser> parser(ThrowingFunction<IonFactory, JsonParser> f) { |
| 69 | + return () -> (IonParser) f.apply(new IonFactory()); |
| 70 | + } |
| 71 | +} |
0 commit comments