Skip to content

Commit 2615a13

Browse files
authored
Merge pull request #11 from jamezp/test
Add a simple test and CI runner
2 parents e4f14e5 + b4c3909 commit 2615a13

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

.github/workflows/ci.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This workflow will build a Java project with Maven
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
4+
name: SLF4J JBoss Log Manager Binding - CI
5+
6+
on:
7+
push:
8+
branches:
9+
- 'master'
10+
pull_request:
11+
types: [opened, synchronize, reopened, ready_for_review]
12+
13+
jobs:
14+
build:
15+
name: ${{ matrix.os }}-jdk${{ matrix.java }}
16+
runs-on: ${{ matrix.os }}
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
os: [ubuntu-latest, windows-latest ]
21+
java: ['1.8', '11']
22+
23+
steps:
24+
- uses: actions/checkout@v2
25+
- uses: actions/cache@v1
26+
with:
27+
path: ~/.m2/repository
28+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
29+
restore-keys: |
30+
${{ runner.os }}-maven-
31+
- name: Set up JDK ${{ matrix.java }}
32+
uses: actions/setup-java@v1
33+
with:
34+
java-version: ${{ matrix.java }}
35+
- name: Build and Test on ${{ matrix.java }}
36+
run: mvn clean install
37+
- uses: actions/upload-artifact@v2
38+
if: failure()
39+
with:
40+
name: surefire-reports-${{ matrix.os }}-${{ matrix.java }}
41+
path: '**/surefire-reports/*.txt'

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
</parent>
3838

3939
<properties>
40+
<version.junit>4.13.1</version.junit>
4041
<version.org.jboss.logmanager>2.1.17.Final</version.org.jboss.logmanager>
4142
<version.org.slf4j>1.7.30</version.org.slf4j>
4243
</properties>
@@ -72,5 +73,11 @@
7273
<version>${version.org.slf4j}</version>
7374
<scope>provided</scope>
7475
</dependency>
76+
<dependency>
77+
<groupId>junit</groupId>
78+
<artifactId>junit</artifactId>
79+
<version>${version.junit}</version>
80+
<scope>test</scope>
81+
</dependency>
7582
</dependencies>
7683
</project>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
*
4+
* Copyright 2020 Red Hat, Inc., and individual contributors
5+
* as indicated by the @author tags.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.slf4j.impl;
21+
22+
import java.util.concurrent.BlockingDeque;
23+
import java.util.concurrent.LinkedBlockingDeque;
24+
import java.util.logging.Level;
25+
import java.util.logging.LogRecord;
26+
27+
import org.jboss.logmanager.ExtHandler;
28+
import org.jboss.logmanager.ExtLogRecord;
29+
import org.jboss.logmanager.LogContext;
30+
import org.jboss.logmanager.LogContextSelector;
31+
import org.junit.After;
32+
import org.junit.AfterClass;
33+
import org.junit.Assert;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
38+
import org.slf4j.MDC;
39+
40+
/**
41+
* @author <a href="mailto:[email protected]">James R. Perkins</a>
42+
*/
43+
public class LoggerTestCase {
44+
private static final LogContext LOG_CONTEXT = LogContext.create();
45+
private static final java.util.logging.Logger ROOT = LOG_CONTEXT.getLogger("");
46+
private static final QueueHandler HANDLER = new QueueHandler();
47+
48+
private static final LogContextSelector DEFAULT_SELECTOR = LogContext.getLogContextSelector();
49+
50+
@BeforeClass
51+
public static void configureLogManager() {
52+
LogContext.setLogContextSelector(() -> LOG_CONTEXT);
53+
ROOT.addHandler(HANDLER);
54+
}
55+
56+
@AfterClass
57+
public static void cleanup() throws Exception {
58+
LOG_CONTEXT.close();
59+
LogContext.setLogContextSelector(DEFAULT_SELECTOR);
60+
}
61+
62+
@After
63+
public void clearHandler() {
64+
HANDLER.close();
65+
}
66+
67+
@Test
68+
public void testLogger() {
69+
final Logger logger = LoggerFactory.getLogger(LoggerTestCase.class);
70+
Assert.assertTrue(expectedTypeMessage(Slf4jLogger.class, logger.getClass()), logger instanceof Slf4jLogger);
71+
72+
// Ensure the logger logs something
73+
final String testMsg = "This is a test message";
74+
logger.info(testMsg);
75+
ExtLogRecord record = HANDLER.messages.poll();
76+
Assert.assertNotNull(record);
77+
Assert.assertEquals(testMsg, record.getMessage());
78+
Assert.assertNull(record.getParameters());
79+
80+
// Test a formatted message
81+
logger.info("This is a test formatted {}", "{message}");
82+
record = HANDLER.messages.poll();
83+
Assert.assertNotNull(record);
84+
Assert.assertEquals("This is a test formatted {message}", record.getFormattedMessage());
85+
Assert.assertArrayEquals("Expected parameter not found.", new Object[] {"{message}"}, record.getParameters());
86+
}
87+
88+
@Test
89+
public void testLoggerWithExceptions() {
90+
final Logger logger = LoggerFactory.getLogger(LoggerTestCase.class);
91+
92+
final RuntimeException e = new RuntimeException("Test exception");
93+
final String testMsg = "This is a test message";
94+
logger.info(testMsg, e);
95+
LogRecord record = HANDLER.messages.poll();
96+
Assert.assertNotNull(record);
97+
Assert.assertEquals(testMsg, record.getMessage());
98+
Assert.assertEquals("Cause is different from the expected cause", e, record.getThrown());
99+
100+
// Test format with the last parameter being the throwable which should set be set on the record
101+
logger.info("This is a test formatted {}", "{message}", e);
102+
record = HANDLER.messages.poll();
103+
Assert.assertNotNull(record);
104+
Assert.assertEquals("This is a test formatted {message}", record.getMessage());
105+
Assert.assertEquals("Cause is different from the expected cause", e, record.getThrown());
106+
107+
}
108+
109+
@Test
110+
public void testMDC() {
111+
Assert.assertSame(expectedTypeMessage(Slf4jMDCAdapter.class, MDC.getMDCAdapter().getClass()), MDC.getMDCAdapter().getClass(), Slf4jMDCAdapter.class);
112+
final String key = Long.toHexString(System.currentTimeMillis());
113+
MDC.put(key, "value");
114+
Assert.assertEquals("MDC value should be \"value\"", "value", MDC.get(key));
115+
Assert.assertEquals("MDC value should be \"value\"", "value", org.jboss.logmanager.MDC.get(key));
116+
}
117+
118+
private static String expectedTypeMessage(final Class<?> expected, final Class<?> found) {
119+
return String.format("Expected type %s but found type %s", expected.getName(), found.getName());
120+
}
121+
122+
private static class QueueHandler extends ExtHandler {
123+
final BlockingDeque<ExtLogRecord> messages = new LinkedBlockingDeque<>();
124+
125+
@Override
126+
protected void doPublish(final ExtLogRecord record) {
127+
messages.add(record);
128+
}
129+
130+
@Override
131+
public void flush() {
132+
}
133+
134+
@Override
135+
public void close() throws SecurityException {
136+
messages.clear();
137+
setLevel(Level.ALL);
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)