17
17
package org .apache .logging .slf4j ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
- import static org .junit .Assert .assertEquals ;
21
- import static org .junit .Assert .assertNotNull ;
20
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
21
+ import static org .junit .jupiter . api . Assertions .assertNotNull ;
22
22
23
23
import java .util .List ;
24
24
import org .apache .logging .log4j .Level ;
25
25
import org .apache .logging .log4j .core .LogEvent ;
26
+ import org .apache .logging .log4j .core .LoggerContext ;
26
27
import org .apache .logging .log4j .core .config .Configurator ;
27
28
import org .apache .logging .log4j .core .test .appender .ListAppender ;
28
- import org .apache .logging .log4j .core .test .junit .LoggerContextRule ;
29
+ import org .apache .logging .log4j .core .test .junit .LoggerContextSource ;
30
+ import org .apache .logging .log4j .core .test .junit .Named ;
29
31
import org .apache .logging .log4j .util .Strings ;
30
- import org .junit .After ;
31
- import org .junit .Before ;
32
- import org .junit .ClassRule ;
33
- import org .junit .Test ;
32
+ import org .junit .jupiter .api .AfterEach ;
33
+ import org .junit .jupiter .api .BeforeEach ;
34
+ import org .junit .jupiter .api .Test ;
34
35
import org .slf4j .Logger ;
35
36
import org .slf4j .LoggerFactory ;
36
37
import org .slf4j .MDC ;
41
42
/**
42
43
*
43
44
*/
45
+ @ LoggerContextSource ("log4j-test1.xml" )
44
46
public class LoggerTest {
45
47
46
- private static final String CONFIG = "log4j-test1.xml" ;
47
-
48
- @ ClassRule
49
- public static LoggerContextRule ctx = new LoggerContextRule (CONFIG );
50
-
51
- Logger logger = LoggerFactory .getLogger ("LoggerTest" );
48
+ private final Logger logger ;
49
+ private final LoggerContext ctx ;
52
50
53
51
@ Test
54
52
public void debug () {
55
53
logger .debug ("Debug message" );
56
54
verify ("o.a.l.s.LoggerTest Debug message MDC{}" + Strings .LINE_SEPARATOR );
57
55
}
58
56
57
+ public LoggerTest (final LoggerContext context ) {
58
+ this .ctx = context ;
59
+ this .logger = LoggerFactory .getLogger ("LoggerTest" );
60
+ }
61
+
59
62
@ Test
60
63
public void debugNoParms () {
61
64
logger .debug ("Debug message {}" );
@@ -113,7 +116,7 @@ public void supportsCustomSLF4JMarkers() {
113
116
@ Test
114
117
public void testRootLogger () {
115
118
final Logger l = LoggerFactory .getLogger (Logger .ROOT_LOGGER_NAME );
116
- assertNotNull ("No Root Logger" , l );
119
+ assertNotNull (l , "No Root Logger" );
117
120
assertEquals (Logger .ROOT_LOGGER_NAME , l .getName ());
118
121
}
119
122
@@ -159,7 +162,7 @@ public void testThrowable() {
159
162
160
163
@ Test
161
164
public void testLazyLoggingEventBuilder () {
162
- final ListAppender appender = ctx .getListAppender ("UnformattedList" );
165
+ final ListAppender appender = ctx .getConfiguration (). getAppender ("UnformattedList" );
163
166
final Level oldLevel = ctx .getRootLogger ().getLevel ();
164
167
try {
165
168
Configurator .setRootLevel (Level .ERROR );
@@ -173,34 +176,35 @@ public void testLazyLoggingEventBuilder() {
173
176
}
174
177
175
178
private ListAppender getAppenderByName (final String name ) {
176
- final ListAppender listApp = ctx .getListAppender (name );
177
- assertNotNull ("Missing Appender" , listApp );
179
+ final ListAppender listApp = ctx .getConfiguration (). getAppender (name );
180
+ assertNotNull (listApp , "Missing Appender" );
178
181
return listApp ;
179
182
}
180
183
181
184
private void verify (final String expected ) {
182
185
final ListAppender listApp = getAppenderByName ("List" );
183
186
final List <String > events = listApp .getMessages ();
184
- assertEquals ("Incorrect number of messages. Expected 1 Actual " + events . size (), 1 , events .size ());
187
+ assertEquals (1 , events . size (), "Incorrect number of messages. Expected 1 Actual " + events .size ());
185
188
final String actual = events .get (0 );
186
- assertEquals ("Incorrect message. Expected " + expected + ". Actual " + actual , expected , actual );
189
+ assertEquals (expected , actual , "Incorrect message. Expected \ " + expected + \ " . Actual \ " + actual" );
187
190
listApp .clear ();
188
191
}
189
192
190
193
private void verifyThrowable (final Throwable expected ) {
191
194
final ListAppender listApp = getAppenderByName ("UnformattedList" );
192
195
final List <LogEvent > events = listApp .getEvents ();
193
- assertEquals ("Incorrect number of messages" , 1 , events . size () );
196
+ assertEquals (1 , events . size (), "Incorrect number of messages" );
194
197
final LogEvent actual = events .get (0 );
195
- assertEquals ("Incorrect throwable." , expected , actual .getThrown ());
198
+ assertEquals (expected , actual .getThrown (), "Incorrect throwable." );
196
199
listApp .clear ();
197
200
}
198
201
199
- @ Before
200
- @ After
201
- public void cleanup () {
202
+ @ BeforeEach
203
+ @ AfterEach
204
+ public void cleanup (
205
+ @ Named ("List" ) final ListAppender list , @ Named ("UnformattedList" ) final ListAppender unformattedList ) {
202
206
MDC .clear ();
203
- ctx . getListAppender ( "List" ) .clear ();
204
- ctx . getListAppender ( "UnformattedList" ) .clear ();
207
+ list .clear ();
208
+ unformattedList .clear ();
205
209
}
206
210
}
0 commit comments