patterntesting.check.runtime.junit.JUnit3Runner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-check-rt Show documentation
Show all versions of patterntesting-check-rt Show documentation
PatternTesting Check.RT (patterntesting-check-rt) provides different runtime
checks of known anti patterns (like using null values as arguments or
return values) but provides also a test framework for better testing.
/*
* $Id: JUnit3Runner.java,v 1.4 2010/06/08 21:10:12 oboehm Exp $
*
* Copyright (c) 2010 by Oliver Boehm
*
* 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 orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 12.03.2010 by oliver ([email protected])
*/
package patterntesting.check.runtime.junit;
import java.lang.reflect.*;
import org.apache.commons.logging.*;
import patterntesting.runtime.util.ReflectionHelper;
import junit.framework.TestCase;
/**
* This is a helper class to be able to start setUp and tearDown methods
* (or maybe later other methods) separately. It is e.g. used by the
* SmokeTestAspect.
*
* @author oliver
* @since 1.0 (12.03.2010)
*/
public final class JUnit3Runner {
private static final Log log = LogFactory.getLog(JUnit3Runner.class);
/** This is a static utility class (no need to instantiate it). */
private JUnit3Runner() {}
/**
* The setUp method of the give JUnit test case is called here.
*
* @since 1.0
* @param tc the JUnit test case
*/
public static void setUp(final TestCase tc) {
invoke(tc, "setUp");
}
/**
* The tearDown method of the give JUnit test case is called here.
*
* @since 1.0
* @param tc the JUnit test case
*/
public static void tearDown(final TestCase tc) {
invoke(tc, "tearDown");
}
private static void invoke(final TestCase tc, final String name) {
try {
Method method = ReflectionHelper.getMethod(tc.getClass(), name);
method.setAccessible(true);
method.invoke(tc);
} catch (NoSuchMethodException e) {
if (log.isDebugEnabled()) {
log.debug("no " + name + " method inside " + tc, e);
}
} catch (IllegalAccessException e) {
throw new RuntimeException("can't access " + name + "()", e);
} catch (InvocationTargetException e) {
throw new RuntimeException(name + "() failed", e);
}
}
}