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 * Setup the test (called before the test is run).
47 public void setUp() throws Exception
{
51 * Tear-down the test (called when the test has been ran).
56 public void tearDown() throws Exception
{
64 public String
getName() {
69 * Actually do the test.
74 abstract public void test() throws Exception
;
79 * @throws AssertException
82 public void fail() throws AssertException
{
91 * @throws AssertException
94 public void fail(String reason
) throws AssertException
{
95 throw new AssertException("Failed!" + //
96 reason
!= null ?
"\n" + reason
: "");
100 * Check that 2 {@link Object}s are equals.
107 * @throws AssertException
108 * in case they differ
110 public void assertEquals(Object expected
, Object actual
)
111 throws AssertException
{
112 assertEquals(null, expected
, actual
);
116 * Check that 2 {@link Object}s are equals.
118 * @param errorMessage
119 * the error message to display if they differ
125 * @throws AssertException
126 * in case they differ
128 public void assertEquals(String errorMessage
, Object expected
, Object actual
)
129 throws AssertException
{
130 if ((expected
== null && actual
!= null)
131 || (expected
!= null && !expected
.equals(actual
))) {
132 if (errorMessage
== null) {
133 throw new AssertException(generateAssertMessage(expected
,
137 throw new AssertException(errorMessage
, new AssertException(
138 generateAssertMessage(expected
, actual
)));
143 * Check that 2 longs are equals.
150 * @throws AssertException
151 * in case they differ
153 public void assertEquals(long expected
, long actual
) throws AssertException
{
154 assertEquals(Long
.valueOf(expected
), Long
.valueOf(actual
));
158 * Check that 2 longs are equals.
160 * @param errorMessage
161 * the error message to display if they differ
167 * @throws AssertException
168 * in case they differ
170 public void assertEquals(String errorMessage
, long expected
, long actual
)
171 throws AssertException
{
172 assertEquals(errorMessage
, Long
.valueOf(expected
), Long
.valueOf(actual
));
176 * Check that 2 booleans are equals.
183 * @throws AssertException
184 * in case they differ
186 public void assertEquals(boolean expected
, boolean actual
)
187 throws AssertException
{
188 assertEquals(Boolean
.valueOf(expected
), Boolean
.valueOf(actual
));
192 * Check that 2 booleans are equals.
194 * @param errorMessage
195 * the error message to display if they differ
201 * @throws AssertException
202 * in case they differ
204 public void assertEquals(String errorMessage
, boolean expected
,
205 boolean actual
) throws AssertException
{
206 assertEquals(errorMessage
, Boolean
.valueOf(expected
),
207 Boolean
.valueOf(actual
));
211 * Check that 2 doubles are equals.
218 * @throws AssertException
219 * in case they differ
221 public void assertEquals(double expected
, double actual
)
222 throws AssertException
{
223 assertEquals(Double
.valueOf(expected
), Double
.valueOf(actual
));
227 * Check that 2 doubles are equals.
229 * @param errorMessage
230 * the error message to display if they differ
236 * @throws AssertException
237 * in case they differ
239 public void assertEquals(String errorMessage
, double expected
, double actual
)
240 throws AssertException
{
241 assertEquals(errorMessage
, Double
.valueOf(expected
),
242 Double
.valueOf(actual
));
246 * Check that 2 {@link List}s are equals.
248 * @param errorMessage
249 * the error message to display if they differ
255 * @throws AssertException
256 * in case they differ
258 public void assertEquals(List
<?
> expected
, List
<?
> actual
)
259 throws AssertException
{
260 assertEquals("Assertion failed", expected
, actual
);
264 * Check that 2 {@link List}s are equals.
266 * @param errorMessage
267 * the error message to display if they differ
272 * @param errorMessage
273 * the error message to display if they differ
275 * @throws AssertException
276 * in case they differ
278 public void assertEquals(String errorMessage
, List
<?
> expected
,
279 List
<?
> actual
) throws AssertException
{
281 if (expected
.size() != actual
.size()) {
282 assertEquals(errorMessage
+ ": not same number of items",
283 list(expected
), list(actual
));
286 int size
= expected
.size();
287 for (int i
= 0; i
< size
; i
++) {
288 assertEquals(errorMessage
+ ": item " + i
289 + " (0-based) is not correct", expected
.get(i
),
295 * Check that given {@link Object} is not NULL.
297 * @param errorMessage
298 * the error message to display if it is NULL
302 * @throws AssertException
303 * in case they differ
305 public void assertNotNull(String errorMessage
, Object actual
)
306 throws AssertException
{
307 if (actual
== null) {
308 String defaultReason
= String
.format("" //
309 + "Assertion failed!%n" //
310 + "Object should not have been NULL");
312 if (errorMessage
== null) {
313 throw new AssertException(defaultReason
);
316 throw new AssertException(errorMessage
, new AssertException(
322 * Generate the default assert message for 2 different values that were
323 * supposed to be equals.
330 * @return the message
332 public static String
generateAssertMessage(Object expected
, Object actual
) {
333 return String
.format("" //
334 + "Assertion failed!%n" //
335 + "Expected value: [%s]%n" //
336 + "Actual value: [%s]", expected
, actual
);
339 private static String
list(List
<?
> items
) {
340 StringBuilder builder
= new StringBuilder();
341 for (Object item
: items
) {
342 if (builder
.length() == 0) {
343 builder
.append(items
.size() + " item(s): ");
345 builder
.append(", ");
348 builder
.append("" + item
);
350 if (builder
.length() > 60) {
351 builder
.setLength(57);
352 builder
.append("...");
357 return builder
.toString();