From 1aceed092b6215e89bfbe995f7bd283e2de36668 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Wed, 28 Sep 2016 09:56:24 +0200 Subject: [PATCH] Add unit tests for #3022 (cherry picked from commit 3f838bcfa94153c304d76c4b434b831af57be5b0) --- .../test/java/org/graylog2/inputs/TestHelper.java | 11 ++++++++--- .../inputs/codecs/gelf/GELFMessageTest.java | 13 +++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/graylog2-server/src/test/java/org/graylog2/inputs/TestHelper.java b/graylog2-server/src/test/java/org/graylog2/inputs/TestHelper.java index 3974b55e057e..5b3898a03a8b 100644 --- a/graylog2-server/src/test/java/org/graylog2/inputs/TestHelper.java +++ b/graylog2-server/src/test/java/org/graylog2/inputs/TestHelper.java @@ -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); } diff --git a/graylog2-server/src/test/java/org/graylog2/inputs/codecs/gelf/GELFMessageTest.java b/graylog2-server/src/test/java/org/graylog2/inputs/codecs/gelf/GELFMessageTest.java index dd39c0b874d8..bb83115c5962 100644 --- a/graylog2-server/src/test/java/org/graylog2/inputs/codecs/gelf/GELFMessageTest.java +++ b/graylog2-server/src/test/java/org/graylog2/inputs/codecs/gelf/GELFMessageTest.java @@ -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 { @@ -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 @@ -104,5 +102,4 @@ public void testGelfMessageChunkCreation() throws Exception { assertEquals(seqCnt, chunk.getSequenceCount()); assertArrayEquals(data, chunk.getData()); } - }