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