Skip to content

Commit

Permalink
+ update chapter 9
Browse files Browse the repository at this point in the history
  • Loading branch information
He-Pin committed Apr 19, 2017
1 parent 77847ef commit 9bc5324
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 15 deletions.
4 changes: 2 additions & 2 deletions chapter9/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</parent>

<artifactId>chapter9</artifactId>
<name>Chapter 9. Unit Testing</name>

<name>Chapter 9. Unit testing</name>
<description>Unit testing</description>
</project>
5 changes: 3 additions & 2 deletions chapter9/src/main/java/nia/chapter9/AbsIntegerEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import java.util.List;

/**
* Listing 9.3 of <i>Netty in Action</i>
* Listing 9.3 AbsIntegerEncoder
*
* @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
*/
public class AbsIntegerEncoder
extends MessageToMessageEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> out)
protected void encode(ChannelHandlerContext channelHandlerContext,
ByteBuf in, List<Object> out)
throws Exception {
while (in.readableBytes() >= 4) {
int value = Math.abs(in.readInt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

/**
* Listing 9.1 of <i>Netty in Action</i>
* Listing 9.1 FixedLengthFrameDecoder
*
* @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
*/
Expand All @@ -24,7 +24,7 @@ public FixedLengthFrameDecoder(int frameLength) {

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception {
List<Object> out) throws Exception {
while (in.readableBytes() >= frameLength) {
ByteBuf buf = in.readBytes(frameLength);
out.add(buf);
Expand Down
9 changes: 4 additions & 5 deletions chapter9/src/main/java/nia/chapter9/FrameChunkDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@
import java.util.List;

/**
* Listing 9.5 of <i>Netty in Action</i>
* Listing 9.5 FrameChunkDecoder
*
* @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
*/
public class FrameChunkDecoder
extends ByteToMessageDecoder {

public class FrameChunkDecoder extends ByteToMessageDecoder {
private final int maxFrameSize;

public FrameChunkDecoder(int maxFrameSize) {
this.maxFrameSize = maxFrameSize;
}

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out)
throws Exception {
int readableBytes = in.readableBytes();
if (readableBytes > maxFrameSize) {
Expand Down
10 changes: 10 additions & 0 deletions chapter9/src/main/java/nia/chapter9/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Created by kerr.
*
* Listing 9.1 FixedLengthFrameDecoder {@link nia.chapter9.FixedLengthFrameDecoder}
*
* Listing 9.3 AbsIntegerEncoder {@link nia.chapter9.AbsIntegerEncoder}
*
* Listing 9.5 FrameChunkDecoder {@link nia.chapter9.FrameChunkDecoder}
*/
package nia.chapter9;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static org.junit.Assert.*;

/**
* Listing 9.4 of <i>Netty in Action</i>
* Listing 9.4 Testing the AbsIntegerEncoder
*
* @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
*/
Expand All @@ -20,10 +20,12 @@ public void testEncoded() {
for (int i = 1; i < 10; i++) {
buf.writeInt(i * -1);
}

EmbeddedChannel channel = new EmbeddedChannel(
new AbsIntegerEncoder());
assertTrue(channel.writeOutbound(buf));
assertTrue(channel.finish());

// read bytes
for (int i = 1; i < 10; i++) {
assertEquals(i, channel.readOutbound());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static org.junit.Assert.*;

/**
* Listing 9.2 of <i>Netty in Action</i>
* Listing 9.2 Testing the FixedLengthFrameDecoder
*
* @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
*/
Expand All @@ -26,17 +26,20 @@ public void testFramesDecoded() {
// write bytes
assertTrue(channel.writeInbound(input.retain()));
assertTrue(channel.finish());
// read messages

// read messages
ByteBuf read = (ByteBuf) channel.readInbound();
assertEquals(buf.readSlice(3), read);
read.release();

read = (ByteBuf) channel.readInbound();
assertEquals(buf.readSlice(3), read);
read.release();

read = (ByteBuf) channel.readInbound();
assertEquals(buf.readSlice(3), read);
read.release();

assertNull(channel.readInbound());
buf.release();
}
Expand All @@ -48,20 +51,25 @@ public void testFramesDecoded2() {
buf.writeByte(i);
}
ByteBuf input = buf.duplicate();

EmbeddedChannel channel = new EmbeddedChannel(
new FixedLengthFrameDecoder(3));
assertFalse(channel.writeInbound(input.readBytes(2)));
assertTrue(channel.writeInbound(input.readBytes(7)));

assertTrue(channel.finish());
ByteBuf read = (ByteBuf) channel.readInbound();
assertEquals(buf.readSlice(3), read);
read.release();

read = (ByteBuf) channel.readInbound();
assertEquals(buf.readSlice(3), read);
read.release();

read = (ByteBuf) channel.readInbound();
assertEquals(buf.readSlice(3), read);
read.release();

assertNull(channel.readInbound());
buf.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.junit.Assert.*;

/**
* Listing 9.6 of <i>Netty in Action</i>
* Listing 9.6 Testing FrameChunkDecoder
*
* @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
*/
Expand All @@ -23,8 +23,10 @@ public void testFramesDecoded() {
buf.writeByte(i);
}
ByteBuf input = buf.duplicate();

EmbeddedChannel channel = new EmbeddedChannel(
new FrameChunkDecoder(3));

assertTrue(channel.writeInbound(input.readBytes(2)));
try {
channel.writeInbound(input.readBytes(4));
Expand All @@ -34,10 +36,12 @@ public void testFramesDecoded() {
}
assertTrue(channel.writeInbound(input.readBytes(3)));
assertTrue(channel.finish());

// Read frames
ByteBuf read = (ByteBuf) channel.readInbound();
assertEquals(buf.readSlice(2), read);
read.release();

read = (ByteBuf) channel.readInbound();
assertEquals(buf.skipBytes(4).readSlice(3), read);
read.release();
Expand Down
10 changes: 10 additions & 0 deletions chapter9/src/test/java/nia/test/chapter9/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Created by kerr.
*
* Listing 9.2 Testing the FixedLengthFrameDecoder {@link nia.test.chapter9.FixedLengthFrameDecoderTest}
*
* Listing 9.4 Testing the AbsIntegerEncoder {@link nia.test.chapter9.AbsIntegerEncoderTest}
*
* Listing 9.6 Testing FrameChunkDecoder {@link nia.test.chapter9.FrameChunkDecoderTest}
*/
package nia.test.chapter9;

0 comments on commit 9bc5324

Please sign in to comment.