1 package be
.nikiroo
.utils
.test
;
4 import java
.util
.Arrays
;
7 import java
.util
.Map
.Entry
;
9 import be
.nikiroo
.utils
.IOUtils
;
12 * A {@link TestCase} that can be run with {@link TestLauncher}.
16 abstract public class TestCase
{
18 * The type of {@link Exception} used to signal a failed assertion or a
23 class AssertException
extends Exception
{
24 private static final long serialVersionUID
= 1L;
26 public AssertException(String reason
, Exception source
) {
27 super(reason
, source
);
30 public AssertException(String reason
) {
38 * Create a new {@link TestCase}.
43 public TestCase(String name
) {
48 * This constructor can be used if you require a no-param constructor. In
49 * this case, you are allowed to set the name manually via
50 * {@link TestCase#setName}.
52 protected TestCase() {
57 * Setup the test (called before the test is run).
62 public void setUp() throws Exception
{
66 * Tear-down the test (called when the test has been ran).
71 public void tearDown() throws Exception
{
79 public String
getName() {
87 * the new name (internal use only)
89 * @return this (so we can chain and so we can initialize it in a member
90 * variable if this is an anonymous inner class)
92 protected TestCase
setName(String name
) {
98 * Actually do the test.
103 abstract public void test() throws Exception
;
108 * @throws AssertException
111 public void fail() throws AssertException
{
121 * @throws AssertException
124 public void fail(String reason
) throws AssertException
{
134 * the exception that caused the failure (can be NULL)
136 * @throws AssertException
139 public void fail(String reason
, Exception e
) throws AssertException
{
140 throw new AssertException("Failed!" + //
141 reason
!= null ?
"\n" + reason
: "", e
);
145 * Check that 2 {@link Object}s are equals.
152 * @throws AssertException
153 * in case they differ
155 public void assertEquals(Object expected
, Object actual
)
156 throws AssertException
{
157 assertEquals(null, expected
, actual
);
161 * Check that 2 {@link Object}s are equals.
163 * @param errorMessage
164 * the error message to display if they differ
170 * @throws AssertException
171 * in case they differ
173 public void assertEquals(String errorMessage
, Object expected
, Object actual
)
174 throws AssertException
{
175 if ((expected
== null && actual
!= null)
176 || (expected
!= null && !expected
.equals(actual
))) {
177 if (errorMessage
== null) {
178 throw new AssertException(generateAssertMessage(expected
,
182 throw new AssertException(errorMessage
, new AssertException(
183 generateAssertMessage(expected
, actual
)));
188 * Check that 2 longs are equals.
195 * @throws AssertException
196 * in case they differ
198 public void assertEquals(long expected
, long actual
) throws AssertException
{
199 assertEquals(Long
.valueOf(expected
), Long
.valueOf(actual
));
203 * Check that 2 longs are equals.
205 * @param errorMessage
206 * the error message to display if they differ
212 * @throws AssertException
213 * in case they differ
215 public void assertEquals(String errorMessage
, long expected
, long actual
)
216 throws AssertException
{
217 assertEquals(errorMessage
, Long
.valueOf(expected
), Long
.valueOf(actual
));
221 * Check that 2 booleans are equals.
228 * @throws AssertException
229 * in case they differ
231 public void assertEquals(boolean expected
, boolean actual
)
232 throws AssertException
{
233 assertEquals(Boolean
.valueOf(expected
), Boolean
.valueOf(actual
));
237 * Check that 2 booleans are equals.
239 * @param errorMessage
240 * the error message to display if they differ
246 * @throws AssertException
247 * in case they differ
249 public void assertEquals(String errorMessage
, boolean expected
,
250 boolean actual
) throws AssertException
{
251 assertEquals(errorMessage
, Boolean
.valueOf(expected
),
252 Boolean
.valueOf(actual
));
256 * Check that 2 doubles are equals.
263 * @throws AssertException
264 * in case they differ
266 public void assertEquals(double expected
, double actual
)
267 throws AssertException
{
268 assertEquals(Double
.valueOf(expected
), Double
.valueOf(actual
));
272 * Check that 2 doubles are equals.
274 * @param errorMessage
275 * the error message to display if they differ
281 * @throws AssertException
282 * in case they differ
284 public void assertEquals(String errorMessage
, double expected
, double actual
)
285 throws AssertException
{
286 assertEquals(errorMessage
, Double
.valueOf(expected
),
287 Double
.valueOf(actual
));
291 * Check that 2 {@link List}s are equals.
293 * @param errorMessage
294 * the error message to display if they differ
300 * @throws AssertException
301 * in case they differ
303 public void assertEquals(List
<?
> expected
, List
<?
> actual
)
304 throws AssertException
{
305 assertEquals("Assertion failed", expected
, actual
);
309 * Check that 2 {@link List}s are equals.
311 * @param errorMessage
312 * the error message to display if they differ
318 * @throws AssertException
319 * in case they differ
321 public void assertEquals(String errorMessage
, List
<?
> expected
,
322 List
<?
> actual
) throws AssertException
{
324 if (expected
.size() != actual
.size()) {
325 assertEquals(errorMessage
+ ": not same number of items",
326 list(expected
), list(actual
));
329 int size
= expected
.size();
330 for (int i
= 0; i
< size
; i
++) {
331 assertEquals(errorMessage
+ ": item " + i
332 + " (0-based) is not correct", expected
.get(i
),
338 * Check that 2 {@link File}s are equals, by doing a line-by-line
345 * @param errorMessage
346 * the error message to display if they differ
348 * @throws AssertException
349 * in case they differ
351 public void assertEquals(File expected
, File actual
) throws AssertException
{
352 assertEquals(generateAssertMessage(expected
, actual
), expected
, actual
);
356 * Check that 2 {@link File}s are equals, by doing a line-by-line
359 * @param errorMessage
360 * the error message to display if they differ
366 * @throws AssertException
367 * in case they differ
369 public void assertEquals(String errorMessage
, File expected
, File actual
)
370 throws AssertException
{
371 assertEquals(errorMessage
, expected
, actual
, null);
375 * Check that 2 {@link File}s are equals, by doing a line-by-line
378 * @param errorMessage
379 * the error message to display if they differ
385 * skip the lines starting with some values for the given files
386 * (relative path from base directory in recursive mode)
388 * @throws AssertException
389 * in case they differ
391 public void assertEquals(String errorMessage
, File expected
, File actual
,
392 Map
<String
, List
<String
>> skipCompare
) throws AssertException
{
393 assertEquals(errorMessage
, expected
, actual
, skipCompare
, null);
396 private void assertEquals(String errorMessage
, File expected
, File actual
,
397 Map
<String
, List
<String
>> skipCompare
, String removeFromName
)
398 throws AssertException
{
400 if (expected
.isDirectory() || actual
.isDirectory()) {
401 assertEquals(errorMessage
+ ": type mismatch: expected a "
402 + (expected
.isDirectory() ?
"directory" : "file")
404 + (actual
.isDirectory() ?
"directory" : "file"),
405 expected
.isDirectory(), actual
.isDirectory());
407 List
<String
> expectedFiles
= Arrays
.asList(expected
.list());
408 expectedFiles
.sort(null);
409 List
<String
> actualFiles
= Arrays
.asList(actual
.list());
410 actualFiles
.sort(null);
412 assertEquals(errorMessage
, expectedFiles
, actualFiles
);
413 for (int i
= 0; i
< actualFiles
.size(); i
++) {
414 File expectedFile
= new File(expected
, expectedFiles
.get(i
));
415 File actualFile
= new File(actual
, actualFiles
.get(i
));
417 assertEquals(errorMessage
, expectedFile
, actualFile
,
418 skipCompare
, expected
.getAbsolutePath());
422 List
<String
> expectedLines
= Arrays
.asList(IOUtils
423 .readSmallFile(expected
).split("\n"));
424 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
425 actual
).split("\n"));
427 String name
= expected
.getAbsolutePath();
428 if (removeFromName
!= null && name
.startsWith(removeFromName
)) {
429 name
= expected
.getName()
430 + name
.substring(removeFromName
.length());
433 assertEquals(errorMessage
+ ": " + name
434 + ": the number of lines is not the same",
435 expectedLines
.size(), resultLines
.size());
437 for (int j
= 0; j
< expectedLines
.size(); j
++) {
438 String expectedLine
= expectedLines
.get(j
);
439 String resultLine
= resultLines
.get(j
);
441 boolean skip
= false;
442 for (Entry
<String
, List
<String
>> skipThose
: skipCompare
444 for (String skipStart
: skipThose
.getValue()) {
445 if (name
.endsWith(skipThose
.getKey())
446 && expectedLine
.startsWith(skipStart
)
447 && resultLine
.startsWith(skipStart
)) {
457 assertEquals(errorMessage
+ ": line " + (j
+ 1)
458 + " is not the same in file " + name
, expectedLine
,
461 } catch (Exception e
) {
462 throw new AssertException(errorMessage
, e
);
468 * Check that given {@link Object} is not NULL.
470 * @param errorMessage
471 * the error message to display if it is NULL
475 * @throws AssertException
476 * in case they differ
478 public void assertNotNull(String errorMessage
, Object actual
)
479 throws AssertException
{
480 if (actual
== null) {
481 String defaultReason
= String
.format("" //
482 + "Assertion failed!%n" //
483 + "Object should not have been NULL");
485 if (errorMessage
== null) {
486 throw new AssertException(defaultReason
);
489 throw new AssertException(errorMessage
, new AssertException(
495 * Generate the default assert message for 2 different values that were
496 * supposed to be equals.
503 * @return the message
505 public static String
generateAssertMessage(Object expected
, Object actual
) {
506 return String
.format("" //
507 + "Assertion failed!%n" //
508 + "Expected value: [%s]%n" //
509 + "Actual value: [%s]", expected
, actual
);
512 private static String
list(List
<?
> items
) {
513 StringBuilder builder
= new StringBuilder();
514 for (Object item
: items
) {
515 if (builder
.length() == 0) {
516 builder
.append(items
.size() + " item(s): ");
518 builder
.append(", ");
521 builder
.append("" + item
);
523 if (builder
.length() > 60) {
524 builder
.setLength(57);
525 builder
.append("...");
530 return builder
.toString();