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 java.util.Arrays; |
16 | import java.util.HashSet; |
17 | import java.util.Set; |
18 | |
19 | /** |
20 | * The default implementation of setting up and tearing down a unit test case. |
21 | * This implementation searches for the {@link SetUp} annotation present on a method and calls it to set up a test case. |
22 | * This implementation searches for the {@link TearDown} annotation present on a method and calls it to tear down a test |
23 | * case. If more than one method contains either of these annotations, it cannot be guaranteed which order they are |
24 | * executed in. |
25 | * |
26 | * @see SetUpTearDown |
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 DefaultSetUpTearDownFactory |
33 | { |
34 | private DefaultSetUpTearDownFactory() |
35 | { |
36 | |
37 | } |
38 | |
39 | /** |
40 | * Returns a new instance of the default implementation to handle setting up and tearing down of unit test cases. |
41 | * |
42 | * @return A new instance of the default implementation to handle setting up and tearing down of unit test cases. |
43 | */ |
44 | public static SetUpTearDown newDefaultSetUpTearDown() |
45 | { |
46 | return new DefaultSetUpTearDownImpl(); |
47 | } |
48 | |
49 | private static final class DefaultSetUpTearDownImpl implements SetUpTearDown |
50 | { |
51 | DefaultSetUpTearDownImpl() |
52 | { |
53 | |
54 | } |
55 | |
56 | public void setUp(final Object fixture) throws SetUpException |
57 | { |
58 | final Class<?> c = fixture.getClass(); |
59 | |
60 | final Method[] methods = c.getMethods(); |
61 | final Method[] declaredMethods = c.getDeclaredMethods(); |
62 | final Set<Method> allMethods = new HashSet<Method>(Arrays.asList(methods)); |
63 | allMethods.addAll(Arrays.asList(declaredMethods)); |
64 | |
65 | for(Method m : allMethods) |
66 | { |
67 | if(m.getParameterTypes().length == 0 && m.isAnnotationPresent(SetUp.class)) |
68 | { |
69 | final boolean isAccessible = m.isAccessible(); |
70 | |
71 | if(!isAccessible) |
72 | { |
73 | m.setAccessible(true); |
74 | } |
75 | |
76 | try |
77 | { |
78 | m.invoke(fixture); |
79 | } |
80 | catch(IllegalAccessException e) |
81 | { |
82 | throw new SetUpException(e.getMessage(), e); |
83 | } |
84 | catch(InvocationTargetException e) |
85 | { |
86 | throw new SetUpException(e.getMessage(), e); |
87 | } |
88 | |
89 | m.setAccessible(isAccessible); |
90 | } |
91 | } |
92 | } |
93 | |
94 | public void tearDown(final Object fixture) throws TearDownException |
95 | { |
96 | final Class<?> c = fixture.getClass(); |
97 | |
98 | final Method[] methods = c.getMethods(); |
99 | final Method[] declaredMethods = c.getDeclaredMethods(); |
100 | final Set<Method> allMethods = new HashSet<Method>(Arrays.asList(methods)); |
101 | allMethods.addAll(Arrays.asList(declaredMethods)); |
102 | |
103 | for(Method m : allMethods) |
104 | { |
105 | if(m.getParameterTypes().length == 0 && m.isAnnotationPresent(TearDown.class)) |
106 | { |
107 | final boolean isAccessible = m.isAccessible(); |
108 | |
109 | if(!isAccessible) |
110 | { |
111 | m.setAccessible(true); |
112 | } |
113 | |
114 | try |
115 | { |
116 | m.invoke(fixture); |
117 | } |
118 | catch(IllegalAccessException e) |
119 | { |
120 | throw new TearDownException(e.getMessage(), e); |
121 | } |
122 | catch(InvocationTargetException e) |
123 | { |
124 | throw new TearDownException(e.getMessage(), e); |
125 | } |
126 | |
127 | m.setAccessible(isAccessible); |
128 | } |
129 | } |
130 | } |
131 | } |
132 | } |