use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class OutputStreamWriterTestCase method testFlushing.
private void testFlushing() throws IOException {
IonDatagram expected = system().newDatagram();
iw.getSymbolTable().makeReadOnly();
iw.writeSymbol("fred_1");
expected.add().newSymbol("fred_1");
iw.flush();
checkFlushed(true);
myOutputStreamWrapper.flushed = false;
byte[] bytes = myOutputStream.toByteArray();
assertEquals(expected, loader().load(bytes));
// Try flushing when there's just a pending annotation.
iw.addTypeAnnotation("fred_1");
iw.flush();
myOutputStreamWrapper.flushed = false;
bytes = myOutputStream.toByteArray();
assertEquals(expected, loader().load(bytes));
iw.writeSymbol("fred_2");
expected.add().newSymbol("fred_2").addTypeAnnotation("fred_1");
iw.flush();
checkFlushed(true);
myOutputStreamWrapper.flushed = false;
bytes = myOutputStream.toByteArray();
assertEquals(expected, loader().load(bytes));
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class SymbolTableTest method testInitialSystemSymtab.
@Test
public void testInitialSystemSymtab() {
final SymbolTable systemTable = system().getSystemSymbolTable(ION_1_0);
assertEquals(ION, systemTable.getName());
String text = "0";
IonValue v = oneValue(text);
SymbolTable st = v.getSymbolTable();
assertSame(systemTable, st.getSystemSymbolTable());
IonDatagram dg = loader().load(text);
IonSymbol sysId = (IonSymbol) dg.systemGet(0);
checkSymbol(ION_1_0, ION_1_0_SID, sysId);
assertSame(systemTable, sysId.getSymbolTable());
v = dg.get(0);
st = v.getSymbolTable();
assertSame(systemTable, st.getSystemSymbolTable());
}
use of com.amazon.ion.IonDatagram 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());
}
use of com.amazon.ion.IonDatagram 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());
}
use of com.amazon.ion.IonDatagram 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);
}
Aggregations