This commit is contained in:
Phillip Webb
2014-12-02 15:19:42 -08:00
parent 0a8bc482fa
commit 447c9ff204
7 changed files with 48 additions and 39 deletions

View File

@@ -62,6 +62,16 @@ public class CommandException extends RuntimeException {
this.options = asEnumSet(options);
}
/**
* Create a new {@link CommandException} with the specified options.
* @param cause the underlying cause
* @param options the exception options
*/
public CommandException(Throwable cause, Option... options) {
super(cause);
this.options = asEnumSet(options);
}
private EnumSet<Option> asEnumSet(Option[] options) {
if (options == null || options.length == 0) {
return EnumSet.noneOf(Option.class);
@@ -100,6 +110,7 @@ public class CommandException extends RuntimeException {
* Re-throw the exception rather than dealing with it.
*/
RETHROW
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 original authors
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,16 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.cli.command.test;
import org.springframework.boot.cli.command.CommandException;
/**
* Thrown when tests fail to execute
* Thrown when tests fail to execute.
*
* @author Graeme Rocher
* @since 1.2
* @since 1.2.0
*/
public class TestFailedException extends RuntimeException {
public TestFailedException(Throwable cause) {
super(cause);
}
public class TestFailedException extends CommandException {
public TestFailedException(Throwable cause) {
super(cause, Option.HIDE_MESSAGE);
}
}

View File

@@ -17,18 +17,19 @@
package org.springframework.boot.cli.command.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.cli.compiler.GroovyCompiler;
import org.springframework.boot.groovy.DelegateTestRunner;
import org.springframework.util.ReflectionUtils;
/**
* Compile and run groovy based tests.
*
* @author Phillip Webb
* @author Graeme Rocher
*/
public class TestRunner {
@@ -63,17 +64,16 @@ public class TestRunner {
runThread.start();
runThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
// rethrow exception
threadException = e;
public void uncaughtException(Thread t, Throwable ex) {
TestRunner.this.threadException = ex;
}
});
runThread.join();
if(threadException != null) {
Throwable t = threadException;
threadException = null;
throw new TestFailedException(t);
if (this.threadException != null) {
TestFailedException ex = new TestFailedException(this.threadException);
this.threadException = null;
throw ex;
}
}
@@ -150,30 +150,22 @@ public class TestRunner {
ClassLoader contextClassLoader = Thread.currentThread()
.getContextClassLoader();
Class<?> delegateClass = contextClassLoader
.loadClass(DelegateTestRunner.class.getName());
Class resultClass = contextClassLoader.loadClass("org.junit.runner.Result");
Method runMethod = delegateClass.getMethod("run", Class[].class, resultClass);
.loadClass(DelegateTestRunner.class.getName());
Class<?> resultClass = contextClassLoader
.loadClass("org.junit.runner.Result");
Method runMethod = delegateClass.getMethod("run", Class[].class,
resultClass);
Object result = resultClass.newInstance();
runMethod.invoke(null, this.testClasses, result);
Boolean wasSuccessful = (Boolean)resultClass.getMethod("wasSuccessful").invoke(result);
if(!wasSuccessful) {
try {
throw new RuntimeException("Tests Failed.");
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
boolean wasSuccessful = (Boolean) resultClass.getMethod(
"wasSuccessful").invoke(result);
if (!wasSuccessful) {
throw new RuntimeException("Tests Failed.");
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("Exception occurred running tests: " + e.getMessage(), e);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Exception occurred running tests: " + e.getMessage(), e);
} catch (InstantiationException e) {
throw new RuntimeException("Exception occurred running tests: " + e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Exception occurred running tests: " + e.getMessage(), e);
} catch (InvocationTargetException e) {
throw new RuntimeException("Exception occurred running tests: " + e.getMessage(), e);
}
catch (Exception ex) {
ReflectionUtils.rethrowRuntimeException(ex);
}
}
}