Commit | Line | Data |
---|---|---|
80383c14 NR |
1 | package be.nikiroo.utils.test; |
2 | ||
3 | /** | |
4 | * A {@link TestCase} that can be run with {@link TestLauncher}. | |
5 | * | |
6 | * @author niki | |
7 | */ | |
8 | abstract public class TestCase { | |
9 | /** | |
10 | * The type of {@link Exception} used to signal a failed assertion or a | |
11 | * force-fail. | |
12 | * | |
13 | * @author niki | |
14 | */ | |
15 | class AssertException extends Exception { | |
16 | private static final long serialVersionUID = 1L; | |
17 | ||
18 | public AssertException(String reason) { | |
19 | super(reason); | |
20 | } | |
21 | } | |
22 | ||
23 | private String name; | |
24 | ||
25 | /** | |
26 | * Create a new {@link TestCase}. | |
27 | * | |
28 | * @param name | |
29 | * the test name | |
30 | */ | |
31 | public TestCase(String name) { | |
32 | this.name = name; | |
33 | } | |
34 | ||
35 | /** | |
36 | * Setup the test (called before the test is run). | |
37 | * | |
38 | * @throws Exception | |
39 | * in case of error | |
40 | */ | |
41 | public void setUp() throws Exception { | |
42 | } | |
43 | ||
44 | /** | |
45 | * Tear-down the test (called when the test has been ran). | |
46 | * | |
47 | * @throws Exception | |
48 | * in case of error | |
49 | */ | |
50 | public void tearDown() throws Exception { | |
51 | } | |
52 | ||
53 | /** | |
54 | * The test name. | |
55 | * | |
56 | * @return the name | |
57 | */ | |
58 | public String getName() { | |
59 | return name; | |
60 | } | |
61 | ||
62 | /** | |
63 | * Actually do the test. | |
64 | * | |
65 | * @throws Exception | |
66 | * in case of error | |
67 | */ | |
68 | abstract public void test() throws Exception; | |
69 | ||
70 | /** | |
71 | * Force a failure. | |
72 | * | |
73 | * @throws AssertException | |
74 | * every time | |
75 | */ | |
76 | public void fail() throws AssertException { | |
77 | fail(null); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Force a failure. | |
82 | * | |
83 | * @param reason | |
84 | * the failure reason | |
85 | * @throws AssertException | |
86 | * every time | |
87 | */ | |
88 | public void fail(String reason) throws AssertException { | |
89 | throw new AssertException("Failed!" + // | |
90 | reason != null ? "\n" + reason : ""); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Check that 2 {@link Object}s are equals. | |
95 | * | |
96 | * @param expected | |
97 | * the expected value | |
98 | * @param actual | |
99 | * the actual value | |
100 | * | |
101 | * @throws AssertException | |
102 | * in case they differ | |
103 | */ | |
104 | public void assertEquals(Object expected, Object actual) | |
105 | throws AssertException { | |
106 | assertEquals(null, expected, actual); | |
107 | } | |
108 | ||
109 | /** | |
110 | * Check that 2 {@link Object}s are equals. | |
111 | * | |
112 | * @param the | |
113 | * error message to display if they differ | |
114 | * @param expected | |
115 | * the expected value | |
116 | * @param actual | |
117 | * the actual value | |
118 | * | |
119 | * @throws AssertException | |
120 | * in case they differ | |
121 | */ | |
122 | public void assertEquals(String errorMessage, Object expected, Object actual) | |
123 | throws AssertException { | |
124 | ||
125 | if (errorMessage == null) { | |
86057589 | 126 | errorMessage = generateAssertMessage(expected, actual); |
80383c14 NR |
127 | } |
128 | ||
129 | if ((expected == null && actual != null) | |
130 | || (expected != null && !expected.equals(actual))) { | |
131 | throw new AssertException(errorMessage); | |
132 | } | |
133 | } | |
134 | ||
135 | /** | |
136 | * Check that 2 {@link Object}s are equals. | |
137 | * | |
138 | * @param expected | |
139 | * the expected value | |
140 | * @param actual | |
141 | * the actual value | |
142 | * | |
143 | * @throws AssertException | |
144 | * in case they differ | |
145 | */ | |
146 | public void assertEquals(long expected, long actual) throws AssertException { | |
147 | assertEquals(new Long(expected), new Long(actual)); | |
148 | } | |
149 | ||
150 | /** | |
151 | * Check that 2 {@link Object}s are equals. | |
152 | * | |
153 | * @param the | |
154 | * error message to display if they differ | |
155 | * @param expected | |
156 | * the expected value | |
157 | * @param actual | |
158 | * the actual value | |
159 | * | |
160 | * @throws AssertException | |
161 | * in case they differ | |
162 | */ | |
163 | public void assertEquals(String errorMessage, long expected, long actual) | |
164 | throws AssertException { | |
165 | assertEquals(errorMessage, new Long(expected), new Long(actual)); | |
166 | } | |
167 | ||
168 | /** | |
169 | * Check that 2 {@link Object}s are equals. | |
170 | * | |
171 | * @param expected | |
172 | * the expected value | |
173 | * @param actual | |
174 | * the actual value | |
175 | * | |
176 | * @throws AssertException | |
177 | * in case they differ | |
178 | */ | |
179 | public void assertEquals(boolean expected, boolean actual) | |
180 | throws AssertException { | |
181 | assertEquals(new Boolean(expected), new Boolean(actual)); | |
182 | } | |
183 | ||
184 | /** | |
185 | * Check that 2 {@link Object}s are equals. | |
186 | * | |
187 | * @param the | |
188 | * error message to display if they differ | |
189 | * @param expected | |
190 | * the expected value | |
191 | * @param actual | |
192 | * the actual value | |
193 | * | |
194 | * @throws AssertException | |
195 | * in case they differ | |
196 | */ | |
197 | public void assertEquals(String errorMessage, boolean expected, | |
198 | boolean actual) throws AssertException { | |
199 | assertEquals(errorMessage, new Boolean(expected), new Boolean(actual)); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Check that 2 {@link Object}s are equals. | |
204 | * | |
205 | * @param expected | |
206 | * the expected value | |
207 | * @param actual | |
208 | * the actual value | |
209 | * | |
210 | * @throws AssertException | |
211 | * in case they differ | |
212 | */ | |
213 | public void assertEquals(double expected, double actual) | |
214 | throws AssertException { | |
215 | assertEquals(new Double(expected), new Double(actual)); | |
216 | } | |
217 | ||
218 | /** | |
219 | * Check that 2 {@link Object}s are equals. | |
220 | * | |
221 | * @param the | |
222 | * error message to display if they differ | |
223 | * @param expected | |
224 | * the expected value | |
225 | * @param actual | |
226 | * the actual value | |
227 | * | |
228 | * @throws AssertException | |
229 | * in case they differ | |
230 | */ | |
231 | public void assertEquals(String errorMessage, double expected, double actual) | |
232 | throws AssertException { | |
233 | assertEquals(errorMessage, new Double(expected), new Double(actual)); | |
234 | } | |
86057589 | 235 | |
16d59378 NR |
236 | /** |
237 | * Check that given {@link Object} is not NULL. | |
238 | * | |
239 | * @param the | |
240 | * error message to display if it is NULL | |
241 | * @param actual | |
242 | * the actual value | |
243 | * | |
244 | * @throws AssertException | |
245 | * in case they differ | |
246 | */ | |
247 | public void assertNotNull(String errorMessage, Object actual) | |
248 | throws AssertException { | |
249 | if (actual == null) { | |
250 | if (errorMessage == null) { | |
251 | errorMessage = String.format("" // | |
252 | + "Assertion failed!\n" // | |
253 | + "Object should have been NULL: [%s]", actual); | |
254 | } | |
255 | throw new AssertException(errorMessage); | |
256 | } | |
257 | } | |
258 | ||
86057589 NR |
259 | /** |
260 | * Generate the default assert message for 2 different values that were | |
261 | * supposed to be equals. | |
262 | * | |
263 | * @param expected | |
264 | * the expected value | |
265 | * @param actual | |
266 | * the actual value | |
267 | * | |
268 | * @return the message | |
269 | */ | |
270 | public static String generateAssertMessage(Object expected, Object actual) { | |
271 | return String.format("" // | |
272 | + "Assertion failed!\n" // | |
273 | + "Expected value: [%s]\n" // | |
274 | + "Actual value: [%s]", expected, actual); | |
275 | } | |
80383c14 | 276 | } |