patterntesting.check.runtime.junit.JUnit4Runner 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: JUnit4Runner.java,v 1.3 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.annotation.Annotation;
import java.lang.reflect.*;
import org.apache.commons.logging.*;
import org.junit.*;
/**
* 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 JUnit4Runner {
private static final Log log = LogFactory.getLog(JUnit4Runner.class);
/** This is a static utility class (no need to instantiate it). */
private JUnit4Runner() {}
/**
* 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 Object tc) {
invoke(tc, Before.class);
}
/**
* 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 Object tc) {
invoke(tc, After.class);
}
private static void invoke(final Object tc,
final Class extends Annotation> annotationClass) {
Method[] methods = tc.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.getAnnotation(annotationClass) != null) {
try {
method.invoke(tc);
} catch (IllegalAccessException e) {
throw new RuntimeException("can't access " + method, e);
} catch (InvocationTargetException e) {
throw new RuntimeException(method + " failed", e);
}
return;
}
}
if (log.isDebugEnabled()) {
log.debug("no @" + annotationClass.getSimpleName()
+ " method inside " + tc);
}
}
}