1 package junit.framework; 2 3 import java.util.List; 4 5 import org.junit.Ignore; 6 import org.junit.runner.Describable; 7 import org.junit.runner.Description; 8 import org.junit.runner.Request; 9 import org.junit.runner.Runner; 10 import org.junit.runner.manipulation.Filter; 11 import org.junit.runner.manipulation.Filterable; 12 import org.junit.runner.manipulation.NoTestsRemainException; 13 import org.junit.runner.manipulation.Sortable; 14 import org.junit.runner.manipulation.Sorter; 15 16 public class JUnit4TestAdapter implements Test, Filterable, Sortable, Describable { 17 private final Class<?> fNewTestClass; 18 19 private final Runner fRunner; 20 21 private final JUnit4TestAdapterCache fCache; 22 JUnit4TestAdapter(Class<?> newTestClass)23 public JUnit4TestAdapter(Class<?> newTestClass) { 24 this(newTestClass, JUnit4TestAdapterCache.getDefault()); 25 } 26 JUnit4TestAdapter(final Class<?> newTestClass, JUnit4TestAdapterCache cache)27 public JUnit4TestAdapter(final Class<?> newTestClass, JUnit4TestAdapterCache cache) { 28 fCache = cache; 29 fNewTestClass = newTestClass; 30 fRunner = Request.classWithoutSuiteMethod(newTestClass).getRunner(); 31 } 32 countTestCases()33 public int countTestCases() { 34 return fRunner.testCount(); 35 } 36 run(TestResult result)37 public void run(TestResult result) { 38 fRunner.run(fCache.getNotifier(result, this)); 39 } 40 41 // reflective interface for Eclipse getTests()42 public List<Test> getTests() { 43 return fCache.asTestList(getDescription()); 44 } 45 46 // reflective interface for Eclipse getTestClass()47 public Class<?> getTestClass() { 48 return fNewTestClass; 49 } 50 getDescription()51 public Description getDescription() { 52 Description description = fRunner.getDescription(); 53 return removeIgnored(description); 54 } 55 removeIgnored(Description description)56 private Description removeIgnored(Description description) { 57 if (isIgnored(description)) { 58 return Description.EMPTY; 59 } 60 Description result = description.childlessCopy(); 61 for (Description each : description.getChildren()) { 62 Description child = removeIgnored(each); 63 if (!child.isEmpty()) { 64 result.addChild(child); 65 } 66 } 67 return result; 68 } 69 isIgnored(Description description)70 private boolean isIgnored(Description description) { 71 return description.getAnnotation(Ignore.class) != null; 72 } 73 74 @Override toString()75 public String toString() { 76 return fNewTestClass.getName(); 77 } 78 filter(Filter filter)79 public void filter(Filter filter) throws NoTestsRemainException { 80 filter.apply(fRunner); 81 } 82 sort(Sorter sorter)83 public void sort(Sorter sorter) { 84 sorter.apply(fRunner); 85 } 86 }