public void testAutoDecode() throws Exception {
TextLineDecoder decoder = new TextLineDecoder(Charset.forName("UTF-8"), LineDelimiter.AUTO);
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
ProtocolCodecSession session = new ProtocolCodecSession();
ProtocolDecoderOutput out = session.getDecoderOutput();
IoBuffer in = IoBuffer.allocate(16);
// Test one decode and one output
in.putString("ABC\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(1, session.getDecoderOutputQueue().size());
assertEquals("ABC", session.getDecoderOutputQueue().poll());
// Test two decode and one output
in.clear();
in.putString("DEF", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("GHI\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(1, session.getDecoderOutputQueue().size());
assertEquals("DEFGHI", session.getDecoderOutputQueue().poll());
// Test one decode and two output
in.clear();
in.putString("JKL\r\nMNO\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(2, session.getDecoderOutputQueue().size());
assertEquals("JKL", session.getDecoderOutputQueue().poll());
assertEquals("MNO", session.getDecoderOutputQueue().poll());
// Test multiple '\n's
in.clear();
in.putString("\n\n\n", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(3, session.getDecoderOutputQueue().size());
assertEquals("", session.getDecoderOutputQueue().poll());
assertEquals("", session.getDecoderOutputQueue().poll());
assertEquals("", session.getDecoderOutputQueue().poll());
// Test splitted long delimiter (\r\r\n)
in.clear();
in.putString("PQR\r", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\r", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\n", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(1, session.getDecoderOutputQueue().size());
assertEquals("PQR", session.getDecoderOutputQueue().poll());
// Test splitted long delimiter (\r\r\n) which produces two output
in.clear();
in.putString("PQR\r", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\r", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\nSTU\r\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(2, session.getDecoderOutputQueue().size());
assertEquals("PQR", session.getDecoderOutputQueue().poll());
assertEquals("STU", session.getDecoderOutputQueue().poll());
// Test splitted long delimiter mixed with partial non-delimiter.
in.clear();
in.putString("PQR\r", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("X\r", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\r\nSTU\r\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(2, session.getDecoderOutputQueue().size());
assertEquals("PQR\rX", session.getDecoderOutputQueue().poll());
assertEquals("STU", session.getDecoderOutputQueue().poll());
in.clear();
String s = new String(new byte[] { 0, 77, 105, 110, 97 });
in.putString(s, encoder);
in.flip();
decoder.decode(session, in, out);
assertEquals(1, session.getDecoderOutputQueue().size());
assertEquals(s, session.getDecoderOutputQueue().poll());
}