1.0.0
[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
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) {
126 errorMessage = String.format("" //
127 + "Assertion failed!\n" //
128 + "Expected value: [%s]\n" //
129 + "Actual value: [%s]", expected, actual);
130 }
131
132 if ((expected == null && actual != null)
133 || (expected != null && !expected.equals(actual))) {
134 throw new AssertException(errorMessage);
135 }
136 }
137
138 /**
139 * Check that 2 {@link Object}s are equals.
140 *
141 * @param expected
142 * the expected value
143 * @param actual
144 * the actual value
145 *
146 * @throws AssertException
147 * in case they differ
148 */
149 public void assertEquals(long expected, long actual) throws AssertException {
150 assertEquals(new Long(expected), new Long(actual));
151 }
152
153 /**
154 * Check that 2 {@link Object}s are equals.
155 *
156 * @param the
157 * error message to display if they differ
158 * @param expected
159 * the expected value
160 * @param actual
161 * the actual value
162 *
163 * @throws AssertException
164 * in case they differ
165 */
166 public void assertEquals(String errorMessage, long expected, long actual)
167 throws AssertException {
168 assertEquals(errorMessage, new Long(expected), new Long(actual));
169 }
170
171 /**
172 * Check that 2 {@link Object}s are equals.
173 *
174 * @param expected
175 * the expected value
176 * @param actual
177 * the actual value
178 *
179 * @throws AssertException
180 * in case they differ
181 */
182 public void assertEquals(boolean expected, boolean actual)
183 throws AssertException {
184 assertEquals(new Boolean(expected), new Boolean(actual));
185 }
186
187 /**
188 * Check that 2 {@link Object}s are equals.
189 *
190 * @param the
191 * error message to display if they differ
192 * @param expected
193 * the expected value
194 * @param actual
195 * the actual value
196 *
197 * @throws AssertException
198 * in case they differ
199 */
200 public void assertEquals(String errorMessage, boolean expected,
201 boolean actual) throws AssertException {
202 assertEquals(errorMessage, new Boolean(expected), new Boolean(actual));
203 }
204
205 /**
206 * Check that 2 {@link Object}s are equals.
207 *
208 * @param expected
209 * the expected value
210 * @param actual
211 * the actual value
212 *
213 * @throws AssertException
214 * in case they differ
215 */
216 public void assertEquals(double expected, double actual)
217 throws AssertException {
218 assertEquals(new Double(expected), new Double(actual));
219 }
220
221 /**
222 * Check that 2 {@link Object}s are equals.
223 *
224 * @param the
225 * error message to display if they differ
226 * @param expected
227 * the expected value
228 * @param actual
229 * the actual value
230 *
231 * @throws AssertException
232 * in case they differ
233 */
234 public void assertEquals(String errorMessage, double expected, double actual)
235 throws AssertException {
236 assertEquals(errorMessage, new Double(expected), new Double(actual));
237 }
238}