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