Skip to content

Commit

Permalink
#782: Polished
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Dec 5, 2013
1 parent 383e935 commit c7b7b62
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 13 deletions.
30 changes: 17 additions & 13 deletions hawtio-maven-plugin/src/main/java/io/hawt/maven/TestMojo.java
Expand Up @@ -62,6 +62,10 @@ protected MojoLifecycle createMojoLifecycle() {
@Override
protected void doPrepareArguments() throws Exception {
bootstrapMain = false;

// the main class is our unit test class
mainClass = className;

super.doPrepareArguments();
}

Expand All @@ -75,16 +79,18 @@ protected void addCustomClasspaths(Set<URL> classpathURLs, boolean first) throws
@Override
protected void afterBootstrapMain() throws Exception {
// must load class and methods using reflection as otherwise we have class-loader/compiled class issues
getLog().info("Starting " + className + "...");
getLog().info("*************************************");
getLog().info("Testing: " + className);
getLog().info("*************************************");

Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
Object instance = ReflectionHelper.newInstance(clazz);
getLog().debug("Loaded " + className + " and instantiated " + instance);

Method method = clazz.getMethod("setUp");
ReflectionHelper.invokeMethod(method, instance);
getLog().debug("setUp() invoked");
Method beforeClass = jUnitService.findBeforeClass(clazz);
ReflectionHelper.invokeMethod(beforeClass, instance);
Method before = jUnitService.findBefore(clazz);
ReflectionHelper.invokeMethod(before, instance);

// loop all test methods
List<Method> testMethods = jUnitService.findTestMethods(clazz);
Expand All @@ -96,19 +102,17 @@ protected void afterBootstrapMain() throws Exception {
ReflectionHelper.invokeMethod(testMethod, instance);
}

getLog().info("... press ENTER to tear down tests from " + className);
getLog().info("*************************************");
getLog().info(" Press ENTER to exit ");
getLog().info("*************************************");
Console console = System.console();
console.readLine();

try {
method = clazz.getMethod("tearDown");
ReflectionHelper.invokeMethod(method, instance);
getLog().debug("tearDown() invoked");
method = clazz.getMethod("tearDownAfterClass");
ReflectionHelper.invokeMethod(method, null);
getLog().debug("tearDownAfterClass() invoked");

getLog().info("*************************************");
Method after = jUnitService.findAfter(clazz);
ReflectionHelper.invokeMethod(after, instance);
Method afterClass = jUnitService.findAfterClass(clazz);
ReflectionHelper.invokeMethod(afterClass, instance);
} finally {
// signal we are done
latch.countDown();
Expand Down
Expand Up @@ -72,7 +72,68 @@ public List<Method> filterTestMethods(List<Method> methods, String filter) {
}

return result;
}

@Override
public Method findBefore(Class clazz) throws Exception {
// first find annotations
Class ann = clazz.getClassLoader().loadClass("org.junit.Before");
List<Method> annotations = findMethodsWithAnnotation(clazz, ann, false);
if (!annotations.isEmpty()) {
return annotations.get(0);
}

// if no annotations then assume setUp
List<Method> names = findMethodsWithName(clazz, "setUp");
if (names.isEmpty()) {
return names.get(0);
}

return null;
}

@Override
public Method findBeforeClass(Class clazz) throws Exception {
// first find annotations
Class ann = clazz.getClassLoader().loadClass("org.junit.BeforeClass");
List<Method> annotations = findMethodsWithAnnotation(clazz, ann, false);
if (!annotations.isEmpty()) {
return annotations.get(0);
}

// there is no naming convention for before class
return null;
}

@Override
public Method findAfter(Class clazz) throws Exception {
// first find annotations
Class ann = clazz.getClassLoader().loadClass("org.junit.After");
List<Method> annotations = findMethodsWithAnnotation(clazz, ann, false);
if (!annotations.isEmpty()) {
return annotations.get(0);
}

// if no annotations then assume setUp
List<Method> names = findMethodsWithName(clazz, "tearDown");
if (names.isEmpty()) {
return names.get(0);
}

return null;
}

@Override
public Method findAfterClass(Class clazz) throws Exception {
// first find annotations
Class ann = clazz.getClassLoader().loadClass("org.junit.AfterClass");
List<Method> annotations = findMethodsWithAnnotation(clazz, ann, false);
if (!annotations.isEmpty()) {
return annotations.get(0);
}

// there is no naming convention for after class
return null;
}

private static List<Method> findMethodsWithName(Class<?> type, String namePattern) {
Expand Down
Expand Up @@ -8,4 +8,12 @@ public interface JUnitService {
List<Method> findTestMethods(Class clazz) throws Exception;

List<Method> filterTestMethods(List<Method> methods, String filter);

Method findBefore(Class clazz) throws Exception;

Method findBeforeClass(Class clazz) throws Exception;

Method findAfter(Class clazz) throws Exception;

Method findAfterClass(Class clazz) throws Exception;
}
Expand Up @@ -29,6 +29,9 @@ public static <T> T newInstance(Class<T> type) {
* @return the result of the method invocation
*/
public static Object invokeMethod(Method method, Object instance, Object... parameters) {
if (method == null) {
return null;
}
try {
return method.invoke(instance, parameters);
} catch (IllegalAccessException e) {
Expand Down

0 comments on commit c7b7b62

Please sign in to comment.