Skip to content

Commit

Permalink
fix test cases for pull request #92:
Browse files Browse the repository at this point in the history
Parse key=values even with whitespace around the equals sign. Prevent ArrayOutOfBounds exception.

add TestAppender class to check for logged statements
  • Loading branch information
Kay Roepke committed Oct 14, 2012
1 parent a7553e9 commit 80e7b3a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
54 changes: 54 additions & 0 deletions src/test/java/org/graylog2/TestAppender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright 2012 Kay Roepke <[email protected]>
*
* This file is part of Graylog2.
*
* Graylog2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog2. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.graylog2;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import java.util.List;

/**
* Appender which buffers all log statements, useful for testing that some class under test did actually log information.
*/
public class TestAppender extends AppenderSkeleton {

private final List<LoggingEvent> events = Lists.newArrayList();

@Override
protected void append(LoggingEvent event) {
events.add(event);
}

@Override
public void close() {
events.clear();
}

@Override
public boolean requiresLayout() {
return false;
}

public List<LoggingEvent> getEvents() {
return ImmutableList.copyOf(events);
}
}
15 changes: 11 additions & 4 deletions src/test/java/org/graylog2/filters/TokenizerFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

package org.graylog2.filters;

import org.apache.log4j.Logger;
import org.graylog2.GraylogServerStub;
import org.graylog2.TestAppender;
import org.graylog2.logmessage.LogMessageImpl;
import org.junit.Test;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -134,12 +136,17 @@ public void testFilterWillNotOverwriteExistingAdditionalFields() {
@Test
public void testFilterWithWhitespaceAroundKVNoException() {
LogMessageImpl msg = new LogMessageImpl();
msg.setShortMessage("k1 = v1");
msg.setShortMessage("k1 = ");
TokenizerFilter f = new TokenizerFilter();

Logger logger = Logger.getLogger(TokenizerFilter.class);
TestAppender testAppender = new TestAppender();
logger.addAppender(testAppender);

f.filter(msg, new GraylogServerStub());

assertEquals(1, msg.getAdditionalData().size());
assertEquals("v1", msg.getAdditionalData().get("_k1"));
assertEquals("Should not log anything", 0, testAppender.getEvents().size());
assertEquals(0, msg.getAdditionalData().size());
}

@Test
Expand All @@ -149,7 +156,7 @@ public void testFilterWithWhitespaceAroundKV() {
TokenizerFilter f = new TokenizerFilter();
f.filter(msg, new GraylogServerStub());

assertEquals(4, msg.getAdditionalData().size());
assertEquals("There should be 4 kv pairs", 4, msg.getAdditionalData().size());
assertEquals("v1", msg.getAdditionalData().get("_k1"));
assertEquals("v2", msg.getAdditionalData().get("_k2"));
assertEquals("v3", msg.getAdditionalData().get("_k3"));
Expand Down

0 comments on commit 80e7b3a

Please sign in to comment.