for (Charset charset : charsets) {
for (int i = 0; i < msgs.length; i++) {
String msg = msgs[i];
String body = bodies[i];
parsedBuf.buffer = IoBuffer.wrap(msg.getBytes(charset));
Event evt = handler.parseEvent(parsedBuf, charset.newDecoder());
String result = new String(evt.getBody(), charset);
// this doesn't work with non-UTF-8 chars... not sure why...
Assert.assertEquals(charset + " parse error: " + msg, body, result);
Assert.assertNull(
evt.getHeaders().get(SyslogUtils.EVENT_STATUS));
}
}
// Construct an invalid UTF-8 sequence.
// The parser should still generate an Event, but mark it as INVALID.
byte[] badUtf8Seq = enMsg.getBytes(Charsets.ISO_8859_1);
int badMsgLen = badUtf8Seq.length;
badUtf8Seq[badMsgLen - 2] = (byte)0xFE; // valid ISO-8859-1, invalid UTF-8
badUtf8Seq[badMsgLen - 1] = (byte)0xFF; // valid ISO-8859-1, invalid UTF-8
parsedBuf.buffer = IoBuffer.wrap(badUtf8Seq);
Event evt = handler.parseEvent(parsedBuf, Charsets.UTF_8.newDecoder());
Assert.assertEquals("event body: " +
new String(evt.getBody(), Charsets.ISO_8859_1) +
" and my default charset = " + Charset.defaultCharset() +
" with event = " + evt,
SyslogUtils.SyslogStatus.INVALID.getSyslogStatus(),
evt.getHeaders().get(SyslogUtils.EVENT_STATUS));
Assert.assertArrayEquals("Raw message data should be kept in body of event",
badUtf8Seq, evt.getBody());
}