19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
20
21
21
import java .util .List ;
22
+ import java .util .ResourceBundle ;
23
+ import java .util .logging .ConsoleHandler ;
24
+ import java .util .logging .Filter ;
22
25
import java .util .logging .Logger ;
23
26
import org .apache .logging .log4j .Level ;
24
27
import org .apache .logging .log4j .core .LogEvent ;
33
36
*/
34
37
public abstract class AbstractLoggerTest {
35
38
public static final String LOGGER_NAME = "Test" ;
39
+
40
+ static final java .util .logging .Level [] LEVELS = new java .util .logging .Level [] {
41
+ java .util .logging .Level .ALL ,
42
+ java .util .logging .Level .FINEST ,
43
+ java .util .logging .Level .FINER ,
44
+ java .util .logging .Level .FINE ,
45
+ java .util .logging .Level .CONFIG ,
46
+ java .util .logging .Level .INFO ,
47
+ java .util .logging .Level .WARNING ,
48
+ java .util .logging .Level .SEVERE ,
49
+ java .util .logging .Level .OFF
50
+ };
51
+
52
+ static java .util .logging .Level getEffectiveLevel (final Logger logger ) {
53
+ for (final java .util .logging .Level level : LEVELS ) {
54
+ if (logger .isLoggable (level )) {
55
+ return level ;
56
+ }
57
+ }
58
+ throw new RuntimeException ("No level is enabled." );
59
+ }
60
+
36
61
protected Logger logger ;
37
62
protected ListAppender eventAppender ;
38
63
protected ListAppender flowAppender ;
39
64
protected ListAppender stringAppender ;
40
65
41
66
@ Test
42
- public void testGetName () throws Exception {
67
+ public void testGetName () {
43
68
assertThat (logger .getName ()).isEqualTo (LOGGER_NAME );
44
69
}
45
70
46
71
@ Test
47
- public void testGlobalLogger () throws Exception {
72
+ public void testGlobalLogger () {
48
73
final Logger root = Logger .getLogger (Logger .GLOBAL_LOGGER_NAME );
49
74
root .info ("Test info message" );
50
75
root .config ("Test info message" );
@@ -58,18 +83,18 @@ public void testGlobalLogger() throws Exception {
58
83
}
59
84
60
85
@ Test
61
- public void testGlobalLoggerName () throws Exception {
86
+ public void testGlobalLoggerName () {
62
87
final Logger root = Logger .getLogger (Logger .GLOBAL_LOGGER_NAME );
63
88
assertThat (root .getName ()).isEqualTo (Logger .GLOBAL_LOGGER_NAME );
64
89
}
65
90
66
91
@ Test
67
- public void testIsLoggable () throws Exception {
92
+ public void testIsLoggable () {
68
93
assertThat (logger .isLoggable (java .util .logging .Level .SEVERE )).isTrue ();
69
94
}
70
95
71
96
@ Test
72
- public void testLog () throws Exception {
97
+ public void testLog () {
73
98
logger .info ("Informative message here." );
74
99
final List <LogEvent > events = eventAppender .getEvents ();
75
100
assertThat (events ).hasSize (1 );
@@ -82,7 +107,7 @@ public void testLog() throws Exception {
82
107
}
83
108
84
109
@ Test
85
- public void testLogFilter () throws Exception {
110
+ public void testLogFilter () {
86
111
logger .setFilter (record -> false );
87
112
logger .severe ("Informative message here." );
88
113
logger .warning ("Informative message here." );
@@ -96,7 +121,7 @@ public void testLogFilter() throws Exception {
96
121
}
97
122
98
123
@ Test
99
- public void testAlteringLogFilter () throws Exception {
124
+ public void testAlteringLogFilter () {
100
125
logger .setFilter (record -> {
101
126
record .setMessage ("This is not the message you are looking for." );
102
127
return true ;
@@ -121,7 +146,7 @@ public void testLogParamMarkers() {
121
146
}
122
147
123
148
@ Test
124
- public void testLogUsingCustomLevel () throws Exception {
149
+ public void testLogUsingCustomLevel () {
125
150
logger .config ("Config level" );
126
151
final List <LogEvent > events = eventAppender .getEvents ();
127
152
assertThat (events ).hasSize (1 );
@@ -130,7 +155,7 @@ public void testLogUsingCustomLevel() throws Exception {
130
155
}
131
156
132
157
@ Test
133
- public void testLogWithCallingClass () throws Exception {
158
+ public void testLogWithCallingClass () {
134
159
final Logger log = Logger .getLogger ("Test.CallerClass" );
135
160
log .config ("Calling from LoggerTest" );
136
161
final List <String > messages = stringAppender .getMessages ();
@@ -201,6 +226,84 @@ public void testLambdasPercentAndCurlyBraces() {
201
226
testLambdaMessages ("message{%s}" );
202
227
}
203
228
229
+ /**
230
+ * This assertion must be true, even if {@code setLevel} has no effect on the logging implementation.
231
+ *
232
+ * @see <a href="https://github.com/apache/logging-log4j2/issues/3119">GH issue #3119</a>
233
+ */
234
+ @ Test
235
+ public void testSetAndGetLevel () {
236
+ final Logger logger = Logger .getLogger (AbstractLoggerTest .class .getName () + ".testSetAndGetLevel" );
237
+ // The logger under test should have no explicit configuration
238
+ assertThat (logger .getLevel ()).isNull ();
239
+
240
+ for (java .util .logging .Level level : LEVELS ) {
241
+ logger .setLevel (level );
242
+ assertThat (logger .getLevel ()).as ("Level set using `setLevel()`" ).isEqualTo (level );
243
+ }
244
+ }
245
+
246
+ /**
247
+ * The value of `useParentHandlers` should be recorded even if it is not effective.
248
+ */
249
+ @ Test
250
+ public void testSetUseParentHandlers () {
251
+ final Logger logger = Logger .getLogger (AbstractLoggerTest .class .getName () + ".testSetUseParentHandlers" );
252
+
253
+ for (boolean useParentHandlers : new boolean [] {false , true }) {
254
+ logger .setUseParentHandlers (useParentHandlers );
255
+ assertThat (logger .getUseParentHandlers ()).isEqualTo (useParentHandlers );
256
+ }
257
+ }
258
+
259
+ /**
260
+ * The programmatically configured handlers should be recorded, even if they are not used.
261
+ */
262
+ @ Test
263
+ public void testAddAndRemoveHandlers () {
264
+ final Logger logger = Logger .getLogger (AbstractLoggerTest .class .getName () + ".testAddAndRemoveHandlers" );
265
+
266
+ assertThat (logger .getHandlers ()).isEmpty ();
267
+ // Add a handler
268
+ ConsoleHandler handler = new ConsoleHandler ();
269
+ logger .addHandler (handler );
270
+ assertThat (logger .getHandlers ()).hasSize (1 ).containsExactly (handler );
271
+ // Remove handler
272
+ logger .removeHandler (handler );
273
+ assertThat (logger .getHandlers ()).isEmpty ();
274
+ }
275
+
276
+ /**
277
+ * The programmatically configured filters should be recorded, even if they are not used.
278
+ */
279
+ @ Test
280
+ public void testSetFilter () {
281
+ final Logger logger = Logger .getLogger (AbstractLoggerTest .class .getName () + ".testSetFilter" );
282
+
283
+ assertThat (logger .getFilter ()).isNull ();
284
+ // Set filter
285
+ Filter denyAllFilter = record -> false ;
286
+ logger .setFilter (denyAllFilter );
287
+ assertThat (logger .getFilter ()).isEqualTo (denyAllFilter );
288
+ // Remove filter
289
+ logger .setFilter (null );
290
+ assertThat (logger .getFilter ()).isNull ();
291
+ }
292
+
293
+ /**
294
+ * The programmatically configured resource bundles should be recorded, even if they are not used.
295
+ */
296
+ @ Test
297
+ public void testSetResourceBundle () {
298
+ final Logger logger = Logger .getLogger (AbstractLoggerTest .class .getName () + ".testSetResourceBundle" );
299
+
300
+ assertThat (logger .getResourceBundle ()).isNull ();
301
+ // Set resource bundle
302
+ ResourceBundle bundle = ResourceBundle .getBundle ("testResourceBundle" );
303
+ logger .setResourceBundle (bundle );
304
+ assertThat (logger .getResourceBundle ()).isSameAs (bundle );
305
+ }
306
+
204
307
private void testLambdaMessages (final String string ) {
205
308
final Logger root = Logger .getLogger (Logger .GLOBAL_LOGGER_NAME );
206
309
root .info (() -> "Test info " + string );
0 commit comments