Search in sources :

Example 16 with IonDatagram

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

the class IonWriterTestCase method testWritingWithSystemImport.

@Test
public void testWritingWithSystemImport() throws Exception {
    final int FRED_ID_OFFSET = systemMaxId();
    final int LOCAL_ID_OFFSET = FRED_ID_OFFSET + FRED_MAX_IDS[1];
    SymbolTable fred1 = Symtabs.register("fred", 1, catalog());
    iw = makeWriter(system().getSystemSymbolTable(), fred1);
    iw.writeSymbol("fred_2");
    iw.writeSymbol("localSym");
    byte[] bytes = outputByteArray();
    IonDatagram dg = loader().load(bytes);
    assertEquals(4, dg.systemSize());
    IonValue f2sym = dg.systemGet(2);
    IonValue local = dg.systemGet(3);
    checkSymbol("fred_2", FRED_ID_OFFSET + 2, f2sym);
    checkSymbol("localSym", local);
    SymbolTable symtab = f2sym.getSymbolTable();
    assertSame(symtab, local.getSymbolTable());
    SymbolTable[] importedTables = symtab.getImportedTables();
    assertEquals(1, importedTables.length);
    assertSame(fred1, importedTables[0]);
}
Also used : IonValue(com.amazon.ion.IonValue) IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 17 with IonDatagram

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

the class IonWriterTestCase method testWritingClearedAnnotations.

/**
 * Test case to ensure that {@link IonWriter#setTypeAnnotations(String...)}
 * and {@link IonWriter#setTypeAnnotationSymbols(SymbolToken...)} clearing
 * of type annotations behavior is correct.
 */
@Test
public void testWritingClearedAnnotations() throws Exception {
    iw = makeWriter();
    IonDatagram expected = system().newDatagram();
    // ===== Test on IonWriter.setTypeAnnotationSymbols(...) =====
    // empty SymbolToken[]
    iw.setTypeAnnotationSymbols(new SymbolToken[0]);
    iw.writeNull();
    IonValue v = expected.add().newNull();
    v.clearTypeAnnotations();
    // null SymbolToken[]
    iw.setTypeAnnotationSymbols((SymbolToken[]) null);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    // empty SymbolToken[]
    iw.setTypeAnnotationSymbols(new SymbolToken[0]);
    // expected: the pending "b" annotation is cleared
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    // null SymbolToken[]
    iw.setTypeAnnotationSymbols((SymbolToken[]) null);
    // expected: the pending "b" annotation is cleared
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    // ===== Test on IonWriter.setTypeAnnotations(...) =====
    // empty String[]
    iw.setTypeAnnotations(new String[0]);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    // null String[]
    iw.setTypeAnnotations((String[]) null);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    // empty String[]
    iw.setTypeAnnotations(new String[0]);
    // expected: the pending "b" annotation is cleared
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    // null String[]
    iw.setTypeAnnotations((String[]) null);
    // expected: the pending "b" annotation is cleared
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    assertEquals(expected, reload());
}
Also used : IonValue(com.amazon.ion.IonValue) SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken) FakeSymbolToken(com.amazon.ion.FakeSymbolToken) IonDatagram(com.amazon.ion.IonDatagram) IonString(com.amazon.ion.IonString) Test(org.junit.Test)

Example 18 with IonDatagram

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

the class IonWriterTestCase method testWriteValuesWithSymtab.

@Test
public void testWriteValuesWithSymtab() throws Exception {
    SymbolTable fredSymtab = Symtabs.register(Symtabs.FRED_NAME, 1, catalog());
    SymbolTable gingerSymtab = Symtabs.register(Symtabs.GINGER_NAME, 1, catalog());
    String gingerSym = gingerSymtab.findKnownSymbol(1);
    // First setup some data to be copied.
    IonDatagram dg = system().newDatagram(gingerSymtab);
    dg.add().newSymbol(gingerSym);
    IonReader r = system().newReader(dg.getBytes());
    // Now copy that data into a non-top-level context
    iw = makeWriter(fredSymtab);
    iw.stepIn(IonType.LIST);
    iw.writeValues(r);
    iw.stepOut();
    IonDatagram result = reload();
    IonList l = (IonList) result.get(0);
    assertEquals(1, l.size());
    IonSymbol s = (IonSymbol) l.get(0);
    // Should've assigned a new SID
    checkSymbol(gingerSym, s);
}
Also used : IonSymbol(com.amazon.ion.IonSymbol) IonDatagram(com.amazon.ion.IonDatagram) IonList(com.amazon.ion.IonList) IonReader(com.amazon.ion.IonReader) SymbolTable(com.amazon.ion.SymbolTable) IonString(com.amazon.ion.IonString) Test(org.junit.Test)

Example 19 with IonDatagram

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

the class IonWriterTestCase method testWritingAnnotations.

@Test
public void testWritingAnnotations() throws Exception {
    iw = makeWriter();
    IonDatagram expected = system().newDatagram();
    iw.addTypeAnnotation("a");
    iw.writeNull();
    IonValue v = expected.add().newNull();
    v.addTypeAnnotation("a");
    iw.addTypeAnnotation("b");
    iw.addTypeAnnotation("c");
    iw.writeNull();
    v = expected.add().newNull();
    v.addTypeAnnotation("b");
    v.addTypeAnnotation("c");
    iw.addTypeAnnotation("b");
    iw.addTypeAnnotation("b");
    iw.writeNull();
    v = expected.add().newNull();
    v.setTypeAnnotations("b", "b");
    iw.setTypeAnnotations("b", "b");
    iw.writeNull();
    v = expected.add().newNull();
    v.setTypeAnnotations("b", "b");
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotations("c", "d");
    iw.writeNull();
    v = expected.add().newNull();
    v.setTypeAnnotations("c", "d");
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotations(new String[0]);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotations((String[]) null);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotations();
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotationSymbols(new SymbolToken[0]);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotationSymbols((SymbolToken[]) null);
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    iw.addTypeAnnotation("b");
    iw.setTypeAnnotationSymbols();
    iw.writeNull();
    v = expected.add().newNull();
    v.clearTypeAnnotations();
    assertEquals(expected, reload());
}
Also used : IonValue(com.amazon.ion.IonValue) SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl._Private_Utils.newSymbolToken(com.amazon.ion.impl._Private_Utils.newSymbolToken) FakeSymbolToken(com.amazon.ion.FakeSymbolToken) IonDatagram(com.amazon.ion.IonDatagram) IonString(com.amazon.ion.IonString) Test(org.junit.Test)

Example 20 with IonDatagram

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

the class IonWriterTestCase method testWritingDeepNestedList.

@Test
public void testWritingDeepNestedList() throws Exception {
    IonDatagram dg = loader().load("[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]");
    iw = makeWriter();
    dg.writeTo(iw);
    iw.writeValue(dg);
}
Also used : 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