1 package be
.nikiroo
.utils
.test
;
4 import java
.util
.Arrays
;
5 import java
.util
.Collections
;
8 import java
.util
.Map
.Entry
;
10 import be
.nikiroo
.utils
.IOUtils
;
13 * A {@link TestCase} that can be run with {@link TestLauncher}.
17 abstract public class TestCase
{
19 * The type of {@link Exception} used to signal a failed assertion or a
24 class AssertException
extends Exception
{
25 private static final long serialVersionUID
= 1L;
27 public AssertException(String reason
, Exception source
) {
28 super(reason
, source
);
31 public AssertException(String reason
) {
39 * Create a new {@link TestCase}.
44 public TestCase(String name
) {
49 * This constructor can be used if you require a no-param constructor. In
50 * this case, you are allowed to set the name manually via
51 * {@link TestCase#setName}.
53 protected TestCase() {
58 * Setup the test (called before the test is run).
63 public void setUp() throws Exception
{
67 * Tear-down the test (called when the test has been ran).
72 public void tearDown() throws Exception
{
80 public String
getName() {
88 * the new name (internal use only)
90 * @return this (so we can chain and so we can initialize it in a member
91 * variable if this is an anonymous inner class)
93 protected TestCase
setName(String name
) {
99 * Actually do the test.
104 abstract public void test() throws Exception
;
109 * @throws AssertException
112 public void fail() throws AssertException
{
122 * @throws AssertException
125 public void fail(String reason
) throws AssertException
{
135 * the exception that caused the failure (can be NULL)
137 * @throws AssertException
140 public void fail(String reason
, Exception e
) throws AssertException
{
141 throw new AssertException("Failed!" + //
142 reason
!= null ?
"\n" + reason
: "", e
);
146 * Check that 2 {@link Object}s are equals.
153 * @throws AssertException
154 * in case they differ
156 public void assertEquals(Object expected
, Object actual
)
157 throws AssertException
{
158 assertEquals(null, expected
, actual
);
162 * Check that 2 {@link Object}s are equals.
164 * @param errorMessage
165 * the error message to display if they differ
171 * @throws AssertException
172 * in case they differ
174 public void assertEquals(String errorMessage
, Object expected
, Object actual
)
175 throws AssertException
{
176 if ((expected
== null && actual
!= null)
177 || (expected
!= null && !expected
.equals(actual
))) {
178 if (errorMessage
== null) {
179 throw new AssertException(generateAssertMessage(expected
,
183 throw new AssertException(errorMessage
, new AssertException(
184 generateAssertMessage(expected
, actual
)));
189 * Check that 2 longs are equals.
196 * @throws AssertException
197 * in case they differ
199 public void assertEquals(long expected
, long actual
) throws AssertException
{
200 assertEquals(Long
.valueOf(expected
), Long
.valueOf(actual
));
204 * Check that 2 longs are equals.
206 * @param errorMessage
207 * the error message to display if they differ
213 * @throws AssertException
214 * in case they differ
216 public void assertEquals(String errorMessage
, long expected
, long actual
)
217 throws AssertException
{
218 assertEquals(errorMessage
, Long
.valueOf(expected
), Long
.valueOf(actual
));
222 * Check that 2 booleans are equals.
229 * @throws AssertException
230 * in case they differ
232 public void assertEquals(boolean expected
, boolean actual
)
233 throws AssertException
{
234 assertEquals(Boolean
.valueOf(expected
), Boolean
.valueOf(actual
));
238 * Check that 2 booleans are equals.
240 * @param errorMessage
241 * the error message to display if they differ
247 * @throws AssertException
248 * in case they differ
250 public void assertEquals(String errorMessage
, boolean expected
,
251 boolean actual
) throws AssertException
{
252 assertEquals(errorMessage
, Boolean
.valueOf(expected
),
253 Boolean
.valueOf(actual
));
257 * Check that 2 doubles are equals.
264 * @throws AssertException
265 * in case they differ
267 public void assertEquals(double expected
, double actual
)
268 throws AssertException
{
269 assertEquals(Double
.valueOf(expected
), Double
.valueOf(actual
));
273 * Check that 2 doubles are equals.
275 * @param errorMessage
276 * the error message to display if they differ
282 * @throws AssertException
283 * in case they differ
285 public void assertEquals(String errorMessage
, double expected
, double actual
)
286 throws AssertException
{
287 assertEquals(errorMessage
, Double
.valueOf(expected
),
288 Double
.valueOf(actual
));
292 * Check that 2 {@link List}s are equals.
294 * @param errorMessage
295 * the error message to display if they differ
301 * @throws AssertException
302 * in case they differ
304 public void assertEquals(List
<?
> expected
, List
<?
> actual
)
305 throws AssertException
{
306 assertEquals("Assertion failed", expected
, actual
);
310 * Check that 2 {@link List}s are equals.
312 * @param errorMessage
313 * the error message to display if they differ
319 * @throws AssertException
320 * in case they differ
322 public void assertEquals(String errorMessage
, List
<?
> expected
,
323 List
<?
> actual
) throws AssertException
{
325 if (expected
.size() != actual
.size()) {
326 assertEquals(errorMessage
+ ": not same number of items",
327 list(expected
), list(actual
));
330 int size
= expected
.size();
331 for (int i
= 0; i
< size
; i
++) {
332 assertEquals(errorMessage
+ ": item " + i
333 + " (0-based) is not correct", expected
.get(i
),
339 * Check that 2 {@link File}s are equals, by doing a line-by-line
346 * @param errorMessage
347 * the error message to display if they differ
349 * @throws AssertException
350 * in case they differ
352 public void assertEquals(File expected
, File actual
) throws AssertException
{
353 assertEquals(generateAssertMessage(expected
, actual
), expected
, actual
);
357 * Check that 2 {@link File}s are equals, by doing a line-by-line
360 * @param errorMessage
361 * the error message to display if they differ
367 * @throws AssertException
368 * in case they differ
370 public void assertEquals(String errorMessage
, File expected
, File actual
)
371 throws AssertException
{
372 assertEquals(errorMessage
, expected
, actual
, null);
376 * Check that 2 {@link File}s are equals, by doing a line-by-line
379 * @param errorMessage
380 * the error message to display if they differ
386 * skip the lines starting with some values for the given files
387 * (relative path from base directory in recursive mode)
389 * @throws AssertException
390 * in case they differ
392 public void assertEquals(String errorMessage
, File expected
, File actual
,
393 Map
<String
, List
<String
>> skipCompare
) throws AssertException
{
394 assertEquals(errorMessage
, expected
, actual
, skipCompare
, null);
397 private void assertEquals(String errorMessage
, File expected
, File actual
,
398 Map
<String
, List
<String
>> skipCompare
, String removeFromName
)
399 throws AssertException
{
401 if (expected
.isDirectory() || actual
.isDirectory()) {
402 assertEquals(errorMessage
+ ": type mismatch: expected a "
403 + (expected
.isDirectory() ?
"directory" : "file")
405 + (actual
.isDirectory() ?
"directory" : "file"),
406 expected
.isDirectory(), actual
.isDirectory());
408 List
<String
> expectedFiles
= Arrays
.asList(expected
.list());
409 Collections
.sort(expectedFiles
);
410 List
<String
> actualFiles
= Arrays
.asList(actual
.list());
411 Collections
.sort(actualFiles
);
413 assertEquals(errorMessage
, expectedFiles
, actualFiles
);
414 for (int i
= 0; i
< actualFiles
.size(); i
++) {
415 File expectedFile
= new File(expected
, expectedFiles
.get(i
));
416 File actualFile
= new File(actual
, actualFiles
.get(i
));
418 assertEquals(errorMessage
, expectedFile
, actualFile
,
419 skipCompare
, expected
.getAbsolutePath());
423 List
<String
> expectedLines
= Arrays
.asList(IOUtils
424 .readSmallFile(expected
).split("\n"));
425 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
426 actual
).split("\n"));
428 String name
= expected
.getAbsolutePath();
429 if (removeFromName
!= null && name
.startsWith(removeFromName
)) {
430 name
= expected
.getName()
431 + name
.substring(removeFromName
.length());
434 assertEquals(errorMessage
+ ": " + name
435 + ": the number of lines is not the same",
436 expectedLines
.size(), resultLines
.size());
438 for (int j
= 0; j
< expectedLines
.size(); j
++) {
439 String expectedLine
= expectedLines
.get(j
);
440 String resultLine
= resultLines
.get(j
);
442 boolean skip
= false;
443 if (skipCompare
!= null) {
444 for (Entry
<String
, List
<String
>> skipThose
: skipCompare
446 for (String skipStart
: skipThose
.getValue()) {
447 if (name
.endsWith(skipThose
.getKey())
448 && expectedLine
.startsWith(skipStart
)
449 && resultLine
.startsWith(skipStart
)) {
460 assertEquals(errorMessage
+ ": line " + (j
+ 1)
461 + " is not the same in file " + name
, expectedLine
,
464 } catch (Exception e
) {
465 throw new AssertException(errorMessage
, e
);
471 * Check that given {@link Object} is not NULL.
473 * @param errorMessage
474 * the error message to display if it is NULL
478 * @throws AssertException
479 * in case they differ
481 public void assertNotNull(String errorMessage
, Object actual
)
482 throws AssertException
{
483 if (actual
== null) {
484 String defaultReason
= String
.format("" //
485 + "Assertion failed!%n" //
486 + "Object should not have been NULL");
488 if (errorMessage
== null) {
489 throw new AssertException(defaultReason
);
492 throw new AssertException(errorMessage
, new AssertException(
498 * Generate the default assert message for 2 different values that were
499 * supposed to be equals.
506 * @return the message
508 public static String
generateAssertMessage(Object expected
, Object actual
) {
509 return String
.format("" //
510 + "Assertion failed!%n" //
511 + "Expected value: [%s]%n" //
512 + "Actual value: [%s]", expected
, actual
);
515 private static String
list(List
<?
> items
) {
516 StringBuilder builder
= new StringBuilder();
517 for (Object item
: items
) {
518 if (builder
.length() == 0) {
519 builder
.append(items
.size() + " item(s): ");
521 builder
.append(", ");
524 builder
.append("" + item
);
526 if (builder
.length() > 60) {
527 builder
.setLength(57);
528 builder
.append("...");
533 return builder
.toString();