1 | /* |
2 | * JTiger Unit Testing Framework for J2SE 1.5 |
3 | * Copyright (C) 2005 Tony Morris |
4 | * |
5 | * This software is licenced under the |
6 | * Common Public Licence version 1.0 |
7 | * http://www.opensource.org/licenses/cpl1.0.php |
8 | * |
9 | * You received a copy of this licence with this software. |
10 | */ |
11 | package org.jtiger.framework; |
12 | |
13 | import java.lang.reflect.InvocationTargetException; |
14 | import java.lang.reflect.Method; |
15 | import static org.jtiger.framework.TestResultFactory.newTestResult; |
16 | import static org.jtiger.framework.TestResultType.SUCCESS; |
17 | import static org.jtiger.framework.TestResultType.FAILURE; |
18 | import static org.jtiger.framework.TestResultType.IGNORED_ANNOTATED; |
19 | import static org.jtiger.framework.TestResultType.IGNORED_CANNOT_INVOKE; |
20 | import static org.jtiger.framework.TestResultType.FAILURE_SETUP; |
21 | import static org.jtiger.framework.TestResultType.FAILURE_TEARDOWN; |
22 | |
23 | final class TestRunnerFactory |
24 | { |
25 | private TestRunnerFactory() |
26 | { |
27 | |
28 | } |
29 | |
30 | static TestRunner newTestRunner() |
31 | { |
32 | return new TestRunnerImpl(); |
33 | } |
34 | |
35 | private static final class TestRunnerImpl implements TestRunner |
36 | { |
37 | TestRunnerImpl() |
38 | { |
39 | |
40 | } |
41 | |
42 | public TestResult run(final Method m, final Object fixture, final SetUpTearDown sutd) throws NullPointerException |
43 | { |
44 | if(m == null || fixture == null || sutd == null) |
45 | { |
46 | throw new NullPointerException(); |
47 | } |
48 | |
49 | if(m.isAnnotationPresent(Ignore.class)) |
50 | { |
51 | return newTestResult(IGNORED_ANNOTATED, |
52 | m.getAnnotation(Ignore.class).value(), null, null, m); |
53 | } |
54 | |
55 | if(m.getDeclaringClass().isAnnotationPresent(Ignore.class)) |
56 | { |
57 | return newTestResult(IGNORED_ANNOTATED, |
58 | m.getDeclaringClass().getAnnotation(Ignore.class).value(), null, null, m); |
59 | } |
60 | |
61 | if(m.getParameterTypes().length != 0) |
62 | { |
63 | return newTestResult(IGNORED_CANNOT_INVOKE, |
64 | Messages.wrongMethodSignature(), null, null, m); |
65 | } |
66 | |
67 | final ExpectException ee = m.getAnnotation(ExpectException.class); |
68 | |
69 | try |
70 | { |
71 | sutd.setUp(fixture); |
72 | } |
73 | catch(SetUpException e) |
74 | { |
75 | return newTestResult(FAILURE_SETUP, e.getMessage(), e, null, m); |
76 | } |
77 | |
78 | try |
79 | { |
80 | final long t1 = System.currentTimeMillis(); |
81 | final Object ret = m.invoke(fixture); |
82 | final long t2 = System.currentTimeMillis(); |
83 | |
84 | m.setAccessible(true); |
85 | |
86 | final String message = ret == null ? null : ret.toString(); |
87 | |
88 | return newTestResult(ee == null ? SUCCESS : FAILURE, message, null, t2 - t1, m); |
89 | } |
90 | catch(IllegalAccessException e) |
91 | { |
92 | return newTestResult(FAILURE, e.getMessage(), null, null, m); |
93 | } |
94 | catch(InvocationTargetException e) |
95 | { |
96 | final Throwable cause = e.getCause(); |
97 | |
98 | if(ee != null) |
99 | { |
100 | final Class<? extends Throwable> expected = ee.value(); |
101 | final boolean subclass = ee.subclass(); |
102 | |
103 | if(subclass ? expected.isAssignableFrom(cause.getClass()) : expected == cause.getClass()) |
104 | { |
105 | return newTestResult(SUCCESS, cause.getMessage(), cause, null, m); |
106 | } |
107 | } |
108 | |
109 | return newTestResult(FAILURE, cause.getMessage(), cause, null, m); |
110 | } |
111 | finally |
112 | { |
113 | try |
114 | { |
115 | sutd.tearDown(fixture); |
116 | } |
117 | catch(TearDownException e) |
118 | { |
119 | return newTestResult(FAILURE_TEARDOWN, e.getMessage(), e, null, m); |
120 | } |
121 | } |
122 | } |
123 | } |
124 | } |