1 /** 2 * 3 */ 4 package junit.framework; 5 6 import java.util.ArrayList; 7 import java.util.Arrays; 8 import java.util.HashMap; 9 import java.util.List; 10 11 import org.junit.runner.Description; 12 import org.junit.runner.notification.Failure; 13 import org.junit.runner.notification.RunListener; 14 import org.junit.runner.notification.RunNotifier; 15 16 public class JUnit4TestAdapterCache extends HashMap<Description, Test> { 17 private static final long serialVersionUID = 1L; 18 private static final JUnit4TestAdapterCache fInstance = new JUnit4TestAdapterCache(); 19 getDefault()20 public static JUnit4TestAdapterCache getDefault() { 21 return fInstance; 22 } 23 asTest(Description description)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 createTest(Description description)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 getNotifier(final TestResult result, final JUnit4TestAdapter adapter)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 asTestList(Description description)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 }