Update Server, breaks API + remove deprecated
[nikiroo-utils.git] / src / be / nikiroo / utils / test / TestCase.java
CommitLineData
80383c14
NR
1package be.nikiroo.utils.test;
2
3/**
4 * A {@link TestCase} that can be run with {@link TestLauncher}.
5 *
6 * @author niki
7 */
8abstract 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
c37cc690
NR
18 public AssertException(String reason, Exception source) {
19 super(reason, source);
20 }
21
80383c14
NR
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 */
79ce1a49 45 @SuppressWarnings("unused")
80383c14
NR
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 */
79ce1a49 55 @SuppressWarnings("unused")
80383c14
NR
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 *
db31c358
NR
118 * @param errorMessage
119 * the error message to display if they differ
80383c14
NR
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 {
80383c14
NR
130 if ((expected == null && actual != null)
131 || (expected != null && !expected.equals(actual))) {
c37cc690
NR
132 if (errorMessage == null) {
133 throw new AssertException(generateAssertMessage(expected,
134 actual));
c37cc690 135 }
cd0c27d2
NR
136
137 throw new AssertException(errorMessage, new AssertException(
138 generateAssertMessage(expected, actual)));
80383c14
NR
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(new Long(expected), new Long(actual));
155 }
156
157 /**
158 * Check that 2 {@link Object}s are equals.
159 *
db31c358
NR
160 * @param errorMessage
161 * the error message to display if they differ
80383c14
NR
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, new Long(expected), new Long(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(new Boolean(expected), new Boolean(actual));
189 }
190
191 /**
192 * Check that 2 {@link Object}s are equals.
193 *
db31c358
NR
194 * @param errorMessage
195 * the error message to display if they differ
80383c14
NR
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, new Boolean(expected), new Boolean(actual));
207 }
208
209 /**
210 * Check that 2 {@link Object}s are equals.
211 *
212 * @param expected
213 * the expected value
214 * @param actual
215 * the actual value
216 *
217 * @throws AssertException
218 * in case they differ
219 */
220 public void assertEquals(double expected, double actual)
221 throws AssertException {
222 assertEquals(new Double(expected), new Double(actual));
223 }
224
225 /**
226 * Check that 2 {@link Object}s are equals.
227 *
db31c358
NR
228 * @param errorMessage
229 * the error message to display if they differ
80383c14
NR
230 * @param expected
231 * the expected value
232 * @param actual
233 * the actual value
234 *
235 * @throws AssertException
236 * in case they differ
237 */
238 public void assertEquals(String errorMessage, double expected, double actual)
239 throws AssertException {
240 assertEquals(errorMessage, new Double(expected), new Double(actual));
241 }
86057589 242
16d59378
NR
243 /**
244 * Check that given {@link Object} is not NULL.
245 *
db31c358
NR
246 * @param errorMessage
247 * the error message to display if it is NULL
16d59378
NR
248 * @param actual
249 * the actual value
250 *
251 * @throws AssertException
252 * in case they differ
253 */
254 public void assertNotNull(String errorMessage, Object actual)
255 throws AssertException {
256 if (actual == null) {
c37cc690
NR
257 String defaultReason = String.format("" //
258 + "Assertion failed!\n" //
259 + "Object should have been NULL: [%s]", actual);
260
16d59378 261 if (errorMessage == null) {
c37cc690 262 throw new AssertException(defaultReason);
16d59378 263 }
cd0c27d2
NR
264
265 throw new AssertException(errorMessage, new AssertException(
266 defaultReason));
16d59378
NR
267 }
268 }
269
86057589
NR
270 /**
271 * Generate the default assert message for 2 different values that were
272 * supposed to be equals.
273 *
274 * @param expected
275 * the expected value
276 * @param actual
277 * the actual value
278 *
279 * @return the message
280 */
281 public static String generateAssertMessage(Object expected, Object actual) {
282 return String.format("" //
283 + "Assertion failed!\n" //
284 + "Expected value: [%s]\n" //
285 + "Actual value: [%s]", expected, actual);
286 }
80383c14 287}