Search in sources :

Example 26 with SymbolTable

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

the class SymbolTableTest method testParsedLocalTableMakeReadOnly.

@Test
public void testParsedLocalTableMakeReadOnly() throws Exception {
    String text = LocalSymbolTablePrefix + "{" + "  symbols:[ \"foo\", \"bar\"]," + "}\n" + "null";
    SymbolTable symbolTable = oneValue(text).getSymbolTable();
    symbolTable.intern("baz");
    symbolTable.makeReadOnly();
    symbolTable.intern("baz");
    try {
        symbolTable.intern("boo");
        fail("expected exception");
    } catch (ReadOnlyValueException e) {
    }
}
Also used : SymbolTable(com.amazon.ion.SymbolTable) ReadOnlyValueException(com.amazon.ion.ReadOnlyValueException) Test(org.junit.Test)

Example 27 with SymbolTable

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

the class SymbolTableTest method testMutateDomAfterLocalSymbolTableAppend.

@Test
public void testMutateDomAfterLocalSymbolTableAppend() throws IOException {
    String text = LocalSymbolTablePrefix + "{" + "   imports:[{name:\"foo\", version:1, max_id:1}], " + "   symbols:[\"s1\", \"s2\"]" + "}\n" + // Symbol with unknown text from "foo"
    "$10\n" + // s1
    "$11\n" + LocalSymbolTablePrefix + "{" + "   imports:" + ION_SYMBOL_TABLE + "," + "   symbols:[\"s3\"]" + "}\n" + // s2
    "$12";
    IonDatagram datagram = loader().load(text);
    datagram.add(1, system().newSymbol("abc"));
    // s3
    datagram.add(system().newSymbol(new SymbolTokenImpl(null, 13)));
    // Symbol with unknown text from "foo"
    datagram.add(system().newSymbol(new SymbolTokenImpl(null, 10)));
    ByteArrayOutputStream textOutput = new ByteArrayOutputStream();
    ByteArrayOutputStream binaryOutput = new ByteArrayOutputStream();
    List<String> fooSymbols = Collections.singletonList("bar");
    SymbolTable fooTable = system().newSharedSymbolTable("foo", 1, fooSymbols.iterator());
    IonWriter textWriter = IonTextWriterBuilder.standard().withImports(fooTable).build(textOutput);
    datagram.writeTo(textWriter);
    textWriter.close();
    IonWriter binaryWriter = IonBinaryWriterBuilder.standard().withImports(fooTable).build(binaryOutput);
    datagram.writeTo(binaryWriter);
    binaryWriter.close();
    SimpleCatalog catalog = new SimpleCatalog();
    catalog.putTable(fooTable);
    IonSystem system = IonSystemBuilder.standard().withCatalog(catalog).build();
    IonDatagram textRoundtrip = system.getLoader().load(textOutput.toByteArray());
    IonDatagram binaryRoundtrip = system.getLoader().load(binaryOutput.toByteArray());
    assertEquals(textRoundtrip, binaryRoundtrip);
    assertEquals("bar", ((IonSymbol) textRoundtrip.get(0)).stringValue());
    assertEquals("abc", ((IonSymbol) textRoundtrip.get(1)).stringValue());
    assertEquals("s1", ((IonSymbol) textRoundtrip.get(2)).stringValue());
    assertEquals("s2", ((IonSymbol) textRoundtrip.get(3)).stringValue());
    assertEquals("s3", ((IonSymbol) textRoundtrip.get(4)).stringValue());
    assertEquals("bar", ((IonSymbol) textRoundtrip.get(0)).stringValue());
}
Also used : SimpleCatalog(com.amazon.ion.system.SimpleCatalog) IonSystem(com.amazon.ion.IonSystem) IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 28 with SymbolTable

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

the class SymbolTableTest method testLocalTableWithGreaterImport.

/**
 * Import v2 but catalog has v3.
 */
