Skip to content

Commit

Permalink
Merge pull request #8 from manhole/junit_4_11_compatibility
Browse files Browse the repository at this point in the history
[S2JUnit4] JUnit4.4と4.11の両方で動作するようにしました。
  • Loading branch information
koichik committed Nov 29, 2014
2 parents 6ae23c8 + 8c4ef19 commit a03f934
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.seasar.framework.unit;

import java.lang.reflect.Method;

import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.seasar.framework.beans.BeanDesc;
import org.seasar.framework.beans.factory.BeanDescFactory;
import org.seasar.framework.util.MethodUtil;

/**
* org.junit.runner.manipulation.Filter クラスのシグニチャ変更に対応するクラスです。
*
* JUnit 4.4: public void apply(Runner)
* ↓
* JUnit 4.5: public void apply(Object)
*
*
* @author manhole
*/
public class FilterCompatibility {

@SuppressWarnings("unchecked")
private static final Class[] APPLY_PARAM_TYPES = new Class[] { Object.class };

/**
* Filter#apply(filterable)メソッドを実行します。
*
* @param filter
* フィルタ
* @param filterable
* フィルタ対象
* @throws NoTestsRemainException
* フィルタ処理によって全てのテストが除外された場合
*/
public static void apply(final Filter filter, final Runner filterable)
throws NoTestsRemainException {
try {
filter.apply(filterable);
} catch (final NoSuchMethodError ignore) {
// JUnit 4.5 or higher
apply45(filter, filterable);
}
}

private static void apply45(final Filter filter, final Object filterable) {
final BeanDesc beanDesc = BeanDescFactory
.getBeanDesc(filter.getClass());
final Method method = beanDesc.getMethod("apply", APPLY_PARAM_TYPES);
MethodUtil.invoke(method, filter, new Object[] { filterable });
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.seasar.framework.unit;

import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

/**
* org.junit.runner.notification.RunNotifier クラスの変更に対応するクラスです。
*
* @author manhole
*/
public class RunNotifierCompatibility {

/**
* RunNotifier#testAborted メソッドと同等の処理をします。
* (このメソッドはJUnit4.5で廃止されました)
*
* @param notifier
* ノティフィアー
* @param description
* テストのディスクリプション
* @param e
* 発生した例外
*/
public static void testAborted(final RunNotifier notifier,
final Description description, final Throwable e) {
notifier.fireTestStarted(description);
notifier.fireTestFailure(new Failure(description, e));
notifier.fireTestFinished(description);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.junit.internal.runners.CompositeRunner;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.manipulation.Sortable;
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Parameterized.Parameters;

Expand Down Expand Up @@ -82,12 +93,88 @@ public void run(final RunNotifier notifier) {

}

/*
* copy from: org.junit.internal.runners.CompositeRunner @JUnit4.4
*
* JUnit license: Eclipse Public License - v 1.0
* https://github.com/junit-team/junit/blob/master/LICENSE-junit.txt
*/
static class MyCompositeRunner extends Runner implements Filterable,
Sortable {

private final List<Runner> fRunners = new ArrayList<Runner>();

private final String fName;

public MyCompositeRunner(final String name) {
fName = name;
}

@Override
public void run(final RunNotifier notifier) {
runChildren(notifier);
}

protected void runChildren(final RunNotifier notifier) {
for (final Runner each : fRunners)
each.run(notifier);
}

@Override
public Description getDescription() {
final Description spec = Description.createSuiteDescription(fName);
for (final Runner runner : fRunners)
spec.addChild(runner.getDescription());
return spec;
}

public List<Runner> getRunners() {
return fRunners;
}

public void addAll(final List<? extends Runner> runners) {
fRunners.addAll(runners);
}

public void add(final Runner runner) {
fRunners.add(runner);
}

public void filter(final Filter filter) throws NoTestsRemainException {
for (final Iterator<Runner> iter = fRunners.iterator(); iter
.hasNext();) {
final Runner runner = iter.next();
if (filter.shouldRun(runner.getDescription()))
filter.apply(runner);
else
iter.remove();
}
}

protected String getName() {
return fName;
}

public void sort(final Sorter sorter) {
Collections.sort(fRunners, new Comparator<Runner>() {

public int compare(final Runner o1, final Runner o2) {
return sorter.compare(o1.getDescription(),
o2.getDescription());
}
});
for (final Runner each : fRunners) {
sorter.apply(each);
}
}
}

/**
* {@link Parameters}が注釈されたすべてのメソッドを実行するランナーです。
*
* @author taedium
*/
public static class RunAllParameterMethods extends CompositeRunner {
public static class RunAllParameterMethods extends MyCompositeRunner {

private final Class<?> klass;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public void run(final RunNotifier notifier) {
*/
protected void runMethods(final RunNotifier notifier) {
if (testMethods.isEmpty()) {
notifier.testAborted(getDescription(), new Exception(
"No runnable methods"));
RunNotifierCompatibility.testAborted(notifier,
getDescription(), new Exception("No runnable methods"));
}
for (final Method method : testMethods) {
invokeTestMethod(method, notifier);
Expand Down Expand Up @@ -228,10 +228,12 @@ protected void invokeTestMethod(final Method method,
try {
test = createTest();
} catch (final InvocationTargetException e) {
notifier.testAborted(methodDescription(method), e.getCause());
RunNotifierCompatibility.testAborted(notifier,
methodDescription(method), e.getCause());
return;
} catch (final Exception e) {
notifier.testAborted(methodDescription(method), e);
RunNotifierCompatibility.testAborted(notifier,
methodDescription(method), e);
return;
}
createMethodRunner(test, method, notifier).run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public Description getDescription() {
}

public void filter(Filter filter) throws NoTestsRemainException {
filter.apply(delegate);
FilterCompatibility.apply(filter, delegate);
}

public void sort(Sorter sorter) {
sorter.apply(delegate);
SorterCompatibility.apply(sorter, delegate);
}
}
4 changes: 2 additions & 2 deletions s2-tiger/src/main/java/org/seasar/framework/unit/Seasar2.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ public void run(final RunNotifier notifier) {
}

public void filter(final Filter filter) throws NoTestsRemainException {
filter.apply(delegate);
FilterCompatibility.apply(filter, delegate);
}

public void sort(final Sorter sorter) {
sorter.apply(delegate);
SorterCompatibility.apply(sorter, delegate);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.seasar.framework.unit;

import java.lang.reflect.Method;

import org.junit.runner.Runner;
import org.junit.runner.manipulation.Sorter;
import org.seasar.framework.beans.BeanDesc;
import org.seasar.framework.beans.factory.BeanDescFactory;
import org.seasar.framework.util.MethodUtil;

/**
* org.junit.runner.manipulation.Sorter クラスのシグニチャ変更に対応するクラスです。
*
* JUnit 4.4: public void apply(Runner)
* ↓
* JUnit 4.5: public void apply(Object)
*
*
* @author manhole
*/
public class SorterCompatibility {

@SuppressWarnings("unchecked")
private static final Class[] APPLY_PARAM_TYPES = new Class[] { Object.class };

/**
* Sorter#apply(sortable)メソッドを実行します。
*
* @param sorter
* ソーター
* @param sortable
* ソート対象
*/
public static void apply(final Sorter sorter, final Runner sortable) {
try {
sorter.apply(sortable);
} catch (final NoSuchMethodError ignore) {
// JUnit 4.5 or higher
apply45(sorter, sortable);
}
}

private static void apply45(final Sorter sorter, final Object sortable) {
final BeanDesc beanDesc = BeanDescFactory
.getBeanDesc(sorter.getClass());
final Method method = beanDesc.getMethod("apply", APPLY_PARAM_TYPES);
MethodUtil.invoke(method, sorter, new Object[] { sortable });
}

}

0 comments on commit a03f934

Please sign in to comment.