-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure IonReader instances created within IonFactory are always resou…
…rce-managed.
- Loading branch information
Showing
2 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
ion/src/test/java/com/fasterxml/jackson/dataformat/ion/IonFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.fasterxml.jackson.dataformat.ion; | ||
|
||
import com.amazon.ion.IonReader; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@RunWith(Parameterized.class) | ||
public class IonFactoryTest { | ||
|
||
// 4-byte Ion 1.0 IVM followed by int 0. | ||
private static final byte[] BINARY_INT_0 = new byte[] {(byte) 0xE0, 0x01, 0x00, (byte) 0xEA, 0x20}; | ||
private static final String TEXT_INT_0 = "0"; | ||
|
||
private enum InputSource { | ||
BYTE_ARRAY() { | ||
@Override | ||
IonParser newParser(IonFactory factory) throws IOException { | ||
return (IonParser) factory.createParser(BINARY_INT_0); | ||
} | ||
}, | ||
CHAR_ARRAY() { | ||
@Override | ||
IonParser newParser(IonFactory factory) throws IOException { | ||
return (IonParser) factory.createParser(TEXT_INT_0.toCharArray()); | ||
} | ||
}, | ||
READER() { | ||
@Override | ||
IonParser newParser(IonFactory factory) throws IOException { | ||
return (IonParser) factory.createParser(new StringReader(TEXT_INT_0)); | ||
} | ||
}, | ||
INPUT_STREAM() { | ||
@Override | ||
IonParser newParser(IonFactory factory) throws IOException { | ||
return (IonParser) factory.createParser(new ByteArrayInputStream(BINARY_INT_0)); | ||
} | ||
}, | ||
ION_READER() { | ||
@Override | ||
IonParser newParser(IonFactory factory) { | ||
return factory.createParser(factory.getIonSystem().newReader(BINARY_INT_0)); | ||
} | ||
}, | ||
ION_VALUE() { | ||
@Override | ||
IonParser newParser(IonFactory factory) { | ||
return factory.createParser(factory.getIonSystem().newInt(0)); | ||
} | ||
}; | ||
|
||
abstract IonParser newParser(IonFactory factory) throws IOException; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{0}") | ||
public static InputSource[] parameters() { | ||
return InputSource.values(); | ||
} | ||
|
||
@Parameterized.Parameter | ||
public InputSource inputSource; | ||
|
||
@Test | ||
public void testIonParserIsResourceManaged() throws IOException { | ||
IonFactory factory = new IonFactory(); | ||
IonParser parser = inputSource.newParser(factory); | ||
// When the user provides an IonReader, it is not resource-managed, meaning that the user retains the | ||
// responsibility to close it. In all other cases, the IonReader is created internally, is resource-managed, | ||
// and is closed automatically in IonParser.close(). | ||
assertEquals(inputSource != InputSource.ION_READER, parser._ioContext.isResourceManaged()); | ||
assertTrue(IonReader.class.isAssignableFrom(parser._ioContext.contentReference().getRawContent().getClass())); | ||
parser.close(); | ||
} | ||
} |