Home | History | Annotate | Download | only in helpers
      1 /**
      2  * Copyright (c) 2004-2011 QOS.ch
      3  * All rights reserved.
      4  *
      5  * Permission is hereby granted, free  of charge, to any person obtaining
      6  * a  copy  of this  software  and  associated  documentation files  (the
      7  * "Software"), to  deal in  the Software without  restriction, including
      8  * without limitation  the rights to  use, copy, modify,  merge, publish,
      9  * distribute,  sublicense, and/or sell  copies of  the Software,  and to
     10  * permit persons to whom the Software  is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The  above  copyright  notice  and  this permission  notice  shall  be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
     17  * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
     18  * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  */
     25 package org.slf4j.helpers;
     26 
     27 import java.lang.reflect.InvocationHandler;
     28 import java.lang.reflect.InvocationTargetException;
     29 import java.lang.reflect.Method;
     30 import java.lang.reflect.Proxy;
     31 import java.util.ArrayList;
     32 import java.util.Arrays;
     33 import java.util.HashSet;
     34 import java.util.List;
     35 import java.util.Set;
     36 
     37 import junit.framework.TestCase;
     38 import org.slf4j.Logger;
     39 
     40 /**
     41  * @author Chetan Mehrotra
     42  */
     43 public class SubstitutableLoggerTest extends TestCase {
     44     private static final Set<String> EXCLUDED_METHODS = new HashSet<String>(Arrays.asList("getName"));
     45 
     46     public void testDelegate() throws Exception {
     47         SubstituteLogger log = new SubstituteLogger("foo");
     48         assertTrue(log.delegate() instanceof NOPLogger);
     49 
     50         Set<String> expectedMethodSignatures = determineMethodSignatures(Logger.class);
     51         LoggerInvocationHandler ih = new LoggerInvocationHandler();
     52         Logger proxyLogger = (Logger) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Logger.class }, ih);
     53         log.setDelegate(proxyLogger);
     54 
     55         invokeMethods(log);
     56 
     57         // Assert that all methods are delegated
     58         expectedMethodSignatures.removeAll(ih.getInvokedMethodSignatures());
     59         if (!expectedMethodSignatures.isEmpty()) {
     60             fail("Following methods are not delegated " + expectedMethodSignatures.toString());
     61         }
     62     }
     63 
     64     private void invokeMethods(Logger proxyLogger) throws InvocationTargetException, IllegalAccessException {
     65         for (Method m : Logger.class.getDeclaredMethods()) {
     66             if (!EXCLUDED_METHODS.contains(m.getName())) {
     67                 m.invoke(proxyLogger, new Object[m.getParameterTypes().length]);
     68             }
     69         }
     70     }
     71 
     72     private class LoggerInvocationHandler implements InvocationHandler {
     73         private final Set<String> invokedMethodSignatures = new HashSet<String>();
     74 
     75         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
     76             invokedMethodSignatures.add(getMethodSignature(method));
     77             if (method.getName().startsWith("is")) {
     78                 return true;
     79             }
     80             return null;
     81         }
     82 
     83         public Set<String> getInvokedMethodSignatures() {
     84             return invokedMethodSignatures;
     85         }
     86     }
     87 
     88     private static Set<String> determineMethodSignatures(Class<Logger> loggerClass) {
     89         Set<String> methodSignatures = new HashSet<String>();
     90         for (Method m : loggerClass.getDeclaredMethods()) {
     91             if (!EXCLUDED_METHODS.contains(m.getName())) {
     92                 methodSignatures.add(getMethodSignature(m));
     93             }
     94         }
     95         return methodSignatures;
     96     }
     97 
     98     private static String getMethodSignature(Method m) {
     99         List<String> result = new ArrayList<String>();
    100         result.add(m.getName());
    101         for (Class<?> clazz : m.getParameterTypes()) {
    102             result.add(clazz.getSimpleName());
    103         }
    104         return result.toString();
    105     }
    106 }
    107