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.junit; |
12 | |
13 | import java.lang.reflect.InvocationTargetException; |
14 | import java.lang.reflect.Method; |
15 | import org.jtiger.framework.SetUpException; |
16 | import org.jtiger.framework.SetUpTearDown; |
17 | import org.jtiger.framework.TearDownException; |
18 | |
19 | /** |
20 | * This implementation of {@link SetUpTearDown} is intended to be used to execute test cases that have been written |
21 | * using <a href="%junit.url%" target="_blank">the JUnit Test Framework</a>. The implementation is passed to the |
22 | * framework by returning an instance from {@link org.jtiger.framework.FixturesRunnerConfig#getSutdClass()} or by |
23 | * passing the qualified class name as {@link org.jtiger.framework.FixturesRunnerMain#ARG_SUTD_CLASS the appropriate |
24 | * argument} on the command line to {@link org.jtiger.framework.FixturesRunnerMain#main(String[]) the framework main |
25 | * method}. |
26 | * |
27 | * @author %javadoc.author.tag% |
28 | * @version %version%<br/> |
29 | * <i>Build Number %build.number%</i><br/> |
30 | * <i>Build Time %build.time% CET (GMT + 1)</i> |
31 | */ |
32 | public final class JUnitSetUpTearDown implements SetUpTearDown |
33 | { |
34 | private static final String SETUP_METHOD = "setUp"; |
35 | private static final String TEARDOWN_METHOD = "tearDown"; |
36 | |
37 | /** |
38 | * Create a default instance of <tt>JUnitSetUpTearDown</tt>. |
39 | */ |
40 | public JUnitSetUpTearDown() |
41 | { |
42 | |
43 | } |
44 | |
45 | /** |
46 | * Perform the setting up of <a href="%junit.url%" target="_blank">JUnit Test Framework</a> test cases. |
47 | * |
48 | * @param fixture The test fixture to set up. |
49 | * @throws SetUpException If a <code>setUp</code> method cannot be found or if an error occurs during reflective |
50 | * invocation of the JUnit <code>setUp</code> method. |
51 | */ |
52 | public void setUp(final Object fixture) throws SetUpException |
53 | { |
54 | final Method m = JUnitSetUpTearDown.getSetUpMethod(fixture.getClass()); |
55 | |
56 | if(m == null) |
57 | { |
58 | throw new SetUpException(); |
59 | } |
60 | else |
61 | { |
62 | if(!m.isAccessible()) |
63 | { |
64 | m.setAccessible(true); |
65 | } |
66 | |
67 | try |
68 | { |
69 | m.invoke(fixture); |
70 | } |
71 | catch(IllegalAccessException e) |
72 | { |
73 | throw new SetUpException(e.getMessage(), e); |
74 | } |
75 | catch(InvocationTargetException e) |
76 | { |
77 | throw new SetUpException(e.getMessage(), e); |
78 | } |
79 | } |
80 | } |
81 | |
82 | /** |
83 | * Perform the tearing down of <a href="%junit.url%" target="_blank">JUnit Test Framework</a> test cases. |
84 | * |
85 | * @param fixture The test fixture to tear down. |
86 | * @throws TearDownException If a <code>tearDown</code> method cannot be found or if an error occurs during |
87 | * reflective invocation of the JUnit <code>tearDown</code> method. |
88 | */ |
89 | public void tearDown(final Object fixture) throws TearDownException |
90 | { |
91 | final Method m = JUnitSetUpTearDown.getTearDownMethod(fixture.getClass()); |
92 | |
93 | if(m == null) |
94 | { |
95 | throw new TearDownException(); |
96 | } |
97 | else |
98 | { |
99 | if(!m.isAccessible()) |
100 | { |
101 | m.setAccessible(true); |
102 | } |
103 | |
104 | try |
105 | { |
106 | m.invoke(fixture); |
107 | } |
108 | catch(IllegalAccessException e) |
109 | { |
110 | throw new TearDownException(e.getMessage(), e); |
111 | } |
112 | catch(InvocationTargetException e) |
113 | { |
114 | throw new TearDownException(e.getMessage(), e); |
115 | } |
116 | } |
117 | } |
118 | |
119 | private static Method getSetUpMethod(Class<?> c) |
120 | { |
121 | while(c != null) |
122 | { |
123 | final Method[] methods = c.getDeclaredMethods(); |
124 | |
125 | for(Method m : methods) |
126 | { |
127 | if(m.getName().equals(JUnitSetUpTearDown.SETUP_METHOD) && |
128 | m.getParameterTypes().length == 0) |
129 | { |
130 | return m; |
131 | } |
132 | } |
133 | |
134 | c = c.getSuperclass(); |
135 | } |
136 | |
137 | return null; |
138 | } |
139 | |
140 | private static Method getTearDownMethod(Class<?> c) |
141 | { |
142 | while(c != null) |
143 | { |
144 | final Method[] methods = c.getDeclaredMethods(); |
145 | |
146 | for(Method m : methods) |
147 | { |
148 | if(m.getName().equals(JUnitSetUpTearDown.TEARDOWN_METHOD) && |
149 | m.getParameterTypes().length == 0) |
150 | { |
151 | return m; |
152 | } |
153 | } |
154 | |
155 | c = c.getSuperclass(); |
156 | } |
157 | |
158 | return null; |
159 | } |
160 | } |