KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > internal > requests > ClassRequest


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 JavaDoc CONSTRUCTOR_ERROR_FORMAT= "Custom runner class %s should have a public constructor with signature %s(Class testClass)";
14     private final Class JavaDoc<?> fTestClass;
15     private boolean fCanUseSuiteMethod;
16
17     public ClassRequest(Class JavaDoc<?> testClass, boolean canUseSuiteMethod) {
18         fTestClass= testClass;
19         fCanUseSuiteMethod= canUseSuiteMethod;
20     }
21
22     public ClassRequest(Class JavaDoc<?> testClass) {
23         this(testClass, true);
24     }
25     
26     @Override JavaDoc
27     public Runner getRunner() {
28         return buildRunner(getRunnerClass(fTestClass));
29     }
30
31     public Runner buildRunner(Class JavaDoc<? extends Runner> runnerClass) {
32         try {
33             return runnerClass.getConstructor(Class JavaDoc.class).newInstance(new Object JavaDoc[] { fTestClass });
34         } catch (NoSuchMethodException JavaDoc e) {
35             String JavaDoc 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 JavaDoc e) {
40             return Request.errorReport(fTestClass, e).getRunner();
41         }
42     }
43
44     Class JavaDoc<? extends Runner> getRunnerClass(final Class JavaDoc<?> 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         // TODO: check all attributes
61
try {
62             fTestClass.getMethod("suite");
63         } catch (NoSuchMethodException JavaDoc e) {
64             return false;
65         }
66         return true;
67     }
68
69     boolean isPre4Test(Class JavaDoc<?> testClass) {
70         return junit.framework.TestCase.class.isAssignableFrom(testClass);
71     }
72 }
Popular Tags