/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.logging.logback.internal;

import java.util.Arrays;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.logging.LogLevel;
import org.xwiki.logging.event.LogEvent;
import org.xwiki.observation.EventListener;
import org.xwiki.observation.ObservationManager;
import org.xwiki.observation.event.Event;
import org.xwiki.observation.internal.DefaultObservationManager;
import org.xwiki.test.ComponentManagerRule;
import org.xwiki.test.LogRule;
import org.xwiki.test.annotation.ComponentList;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

/**
 * Unit tests for {@link LogbackEventGenerator}.
 *
 * @version $Id$
 * @since 3.2M3
 */
@ComponentList({
    DefaultObservationManager.class,
    LogbackEventGenerator.class
})
public class LogbackEventGeneratorTest
{
    @Rule
    public final ComponentManagerRule componentManager = new ComponentManagerRule();

    @Rule
    public final LogRule logCapture = new LogRule();

    private Logger logger;

    private ObservationManager observationManager;

    @Before
    public void setUp() throws Exception
    {
        this.observationManager = this.componentManager.getInstance(ObservationManager.class);

        this.logger = LoggerFactory.getLogger(getClass());
    }

    /**
     * Verify that logging an error will generate a Log Event.
     */
    @Test
    public void verifyThatLoggingGeneratesALogEvent()
    {
        Event event = new LogEvent(null, LogLevel.INFO, "dummy", null, null);

        EventListener listener = mock(EventListener.class);
        when(listener.getName()).thenReturn("mylistener");
        when(listener.getEvents()).thenReturn(Arrays.asList(event));

        this.observationManager.addListener(listener);

        this.logger.error("error message");

        Event expected = new LogEvent(null, LogLevel.ERROR, "error message", null, null);
        verify(listener).onEvent(eq(expected), eq(getClass().getName()), eq(null));
    }

    @Test
    public void initializeWhenNoLogback() throws Exception
    {
        // Simulate that the Logging implementation is not Logback
        LogbackEventGenerator generator =
            (LogbackEventGenerator) this.componentManager.getInstance(EventListener.class, "LogbackEventGenerator");
        LogbackEventGenerator spyGenerator = spy(generator);
        when(spyGenerator.getRootLogger()).thenReturn(null);

        // Capture logs
        this.logCapture.recordLoggingForType(LogbackEventGenerator.class);

        spyGenerator.initialize();

        Assert.assertEquals(1, this.logCapture.size());
        Assert.assertEquals("Could not find any Logback root logger. The logging module won't be able to catch "
            + "logs.", this.logCapture.getMessage(0));
    }
}

@@ -1409,32 +1409,22 @@
 ki.test.
-ComponentManager
+AllLog
 Rule;%0Aim
@@ -1443,19 +1443,32 @@
 ki.test.
-Log
+ComponentManager
 Rule;%0Aim
@@ -1514,16 +1514,61 @@
 tList;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -1982,16 +1982,19 @@
 c final 
+All
 LogRule 
@@ -2010,16 +2010,19 @@
 e = new 
+All
 LogRule(
@@ -3014,24 +3014,94 @@
  eq(null));%0A
+        assertEquals(%22error message%22, this.logCapture.getMessage(0));%0A
     %7D%0A%0A    @
@@ -3457,16 +3457,16 @@
 rator);%0A
+
         
@@ -3523,108 +3523,8 @@
 );%0A%0A
-        // Capture logs%0A        this.logCapture.recordLoggingForType(LogbackEventGenerator.class);%0A%0A
     

Make the test don't output any log to the console