new TextLineDecoder(
Charset.forName( "UTF-8" ), LineDelimiter.AUTO );
CharsetEncoder encoder = Charset.forName( "UTF-8" ).newEncoder();
IoSession session = new DummySession();
SimpleProtocolDecoderOutput out = new SimpleProtocolDecoderOutput();
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().pop() );
// 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().pop() );
// 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().pop() );
Assert.assertEquals( "MNO", out.getMessageQueue().pop() );
// 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().pop() );
Assert.assertEquals( "", out.getMessageQueue().pop() );
Assert.assertEquals( "", out.getMessageQueue().pop() );
// 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().pop() );
}