001    /*
002     * $Id: JUnit3Runner.java,v 1.5 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.reflect.*;
024    
025    import junit.framework.TestCase;
026    
027    import org.slf4j.*;
028    
029    import patterntesting.runtime.util.ReflectionHelper;
030    
031    /**
032     * This is a helper class to be able to start setUp and tearDown methods
033     * (or maybe later other methods) separately. It is e.g. used by the
034     * SmokeTestAspect.
035     *
036     * @author oliver
037     * @since 1.0 (12.03.2010)
038     */
039    public final class JUnit3Runner {
040    
041        private static final Logger log = LoggerFactory.getLogger(JUnit3Runner.class);
042    
043        /** This is a static utility class (no need to instantiate it). */
044        private JUnit3Runner() {}
045    
046        /**
047         * The setUp method of the give JUnit test case is called here.
048         *
049         * @since 1.0
050         * @param tc the JUnit test case
051         */
052        public static void setUp(final TestCase tc) {
053            invoke(tc, "setUp");
054        }
055    
056        /**
057         * The tearDown method of the give JUnit test case is called here.
058         *
059         * @since 1.0
060         * @param tc the JUnit test case
061         */
062        public static void tearDown(final TestCase tc) {
063            invoke(tc, "tearDown");
064        }
065    
066        private static void invoke(final TestCase tc, final String name) {
067            try {
068                Method method = ReflectionHelper.getMethod(tc.getClass(), name);
069                method.setAccessible(true);
070                method.invoke(tc);
071            } catch (NoSuchMethodException e) {
072                if (log.isDebugEnabled()) {
073                    log.debug("no " + name + " method inside " + tc, e);
074                }
075            } catch (IllegalAccessException e) {
076                throw new RuntimeException("can't access " + name + "()", e);
077            } catch (InvocationTargetException e) {
078                throw new RuntimeException(name + "() failed", e);
079            }
080        }
081    
082    }
083