Search in sources :

Example 86 with IonDatagram

use of com.amazon.ion.IonDatagram in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method incrementalMultipleValuesLoadFromReader.

@Test
public void incrementalMultipleValuesLoadFromReader() throws Exception {
    ResizingPipedInputStream pipe = new ResizingPipedInputStream(128);
    final IonReaderBinaryIncremental reader = new IonReaderBinaryIncremental(STANDARD_READER_BUILDER, pipe);
    final IonLoader loader = SYSTEM.getLoader();
    byte[] bytes = toBinary("value_type::\"StringValueLong\"");
    for (byte b : bytes) {
        IonDatagram empty = loader.load(reader);
        assertTrue(empty.isEmpty());
        pipe.receive(b);
    }
    IonDatagram firstValue = loader.load(reader);
    assertEquals(1, firstValue.size());
    IonString string = (IonString) firstValue.get(0);
    assertEquals("StringValueLong", string.stringValue());
    assertEquals(Collections.singletonList("value_type"), Arrays.asList(string.getTypeAnnotations()));
    bytes = toBinary("{foobar: \"StringValueLong\"}");
    for (byte b : bytes) {
        IonDatagram empty = loader.load(reader);
        assertTrue(empty.isEmpty());
        pipe.receive(b);
    }
    IonDatagram secondValue = loader.load(reader);
    assertEquals(1, secondValue.size());
    IonStruct struct = (IonStruct) secondValue.get(0);
    string = (IonString) struct.get("foobar");
    assertEquals("StringValueLong", string.stringValue());
    IonDatagram empty = loader.load(reader);
    assertTrue(empty.isEmpty());
    reader.close();
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonString(com.amazon.ion.IonString) IonDatagram(com.amazon.ion.IonDatagram) IonLoader(com.amazon.ion.IonLoader) Test(org.junit.Test)

Example 87 with IonDatagram

use of com.amazon.ion.IonDatagram in project ion-java by amzn.

the class IonReaderBinaryIncrementalTest method incrementalMultipleValuesLoadFromInputStreamFails.

@Test
public void incrementalMultipleValuesLoadFromInputStreamFails() throws Exception {
    final ResizingPipedInputStream pipe = new ResizingPipedInputStream(1);
    final IonLoader loader = IonSystemBuilder.standard().withReaderBuilder(STANDARD_READER_BUILDER).build().getLoader();
    IonDatagram empty = loader.load(pipe);
    assertTrue(empty.isEmpty());
    pipe.receive(_Private_IonConstants.BINARY_VERSION_MARKER_1_0[0]);
    // Because reader does not persist across load invocations, the loader must throw an exception if the reader
    // had an incomplete value buffered.
    thrown.expect(IonException.class);
    loader.load(pipe);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonLoader(com.amazon.ion.IonLoader) Test(org.junit.Test)

Example 88 with IonDatagram

use of com.amazon.ion.IonDatagram in project ion-java by amzn.

the class BinaryWriterWithLocalSymtabsTest method testWritingWithImports.

@Test
public void testWritingWithImports() throws Exception {
    final int fredSidOffset = systemMaxId();
    final int localSidOffset = fredSidOffset + FRED_MAX_IDS[1];
    SymbolTable fred1 = Symtabs.register("fred", 1, catalog());
    SymbolTable localSymtab = makeLocalSymtab(system(), LOCAL_SYMBOLS_ABC, fred1);
    makeBinaryWriterWithLocalSymbols(localSymtab);
    // write test data
    myWriter.writeSymbol("a");
    // Builder makes a copy of the symtab
    assertNotSame(localSymtab, myWriter.getSymbolTable());
    localSymtab = myWriter.getSymbolTable();
    myWriter.writeSymbol("not_interned");
    assertSame(localSymtab, myWriter.getSymbolTable());
    myWriter.writeSymbol("fred_1");
    assertSame(localSymtab, myWriter.getSymbolTable());
    myWriter.writeInt(1234);
    assertSame(localSymtab, myWriter.getSymbolTable());
    // check written bytes
    byte[] bytes = outputByteArray();
    IonDatagram dg = loader().load(bytes);
    assertEquals(6, dg.systemSize());
    IonValue val = dg.systemGet(2);
    checkSymbol("a", localSidOffset + 1, val);
    val = dg.systemGet(3);
    checkSymbol("not_interned", localSidOffset + 4, val);
    val = dg.systemGet(4);
    checkSymbol("fred_1", fredSidOffset + 1, val);
    val = dg.systemGet(5);
    checkInt(1234, val);
}
Also used : IonValue(com.amazon.ion.IonValue) IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 89 with IonDatagram

use of com.amazon.ion.IonDatagram in project ion-java by amzn.

the class BinaryWriterWithLocalSymtabsTest method testWritingWithNoImports.

@Test
public void testWritingWithNoImports() throws Exception {
    final int localSidOffset = systemMaxId();
    SymbolTable localSymtab = makeLocalSymtab(system(), LOCAL_SYMBOLS_ABC);
    makeBinaryWriterWithLocalSymbols(localSymtab);
    // write test data
    myWriter.writeSymbol("a");
    // Builder makes a copy of the symtab
    assertNotSame(localSymtab, myWriter.getSymbolTable());
    localSymtab = myWriter.getSymbolTable();
    myWriter.writeSymbol("not_interned");
    assertSame(localSymtab, myWriter.getSymbolTable());
    myWriter.writeInt(1234);
    assertSame(localSymtab, myWriter.getSymbolTable());
    byte[] bytes = outputByteArray();
    IonDatagram dg = loader().load(bytes);
    // check written bytes
    assertEquals(5, dg.systemSize());
    IonValue val = dg.systemGet(2);
    checkSymbol("a", localSidOffset + 1, val);
    val = dg.systemGet(3);
    checkSymbol("not_interned", localSidOffset + 4, val);
    val = dg.systemGet(4);
    checkInt(1234, val);
}
Also used : IonValue(com.amazon.ion.IonValue) IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 90 with IonDatagram

use of com.amazon.ion.IonDatagram in project ion-java by amzn.

the class MiscStreamingTest method testValue2.

@Test
public void testValue2() throws Exception {
    String s = "item_view::{item_id:\"B00096H8Q4\",marketplace_id:2," + "product:{item_name:[" + "{value:'''Method 24CT Leather Wipes''',lang:EN_CA}," + "{value:'''Method 24CT Chiffons de Cuir''',lang:FR_CA}]," + "list_price:{value:18.23,unit:EUR},}" + ",index_suppressed:true," + "offline_store_only:true,version:2,}";
    IonSystem sys = system();
    IonDatagram dg = sys.getLoader().load(s);
    IonValue v = dg.get(0);
    IonType t = v.getType();
    assertSame("should be a struct", IonType.STRUCT, t);
    int tree_count = ((IonStruct) v).size();
    IonReader it = system().newReader(s);
    t = it.next();
    assertSame("should be a struct", IonType.STRUCT, t);
    int string_count = 0;
    it.stepIn();
    while (it.next() != null) {
        string_count++;
    }
    it.stepOut();
    assertSame("tree and string iterator should have the same size", tree_count, string_count);
    byte[] buf = dg.getBytes();
    it = system().newReader(buf);
    t = it.next();
    assertSame("should be a struct", IonType.STRUCT, t);
    int bin_count = 0;
    it.stepIn();
    while (it.next() != null) {
        bin_count++;
    }
    it.stepOut();
    assertSame("tree and binary iterator should have the same size", tree_count, bin_count);
}
Also used : IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) IonStruct(com.amazon.ion.IonStruct) IonType(com.amazon.ion.IonType) IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) IonString(com.amazon.ion.IonString) Test(org.junit.Test) BinaryTest(com.amazon.ion.BinaryTest)

Aggregations

IonDatagram (com.amazon.ion.IonDatagram)92 Test (org.junit.Test)77 IonValue (com.amazon.ion.IonValue)20 SymbolTable (com.amazon.ion.SymbolTable)18 IonReader (com.amazon.ion.IonReader)17 IonString (com.amazon.ion.IonString)15 IonStruct (com.amazon.ion.IonStruct)13 IonWriter (com.amazon.ion.IonWriter)11 IonSymbol (com.amazon.ion.IonSymbol)6 IonSystem (com.amazon.ion.IonSystem)6 IonLoader (com.amazon.ion.IonLoader)5 IonType (com.amazon.ion.IonType)5 SimpleCatalog (com.amazon.ion.system.SimpleCatalog)5 BlobTest (com.amazon.ion.BlobTest)4 ClobTest (com.amazon.ion.ClobTest)4 IntTest (com.amazon.ion.IntTest)4 IonList (com.amazon.ion.IonList)4 IonTextWriterBuilder (com.amazon.ion.system.IonTextWriterBuilder)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BinaryTest (com.amazon.ion.BinaryTest)2