public void testNormalDecode() throws Exception {
TextLineDecoder decoder = new TextLineDecoder(Charset.forName("UTF-8"),
LineDelimiter.WINDOWS);
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);
Assert.assertEquals(1, session.getDecoderOutputQueue().size());
Assert.assertEquals("ABC", session.getDecoderOutputQueue().poll());
// Test two decode and one output
in.clear();
in.putString("DEF", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("GHI\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(1, session.getDecoderOutputQueue().size());
Assert.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);
Assert.assertEquals(2, session.getDecoderOutputQueue().size());
Assert.assertEquals("JKL", session.getDecoderOutputQueue().poll());
Assert.assertEquals("MNO", session.getDecoderOutputQueue().poll());
// Test aborted delimiter (DIRMINA-506)
in.clear();
in.putString("ABC\r\r\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(1, session.getDecoderOutputQueue().size());
Assert.assertEquals("ABC\r", session.getDecoderOutputQueue().poll());
// Test splitted long delimiter
decoder = new TextLineDecoder(Charset.forName("UTF-8"),
new LineDelimiter("\n\n\n"));
in.clear();
in.putString("PQR\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(1, session.getDecoderOutputQueue().size());
Assert.assertEquals("PQR", session.getDecoderOutputQueue().poll());
// Test splitted long delimiter which produces two output
decoder = new TextLineDecoder(Charset.forName("UTF-8"),
new LineDelimiter("\n\n\n"));
in.clear();
in.putString("PQR\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\nSTU\n\n\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(2,session.getDecoderOutputQueue().size());
Assert.assertEquals("PQR", session.getDecoderOutputQueue().poll());
Assert.assertEquals("STU", session.getDecoderOutputQueue().poll());
// Test splitted long delimiter mixed with partial non-delimiter.
decoder = new TextLineDecoder(Charset.forName("UTF-8"),
new LineDelimiter("\n\n\n"));
in.clear();
in.putString("PQR\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("X\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(0, session.getDecoderOutputQueue().size());
in.clear();
in.putString("\n\nSTU\n\n\n", encoder);
in.flip();
decoder.decode(session, in, out);
Assert.assertEquals(2, session.getDecoderOutputQueue().size());
Assert.assertEquals("PQR\nX", session.getDecoderOutputQueue().poll());
Assert.assertEquals("STU", session.getDecoderOutputQueue().poll());
}