xapi.test.junit.JUnit4Runner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xapi-dev Show documentation
Show all versions of xapi-dev Show documentation
Everything needed to run a comprehensive dev environment.
Just type X_ and pick a service from autocomplete;
new dev modules will be added as they are built.
The only dev service not included in the uber jar is xapi-dev-maven,
as it includes all runtime dependencies of maven, adding ~4 seconds to build time,
and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).
The newest version!
package xapi.test.junit;
import static xapi.test.Assert.assertEquals;
import static xapi.test.Assert.assertFalse;
import static xapi.test.Assert.assertTrue;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Provider;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import xapi.annotation.compile.Resource;
import xapi.gwtc.api.Gwtc;
import xapi.util.X_Debug;
import com.google.gwt.core.shared.GWT;
@Gwtc(
includeSource="",
includeGwtXml=@Resource("xapi.X_Test")
)
public class JUnit4Runner {
public static void main(String [] args) {
}
private class Lifecycle {
Map after = newMap();
Map afterClass = newMap();
Map before = newMap();
Map beforeClass = newMap();
Map tests = newMap();
@SuppressWarnings("rawtypes")
public Lifecycle(Class testClass) {
Class init = testClass;
while (init != null && init != Object.class) {
try {
for (Method method : init.getDeclaredMethods()) {
if (method.getAnnotation(Test.class) != null) {
assertPublicZeroArgInstanceMethod(method, Test.class);
if (!tests.containsKey(method.getName()))
tests.put(method.getName(), method);
}
maybeAdd(method);
}
} catch (NoSuchMethodError ignored) {
debug("Class "+init+" is not enhanced", null);
} catch (Exception e) {
debug("Error getting declared methods for "+init, e);
}
init = init.getSuperclass();
}
}
public List after() {
return newList(after, true);
}
public List afterClass() {
return newList(afterClass, true);
}
public List before() {
return newList(before, true);
}
public List beforeClass() {
return newList(beforeClass, true);
}
public void maybeAdd(Method method) {
if (method.getAnnotation(Before.class) != null) {
assertPublicZeroArgInstanceMethod(method, Before.class);
if (!before.containsKey(method.getName()))
before.put(method.getName(), method);
}
if (method.getAnnotation(BeforeClass.class) != null) {
assertPublicZeroArgStaticMethod(method, BeforeClass.class);
if (!beforeClass.containsKey(method.getName()))
beforeClass.put(method.getName(), method);
}
if (method.getAnnotation(After.class) != null) {
assertPublicZeroArgInstanceMethod(method, After.class);
if (!after.containsKey(method.getName()))
after.put(method.getName(), method);
}
if (method.getAnnotation(AfterClass.class) != null) {
assertPublicZeroArgStaticMethod(method, AfterClass.class);
if (!afterClass.containsKey(method.getName()))
afterClass.put(method.getName(), method);
}
}
}
public static Method[] findTests(Class testClass) throws Throwable {
return new JUnit4Runner().findAnnotated(testClass);
}
public static void runTest(Provider