From 516351b82cd3d4f5c1313ffc99cc13194973c02b Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sat, 15 Sep 2012 19:14:59 +0200 Subject: [PATCH] Add a basic unit test that I wrote while porting niosmtp to netty 4 --- .../codec/DelimiterBasedFrameDecoderTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 codec/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java diff --git a/codec/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java new file mode 100644 index 0000000000..61f216614c --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec; + +import static org.junit.Assert.*; + +import java.nio.charset.Charset; + +import org.junit.Test; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.embedded.EmbeddedByteChannel; + +public class DelimiterBasedFrameDecoderTest { + + @Test + public void testMultipleLinesStrippedDelimiters() { + EmbeddedByteChannel ch = new EmbeddedByteChannel(new DelimiterBasedFrameDecoder(8192, true, Delimiters.lineDelimiter())); + ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset())); + assertEquals("TestLine", ((ByteBuf)ch.readInbound()).toString(Charset.defaultCharset())); + assertEquals("g", ((ByteBuf)ch.readInbound()).toString(Charset.defaultCharset())); + assertNull(ch.readInbound()); + } + + @Test + public void testIncompleteLinesStrippedDelimiters() { + EmbeddedByteChannel ch = new EmbeddedByteChannel(new DelimiterBasedFrameDecoder(8192, true, Delimiters.lineDelimiter())); + ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset())); + assertNull(ch.readInbound()); + ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset())); + assertEquals("TestLine", ((ByteBuf)ch.readInbound()).toString(Charset.defaultCharset())); + assertEquals("g", ((ByteBuf)ch.readInbound()).toString(Charset.defaultCharset())); + assertNull(ch.readInbound()); + } + + @Test + public void testMultipleLines() { + EmbeddedByteChannel ch = new EmbeddedByteChannel(new DelimiterBasedFrameDecoder(8192, false, Delimiters.lineDelimiter())); + ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset())); + assertEquals("TestLine\r\n", ((ByteBuf)ch.readInbound()).toString(Charset.defaultCharset())); + assertEquals("g\r\n", ((ByteBuf)ch.readInbound()).toString(Charset.defaultCharset())); + assertNull(ch.readInbound()); + } + + @Test + public void testIncompleteLines() { + EmbeddedByteChannel ch = new EmbeddedByteChannel(new DelimiterBasedFrameDecoder(8192, false, Delimiters.lineDelimiter())); + ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset())); + assertNull(ch.readInbound()); + ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset())); + assertEquals("TestLine\r\n", ((ByteBuf)ch.readInbound()).toString(Charset.defaultCharset())); + assertEquals("g\r\n", ((ByteBuf)ch.readInbound()).toString(Charset.defaultCharset())); + assertNull(ch.readInbound()); + } +}