TransactionReader { public Collection<TransactionOperation> parse(InputStream in) throws SAXException, IOException { SimpleSAXParser saxParser = new SimpleSAXParser(); saxParser.setPreserveWhitespace(true); TransactionSAXParser handler = new TransactionSAXParser(); saxParser.setListener(handler); saxParser.parse(in); return handler.getTxn(); }  Collection<TransactionOperation> parse(InputStream in); Collection<TransactionOperation> parse(Reader in);  }
@Test public void testRoundtrip() throws Exception { AddStatementOperation operation = new AddStatementOperation(bob, knows, alice, context1, context2); List<TransactionOperation> txn = new ArrayList<>(); txn.add(operation); ByteArrayOutputStream out = new ByteArrayOutputStream(4096); TransactionWriter w = new TransactionWriter(); w.serialize(txn, out); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); TransactionReader r = new TransactionReader(); Collection<TransactionOperation> parsedTxn = r.parse(in); assertNotNull(parsedTxn); for (TransactionOperation op : parsedTxn) { assertTrue(op instanceof AddStatementOperation); AddStatementOperation addOp = (AddStatementOperation) op; Resource[] contexts = addOp.getContexts(); assertEquals(2, contexts.length); assertTrue(contexts[0].equals(context1) || contexts[1].equals(context1)); assertTrue(contexts[0].equals(context2) || contexts[1].equals(context2)); } }
@Test public void testControlCharHandling() throws Exception { AddStatementOperation operation = new AddStatementOperation(bob, knows, controlCharText); List<TransactionOperation> txn = new ArrayList<>(); txn.add(operation); ByteArrayOutputStream out = new ByteArrayOutputStream(4096); TransactionWriter w = new TransactionWriter(); w.serialize(txn, out); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); TransactionReader r = new TransactionReader(); Collection<TransactionOperation> parsedTxn = r.parse(in); assertNotNull(parsedTxn); for (TransactionOperation op : parsedTxn) { assertTrue(op instanceof AddStatementOperation); AddStatementOperation addOp = (AddStatementOperation) op; assertTrue(addOp.getObject().equals(controlCharText)); } }