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
{
114 * @throws AssertException
117 public void fail(String reason
) throws AssertException
{
118 throw new AssertException("Failed!" + //
119 reason
!= null ?
"\n" + reason
: "");
123 * Check that 2 {@link Object}s are equals.
130 * @throws AssertException
131 * in case they differ
133 public void assertEquals(Object expected
, Object actual
)
134 throws AssertException
{
135 assertEquals(null, expected
, actual
);
139 * Check that 2 {@link Object}s are equals.
141 * @param errorMessage
142 * the error message to display if they differ
148 * @throws AssertException
149 * in case they differ
151 public void assertEquals(String errorMessage
, Object expected
, Object actual
)
152 throws AssertException
{
153 if ((expected
== null && actual
!= null)
154 || (expected
!= null && !expected
.equals(actual
))) {
155 if (errorMessage
== null) {
156 throw new AssertException(generateAssertMessage(expected
,
160 throw new AssertException(errorMessage
, new AssertException(
161 generateAssertMessage(expected
, actual
)));
166 * Check that 2 longs are equals.
173 * @throws AssertException
174 * in case they differ
176 public void assertEquals(long expected
, long actual
) throws AssertException
{
177 assertEquals(Long
.valueOf(expected
), Long
.valueOf(actual
));
181 * Check that 2 longs are equals.
183 * @param errorMessage
184 * the error message to display if they differ
190 * @throws AssertException
191 * in case they differ
193 public void assertEquals(String errorMessage
, long expected
, long actual
)
194 throws AssertException
{
195 assertEquals(errorMessage
, Long
.valueOf(expected
), Long
.valueOf(actual
));
199 * Check that 2 booleans are equals.
206 * @throws AssertException
207 * in case they differ
209 public void assertEquals(boolean expected
, boolean actual
)
210 throws AssertException
{
211 assertEquals(Boolean
.valueOf(expected
), Boolean
.valueOf(actual
));
215 * Check that 2 booleans are equals.
217 * @param errorMessage
218 * the error message to display if they differ
224 * @throws AssertException
225 * in case they differ
227 public void assertEquals(String errorMessage
, boolean expected
,
228 boolean actual
) throws AssertException
{
229 assertEquals(errorMessage
, Boolean
.valueOf(expected
),
230 Boolean
.valueOf(actual
));
234 * Check that 2 doubles are equals.
241 * @throws AssertException
242 * in case they differ
244 public void assertEquals(double expected
, double actual
)
245 throws AssertException
{
246 assertEquals(Double
.valueOf(expected
), Double
.valueOf(actual
));
250 * Check that 2 doubles are equals.
252 * @param errorMessage
253 * the error message to display if they differ
259 * @throws AssertException
260 * in case they differ
262 public void assertEquals(String errorMessage
, double expected
, double actual
)
263 throws AssertException
{
264 assertEquals(errorMessage
, Double
.valueOf(expected
),
265 Double
.valueOf(actual
));
269 * Check that 2 {@link List}s are equals.
271 * @param errorMessage
272 * the error message to display if they differ
278 * @throws AssertException
279 * in case they differ
281 public void assertEquals(List
<?
> expected
, List
<?
> actual
)
282 throws AssertException
{
283 assertEquals("Assertion failed", expected
, actual
);
287 * Check that 2 {@link List}s are equals.
289 * @param errorMessage
290 * the error message to display if they differ
295 * @param errorMessage
296 * the error message to display if they differ
298 * @throws AssertException
299 * in case they differ
301 public void assertEquals(String errorMessage
, List
<?
> expected
,
302 List
<?
> actual
) throws AssertException
{
304 if (expected
.size() != actual
.size()) {
305 assertEquals(errorMessage
+ ": not same number of items",
306 list(expected
), list(actual
));
309 int size
= expected
.size();
310 for (int i
= 0; i
< size
; i
++) {
311 assertEquals(errorMessage
+ ": item " + i
312 + " (0-based) is not correct", expected
.get(i
),
318 * Check that given {@link Object} is not NULL.
320 * @param errorMessage
321 * the error message to display if it is NULL
325 * @throws AssertException
326 * in case they differ
328 public void assertNotNull(String errorMessage
, Object actual
)
329 throws AssertException
{
330 if (actual
== null) {
331 String defaultReason
= String
.format("" //
332 + "Assertion failed!%n" //
333 + "Object should not have been NULL");
335 if (errorMessage
== null) {
336 throw new AssertException(defaultReason
);
339 throw new AssertException(errorMessage
, new AssertException(
345 * Generate the default assert message for 2 different values that were
346 * supposed to be equals.
353 * @return the message
355 public static String
generateAssertMessage(Object expected
, Object actual
) {
356 return String
.format("" //
357 + "Assertion failed!%n" //
358 + "Expected value: [%s]%n" //
359 + "Actual value: [%s]", expected
, actual
);
362 private static String
list(List
<?
> items
) {
363 StringBuilder builder
= new StringBuilder();
364 for (Object item
: items
) {
365 if (builder
.length() == 0) {
366 builder
.append(items
.size() + " item(s): ");
368 builder
.append(", ");
371 builder
.append("" + item
);
373 if (builder
.length() > 60) {
374 builder
.setLength(57);
375 builder
.append("...");
380 return builder
.toString();