Refactor Bzip2 tests

Motivation:

Complicated code of Bzip2 tests with some unnecessary actions.

Modifications:

- Reduce size of BYTES_LARGE array of random test data for Bzip2  tests.
- Removed unnecessary creations of EmbeddedChannel instances in Bzip2 tests.
- Simplified tests in Bzip2DecoderTest which expect exception.
- Removed unnecessary testStreamInitialization() from Bzip2EncoderTest.

Result:

Reduced time to test the 'codec' package by 7 percent, simplified code of Bzip2 tests.
This commit is contained in:
Idel Pivnitskiy 2014-07-23 18:30:41 +04:00 committed by Norman Maurer
parent 42e1cc23fe
commit 1a8b29cfb7
2 changed files with 37 additions and 64 deletions

View File

@ -27,16 +27,24 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import static io.netty.handler.codec.compression.Bzip2Constants.*; import static io.netty.handler.codec.compression.Bzip2Constants.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class Bzip2DecoderTest { public class Bzip2DecoderTest {
private static final byte[] DATA = { 0x42, 0x5A, 0x68, 0x37, 0x31, 0x41, 0x59, 0x26, 0x53,
0x59, 0x77, 0x7B, (byte) 0xCA, (byte) 0xC0, 0x00, 0x00,
0x00, 0x05, (byte) 0x80, 0x00, 0x01, 0x02, 0x00, 0x04,
0x20, 0x20, 0x00, 0x30, (byte) 0xCD, 0x34, 0x19, (byte) 0xA6,
(byte) 0x89, (byte) 0x99, (byte) 0xC5, (byte) 0xDC, (byte) 0x91,
0x4E, 0x14, 0x24, 0x1D, (byte) 0xDE, (byte) 0xF2, (byte) 0xB0, 0x00 };
private static final ThreadLocalRandom rand; private static final ThreadLocalRandom rand;
private static final byte[] BYTES_SMALL = new byte[256]; private static final byte[] BYTES_SMALL = new byte[256];
private static final byte[] BYTES_LARGE = new byte[MAX_BLOCK_SIZE * BASE_BLOCK_SIZE * 2]; private static final byte[] BYTES_LARGE = new byte[MAX_BLOCK_SIZE * BASE_BLOCK_SIZE + 256];
static { static {
rand = ThreadLocalRandom.current(); rand = ThreadLocalRandom.current();
@ -112,12 +120,8 @@ public class Bzip2DecoderTest {
expected.expect(DecompressionException.class); expected.expect(DecompressionException.class);
expected.expectMessage("stream CRC error"); expected.expectMessage("stream CRC error");
final byte[] data = { 0x42, 0x5A, 0x68, 0x37, 0x31, 0x41, 0x59, 0x26, 0x53, final byte[] data = Arrays.copyOf(DATA, DATA.length);
0x59, 0x77, 0x7B, (byte) 0xCA, (byte) 0xC0, 0x00, 0x00, data[41] = (byte) 0xDD;
0x00, 0x05, (byte) 0x80, 0x00, 0x01, 0x02, 0x00, 0x04,
0x20, 0x20, 0x00, 0x30, (byte) 0xCD, 0x34, 0x19, (byte) 0xA6,
(byte) 0x89, (byte) 0x99, (byte) 0xC5, (byte) 0xDC, (byte) 0x91,
0x4E, 0x14, 0x24, 0x1D, (byte) 0xDD, (byte) 0xF2, (byte) 0xB0, 0x00 };
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
try { try {
@ -139,12 +143,8 @@ public class Bzip2DecoderTest {
expected.expect(DecompressionException.class); expected.expect(DecompressionException.class);
expected.expectMessage("incorrect huffman groups number"); expected.expectMessage("incorrect huffman groups number");
final byte[] data = { 0x42, 0x5A, 0x68, 0x37, 0x31, 0x41, 0x59, 0x26, 0x53, final byte[] data = Arrays.copyOf(DATA, DATA.length);
0x59, 0x77, 0x7B, (byte) 0xCA, (byte) 0xC0, 0x00, 0x00, data[25] = 0x70;
0x00, 0x05, (byte) 0x80, 0x00, 0x01, 0x02, 0x00, 0x04,
0x20, 0x70, 0x00, 0x30, (byte) 0xCD, 0x34, 0x19, (byte) 0xA6,
(byte) 0x89, (byte) 0x99, (byte) 0xC5, (byte) 0xDC, (byte) 0x91,
0x4E, 0x14, 0x24, 0x1D, (byte) 0xDE, (byte) 0xF2, (byte) 0xB0, 0x00 };
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); channel.writeInbound(in);
@ -155,12 +155,8 @@ public class Bzip2DecoderTest {
expected.expect(DecompressionException.class); expected.expect(DecompressionException.class);
expected.expectMessage("incorrect selectors number"); expected.expectMessage("incorrect selectors number");
final byte[] data = { 0x42, 0x5A, 0x68, 0x37, 0x31, 0x41, 0x59, 0x26, 0x53, final byte[] data = Arrays.copyOf(DATA, DATA.length);
0x59, 0x77, 0x7B, (byte) 0xCA, (byte) 0xC0, 0x00, 0x00, data[25] = 0x2F;
0x00, 0x05, (byte) 0x80, 0x00, 0x01, 0x02, 0x00, 0x04,
0x20, 0x2F, (byte) 0xFF, 0x30, (byte) 0xCD, 0x34, 0x19, (byte) 0xA6,
(byte) 0x89, (byte) 0x99, (byte) 0xC5, (byte) 0xDC, (byte) 0x91,
0x4E, 0x14, 0x24, 0x1D, (byte) 0xDE, (byte) 0xF2, (byte) 0xB0, 0x00 };
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); channel.writeInbound(in);
@ -171,12 +167,8 @@ public class Bzip2DecoderTest {
expected.expect(DecompressionException.class); expected.expect(DecompressionException.class);
expected.expectMessage("block CRC error"); expected.expectMessage("block CRC error");
final byte[] data = { 0x42, 0x5A, 0x68, 0x37, 0x31, 0x41, 0x59, 0x26, 0x53, final byte[] data = Arrays.copyOf(DATA, DATA.length);
0x59, 0x77, 0x77, (byte) 0xCA, (byte) 0xC0, 0x00, 0x00, data[11] = 0x77;
0x00, 0x05, (byte) 0x80, 0x00, 0x01, 0x02, 0x00, 0x04,
0x20, 0x20, 0x00, 0x30, (byte) 0xCD, 0x34, 0x19, (byte) 0xA6,
(byte) 0x89, (byte) 0x99, (byte) 0xC5, (byte) 0xDC, (byte) 0x91,
0x4E, 0x14, 0x24, 0x1D, (byte) 0xDE, (byte) 0xF2, (byte) 0xB0, 0x00 };
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); channel.writeInbound(in);
@ -187,20 +179,14 @@ public class Bzip2DecoderTest {
expected.expect(DecompressionException.class); expected.expect(DecompressionException.class);
expected.expectMessage("start pointer invalid"); expected.expectMessage("start pointer invalid");
final byte[] data = { 0x42, 0x5A, 0x68, 0x37, 0x31, 0x41, 0x59, 0x26, 0x53, final byte[] data = Arrays.copyOf(DATA, DATA.length);
0x59, 0x77, 0x7B, (byte) 0xCA, (byte) 0xC0, (byte) 0xFF, 0x00, data[14] = (byte) 0xFF;
0x00, 0x05, (byte) 0x80, 0x00, 0x01, 0x02, 0x00, 0x04,
0x20, 0x20, 0x00, 0x30, (byte) 0xCD, 0x34, 0x19, (byte) 0xA6,
(byte) 0x89, (byte) 0x99, (byte) 0xC5, (byte) 0xDC, (byte) 0x91,
0x4E, 0x14, 0x24, 0x1D, (byte) 0xDE, (byte) 0xF2, (byte) 0xB0, 0x00 };
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeInbound(in); channel.writeInbound(in);
} }
private static void testDecompression(final byte[] data) throws Exception { private static void testDecompression(final EmbeddedChannel channel, final byte[] data) throws Exception {
final EmbeddedChannel channel = new EmbeddedChannel(new Bzip2Decoder());
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
BZip2CompressorOutputStream bZip2Os = new BZip2CompressorOutputStream(os, randomBlockSize()); BZip2CompressorOutputStream bZip2Os = new BZip2CompressorOutputStream(os, randomBlockSize());
bZip2Os.write(data); bZip2Os.write(data);
@ -220,12 +206,12 @@ public class Bzip2DecoderTest {
@Test @Test
public void testDecompressionOfSmallChunkOfData() throws Exception { public void testDecompressionOfSmallChunkOfData() throws Exception {
testDecompression(BYTES_SMALL); testDecompression(channel, BYTES_SMALL);
} }
@Test @Test
public void testDecompressionOfLargeChunkOfData() throws Exception { public void testDecompressionOfLargeChunkOfData() throws Exception {
testDecompression(BYTES_LARGE); testDecompression(channel, BYTES_LARGE);
} }
@Test @Test

View File

@ -21,6 +21,7 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.ThreadLocalRandom; import io.netty.util.internal.ThreadLocalRandom;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -33,7 +34,7 @@ public class Bzip2EncoderTest {
private static final ThreadLocalRandom rand; private static final ThreadLocalRandom rand;
private static final byte[] BYTES_SMALL = new byte[256]; private static final byte[] BYTES_SMALL = new byte[256];
private static final byte[] BYTES_LARGE = new byte[MAX_BLOCK_SIZE * BASE_BLOCK_SIZE * 2]; private static final byte[] BYTES_LARGE = new byte[MAX_BLOCK_SIZE * BASE_BLOCK_SIZE + 256];
static { static {
rand = ThreadLocalRandom.current(); rand = ThreadLocalRandom.current();
@ -41,28 +42,14 @@ public class Bzip2EncoderTest {
rand.nextBytes(BYTES_LARGE); rand.nextBytes(BYTES_LARGE);
} }
@Test private EmbeddedChannel channel;
public void testStreamInitialization() throws Exception {
final EmbeddedChannel channel = new EmbeddedChannel(new Bzip2Encoder());
ByteBuf in = Unpooled.wrappedBuffer("test".getBytes()); @Before
channel.writeOutbound(in); public void initChannel() {
channel = new EmbeddedChannel(new Bzip2Encoder(randomBlockSize()));
ByteBuf out = channel.readOutbound();
assertEquals(MAGIC_NUMBER, out.readMedium());
assertEquals(9 + '0', out.readByte());
out.release();
assertTrue(channel.finish());
out = channel.readOutbound();
out.release();
assertNull(channel.readOutbound());
} }
private static void testCompression(final byte[] data) throws Exception { private static void testCompression(final EmbeddedChannel channel, final byte[] data) throws Exception {
final EmbeddedChannel channel = new EmbeddedChannel(new Bzip2Encoder(randomBlockSize()));
ByteBuf in = Unpooled.wrappedBuffer(data); ByteBuf in = Unpooled.wrappedBuffer(data);
channel.writeOutbound(in); channel.writeOutbound(in);
channel.finish(); channel.finish();
@ -74,32 +61,32 @@ public class Bzip2EncoderTest {
@Test @Test
public void testCompressionOfSmallChunkOfData() throws Exception { public void testCompressionOfSmallChunkOfData() throws Exception {
testCompression(BYTES_SMALL); testCompression(channel, BYTES_SMALL);
} }
@Test @Test
public void testCompressionOfLargeChunkOfData() throws Exception { public void testCompressionOfLargeChunkOfData() throws Exception {
testCompression(BYTES_LARGE); testCompression(channel, BYTES_LARGE);
} }
@Test @Test
public void testCompressionOfBatchedFlowOfData() throws Exception { public void testCompressionOfBatchedFlowOfData() throws Exception {
final EmbeddedChannel channel = new EmbeddedChannel(new Bzip2Encoder(randomBlockSize())); final byte[] data = BYTES_LARGE;
int written = 0, length = rand.nextInt(100); int written = 0, length = rand.nextInt(100);
while (written + length < BYTES_LARGE.length) { while (written + length < data.length) {
ByteBuf in = Unpooled.wrappedBuffer(BYTES_LARGE, written, length); ByteBuf in = Unpooled.wrappedBuffer(data, written, length);
channel.writeOutbound(in); channel.writeOutbound(in);
written += length; written += length;
length = rand.nextInt(100); length = rand.nextInt(100);
} }
ByteBuf in = Unpooled.wrappedBuffer(BYTES_LARGE, written, BYTES_LARGE.length - written); ByteBuf in = Unpooled.wrappedBuffer(data, written, data.length - written);
channel.writeOutbound(in); channel.writeOutbound(in);
channel.finish(); channel.finish();
byte[] uncompressed = uncompress(channel, BYTES_LARGE.length); byte[] uncompressed = uncompress(channel, data.length);
assertArrayEquals(BYTES_LARGE, uncompressed); assertArrayEquals(data, uncompressed);
} }
private static byte[] uncompress(EmbeddedChannel channel, int length) throws Exception { private static byte[] uncompress(EmbeddedChannel channel, int length) throws Exception {