@Test
public void testLocalTableWithGreaterImport() throws IOException {
    final int import1id = systemMaxId() + 1;
    final int import2id = systemMaxId() + 2;
    final int fred3id = systemMaxId() + 3;
    final int maxLocalId = systemMaxId() + IMPORTED_2_MAX_ID + 3;
    registerImportedV1();
    registerImportedV2();
    registerImportedV3();
    // fred5 is not in table version 2, so it gets local symbol
    String text = LocalSymbolTablePrefix + "{" + "  imports:[{name:\"imported\", version:2, " + "            max_id:" + IMPORTED_2_MAX_ID + "}]," + "}\n" + "local1 local2 'imported 1' 'imported 2' fred3 fred5";
    byte[] binary = encode(text);
    // Remove the imported table before decoding the binary.
    SimpleCatalog catalog = (SimpleCatalog) system().getCatalog();
    assertNotNull(catalog.removeTable("imported", 2));
    IonDatagram dg = loader().load(binary);
    checkSymbol("local1", dg.get(0));
    checkSymbol("local2", dg.get(1));
    checkSymbol("imported 1", import1id, dg.get(2));
    checkUnknownSymbol(import2id, dg.get(3));
    checkSymbol("fred3", fred3id, dg.get(4));
    checkSymbol("fred5", dg.get(5));
    SymbolTable st = dg.get(0).getSymbolTable();
    checkFirstImport("imported", 2, new String[] { "imported 1", null, "fred3", "fred4" }, st);
    SymbolTable imported = st.getImportedTables()[0];
    checkUnknownSymbol("fred5", 5, imported);
    assertTrue(st.isLocalTable());
    assertEquals(maxLocalId, st.getMaxId());
}
Also used : SimpleCatalog(com.amazon.ion.system.SimpleCatalog) IonDatagram(com.amazon.ion.IonDatagram) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 29 with SymbolTable

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

the class BinaryStreamingTest 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);
    IonValue v2 = ((IonStruct) v).get("offline_store_only");
    SymbolTable sym = v.getSymbolTable();
    assert v2.getSymbolTable() == sym;
    IonReader ir = system().newReader(s);
    Iterator<String> symbols = sym.iterateDeclaredSymbolNames();
    SymbolTable u = system().newSharedSymbolTable("items", 1, symbols);
    IonBinaryWriter wr = system().newBinaryWriter(u);
    wr.writeValues(ir);
    byte[] buffer = wr.getBytes();
    dumpBuffer(buffer, buffer.length);
    ir = getStreamingMode().newIonReader(system().getCatalog(), buffer);
    wr = system().newBinaryWriter(u);
    wr.writeValues(ir);
    buffer = wr.getBytes();
    dumpBuffer(buffer, buffer.length);
}
Also used : IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) IonStruct(com.amazon.ion.IonStruct) IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) IonBinaryWriter(com.amazon.ion.IonBinaryWriter) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 30 with SymbolTable

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

the class SymbolTableTest method registerImportedV2.

public SymbolTable registerImportedV2() {
    IonSystem system = system();
    String importingText = SharedSymbolTablePrefix + "{" + "  name:'''imported''', version:2," + "  symbols:[" + "    '''imported 1'''," + "    '''imported 2'''," + "    '''fred3'''," + "    '''fred4'''," + "  ]" + "}";
    SymbolTable shared = registerSharedSymtab(importingText);
    assertEquals(IMPORTED_2_MAX_ID, shared.getMaxId());
    SymbolTable importedTable = system.getCatalog().getTable("imported", 2);
    assertSame(shared, importedTable);
    return importedTable;
}
Also used : IonSystem(com.amazon.ion.IonSystem) SymbolTable(com.amazon.ion.SymbolTable)

Aggregations

SymbolTable (com.amazon.ion.SymbolTable)177 Test (org.junit.Test)105 IonValue (com.amazon.ion.IonValue)21 IonDatagram (com.amazon.ion.IonDatagram)18 com.amazon.ion.impl._Private_Utils.copyLocalSymbolTable (com.amazon.ion.impl._Private_Utils.copyLocalSymbolTable)17 SymbolToken (com.amazon.ion.SymbolToken)14 IonStruct (com.amazon.ion.IonStruct)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 IonWriter (com.amazon.ion.IonWriter)12 SimpleCatalog (com.amazon.ion.system.SimpleCatalog)12 IonReader (com.amazon.ion.IonReader)11 IonSystem (com.amazon.ion.IonSystem)10 IOException (java.io.IOException)9 IonType (com.amazon.ion.IonType)8 ArrayList (java.util.ArrayList)7 IonException (com.amazon.ion.IonException)6 com.amazon.ion.impl._Private_IonBinaryWriterBuilder (com.amazon.ion.impl._Private_IonBinaryWriterBuilder)6 com.amazon.ion.impl.bin._Private_IonRawWriter (com.amazon.ion.impl.bin._Private_IonRawWriter)6 IonCatalog (com.amazon.ion.IonCatalog)5 IonList (com.amazon.ion.IonList)5