Search in sources :

Example 56 with IonDatagram

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

the class IonManagedBinaryWriterTest method testFlushImmediatelyAfterIVM.

@Test
public void testFlushImmediatelyAfterIVM() throws Exception {
    writer.flush();
    writer.writeSymbol("burrito");
    writer.finish();
    IonReader reader = system().newReader(writer.getBytes());
    reader.next();
    assertEquals(reader.getSymbolTable().findSymbol("taco"), -1);
    assertEquals(reader.getSymbolTable().findSymbol("burrito"), 15);
    assertNull(reader.next());
    IonDatagram dg = system().getLoader().load(writer.getBytes());
    // Should be IVM SYMTAB burrito
    assertEquals(3, dg.systemSize());
    assertEquals("$ion_symbol_table", ((IonStruct) dg.systemGet(1)).getTypeAnnotations()[0]);
    assertEquals("burrito", ((IonSymbol) dg.systemGet(2)).stringValue());
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Example 57 with IonDatagram

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

the class SymbolTableTest method testLocalTableWithMissingImport.

@Test
public void testLocalTableWithMissingImport() throws IOException {
    // Use a big symtab to get beyond any default allocation within the
    // dummy symtab.  This was done to trap a bug in LocalSymbolTable.
    ArrayList<String> syms = new ArrayList<String>();
    int maxId = 50;
    for (int i = 1; i <= maxId; i++) {
        syms.add("S" + i);
    }
    SymbolTable table = system().newSharedSymbolTable("T", 1, syms.iterator());
    SimpleCatalog catalog = (SimpleCatalog) system().getCatalog();
    catalog.putTable(table);
    final int import1id = systemMaxId() + 1;
    final int import2id = systemMaxId() + 2;
    final int local1id = systemMaxId() + maxId + 1;
    final int local2id = local1id + 1;
    String text = LocalSymbolTablePrefix + "{" + "  symbols:[ \"local1\", \"local2\" ]," + "  imports:[{name:'''T''', version:1," + "            max_id:" + maxId + "}]," + "}\n" + "local1 local2 S1 S2";
    byte[] binary = encode(text);
    // Remove the imported table before decoding the binary.
    assertSame(table, catalog.removeTable("T", 1));
    IonDatagram dg = loader().load(binary);
    checkSymbol("local1", local1id, dg.get(0));
    checkSymbol("local2", local2id, dg.get(1));
    checkUnknownSymbol(import1id, dg.get(2));
    checkUnknownSymbol(import2id, dg.get(3));
    SymbolTable st = dg.get(3).getSymbolTable();
    checkLocalTable(st);
    checkUnknownSymbol("S1", import1id, st);
    checkUnknownSymbol("S2", import2id, st);
    assertEquals(local1id - 1, st.getImportedMaxId());
    SymbolTable dummy = checkFirstImport("T", 1, new String[maxId], st);
    assertTrue(dummy.isSubstitute());
    checkUnknownSymbol("S1", import1id, dummy);
    checkUnknownSymbol("S2", import2id, dummy);
    SymbolTable[] importedTables = st.getImportedTables();
    assertEquals(1, importedTables.length);
}
Also used : SimpleCatalog(com.amazon.ion.system.SimpleCatalog) IonDatagram(com.amazon.ion.IonDatagram) ArrayList(java.util.ArrayList) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 58 with IonDatagram

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

the class SymbolTableTest method testDupLocalSymbolOnDatagram.

@Test
public void testDupLocalSymbolOnDatagram() throws Exception {
    final IonSystem ion1 = system();
    final SymbolTable st = ion1.newSharedSymbolTable("foobar", 1, Arrays.asList("s1").iterator());
    final IonMutableCatalog cat = new SimpleCatalog();
    cat.putTable(st);
    // amzn/ion-java/issues/46 has the datagram producing something like:
    // $ion_1_0 $ion_symbol_table::{imports:[{name: "foobar", version: 1, max_id: 1}], symbols: ["s1", "l1"]} $11 $12
    // local table should not have "s1", user values should be $10 $11
    IonDatagram dg = ion1.newDatagram(st);
    dg.add().newSymbol("s1");
    dg.add().newSymbol("l1");
    final IonSystem ion2 = newSystem(cat);
    dg = ion2.getLoader().load(dg.getBytes());
    checkSymbol("s1", 10, dg.get(0));
    checkSymbol("l1", 11, dg.get(1));
}
Also used : SimpleCatalog(com.amazon.ion.system.SimpleCatalog) IonSystem(com.amazon.ion.IonSystem) IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) IonMutableCatalog(com.amazon.ion.IonMutableCatalog) Test(org.junit.Test)

Example 59 with IonDatagram

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

the class SymbolTableTest method testLocalSymbolTableAppendImportBoundary.

@Test
public void testLocalSymbolTableAppendImportBoundary() throws IOException {
    String text = LocalSymbolTablePrefix + "{" + "  symbols:[ \"s11\"]" + "}\n" + "1\n" + LocalSymbolTablePrefix + "{" + "  imports:" + ION_SYMBOL_TABLE + "," + "  symbols:[ \"s21\"]" + "}\n" + "null";
    IonDatagram datagram = loader().load(text);
    SymbolTable symbolTable = datagram.get(0).getSymbolTable();
    assertSame(symbolTable, datagram.get(1).getSymbolTable());
    symbolTable.intern("o1");
    symbolTable.intern("a1");
    // new symbols don't influence SIDs for existing symbols; they are appended
    checkSymbol("s11", systemMaxId() + 1, symbolTable);
    checkSymbol("s21", systemMaxId() + 2, symbolTable);
    checkSymbol("o1", systemMaxId() + 3, symbolTable);
    checkSymbol("a1", systemMaxId() + 4, symbolTable);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
    datagram.writeTo(writer);
    writer.close();
    IonDatagram roundtripped = loader().load(out.toByteArray());
    assertTrue(Equivalence.ionEquals(datagram, roundtripped));
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 60 with IonDatagram

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

the class SymbolTableTest method testWriteWithSymbolTable.

@Test
public void testWriteWithSymbolTable() throws IOException {
    // this example code is the fix for a previous defect
    // which resulted in a bad assertion in
    // IonWriterUser.close_local_symbol_table_copy
    IonDatagram data;
    data = system().newDatagram();
    insert_local_symbol_table(data);
    append_some_data(data);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = system().newTextWriter(out);
    writer.writeValue(data);
    writer.close();
    // dataMap.put("value", out.toByteArray());
    byte[] bytes = out.toByteArray();
    assertNotNull(bytes);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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