1 package junit.framework; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.HashMap; 6 import java.util.List; 7 8 import org.junit.runner.Description; 9 import org.junit.runner.notification.Failure; 10 import org.junit.runner.notification.RunListener; 11 import org.junit.runner.notification.RunNotifier; 12 13 public class JUnit4TestAdapterCache extends HashMap<Description, Test> { 14 private static final long serialVersionUID = 1L; 15 private static final JUnit4TestAdapterCache fInstance = new JUnit4TestAdapterCache(); 16 getDefault()17 public static JUnit4TestAdapterCache getDefault() { 18 return fInstance; 19 } 20 asTest(Description description)21 public Test asTest(Description description) { 22 if (description.isSuite()) { 23 return createTest(description); 24 } else { 25 if (!containsKey(description)) { 26 put(description, createTest(description)); 27 } 28 return get(description); 29 } 30 } 31 createTest(Description description)32 Test createTest(Description description) { 33 if (description.isTest()) { 34 return new JUnit4TestCaseFacade(description); 35 } else { 36 TestSuite suite = new TestSuite(description.getDisplayName()); 37 for (Description child : description.getChildren()) { 38 suite.addTest(asTest(child)); 39 } 40 return suite; 41 } 42 } 43 getNotifier(final TestResult result, final JUnit4TestAdapter adapter)44 public RunNotifier getNotifier(final TestResult result, final JUnit4TestAdapter adapter) { 45 RunNotifier notifier = new RunNotifier(); 46 notifier.addListener(new RunListener() { 47 @Override 48 public void testFailure(Failure failure) throws Exception { 49 result.addError(asTest(failure.getDescription()), failure.getException()); 50 } 51 52 @Override 53 public void testFinished(Description description) throws Exception { 54 result.endTest(asTest(description)); 55 } 56 57 @Override 58 public void testStarted(Description description) throws Exception { 59 result.startTest(asTest(description)); 60 } 61 }); 62 return notifier; 63 } 64 asTestList(Description description)65 public List<Test> asTestList(Description description) { 66 if (description.isTest()) { 67 return Arrays.asList(asTest(description)); 68 } else { 69 List<Test> returnThis = new ArrayList<Test>(); 70 for (Description child : description.getChildren()) { 71 returnThis.add(asTest(child)); 72 } 73 return returnThis; 74 } 75 } 76 77 }