/*
 * Copyright (c) 2014 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.qiweb.test.util;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import java.util.ArrayList;
import java.util.List;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.slf4j.LoggerFactory;

/**
 * SLF4J JUnit Rule for use with Logback.
 */
// Inspired by https://gist.github.com/tux2323/1005996
public class Slf4jRule
    extends TestWatcher
{
    public enum Level
    {
        TRACE( ch.qos.logback.classic.Level.TRACE ),
        DEBUG( ch.qos.logback.classic.Level.DEBUG ),
        INFO( ch.qos.logback.classic.Level.INFO ),
        WARN( ch.qos.logback.classic.Level.WARN ),
        ERROR( ch.qos.logback.classic.Level.ERROR );

        private final ch.qos.logback.classic.Level internalLevel;

        private Level( ch.qos.logback.classic.Level level )
        {
            this.internalLevel = level;
        }
    }

    private final ListAppender<ILoggingEvent> appender = new ListAppender<>();
    private final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    private final List<String> sources = new ArrayList<>();
    private Level level = Level.TRACE;

    @Override
    protected void starting( Description description )
    {
        resetContext();
        sources.stream().forEach( source -> addAppenderToName( source ) );
        appender.start();
    }

    @Override
    protected void finished( Description description )
    {
        appender.stop();
        resetContext();
    }

    public void record( Level level )
    {
        this.level = level;
    }

    public void recordForObject( Object obj )
    {
        recordForType( obj.getClass() );
    }

    public void recordForType( Class<?> type )
    {
        recordForName( type.getName() );
    }

    public void recordForPackage( Package pack )
    {
        recordForName( pack.getName() );
    }

    public void recordForName( String name )
    {
        sources.add( name );
        addAppenderToName( name );
    }

    public boolean contains( String loggingStatement )
    {
        return appender.list.stream().anyMatch( event -> event.getFormattedMessage().contains( loggingStatement ) );
    }

    public int size()
    {
        return appender.list.size();
    }

    private <T> void addAppenderToName( String name )
    {
        Logger logger = (Logger) LoggerFactory.getLogger( name );
        logger.addAppender( appender );
        logger.setLevel( level.internalLevel );
    }

    private void resetContext()
    {
        context.reset();
    }
}

@@ -773,16 +773,67 @@
 gEvent;%0A
+import ch.qos.logback.classic.spi.IThrowableProxy;%0A
 import c
@@ -915,20 +915,69 @@
 va.util.
-List
+Arrays;%0Aimport java.util.List;%0Aimport java.util.Stack
 ;%0Aimport
@@ -3006,32 +3006,1156 @@
 ent ) );%0A    %7D%0A%0A
+    public boolean containsExMessage( String exceptionMessage )%0A    %7B%0A        return appender.list.stream().anyMatch(%0A            event -%3E%0A            %7B%0A                IThrowableProxy ex = event.getThrowableProxy();%0A                if( ex == null )%0A                %7B%0A                    return false;%0A                %7D%0A                if( ex.getMessage().contains( exceptionMessage ) )%0A                %7B%0A                    return true;%0A                %7D%0A                Stack%3CIThrowableProxy%3E stack = new Stack%3C%3E();%0A                stack.add( ex.getCause() );%0A                stack.addAll( Arrays.asList( ex.getSuppressed() ) );%0A                while( !stack.empty() )%0A                %7B%0A                    IThrowableProxy candidate = stack.pop();%0A                    if( candidate.getMessage().contains( exceptionMessage ) )%0A                    %7B%0A                        return true;%0A                    %7D%0A                    stack.add( candidate.getCause() );%0A                    stack.addAll( Arrays.asList( candidate.getSuppressed() ) );%0A                %7D%0A                return false;%0A            %7D%0A        );%0A    %7D%0A%0A
     public int s

Test Support: enhance Slf4jRule with containsExMessage() predicate