Search in sources :

Example 41 with SymbolTable

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

the class TextWriterTest method testLstMinimizing.

@Test
public void testLstMinimizing() throws Exception {
    SymbolTable fred1 = Symtabs.register("fred", 1, catalog());
    IonBinaryWriter binaryWriter = system().newBinaryWriter(fred1);
    binaryWriter.writeSymbol("fred_1");
    binaryWriter.writeSymbol("ginger");
    binaryWriter.finish();
    byte[] binaryData = binaryWriter.getBytes();
    options = IonTextWriterBuilder.standard();
    // TODO User reader still transfers local symtabs!
    IonReader binaryReader = system().newReader(binaryData);
    iw = makeWriter();
    iw.writeValues(binaryReader);
    String ionText = outputString();
    assertEquals("$ion_symbol_table::{imports:[{name:\"fred\",version:1,max_id:2}]," + "symbols:[\"ginger\"]} " + "fred_1 ginger", ionText);
    options.setLstMinimizing(LstMinimizing.LOCALS);
    binaryReader = system().newReader(binaryData);
    iw = makeWriter();
    iw.writeValues(binaryReader);
    ionText = outputString();
    assertEquals("$ion_symbol_table::{imports:[{name:\"fred\",version:1,max_id:2}]} " + "fred_1 ginger", ionText);
    options.setLstMinimizing(LstMinimizing.EVERYTHING);
    binaryReader = system().newReader(binaryData);
    iw = makeWriter();
    iw.writeValues(binaryReader);
    ionText = outputString();
    assertEquals("$ion_1_0 fred_1 ginger", ionText);
    options.setInitialIvmHandling(SUPPRESS);
    binaryReader = system().newReader(binaryData);
    iw = makeWriter();
    iw.writeValues(binaryReader);
    ionText = outputString();
    assertEquals("fred_1 ginger", ionText);
}
Also used : IonReader(com.amazon.ion.IonReader) IonBinaryWriter(com.amazon.ion.IonBinaryWriter) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 42 with SymbolTable

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

the class ValueWriterTest method testWriteValuesWithSymtabIntoContainer.

// TODO more thorough testing that you can't output from DOM w/ bad text
@Test
public void testWriteValuesWithSymtabIntoContainer() throws Exception {
    myOutputInList = true;
    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.writeValues(r);
    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, systemMaxId() + Symtabs.FRED_MAX_IDS[1] + 1, 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) Test(org.junit.Test)

Example 43 with SymbolTable

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

the class IonReaderBinaryIncrementalTest method symbolTableSnapshotImplementsBasicMethods.

@Test
public void symbolTableSnapshotImplementsBasicMethods() throws Exception {
    IonReaderBinaryIncremental reader = readerFor("'abc'");
    reader.next();
    SymbolTable symbolTable = reader.getSymbolTable();
    assertNull(symbolTable.getName());
    assertEquals(0, symbolTable.getVersion());
    assertTrue(symbolTable.isLocalTable());
    assertTrue(symbolTable.isReadOnly());
    assertFalse(symbolTable.isSharedTable());
    assertFalse(symbolTable.isSystemTable());
    assertFalse(symbolTable.isSubstitute());
    symbolTable.makeReadOnly();
    assertEquals(10, symbolTable.getMaxId());
    assertEquals("abc", symbolTable.findKnownSymbol(10));
    assertNull(symbolTable.findKnownSymbol(symbolTable.getMaxId() + 1));
    thrown.expect(IllegalArgumentException.class);
    symbolTable.findKnownSymbol(-1);
}
Also used : SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 44 with SymbolTable

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

the class IonReaderBinaryIncrementalTest method nonStringInSymbolsListCreatesNullSlot.

@Test
public void nonStringInSymbolsListCreatesNullSlot() throws Exception {
    IonReaderBinaryIncremental reader = readerFor(new RawWriterFunction() {

        @Override
        public void write(_Private_IonRawWriter writer, ByteArrayOutputStream out) throws IOException {
            SymbolTable systemTable = SharedSymbolTable.getSystemSymbolTable(1);
            writer.addTypeAnnotationSymbol(systemTable.findSymbol("$ion_symbol_table"));
            writer.stepIn(IonType.STRUCT);
            writer.setFieldNameSymbol(systemTable.findSymbol("symbols"));
            writer.stepIn(IonType.LIST);
            writer.writeString(null);
            writer.writeString("abc");
            writer.writeInt(123);
            writer.stepOut();
            writer.stepOut();
            writer.writeSymbolToken(10);
            writer.writeSymbolToken(11);
            writer.writeSymbolToken(12);
        }
    });
    assertEquals(IonType.SYMBOL, reader.next());
    SymbolToken symbolValue = reader.symbolValue();
    assertNull(symbolValue.getText());
    assertEquals(0, symbolValue.getSid());
    assertEquals(IonType.SYMBOL, reader.next());
    symbolValue = reader.symbolValue();
    assertEquals("abc", symbolValue.getText());
    assertEquals(IonType.SYMBOL, reader.next());
    symbolValue = reader.symbolValue();
    assertNull(symbolValue.getText());
    assertEquals(0, symbolValue.getSid());
    reader.close();
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) com.amazon.ion.impl.bin._Private_IonRawWriter(com.amazon.ion.impl.bin._Private_IonRawWriter) SymbolTable(com.amazon.ion.SymbolTable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 45 with SymbolTable

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

the class IonReaderBinaryIncrementalTest method symbolTableWithSymbolsThenImports.

@Test
public void symbolTableWithSymbolsThenImports() throws Exception {
    SimpleCatalog catalog = new SimpleCatalog();
    catalog.putTable(SYSTEM.newSharedSymbolTable("foo", 1, Arrays.asList("abc", "def").iterator()));
    readerBuilder = IonReaderBuilder.standard().withCatalog(catalog);
    IonReaderBinaryIncremental reader = readerFor(new RawWriterFunction() {

        @Override
        public void write(_Private_IonRawWriter writer, ByteArrayOutputStream out) throws IOException {
            SymbolTable systemTable = SharedSymbolTable.getSystemSymbolTable(1);
            writer.addTypeAnnotationSymbol(systemTable.findSymbol("$ion_symbol_table"));
            writer.stepIn(IonType.STRUCT);
            writer.setFieldNameSymbol(systemTable.findSymbol("symbols"));
            writer.stepIn(IonType.LIST);
            writer.writeString("ghi");
            writer.stepOut();
            writer.setFieldNameSymbol(systemTable.findSymbol("imports"));
            writer.stepIn(IonType.LIST);
            writer.stepIn(IonType.STRUCT);
            writer.setFieldNameSymbol(systemTable.findSymbol("name"));
            writer.writeString("foo");
            writer.setFieldNameSymbol(systemTable.findSymbol("version"));
            writer.writeInt(1);
            writer.setFieldNameSymbol(systemTable.findSymbol("max_id"));
            writer.writeInt(2);
            writer.stepOut();
            writer.stepOut();
            writer.stepOut();
            writer.writeSymbolToken(10);
            writer.writeSymbolToken(11);
            writer.writeSymbolToken(12);
        }
    });
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("abc", reader.stringValue());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("def", reader.stringValue());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("ghi", reader.stringValue());
    assertNull(reader.next());
    reader.close();
}
Also used : SimpleCatalog(com.amazon.ion.system.SimpleCatalog) com.amazon.ion.impl.bin._Private_IonRawWriter(com.amazon.ion.impl.bin._Private_IonRawWriter) SymbolTable(com.amazon.ion.SymbolTable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

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