1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.logging.logback.internal

File LogbackEventGeneratorTest.java

 

Code metrics

0
17
3
1
110
64
3
0.18
5.67
3
1

Classes

Class Line # Actions
LogbackEventGeneratorTest 54 17 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.logging.logback.internal;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Assert;
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.slf4j.Logger;
29    import org.slf4j.LoggerFactory;
30    import org.xwiki.logging.LogLevel;
31    import org.xwiki.logging.event.LogEvent;
32    import org.xwiki.observation.EventListener;
33    import org.xwiki.observation.ObservationManager;
34    import org.xwiki.observation.event.Event;
35    import org.xwiki.observation.internal.DefaultObservationManager;
36    import org.xwiki.test.AllLogRule;
37    import org.xwiki.test.ComponentManagerRule;
38    import org.xwiki.test.annotation.ComponentList;
39   
40    import static org.junit.Assert.assertEquals;
41    import static org.mockito.ArgumentMatchers.eq;
42    import static org.mockito.Mockito.*;
43   
44    /**
45    * Unit tests for {@link LogbackEventGenerator}.
46    *
47    * @version $Id: b1dcb4b6d38b1d1cb6c8d35173957ff7f68eb5ca $
48    * @since 3.2M3
49    */
50    @ComponentList({
51    DefaultObservationManager.class,
52    LogbackEventGenerator.class
53    })
 
54    public class LogbackEventGeneratorTest
55    {
56    @Rule
57    public final ComponentManagerRule componentManager = new ComponentManagerRule();
58   
59    @Rule
60    public final AllLogRule logCapture = new AllLogRule();
61   
62    private Logger logger;
63   
64    private ObservationManager observationManager;
65   
 
66  2 toggle @Before
67    public void setUp() throws Exception
68    {
69  2 this.observationManager = this.componentManager.getInstance(ObservationManager.class);
70   
71  2 this.logger = LoggerFactory.getLogger(getClass());
72    }
73   
74    /**
75    * Verify that logging an error will generate a Log Event.
76    */
 
77  1 toggle @Test
78    public void verifyThatLoggingGeneratesALogEvent()
79    {
80  1 Event event = new LogEvent(null, LogLevel.INFO, "dummy", null, null);
81   
82  1 EventListener listener = mock(EventListener.class);
83  1 when(listener.getName()).thenReturn("mylistener");
84  1 when(listener.getEvents()).thenReturn(Arrays.asList(event));
85   
86  1 this.observationManager.addListener(listener);
87   
88  1 this.logger.error("error message");
89   
90  1 Event expected = new LogEvent(null, LogLevel.ERROR, "error message", null, null);
91  1 verify(listener).onEvent(eq(expected), eq(getClass().getName()), eq(null));
92  1 assertEquals("error message", this.logCapture.getMessage(0));
93    }
94   
 
95  1 toggle @Test
96    public void initializeWhenNoLogback() throws Exception
97    {
98    // Simulate that the Logging implementation is not Logback
99  1 LogbackEventGenerator generator =
100    (LogbackEventGenerator) this.componentManager.getInstance(EventListener.class, "LogbackEventGenerator");
101  1 LogbackEventGenerator spyGenerator = spy(generator);
102  1 when(spyGenerator.getRootLogger()).thenReturn(null);
103   
104  1 spyGenerator.initialize();
105   
106  1 Assert.assertEquals(1, this.logCapture.size());
107  1 Assert.assertEquals("Could not find any Logback root logger. The logging module won't be able to catch "
108    + "logs.", this.logCapture.getMessage(0));
109    }
110    }