GzipCompression implements Compression { @Override public byte[] compress(byte[] input) { Validate.notNull(input, "Input for compression is null"); ByteArrayOutputStream byteArrayOutputStream = null; GZIPOutputStream gzipOutputStream = null; try { byteArrayOutputStream = new ByteArrayOutputStream(input.length); gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); gzipOutputStream.write(input); gzipOutputStream.close(); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { throw new SamzaException("Failed to compress.", e); } finally { try { if (gzipOutputStream != null) { gzipOutputStream.close(); } if (byteArrayOutputStream != null) { byteArrayOutputStream.close(); } } catch (Exception e) { throw new SamzaException("Failed to close output streams during compression.", e); } } }  @Override byte[] compress(byte[] input); @Override String getFileExtension();  }
@Test public void testCompression() throws IOException { byte[] input = "This is fake input data".getBytes(); byte[] result = compress(input); Assert.assertArrayEquals(gzipCompression.compress(input), result); }
@Test public void testCompressionEmpty() throws IOException { byte[] input = "".getBytes(); byte[] result = compress(input); Assert.assertArrayEquals(gzipCompression.compress(input), result); }
@Test(expected = RuntimeException.class) public void testCompressionNull() { byte[] input = null; gzipCompression.compress(input); }
@Test public void testCompressionZero() throws IOException { byte[] input = new byte[100]; byte[] result = compress(input); Assert.assertArrayEquals(gzipCompression.compress(input), result); }