1 package be
.nikiroo
.utils
.test
;
6 * A {@link TestCase} that can be run with {@link TestLauncher}.
10 abstract public class TestCase
{
12 * The type of {@link Exception} used to signal a failed assertion or a
17 class AssertException
extends Exception
{
18 private static final long serialVersionUID
= 1L;
20 public AssertException(String reason
, Exception source
) {
21 super(reason
, source
);
24 public AssertException(String reason
) {
32 * Create a new {@link TestCase}.
37 public TestCase(String name
) {
42 * This constructor can be used if you require a no-param constructor. In
43 * this case, you are allowed to set the name manually via
44 * {@link TestCase#setName}.
46 protected TestCase() {
51 * Setup the test (called before the test is run).
56 public void setUp() throws Exception
{
60 * Tear-down the test (called when the test has been ran).
65 public void tearDown() throws Exception
{
73 public String
getName() {
81 * the new name (internal use only)
83 * @return this (so we can chain and so we can initialize it in a member
84 * variable if this is an anonymous inner class)
86 protected TestCase
setName(String name
) {
92 * Actually do the test.
97 abstract public void test() throws Exception
;
102 * @throws AssertException
105 public void fail() throws AssertException
{
115 * @throws AssertException
118 public void fail(String reason
) throws AssertException
{
128 * the exception that caused the failure (can be NULL)
130 * @throws AssertException
133 public void fail(String reason
, Exception e
) throws AssertException
{
134 throw new AssertException("Failed!" + //
135 reason
!= null ?
"\n" + reason
: "", e
);
139 * Check that 2 {@link Object}s are equals.
146 * @throws AssertException
147 * in case they differ
149 public void assertEquals(Object expected
, Object actual
)
150 throws AssertException
{
151 assertEquals(null, expected
, actual
);
155 * Check that 2 {@link Object}s are equals.
157 * @param errorMessage
158 * the error message to display if they differ
164 * @throws AssertException
165 * in case they differ
167 public void assertEquals(String errorMessage
, Object expected
, Object actual
)
168 throws AssertException
{
169 if ((expected
== null && actual
!= null)
170 || (expected
!= null && !expected
.equals(actual
))) {
171 if (errorMessage
== null) {
172 throw new AssertException(generateAssertMessage(expected
,
176 throw new AssertException(errorMessage
, new AssertException(
177 generateAssertMessage(expected
, actual
)));
182 * Check that 2 longs are equals.
189 * @throws AssertException
190 * in case they differ
192 public void assertEquals(long expected
, long actual
) throws AssertException
{
193 assertEquals(Long
.valueOf(expected
), Long
.valueOf(actual
));
197 * Check that 2 longs are equals.
199 * @param errorMessage
200 * the error message to display if they differ
206 * @throws AssertException
207 * in case they differ
209 public void assertEquals(String errorMessage
, long expected
, long actual
)
210 throws AssertException
{
211 assertEquals(errorMessage
, Long
.valueOf(expected
), Long
.valueOf(actual
));
215 * Check that 2 booleans are equals.
222 * @throws AssertException
223 * in case they differ
225 public void assertEquals(boolean expected
, boolean actual
)
226 throws AssertException
{
227 assertEquals(Boolean
.valueOf(expected
), Boolean
.valueOf(actual
));
231 * Check that 2 booleans are equals.
233 * @param errorMessage
234 * the error message to display if they differ
240 * @throws AssertException
241 * in case they differ
243 public void assertEquals(String errorMessage
, boolean expected
,
244 boolean actual
) throws AssertException
{
245 assertEquals(errorMessage
, Boolean
.valueOf(expected
),
246 Boolean
.valueOf(actual
));
250 * Check that 2 doubles are equals.
257 * @throws AssertException
258 * in case they differ
260 public void assertEquals(double expected
, double actual
)
261 throws AssertException
{
262 assertEquals(Double
.valueOf(expected
), Double
.valueOf(actual
));
266 * Check that 2 doubles are equals.
268 * @param errorMessage
269 * the error message to display if they differ
275 * @throws AssertException
276 * in case they differ
278 public void assertEquals(String errorMessage
, double expected
, double actual
)
279 throws AssertException
{
280 assertEquals(errorMessage
, Double
.valueOf(expected
),
281 Double
.valueOf(actual
));
285 * Check that 2 {@link List}s are equals.
287 * @param errorMessage
288 * the error message to display if they differ
294 * @throws AssertException
295 * in case they differ
297 public void assertEquals(List
<?
> expected
, List
<?
> actual
)
298 throws AssertException
{
299 assertEquals("Assertion failed", expected
, actual
);
303 * Check that 2 {@link List}s are equals.
305 * @param errorMessage
306 * the error message to display if they differ
311 * @param errorMessage
312 * the error message to display if they differ
314 * @throws AssertException
315 * in case they differ
317 public void assertEquals(String errorMessage
, List
<?
> expected
,
318 List
<?
> actual
) throws AssertException
{
320 if (expected
.size() != actual
.size()) {
321 assertEquals(errorMessage
+ ": not same number of items",
322 list(expected
), list(actual
));
325 int size
= expected
.size();
326 for (int i
= 0; i
< size
; i
++) {
327 assertEquals(errorMessage
+ ": item " + i
328 + " (0-based) is not correct", expected
.get(i
),
334 * Check that given {@link Object} is not NULL.
336 * @param errorMessage
337 * the error message to display if it is NULL
341 * @throws AssertException
342 * in case they differ
344 public void assertNotNull(String errorMessage
, Object actual
)
345 throws AssertException
{
346 if (actual
== null) {
347 String defaultReason
= String
.format("" //
348 + "Assertion failed!%n" //
349 + "Object should not have been NULL");
351 if (errorMessage
== null) {
352 throw new AssertException(defaultReason
);
355 throw new AssertException(errorMessage
, new AssertException(
361 * Generate the default assert message for 2 different values that were
362 * supposed to be equals.
369 * @return the message
371 public static String
generateAssertMessage(Object expected
, Object actual
) {
372 return String
.format("" //
373 + "Assertion failed!%n" //
374 + "Expected value: [%s]%n" //
375 + "Actual value: [%s]", expected
, actual
);
378 private static String
list(List
<?
> items
) {
379 StringBuilder builder
= new StringBuilder();
380 for (Object item
: items
) {
381 if (builder
.length() == 0) {
382 builder
.append(items
.size() + " item(s): ");
384 builder
.append(", ");
387 builder
.append("" + item
);
389 if (builder
.length() > 60) {
390 builder
.setLength(57);
391 builder
.append("...");
396 return builder
.toString();