001package org.slf4j.eventTest;
002
003import org.junit.After;
004import org.junit.Before;
005import org.junit.Test;
006import org.slf4j.Marker;
007import org.slf4j.event.EventRecodingLogger;
008import org.slf4j.event.Level;
009import org.slf4j.event.SubstituteLoggingEvent;
010import org.slf4j.helpers.BasicMarkerFactory;
011import org.slf4j.helpers.SubstituteLogger;
012
013import java.util.Queue;
014import java.util.concurrent.LinkedBlockingQueue;
015
016import static org.junit.Assert.*;
017
018public class EventRecodingLoggerTest {
019    private Queue<SubstituteLoggingEvent> queue;
020    private EventRecodingLogger logger;
021    private String message;
022    private Object param1;
023    private Object param2;
024    private Object param3;
025    private Object[] oneParam;
026    private Object[] twoParams;
027    private Object[] threeParams;
028    private Throwable exception;
029    private Marker marker;
030
031    @Before
032    public void setUp() {
033        queue = new LinkedBlockingQueue<>();
034        logger = new EventRecodingLogger(new SubstituteLogger("testLogger", queue, true), queue);
035        message = "Test message with 3 parameters {} {} {} {}";
036        param1 = 1;
037        param2 = 2;
038        param3 = 3;
039        oneParam = new Object[] { param1 };
040        twoParams = new Object[] { param1, param2 };
041        threeParams = new Object[] { param1, param2, param3 };
042        exception = new IllegalStateException("We just need an exception");
043        marker = new BasicMarkerFactory().getMarker("testMarker");
044    }
045
046    @After
047    public void tearDown() {
048        assertTrue(queue.isEmpty());
049    }
050
051    @Test
052    public void singleMessage() {
053        for (Level level : Level.values()) {
054            singleMessageCheck(level);
055        }
056    }
057
058    private void singleMessageCheck(Level level) {
059        switch (level) {
060        case TRACE:
061            logger.trace(message);
062            break;
063        case DEBUG:
064            logger.debug(message);
065            break;
066        case INFO:
067            logger.info(message);
068            break;
069        case WARN:
070            logger.warn(message);
071            break;
072        case ERROR:
073            logger.error(message);
074            break;
075        }
076        verifyMessageWithoutMarker(level, null, null);
077    }
078
079    @Test
080    public void oneParameter() {
081        for (Level level : Level.values()) {
082            oneParameterCheck(level);
083        }
084    }
085
086    private void oneParameterCheck(Level level) {
087        switch (level) {
088        case TRACE:
089            logger.trace(message, param1);
090            break;
091        case DEBUG:
092            logger.debug(message, param1);
093            break;
094        case INFO:
095            logger.info(message, param1);
096            break;
097        case WARN:
098            logger.warn(message, param1);
099            break;
100        case ERROR:
101            logger.error(message, param1);
102            break;
103        }
104        verifyMessageWithoutMarker(level, oneParam, null);
105    }
106
107    @Test
108    public void messageTwoParameters() {
109        for (Level level : Level.values()) {
110            messageTwoParametersCheck(level);
111        }
112    }
113
114    private void messageTwoParametersCheck(Level level) {
115        switch (level) {
116        case TRACE:
117            logger.trace(message, param1, param2);
118            break;
119        case DEBUG:
120            logger.debug(message, param1, param2);
121            break;
122        case INFO:
123            logger.info(message, param1, param2);
124            break;
125        case WARN:
126            logger.warn(message, param1, param2);
127            break;
128        case ERROR:
129            logger.error(message, param1, param2);
130            break;
131        }
132        verifyMessageWithoutMarker(level, twoParams, null);
133    }
134
135    @Test
136    public void traceMessageThreeParameters() {
137        for (Level level : Level.values()) {
138            threeParameterCheck(level);
139        }
140    }
141
142    private void threeParameterCheck(Level level) {
143        switch (level) {
144        case TRACE:
145            logger.trace(message, param1, param2, param3);
146            break;
147        case DEBUG:
148            logger.debug(message, param1, param2, param3);
149            break;
150        case INFO:
151            logger.info(message, param1, param2, param3);
152            break;
153        case WARN:
154            logger.warn(message, param1, param2, param3);
155            break;
156        case ERROR:
157            logger.error(message, param1, param2, param3);
158            break;
159        }
160        verifyMessageWithoutMarker(level, threeParams, null);
161    }
162
163    @Test
164    public void testMessageThrowable() {
165        for (Level level : Level.values()) {
166            throwableCheck(level);
167        }
168    }
169
170    private void throwableCheck(Level level) {
171        switch (level) {
172        case TRACE:
173            logger.trace(message, exception);
174            break;
175        case DEBUG:
176            logger.debug(message, exception);
177            break;
178        case INFO:
179            logger.info(message, exception);
180            break;
181        case WARN:
182            logger.warn(message, exception);
183            break;
184        case ERROR:
185            logger.error(message, exception);
186            break;
187        }
188        verifyMessageWithoutMarker(level, null, exception);
189    }
190
191    @Test
192    public void traceMessageOneParameterThrowable() {
193        for (Level level : Level.values()) {
194            oneParamThrowableCheck(level);
195        }
196    }
197
198    private void oneParamThrowableCheck(Level level) {
199        switch (level) {
200        case TRACE:
201            logger.trace(message, param1, exception);
202            break;
203        case DEBUG:
204            logger.debug(message, param1, exception);
205            break;
206        case INFO:
207            logger.info(message, param1, exception);
208            break;
209        case WARN:
210            logger.warn(message, param1, exception);
211            break;
212        case ERROR:
213            logger.error(message, param1, exception);
214            break;
215        }
216        verifyMessageWithoutMarker(level, oneParam, exception);
217    }
218
219    @Test
220    public void traceMessageTwoParametersThrowable() {
221        for (Level level : Level.values()) {
222            twoParamThrowableCheck(level);
223        }
224    }
225
226    private void twoParamThrowableCheck(Level level) {
227        switch (level) {
228        case TRACE:
229            logger.trace(message, param1, param2, exception);
230            break;
231        case DEBUG:
232            logger.debug(message, param1, param2, exception);
233            break;
234        case INFO:
235            logger.info(message, param1, param2, exception);
236            break;
237        case WARN:
238            logger.warn(message, param1, param2, exception);
239            break;
240        case ERROR:
241            logger.error(message, param1, param2, exception);
242            break;
243        }
244        verifyMessageWithoutMarker(level, twoParams, exception);
245    }
246
247    @Test
248    public void testMessageThreeParametersThrowable() {
249        for (Level level : Level.values()) {
250            messageWith3ArgsPlusException(level);
251        }
252    }
253
254    private void messageWith3ArgsPlusException(Level level) {
255        switch (level) {
256        case TRACE:
257            logger.trace(message, param1, param2, param3, exception);
258            break;
259        case DEBUG:
260            logger.debug(message, param1, param2, param3, exception);
261            break;
262        case INFO:
263            logger.info(message, param1, param2, param3, exception);
264            break;
265        case WARN:
266            logger.warn(message, param1, param2, param3, exception);
267            break;
268        case ERROR:
269            logger.error(message, param1, param2, param3, exception);
270            break;
271        }
272        verifyMessageWithoutMarker(level, threeParams, exception);
273    }
274
275    @Test
276    public void markerMessage() {
277        for (Level level : Level.values()) {
278            markerMessageCheck(level);
279        }
280    }
281
282    private void markerMessageCheck(Level level) {
283        switch (level) {
284        case TRACE:
285            logger.trace(marker, message);
286            break;
287        case DEBUG:
288            logger.debug(marker, message);
289            break;
290        case INFO:
291            logger.info(marker, message);
292            break;
293        case WARN:
294            logger.warn(marker, message);
295            break;
296        case ERROR:
297            logger.error(marker, message);
298            break;
299        }
300        verifyMessage(level, marker, null, null);
301    }
302
303    @Test
304    public void markerMessageOneParameter() {
305        for (Level level : Level.values()) {
306            markerMessageOneParameter(level);
307        }
308    }
309
310    private void markerMessageOneParameter(Level level) {
311        switch (level) {
312        case TRACE:
313            logger.trace(marker, message, param1);
314            break;
315        case DEBUG:
316            logger.debug(marker, message, param1);
317            break;
318        case INFO:
319            logger.info(marker, message, param1);
320            break;
321        case WARN:
322            logger.warn(marker, message, param1);
323            break;
324        case ERROR:
325            logger.error(marker, message, param1);
326            break;
327        }
328        verifyMessage(level, marker, oneParam, null);
329    }
330
331    @Test
332    public void traceMarkerMessageTwoParameters() {
333        for (Level level : Level.values()) {
334            markerMessageTwoParameters(level);
335        }
336    }
337
338    private void markerMessageTwoParameters(Level level) {
339        switch (level) {
340        case TRACE:
341            logger.trace(marker, message, param1, param2);
342            break;
343        case DEBUG:
344            logger.debug(marker, message, param1, param2);
345            break;
346        case INFO:
347            logger.info(marker, message, param1, param2);
348            break;
349        case WARN:
350            logger.warn(marker, message, param1, param2);
351            break;
352        case ERROR:
353            logger.error(marker, message, param1, param2);
354            break;
355        }
356        verifyMessage(level, marker, twoParams, null);
357    }
358
359    @Test
360    public void traceMarkerMessageThreeParameters() {
361        for (Level level : Level.values()) {
362            markerMessageThreeParameters(level);
363        }
364    }
365
366    private void markerMessageThreeParameters(Level level) {
367        switch (level) {
368        case TRACE:
369            logger.trace(marker, message, param1, param2, param3);
370            break;
371        case DEBUG:
372            logger.debug(marker, message, param1, param2, param3);
373            break;
374        case INFO:
375            logger.info(marker, message, param1, param2, param3);
376            break;
377        case WARN:
378            logger.warn(marker, message, param1, param2, param3);
379            break;
380        case ERROR:
381            logger.error(marker, message, param1, param2, param3);
382            break;
383        }
384        verifyMessage(level, marker, threeParams, null);
385    }
386
387    @Test
388    public void markerMessageThrowable() {
389        for (Level level : Level.values()) {
390            markerMessageThrowable(level);
391        }
392    }
393
394    private void markerMessageThrowable(Level level) {
395        switch (level) {
396        case TRACE:
397            logger.trace(marker, message, exception);
398            break;
399        case DEBUG:
400            logger.debug(marker, message, exception);
401            break;
402        case INFO:
403            logger.info(marker, message, exception);
404            break;
405        case WARN:
406            logger.warn(marker, message, exception);
407            break;
408        case ERROR:
409            logger.error(marker, message, exception);
410            break;
411        }
412        verifyMessage(level, marker, null, exception);
413    }
414
415    @Test
416    public void markerMessageOneParameterThrowable() {
417        for (Level level : Level.values()) {
418            markerMessageOneParameterThrowableCheck(level);
419        }
420    }
421
422    private void markerMessageOneParameterThrowableCheck(Level level) {
423        switch (level) {
424        case TRACE:
425            logger.trace(marker, message, param1, exception);
426            break;
427        case DEBUG:
428            logger.debug(marker, message, param1, exception);
429            break;
430        case INFO:
431            logger.info(marker, message, param1, exception);
432            break;
433        case WARN:
434            logger.warn(marker, message, param1, exception);
435            break;
436        case ERROR:
437            logger.error(marker, message, param1, exception);
438            break;
439        }
440        verifyMessage(level, marker, oneParam, exception);
441    }
442
443    @Test
444    public void traceMarkerMessageTwoParametersThrowable() {
445        for (Level level : Level.values()) {
446            markerMessageTwoParametersThrowableCheck(level);
447        }
448    }
449
450    private void markerMessageTwoParametersThrowableCheck(Level level) {
451        switch (level) {
452        case TRACE:
453            logger.trace(marker, message, param1, param2, exception);
454            break;
455        case DEBUG:
456            logger.debug(marker, message, param1, param2, exception);
457            break;
458        case INFO:
459            logger.info(marker, message, param1, param2, exception);
460            break;
461        case WARN:
462            logger.warn(marker, message, param1, param2, exception);
463            break;
464        case ERROR:
465            logger.error(marker, message, param1, param2, exception);
466            break;
467        }
468        verifyMessage(level, marker, twoParams, exception);
469    }
470
471    @Test
472    public void traceMarkerMessageThreeParametersThrowable() {
473        for (Level level : Level.values()) {
474            markerMessageThreeParametersThrowableCheck(level);
475        }
476    }
477
478    private void markerMessageThreeParametersThrowableCheck(Level level) {
479        switch (level) {
480        case TRACE:
481            logger.trace(marker, message, param1, param2, param3, exception);
482            break;
483        case DEBUG:
484            logger.debug(marker, message, param1, param2, param3, exception);
485            break;
486        case INFO:
487            logger.info(marker, message, param1, param2, param3, exception);
488            break;
489        case WARN:
490            logger.warn(marker, message, param1, param2, param3, exception);
491            break;
492        case ERROR:
493            logger.error(marker, message, param1, param2, param3, exception);
494            break;
495        }
496        verifyMessage(level, marker, threeParams, exception);
497    }
498
499    private void verifyMessageWithoutMarker(Level level, Object[] arguments, Throwable exception) {
500        verifyMessage(level, null, arguments, exception);
501    }
502
503    private void verifyMessage(Level level, Marker marker, Object[] arguments, Throwable exception) {
504
505        assertEquals("missing event: ", 1, queue.size());
506        SubstituteLoggingEvent event = queue.poll();
507        assertNotNull(event);
508
509        if (marker == null) {
510            assertNull(event.getMarkers());
511        } else {
512            assertEquals(marker, event.getMarkers().get(0));
513        }
514
515        assertEquals(message, event.getMessage());
516
517        if (arguments == null) {
518            assertNull(event.getArgumentArray());
519        } else {
520            assertArrayEquals(arguments, event.getArgumentArray());
521        }
522
523        assertEquals("wrong level: ", level, event.getLevel());
524
525        if (exception == null) {
526            assertNull(event.getThrowable());
527        } else {
528            assertEquals(exception, event.getThrowable());
529        }
530    }
531}