001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.helpers;
026
027import static org.junit.Assert.assertTrue;
028import static org.junit.Assert.fail;
029
030import java.lang.reflect.InvocationHandler;
031import java.lang.reflect.InvocationTargetException;
032import java.lang.reflect.Method;
033import java.lang.reflect.Proxy;
034import java.util.Arrays;
035import java.util.HashSet;
036import java.util.Set;
037
038import org.junit.Test;
039import org.slf4j.Logger;
040import org.slf4j.event.EventRecodingLogger;
041
042/**
043 * @author Chetan Mehrotra
044 * @author Ceki Gülcü
045 */
046public class SubstitutableLoggerTest {
047
048    // NOTE: previous implementations of this class performed a hand crafted conversion of 
049    // a method to a string. In this implementation we just invoke method.toString().
050    
051    // WARNING: if you need to add an excluded method to have tests pass, ask yourself whether you
052    // forgot to implement the said method with delegation in SubstituteLogger. You probably did.
053    private static final Set<String> EXCLUDED_METHODS = new HashSet<>(
054            Arrays.asList("getName"));
055
056    
057    /**
058     * Test that all SubstituteLogger methods invoke the delegate, except for explicitly excluded  methods.
059     */
060    @Test
061    public void delegateIsInvokedTest() throws Exception {
062        SubstituteLogger substituteLogger = new SubstituteLogger("foo", null, false);
063        assertTrue(substituteLogger.delegate() instanceof EventRecodingLogger);
064
065        Set<String> expectedMethodSignatures = determineMethodSignatures(Logger.class);
066        LoggerInvocationHandler loggerInvocationHandler = new LoggerInvocationHandler();
067        Logger proxyLogger = (Logger) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Logger.class }, loggerInvocationHandler);
068        substituteLogger.setDelegate(proxyLogger);
069
070        invokeAllMethodsOf(substituteLogger);
071
072        // Assert that all methods are delegated
073        expectedMethodSignatures.removeAll(loggerInvocationHandler.getInvokedMethodSignatures());
074        if (!expectedMethodSignatures.isEmpty()) {
075            fail("Following methods are not delegated " + expectedMethodSignatures.toString());
076        }
077    }
078
079    private void invokeAllMethodsOf(Logger logger) throws InvocationTargetException, IllegalAccessException {
080        for (Method m : Logger.class.getDeclaredMethods()) {
081            if (!EXCLUDED_METHODS.contains(m.getName())) {
082                m.invoke(logger, new Object[m.getParameterTypes().length]);
083            }
084        }
085    }
086
087    private static Set<String> determineMethodSignatures(Class<Logger> loggerClass) {
088        Set<String> methodSignatures = new HashSet<>();
089        // Note: Class.getDeclaredMethods() does not include inherited methods
090        for (Method m : loggerClass.getDeclaredMethods()) {
091            if (!EXCLUDED_METHODS.contains(m.getName())) {
092                methodSignatures.add(m.toString());
093            }
094        }
095        return methodSignatures;
096    }
097
098    
099    // implements InvocationHandler 
100    private class LoggerInvocationHandler implements InvocationHandler {
101        private final Set<String> invokedMethodSignatures = new HashSet<>();
102
103        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
104            invokedMethodSignatures.add(method.toString());
105            if (method.getName().startsWith("is")) {
106                return true;
107            }
108            return null;
109        }
110
111        public Set<String> getInvokedMethodSignatures() {
112            return invokedMethodSignatures;
113        }
114    }
115}