// create an annotation and add to index of both views
AnnotationFS anAnnot = cas.createAnnotation(cas.getAnnotationType(), 0, 5);
cas.getIndexRepository().addFS(anAnnot);
cas2.getIndexRepository().addFS(anAnnot);
FSIndex tIndex = cas.getAnnotationIndex();
FSIndex t2Index = cas2.getAnnotationIndex();
assertTrue(tIndex.size() == 2); // document annot and this one
assertTrue(t2Index.size() == 2); // ditto
// serialize
StringWriter sw = new StringWriter();
XMLSerializer xmlSer = new XMLSerializer(sw, false);
XmiCasSerializer xmiSer = new XmiCasSerializer(cas.getTypeSystem());
xmiSer.serialize(cas, xmlSer.getContentHandler());
String xml = sw.getBuffer().toString();
// deserialize into another CAS (repeat twice to check it still works after reset)
CAS newCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
new FsIndexDescription[0]);
for (int i = 0; i < 2; i++) {
XmiCasDeserializer newDeser = new XmiCasDeserializer(newCas.getTypeSystem());
ContentHandler newDeserHandler = newDeser.getXmiCasHandler(newCas);
SAXParserFactory fact = SAXParserFactory.newInstance();
SAXParser parser = fact.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(newDeserHandler);
xmlReader.parse(new InputSource(new StringReader(xml)));
// check sofas
assertEquals("This is a test", newCas.getDocumentText());
CAS newCas2 = newCas.getView("OtherSofa");
assertEquals("This is only a test", newCas2.getDocumentText());
// check that annotation is still indexed in both views
assertTrue(tIndex.size() == 2); // document annot and this one
assertTrue(t2Index.size() == 2); // ditto
newCas.reset();
}
} catch (Exception e) {
JUnitExtension.handleException(e);