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

File DefaultLoggerManagerTest.java

 

Code metrics

6
70
10
1
253
152
14
0.2
7
10
1.4

Classes

Class Line # Actions
DefaultLoggerManagerTest 54 70 0% 14 4
0.9534883595.3%
 

Contributing tests

This file is covered by 7 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.Iterator;
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.LogQueue;
32    import org.xwiki.logging.event.LogQueueListener;
33    import org.xwiki.observation.internal.DefaultObservationManager;
34    import org.xwiki.test.annotation.ComponentList;
35    import org.xwiki.test.mockito.MockitoComponentMockingRule;
36   
37    import ch.qos.logback.classic.spi.ILoggingEvent;
38    import ch.qos.logback.core.Appender;
39    import ch.qos.logback.core.filter.Filter;
40    import ch.qos.logback.core.read.ListAppender;
41    import ch.qos.logback.core.spi.FilterReply;
42   
43    import static org.mockito.Mockito.spy;
44    import static org.mockito.Mockito.verify;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Unit tests for {@link DefaultLoggerManager}.
49    *
50    * @version $Id: 2df34e21c5197cd11ef9e87dbe3bc9f0cdb4078d $
51    * @since 3.2M3
52    */
53    @ComponentList({ DefaultLoggerManager.class, DefaultObservationManager.class, LogbackEventGenerator.class })
 
