1 package org.junit.internal.requests; 2 3 import org.junit.Ignore; 4 import org.junit.internal.runners.InitializationError; 5 import org.junit.internal.runners.OldTestClassRunner; 6 import org.junit.internal.runners.TestClassRunner; 7 import org.junit.runner.Request; 8 import org.junit.runner.RunWith; 9 import org.junit.runner.Runner; 10 import org.junit.runners.AllTests; 11 12 public class ClassRequest extends Request { 13 private static final String CONSTRUCTOR_ERROR_FORMAT= "Custom runner class %s should have a public constructor with signature %s(Class testClass)"; 14 private final Class <?> fTestClass; 15 private boolean fCanUseSuiteMethod; 16 17 public ClassRequest(Class <?> testClass, boolean canUseSuiteMethod) { 18 fTestClass= testClass; 19 fCanUseSuiteMethod= canUseSuiteMethod; 20 } 21 22 public ClassRequest(Class <?> testClass) { 23 this(testClass, true); 24 } 25 26 @Override 27 public Runner getRunner() { 28 return buildRunner(getRunnerClass(fTestClass)); 29 } 30 31 public Runner buildRunner(Class <? extends Runner> runnerClass) { 32 try { 33 return runnerClass.getConstructor(Class .class).newInstance(new Object [] { fTestClass }); 34 } catch (NoSuchMethodException e) { 35 String simpleName= runnerClass.getSimpleName(); 36 InitializationError error= new InitializationError(String.format( 37 CONSTRUCTOR_ERROR_FORMAT, simpleName, simpleName)); 38 return Request.errorReport(fTestClass, error).getRunner(); 39 } catch (Exception e) { 40 return Request.errorReport(fTestClass, e).getRunner(); 41 } 42 } 43 44 Class <? extends Runner> getRunnerClass(final Class <?> testClass) { 45 if (testClass.getAnnotation(Ignore.class) != null) 46 return new IgnoredClassRunner(testClass).getClass(); 47 RunWith annotation= testClass.getAnnotation(RunWith.class); 48 if (annotation != null) { 49 return annotation.value(); 50 } else if (hasSuiteMethod() && fCanUseSuiteMethod) { 51 return AllTests.class; 52 } else if (isPre4Test(testClass)) { 53 return OldTestClassRunner.class; 54 } else { 55 return TestClassRunner.class; 56 } 57 } 58 59 public boolean hasSuiteMethod() { 60 try { 62 fTestClass.getMethod("suite"); 63 } catch (NoSuchMethodException e) { 64 return false; 65 } 66 return true; 67 } 68 69 boolean isPre4Test(Class <?> testClass) { 70 return junit.framework.TestCase.class.isAssignableFrom(testClass); 71 } 72 } | Popular Tags |