Skip to content

Commit

Permalink
Add unit tests for #3022
Browse files Browse the repository at this point in the history
(cherry picked from commit 3f838bc)
  • Loading branch information
joschi authored and Jochen Schalanda committed Nov 2, 2016
1 parent f262e31 commit 1aceed0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Expand Up @@ -27,28 +27,33 @@
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;

public class TestHelper {

public static byte[] zlibCompress(String what) throws IOException {
public static byte[] zlibCompress(String what, int level) throws IOException {
final ByteArrayInputStream compressMe = new ByteArrayInputStream(what.getBytes(StandardCharsets.UTF_8));
final ByteArrayOutputStream compressedMessage = new ByteArrayOutputStream();

try(DeflaterOutputStream out = new DeflaterOutputStream(compressedMessage)) {
try (DeflaterOutputStream out = new DeflaterOutputStream(compressedMessage, new Deflater(level))) {
ByteStreams.copy(compressMe, out);
}

return compressedMessage.toByteArray();
}

public static byte[] zlibCompress(String what) throws IOException {
return zlibCompress(what, -1);
}

public static byte[] gzipCompress(String what) throws IOException {
// GZIP compress message.
final ByteArrayInputStream compressMe = new ByteArrayInputStream(what.getBytes(StandardCharsets.UTF_8));
final ByteArrayOutputStream compressedMessage = new ByteArrayOutputStream();

try(GZIPOutputStream out = new GZIPOutputStream(compressedMessage)) {
try (GZIPOutputStream out = new GZIPOutputStream(compressedMessage)) {
ByteStreams.copy(compressMe, out);
}

Expand Down
Expand Up @@ -22,12 +22,8 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

/**
* @author lennart.koopmann
*/
public class GELFMessageTest {

public final static String GELF_JSON = "{\"message\":\"foo\",\"host\":\"bar\",\"_lol_utf8\":\"\u00FC\"}";
private static final String GELF_JSON = "{\"version\": \"1.1\", \"message\":\"foobar\",\"host\":\"example.com\",\"_lol_utf8\":\"\u00FC\"}";

@Test
public void testGetGELFTypeDetectsZLIBCompressedMessage() throws Exception {
Expand Down Expand Up @@ -71,8 +67,10 @@ public void testGetGELFTypeDetectsUncompressedMessage() throws Exception {

@Test
public void testGetJSONFromZLIBCompressedMessage() throws Exception {
GELFMessage msg = new GELFMessage(TestHelper.zlibCompress(GELF_JSON));
assertEquals(GELF_JSON, msg.getJSON(1024));
for (int level = -1; level <= 9; level++) {
final GELFMessage msg = new GELFMessage(TestHelper.zlibCompress(GELF_JSON, level));
assertEquals(GELF_JSON, msg.getJSON(1024));
}
}

@Test
Expand Down Expand Up @@ -104,5 +102,4 @@ public void testGelfMessageChunkCreation() throws Exception {
assertEquals(seqCnt, chunk.getSequenceCount());
assertArrayEquals(data, chunk.getData());
}

}

0 comments on commit 1aceed0

Please sign in to comment.