Clean up SnappyIntegrationTest / Fix checksum encoding

This commit is contained in:
Trustin Lee 2013-02-10 00:20:49 +09:00
parent 8bd7ea2f93
commit bd87284829
2 changed files with 19 additions and 12 deletions

View File

@ -114,8 +114,8 @@ public class SnappyFramedEncoder extends ByteToByteEncoder {
private static void calculateAndWriteChecksum(ByteBuf slice, ByteBuf out) { private static void calculateAndWriteChecksum(ByteBuf slice, ByteBuf out) {
int checksum = calculateChecksum(slice); int checksum = calculateChecksum(slice);
out.writeByte(checksum & 0x0ff); out.writeByte(checksum & 0x0ff);
out.writeByte(checksum >> 8 & 0x0ff); out.writeByte(checksum >>> 8 & 0x0ff);
out.writeByte(checksum >> 16 & 0x0ff); out.writeByte(checksum >>> 16 & 0x0ff);
out.writeByte(checksum >> 24 & 0x0ff); out.writeByte(checksum >>> 24);
} }
} }

View File

@ -22,12 +22,10 @@ import io.netty.util.CharsetUtil;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class SnappyIntegrationTest { public class SnappyIntegrationTest {
private final EmbeddedByteChannel channel = new EmbeddedByteChannel(new SnappyFramedEncoder(),
new SnappyFramedDecoder());
@Test @Test
public void testEncoderDecoderIdentity() throws Exception { public void testEncoderDecoderIdentity() throws Exception {
ByteBuf in = Unpooled.copiedBuffer( ByteBuf in = Unpooled.copiedBuffer(
@ -35,9 +33,7 @@ public class SnappyIntegrationTest {
"protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols", "protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols",
CharsetUtil.US_ASCII); CharsetUtil.US_ASCII);
channel.writeOutbound(in.copy()); testEncoderDecoderIdentity(in);
channel.writeInbound(channel.readOutbound());
assertEquals(in, channel.readInbound());
} }
@Test @Test
@ -65,8 +61,19 @@ public class SnappyIntegrationTest {
-1, -1 -1, -1
}); });
channel.writeOutbound(in.copy()); testEncoderDecoderIdentity(in);
channel.writeInbound(channel.readOutbound()); }
assertEquals(in, channel.readInbound());
private static void testEncoderDecoderIdentity(ByteBuf in) {
EmbeddedByteChannel encoder = new EmbeddedByteChannel(new SnappyFramedEncoder());
EmbeddedByteChannel decoder = new EmbeddedByteChannel(new SnappyFramedDecoder());
encoder.writeOutbound(in.copy());
ByteBuf compressed = encoder.readOutbound();
assertThat(compressed, is(not(in)));
decoder.writeInbound(compressed);
assertFalse(compressed.isReadable());
ByteBuf decompressed = (ByteBuf) decoder.readInbound();
assertEquals(in, decompressed);
} }
} }