Search in sources :

Example 51 with IonDatagram

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

the class OptimizedBinaryWriterLengthPatchingTest method testOptimizedWriteLengthPatching.

/**
 * Tests that the internal implementation of binary {@link IonWriter}'s
 * patching mechanism is correct.
 *
 * @see IonWriterSystemBinary
 */
@Test
public void testOptimizedWriteLengthPatching() throws Exception {
    iw = makeWriter();
    // The expected datagram - as values are copied to the IonWriter, we
    // book-keep the values written so as to do an equality check at the end.
    IonDatagram expected = system().newDatagram();
    // === top level ===
    writeAndAddTypeDescLengthVariants(expected);
    // === nested list ===
    iw.stepIn(IonType.LIST);
    IonList expectedNestedList = expected.add().newEmptyList();
    {
        writeAndAddTypeDescLengthVariants(expectedNestedList);
    }
    iw.stepOut();
    // === nested sexp ===
    iw.stepIn(IonType.SEXP);
    IonSexp expectedNestedSexp = expected.add().newEmptySexp();
    {
        writeAndAddTypeDescLengthVariants(expectedNestedSexp);
    }
    iw.stepOut();
    // === nested struct ===
    iw.stepIn(IonType.STRUCT);
    IonStruct expectedNestedStruct = expected.add().newEmptyStruct();
    {
        writeAndAddTypeDescLengthVariants(expectedNestedStruct);
    }
    iw.stepOut();
    // === deeply nested list ===
    iw.stepIn(IonType.LIST);
    IonList nestedList1 = expected.add().newEmptyList();
    {
        writeAndAddTypeDescLengthVariants(nestedList1);
        iw.stepIn(IonType.LIST);
        {
            IonList nestedList2 = nestedList1.add().newEmptyList();
            writeAndAddTypeDescLengthVariants(nestedList2);
        }
        iw.stepOut();
    }
    iw.stepOut();
    IonDatagram actual = loader().load(outputByteArray());
    assertIonEquals(expected, actual);
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonSexp(com.amazon.ion.IonSexp) IonDatagram(com.amazon.ion.IonDatagram) IonList(com.amazon.ion.IonList) Test(org.junit.Test)

Example 52 with IonDatagram

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

the class OptimizedBinaryWriterSymbolTableTest method testOptimizedWriteValue_InterspersedReaderLSTs.

/**
 * Reader's source contains interspersed LSTs.
 * TODO amzn/ion-java/issues/39 Investigate allowing a config. option to copy reader's LST
 *              over to the writer.
 */
@Test
public void testOptimizedWriteValue_InterspersedReaderLSTs() throws Exception {
    String readerLST1 = printLocalSymtab("a", "aa");
    String readerLST2 = printLocalSymtab("b", "bb");
    byte[] source = encode(readerLST1 + "a aa " + readerLST2 + "b bb");
    ir = makeReaderProxy(source);
    iw = makeWriterWithLocalSymtab("a", "aa");
    // writer's symtab same as reader's - optimize
    // a
    checkWriteValueWithCompatibleSymtab();
    // aa
    checkWriteValueWithCompatibleSymtab();
    // writer's symtab diff. from reader's - no optimize
    // b
    checkWriteValueWithIncompatibleSymtab();
    // bb
    checkWriteValueWithIncompatibleSymtab();
    iw.close();
    IonDatagram expected = loader().load("a aa b bb");
    IonDatagram actual = loader().load(outputByteArray());
    assertIonEquals(expected, actual);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) Test(org.junit.Test)

Example 53 with IonDatagram

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

the class IonManagedBinaryWriterTest method testLocalSymbolTableAppend.

@Test
public void testLocalSymbolTableAppend() throws Exception {
    writer.writeSymbol("taco");
    writer.flush();
    writer.writeSymbol("burrito");
    writer.finish();
    IonReader reader = system().newReader(writer.getBytes());
    reader.next();
    assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
    assertEquals(reader.getSymbolTable().findSymbol("burrito"), lstAppendMode.isEnabled() ? -1 : 16);
    reader.next();
    assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
    assertEquals(reader.getSymbolTable().findSymbol("burrito"), 16);
    assertNull(reader.next());
    IonDatagram dg = system().getLoader().load(writer.getBytes());
    assertEquals(2, dg.size());
    assertEquals("taco", ((IonSymbol) dg.get(0)).stringValue());
    assertEquals("burrito", ((IonSymbol) dg.get(1)).stringValue());
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Example 54 with IonDatagram

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

the class IonManagedBinaryWriterTest method testManuallyWriteLSTAppendWithImportsFirst.

@Test
public void testManuallyWriteLSTAppendWithImportsFirst() throws Exception {
    writer.writeSymbol("taco");
    writer.addTypeAnnotation("$ion_symbol_table");
    writer.stepIn(IonType.STRUCT);
    writer.setFieldName("imports");
    writer.writeSymbol("$ion_symbol_table");
    writer.setFieldName("symbols");
    writer.stepIn(IonType.LIST);
    writer.writeString("burrito");
    writer.stepOut();
    writer.stepOut();
    writer.writeSymbol("burrito");
    writer.finish();
    IonReader reader = system().newReader(writer.getBytes());
    reader.next();
    assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
    assertEquals(reader.getSymbolTable().findSymbol("burrito"), lstAppendMode.isEnabled() ? -1 : 16);
    reader.next();
    assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
    assertEquals(reader.getSymbolTable().findSymbol("burrito"), 16);
    assertNull(reader.next());
    IonDatagram dg = system().getLoader().load(writer.getBytes());
    assertEquals(2, dg.size());
    assertEquals("taco", ((IonSymbol) dg.get(0)).stringValue());
    assertEquals("burrito", ((IonSymbol) dg.get(1)).stringValue());
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Example 55 with IonDatagram

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

the class IonManagedBinaryWriterTest method testNoNewSymbolsAfterFlush.

@Test
public void testNoNewSymbolsAfterFlush() throws Exception {
    writer.writeSymbol("taco");
    writer.flush();
    writer.writeInt(123);
    writer.finish();
    IonDatagram dg = system().getLoader().load(writer.getBytes());
    // Should be IVM SYMTAB taco 123
    assertEquals(4, dg.systemSize());
    assertEquals("$ion_symbol_table", ((IonStruct) dg.systemGet(1)).getTypeAnnotations()[0]);
    assertEquals("taco", ((IonSymbol) dg.systemGet(2)).stringValue());
    assertEquals(123, ((IonInt) dg.systemGet(3)).intValue());
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonDatagram(com.amazon.ion.IonDatagram) Test(org.junit.Test)

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