KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > internal > runners > TestMethodRunner


1 package org.junit.internal.runners;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.concurrent.Callable JavaDoc;
6 import java.util.concurrent.ExecutorService JavaDoc;
7 import java.util.concurrent.Executors JavaDoc;
8 import java.util.concurrent.Future JavaDoc;
9 import java.util.concurrent.TimeUnit JavaDoc;
10 import java.util.concurrent.TimeoutException JavaDoc;
11
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.runner.Description;
15 import org.junit.runner.notification.Failure;
16 import org.junit.runner.notification.RunNotifier;
17
18 public class TestMethodRunner extends BeforeAndAfterRunner {
19     private final Object JavaDoc fTest;
20     private final Method JavaDoc fMethod;
21     private final RunNotifier fNotifier;
22     private final TestIntrospector fTestIntrospector;
23     private final Description fDescription;
24
25     public TestMethodRunner(Object JavaDoc test, Method JavaDoc method, RunNotifier notifier, Description description) {
26         super(test.getClass(), Before.class, After.class, test);
27         fTest= test;
28         fMethod= method;
29         fNotifier= notifier;
30         fTestIntrospector= new TestIntrospector(test.getClass());
31         fDescription= description;
32     }
33
34     public void run() {
35         if (fTestIntrospector.isIgnored(fMethod)) {
36             fNotifier.fireTestIgnored(fDescription);
37             return;
38         }
39         fNotifier.fireTestStarted(fDescription);
40         try {
41             long timeout= fTestIntrospector.getTimeout(fMethod);
42             if (timeout > 0)
43                 runWithTimeout(timeout);
44             else
45                 runMethod();
46         } finally {
47             fNotifier.fireTestFinished(fDescription);
48         }
49     }
50
51     private void runWithTimeout(long timeout) {
52         ExecutorService JavaDoc service= Executors.newSingleThreadExecutor();
53         Callable JavaDoc<Object JavaDoc> callable= new Callable JavaDoc<Object JavaDoc>() {
54             public Object JavaDoc call() throws Exception JavaDoc {
55                 runMethod();
56                 return null;
57             }
58         };
59         Future JavaDoc<Object JavaDoc> result= service.submit(callable);
60         service.shutdown();
61         try {
62             boolean terminated= service.awaitTermination(timeout,
63                     TimeUnit.MILLISECONDS);
64             if (!terminated)
65                 service.shutdownNow();
66             result.get(0, TimeUnit.MILLISECONDS); // throws the exception if one occurred during the invocation
67
} catch (TimeoutException JavaDoc e) {
68             addFailure(new Exception JavaDoc(String.format("test timed out after %d milliseconds", timeout)));
69         } catch (Exception JavaDoc e) {
70             addFailure(e);
71         }
72     }
73     
74     private void runMethod() {
75         runProtected();
76     }
77     
78     @Override JavaDoc
79     protected void runUnprotected() {
80         try {
81             executeMethodBody();
82             if (expectsException())
83                 addFailure(new AssertionError JavaDoc("Expected exception: " + expectedException().getName()));
84         } catch (InvocationTargetException JavaDoc e) {
85             Throwable JavaDoc actual= e.getTargetException();
86             if (!expectsException())
87                 addFailure(actual);
88             else if (isUnexpected(actual)) {
89                 String JavaDoc message= "Unexpected exception, expected<" + expectedException().getName() + "> but was<"
90                     + actual.getClass().getName() + ">";
91                 addFailure(new Exception JavaDoc(message, actual));
92             }
93         } catch (Throwable JavaDoc e) {
94             addFailure(e);
95         }
96     }
97
98     protected void executeMethodBody() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
99         fMethod.invoke(fTest);
100     }
101
102     @Override JavaDoc
103     protected void addFailure(Throwable JavaDoc e) {
104         fNotifier.fireTestFailure(new Failure(fDescription, e));
105     }
106     
107     private boolean expectsException() {
108         return expectedException() != null;
109     }
110
111     private Class JavaDoc<? extends Throwable JavaDoc> expectedException() {
112         return fTestIntrospector.expectedException(fMethod);
113     }
114
115     private boolean isUnexpected(Throwable JavaDoc exception) {
116         return ! expectedException().isAssignableFrom(exception.getClass());
117     }
118 }
119
120
Popular Tags