54    public class DefaultLoggerManagerTest
55    {
56    @Rule
57    public final MockitoComponentMockingRule<DefaultLoggerManager> mocker =
58    new MockitoComponentMockingRule<DefaultLoggerManager>(DefaultLoggerManager.class);
59   
60    private DefaultLoggerManager loggerManager;
61   
62    private Logger logger;
63   
64    private ListAppender<ILoggingEvent> listAppender;
65   
66    private LogbackUtils utils = new LogbackUtils();
67   
 
68  7 toggle @Before
69    public void setUp() throws Exception
70    {
71  7 ch.qos.logback.classic.Logger rootLogger = this.utils.getRootLogger();
72   
73    // Disable all appenders to avoid unnecessary log
74  7 Filter<ILoggingEvent> filter = new Filter<ILoggingEvent>()
75    {
 
76  33 toggle @Override
77    public FilterReply decide(ILoggingEvent event)
78    {
79  33 if (event.getMessage() != null && event.getMessage().startsWith("[test]")) {
80  33 return FilterReply.DENY;
81    }
82   
83  0 return FilterReply.NEUTRAL;
84    }
85    };
86  7 Iterator<Appender<ILoggingEvent>> appendersIterator = rootLogger.iteratorForAppenders();
87  35 while (appendersIterator.hasNext()) {
88  28 Appender<ILoggingEvent> appender = appendersIterator.next();
89  28 appender.addFilter(filter);
90    }
91   
92    // Add appender
93  7 this.listAppender = new ListAppender<ILoggingEvent>();
94  7 this.listAppender.start();
95  7 rootLogger.addAppender(this.listAppender);
96   
97  7 this.logger = LoggerFactory.getLogger(getClass());
98  7 this.loggerManager = this.mocker.getComponentUnderTest();
99    }
100   
 
101  1 toggle @Test
102    public void testPushPopLogListener() throws InterruptedException
103    {
104  1 this.logger.error("[test] before push");
105   
106    // Make sure the log has been sent to the logback appender
107  1 Assert.assertEquals("[test] before push", this.listAppender.list.get(0).getMessage());
108   
109  1 LogQueue queue = new LogQueue();
110   
111  1 this.loggerManager.pushLogListener(new LogQueueListener("loglistenerid", queue));
112   
113  1 this.logger.error("[test] after push");
114   
115    // Make sure the log has been added to the queue
116  1 Assert.assertEquals("[test] after push", queue.poll().getMessage());
117   
118    // Make sure the log has not been sent to the logback appender
119  1 Assert.assertEquals(1, this.listAppender.list.size());
120   
121  1 Thread thread = new Thread(new Runnable()
122    {
 
123  1 toggle @Override
124    public void run()
125    {
126  1 DefaultLoggerManagerTest.this.logger.error("[test] other thread");
127    }
128    });
129  1 thread.start();
130  1 thread.join();
131   
132    // Make sure the log has been sent to the logback appender
133  1 Assert.assertEquals("[test] other thread", this.listAppender.list.get(1).getMessage());
134   
135  1 this.loggerManager.popLogListener();
136   
137  1 this.logger.error("[test] after pop");
138   
139  1 Assert.assertTrue(queue.isEmpty());
140  1 Assert.assertEquals("[test] after pop", this.listAppender.list.get(2).getMessage());
141    }
142   
 
143  1 toggle @Test
144    public void testStackedListeners()
145    {
146  1 this.logger.error("[test] before push");
147   
148    // Make sure the log has been sent to the logback appender
149  1 Assert.assertEquals("[test] before push", this.listAppender.list.get(0).getMessage());
150   
151  1 LogQueue queue1 = new LogQueue();
152   
153  1 this.loggerManager.pushLogListener(new LogQueueListener("loglistenerid1", queue1));
154   
155  1 LogQueue queue2 = new LogQueue();
156   
157  1 this.loggerManager.pushLogListener(new LogQueueListener("loglistenerid2", queue2));
158   
159  1 this.logger.error("[test] log queue2");
160   
161    // Make sure the log has not been sent to the stacked listener
162  1 Assert.assertTrue(queue1.isEmpty());
163   
164    // Make sure the log has been sent to the current listener
165  1 Assert.assertEquals("[test] log queue2", queue2.poll().getMessage());
166   
167  1 this.loggerManager.popLogListener();
168   
169  1 this.logger.error("[test] log queue1");
170   
171    // Make sure the log has been sent to the current listener
172  1 Assert.assertEquals("[test] log queue1", queue1.poll().getMessage());
173   
174  1 this.loggerManager.popLogListener();
175    }
176   
 
177  1 toggle @Test
178    public void testNullListeners()
179    {
180  1 this.logger.error("[test] before push");
181   
182    // Make sure the log has been sent to the logback appender
183  1 Assert.assertEquals("[test] before push", this.listAppender.list.get(0).getMessage());
184   
185  1 this.loggerManager.pushLogListener(null);
186   
187  1 this.logger.error("[test] log to null");
188   
189    // Make sure the log has not been sent to the logback appender
190  1 Assert.assertEquals(1, this.listAppender.list.size());
191   
192  1 this.loggerManager.popLogListener();
193   
194  1 this.logger.error("[test] after pop");
195   
196    // Make sure the log has been sent to the logback appender
197  1 Assert.assertEquals("[test] after pop", this.listAppender.list.get(1).getMessage());
198    }
199   
 
200  1 toggle @Test
201    public void testGetSetLoggerLevel()
202    {
203  1 Assert.assertNull(this.loggerManager.getLoggerLevel(getClass().getName()));
204   
205  1 LogQueue queue = new LogQueue();
206   
207  1 this.loggerManager.pushLogListener(new LogQueueListener("loglistenerid", queue));
208   
209  1 this.loggerManager.setLoggerLevel(getClass().getName(), LogLevel.WARN);
210   
211  1 this.logger.debug("[test] debug message 1");
212    // Provide information when the Assert fails
213  1 if (queue.size() > 0) {
214  0 Assert.fail("Should have contained no message but got [" + queue.peek().getFormattedMessage()
215    + "] instead (last message, there might be more)");
216    }
217  1 Assert.assertEquals(0, queue.size());
218   
219  1 this.loggerManager.setLoggerLevel(getClass().getName(), LogLevel.DEBUG);
220   
221  1 this.logger.debug("[test] debug message 2");
222  1 Assert.assertEquals(1, queue.size());
223    }
224   
 
225  1 toggle @Test
226    public void testGetLoggers()
227    {
228  1 this.loggerManager.getLoggers();
229    }
230   
 
231  1 toggle @Test
232    public void initializeWhenNoLogback() throws Exception
233    {
234    // Simulate that the Logging implementation is not Logback
235  1 DefaultLoggerManager spyLoggerManager = spy(this.loggerManager);
236  1 when(spyLoggerManager.getRootLogger()).thenReturn(null);
237   
238  1 spyLoggerManager.initialize();
239   
240  1 verify(this.mocker.getMockedLogger()).warn(
241    "Could not find any Logback root logger. All logging module advanced features will be disabled.");
242    }
243   
 
244  1 toggle @Test
245    public void getLoggerLevelWhenNoLogback() throws Exception
246    {
247    // Simulate that the Logging implementation is not Logback
248  1 DefaultLoggerManager spyLoggerManager = spy(this.loggerManager);
249  1 when(spyLoggerManager.getLoggerContext()).thenReturn(null);
250   
251  1 Assert.assertNull(spyLoggerManager.getLoggerLevel("whatever"));
252    }
253    }