LineDelimiter.AUTO);
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
IoSession session = new DummySession();
TestDecoderOutput out = new TestDecoderOutput();
ByteBuffer in = ByteBuffer.allocate(16);
// Test one decode and one output
in.putString("ABC\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(1, out.getMessageQueue().size());
Assert.assertEquals("ABC", out.getMessageQueue().poll());
// Test two decode and one output
in.clear();
in.putString("DEF", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, out.getMessageQueue().size());
in.clear();
in.putString("GHI\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(1, out.getMessageQueue().size());
Assert.assertEquals("DEFGHI", out.getMessageQueue().poll());
// Test one decode and two output
in.clear();
in.putString("JKL\r\nMNO\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(2, out.getMessageQueue().size());
Assert.assertEquals("JKL", out.getMessageQueue().poll());
Assert.assertEquals("MNO", out.getMessageQueue().poll());
// Test multiple '\n's
in.clear();
in.putString("\n\n\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(3, out.getMessageQueue().size());
Assert.assertEquals("", out.getMessageQueue().poll());
Assert.assertEquals("", out.getMessageQueue().poll());
Assert.assertEquals("", out.getMessageQueue().poll());
// Test splitted long delimiter (\r\r\n)
in.clear();
in.putString("PQR\r", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, out.getMessageQueue().size());
in.clear();
in.putString("\r", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, out.getMessageQueue().size());
in.clear();
in.putString("\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(1, out.getMessageQueue().size());
Assert.assertEquals("PQR", out.getMessageQueue().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);
Assert.assertEquals(0, out.getMessageQueue().size());
in.clear();
in.putString("\r", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, out.getMessageQueue().size());
in.clear();
in.putString("\nSTU\r\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(2, out.getMessageQueue().size());
Assert.assertEquals("PQR", out.getMessageQueue().poll());
Assert.assertEquals("STU", out.getMessageQueue().poll());
// Test splitted long delimiter mixed with partial non-delimiter.
in.clear();
in.putString("PQR\r", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, out.getMessageQueue().size());
in.clear();
in.putString("X\r", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, out.getMessageQueue().size());
in.clear();
in.putString("\r\nSTU\r\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(2, out.getMessageQueue().size());
Assert.assertEquals("PQR\rX", out.getMessageQueue().poll());
Assert.assertEquals("STU", out.getMessageQueue().poll());
}