Skip to content

Commit c5a5ae9

Browse files
committed
Add unit tests for syslog priorities
Closes #10
1 parent 54a5fad commit c5a5ae9

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
<version>4.12</version>
100100
<scope>test</scope>
101101
</dependency>
102+
<dependency>
103+
<groupId>org.mockito</groupId>
104+
<artifactId>mockito-core</artifactId>
105+
<version>2.1.0-RC.1</version>
106+
</dependency>
102107
</dependencies>
103108

104109
<build>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.graylog2.syslog4j.impl.message.processor;
2+
3+
import org.graylog2.syslog4j.SyslogConstants;
4+
import org.junit.Test;
5+
6+
import java.util.Date;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class AbstractSyslogMessageProcessorTest {
11+
@Test
12+
public void appendPriority() throws Exception {
13+
final AbstractSyslogMessageProcessor messageProcessor = new AbstractSyslogMessageProcessor() {
14+
@Override
15+
protected void appendTimestamp(StringBuffer buffer, Date datetime) {
16+
}
17+
18+
@Override
19+
public String createSyslogHeader(int facility, int level, String localName, boolean sendLocalTimestamp, boolean sendLocalName) {
20+
return null;
21+
}
22+
23+
@Override
24+
public String createSyslogHeader(int facility, int level, String localName, boolean sendLocalName, Date datetime) {
25+
return null;
26+
}
27+
};
28+
29+
// For example, a kernel message (Facility=0) with a Severity of Emergency (Severity=0)
30+
// would have a Priority value of 0.
31+
// -- https://tools.ietf.org/html/rfc5424#section-6.2.1
32+
final StringBuffer sb1 = new StringBuffer();
33+
messageProcessor.appendPriority(sb1, SyslogConstants.FACILITY_KERN, SyslogConstants.LEVEL_EMERGENCY);
34+
assertEquals("<0>", sb1.toString());
35+
36+
// Also, a "local use 4" message (Facility=20) with a Severity of Notice (Severity=5)
37+
// would have a Priority value of 165.
38+
// -- https://tools.ietf.org/html/rfc5424#section-6.2.1
39+
final StringBuffer sb2 = new StringBuffer();
40+
messageProcessor.appendPriority(sb2, SyslogConstants.FACILITY_LOCAL4, SyslogConstants.LEVEL_NOTICE);
41+
assertEquals("<165>", sb2.toString());
42+
43+
// Upper bound: Facility = 23 (local7), Level = 7 (Debug)
44+
final StringBuffer sb3 = new StringBuffer();
45+
messageProcessor.appendPriority(sb3, SyslogConstants.FACILITY_LOCAL7, SyslogConstants.LEVEL_DEBUG);
46+
assertEquals("<191>", sb3.toString());
47+
}
48+
49+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.graylog2.syslog4j.impl.unix;
2+
3+
import org.graylog2.syslog4j.SyslogConstants;
4+
import org.junit.Test;
5+
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.times;
8+
import static org.mockito.Mockito.verify;
9+
10+
public class UnixSyslogTest {
11+
@Test
12+
public void writeUsesCorrectSyslogPriority() throws Exception {
13+
final UnixSyslog.CLibrary mockLibrary = mock(UnixSyslog.CLibrary.class);
14+
UnixSyslog.libraryInstance = mockLibrary;
15+
UnixSyslog.currentFacility = SyslogConstants.FACILITY_LOCAL4;
16+
17+
final UnixSyslogConfig config = new UnixSyslogConfig();
18+
config.setFacility(SyslogConstants.FACILITY_LOCAL4);
19+
20+
UnixSyslog.write(SyslogConstants.LEVEL_NOTICE, "Test", config);
21+
22+
verify(mockLibrary, times(1)).syslog(165, "%s", "Test");
23+
}
24+
25+
}

0 commit comments

Comments
 (0)