001    /*
002     * $Id: JUnit4Runner.java,v 1.4 2011/07/29 15:09:42 oboehm Exp $
003     *
004     * Copyright (c) 2010 by Oliver Boehm
005     *
006     * Licensed under the Apache License, Version 2.0 (the "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     *
018     * (c)reated 12.03.2010 by oliver (ob@oasd.de)
019     */
020    
021    package patterntesting.check.runtime.junit;
022    
023    import java.lang.annotation.Annotation;
024    import java.lang.reflect.*;
025    
026    import org.junit.*;
027    import org.slf4j.*;
028    
029    
030    /**
031     * This is a helper class to be able to start setUp and tearDown methods
032     * (or maybe later other methods) separately. It is e.g. used by the
033     * SmokeTestAspect.
034     *
035     * @author oliver
036     * @since 1.0 (12.03.2010)
037     */
038    public final class JUnit4Runner {
039    
040        private static final Logger log = LoggerFactory.getLogger(JUnit4Runner.class);
041    
042        /** This is a static utility class (no need to instantiate it). */
043        private JUnit4Runner() {}
044    
045        /**
046         * The setUp method of the give JUnit test case is called here.
047         *
048         * @since 1.0
049         * @param tc the JUnit test case
050         */
051        public static void setUp(final Object tc) {
052            invoke(tc, Before.class);
053        }
054    
055        /**
056         * The tearDown method of the give JUnit test case is called here.
057         *
058         * @since 1.0
059         * @param tc the JUnit test case
060         */
061        public static void tearDown(final Object tc) {
062            invoke(tc, After.class);
063        }
064    
065        private static void invoke(final Object tc,
066                final Class<? extends Annotation> annotationClass) {
067            Method[] methods = tc.getClass().getMethods();
068            for (int i = 0; i < methods.length; i++) {
069                Method method = methods[i];
070                if (method.getAnnotation(annotationClass) != null) {
071                    try {
072                        method.invoke(tc);
073                    } catch (IllegalAccessException e) {
074                        throw new RuntimeException("can't access " + method, e);
075                    } catch (InvocationTargetException e) {
076                        throw new RuntimeException(method + " failed", e);
077                    }
078                    return;
079                }
080            }
081            if (log.isDebugEnabled()) {
082                log.debug("no @" + annotationClass.getSimpleName()
083                        + " method inside " + tc);
084            }
085        }
086    
087    }
088