public void testInvalidCharsInDocumentText() throws Exception {
CAS cas = CasCreationUtils.createCas(this.typeSystemDesc, null, null);
char badChar = 0x1A;
cas.setDocumentText("Text with bad char: " + badChar);
OutputStream out = new FileOutputStream(this.outputFile);
XMLSerializer xmlSerializer = new XMLSerializer(out);
XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(cas.getTypeSystem());
boolean caughtException = false;
try {
xmiCasSerializer.serialize(cas, xmlSerializer.getContentHandler());
} catch (SAXParseException e) {
caughtException = true;
}
out.close();
assertTrue("XMI serialization of document text with bad XML 1.0 char should throw exception",
caughtException);
//but when XML 1.1 output is being generated, don't fail on control chracters which are valid in 1.1.
out = new FileOutputStream(this.outputFile);
try {
XMLSerializer xml11Serializer = new XMLSerializer(out);
xml11Serializer.setOutputProperty(OutputKeys.VERSION,"1.1");
xmiCasSerializer.serialize(cas, xml11Serializer.getContentHandler());
}
finally {
out.close();
}
}