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 @SuppressWarnings("unused")
48 public void setUp() throws Exception
{
52 * Tear-down the test (called when the test has been ran).
57 @SuppressWarnings("unused")
58 public void tearDown() throws Exception
{
66 public String
getName() {
71 * Actually do the test.
76 abstract public void test() throws Exception
;
81 * @throws AssertException
84 public void fail() throws AssertException
{
93 * @throws AssertException
96 public void fail(String reason
) throws AssertException
{
97 throw new AssertException("Failed!" + //
98 reason
!= null ?
"\n" + reason
: "");
102 * Check that 2 {@link Object}s are equals.
109 * @throws AssertException
110 * in case they differ
112 public void assertEquals(Object expected
, Object actual
)
113 throws AssertException
{
114 assertEquals(null, expected
, actual
);
118 * Check that 2 {@link Object}s are equals.
120 * @param errorMessage
121 * the error message to display if they differ
127 * @throws AssertException
128 * in case they differ
130 public void assertEquals(String errorMessage
, Object expected
, Object actual
)
131 throws AssertException
{
132 if ((expected
== null && actual
!= null)
133 || (expected
!= null && !expected
.equals(actual
))) {
134 if (errorMessage
== null) {
135 throw new AssertException(generateAssertMessage(expected
,
139 throw new AssertException(errorMessage
, new AssertException(
140 generateAssertMessage(expected
, actual
)));
145 * Check that 2 longs are equals.
152 * @throws AssertException
153 * in case they differ
155 public void assertEquals(long expected
, long actual
) throws AssertException
{
156 assertEquals(Long
.valueOf(expected
), Long
.valueOf(actual
));
160 * Check that 2 longs are equals.
162 * @param errorMessage
163 * the error message to display if they differ
169 * @throws AssertException
170 * in case they differ
172 public void assertEquals(String errorMessage
, long expected
, long actual
)
173 throws AssertException
{
174 assertEquals(errorMessage
, Long
.valueOf(expected
), Long
.valueOf(actual
));
178 * Check that 2 booleans are equals.
185 * @throws AssertException
186 * in case they differ
188 public void assertEquals(boolean expected
, boolean actual
)
189 throws AssertException
{
190 assertEquals(Boolean
.valueOf(expected
), Boolean
.valueOf(actual
));
194 * Check that 2 booleans are equals.
196 * @param errorMessage
197 * the error message to display if they differ
203 * @throws AssertException
204 * in case they differ
206 public void assertEquals(String errorMessage
, boolean expected
,
207 boolean actual
) throws AssertException
{
208 assertEquals(errorMessage
, Boolean
.valueOf(expected
),
209 Boolean
.valueOf(actual
));
213 * Check that 2 doubles are equals.
220 * @throws AssertException
221 * in case they differ
223 public void assertEquals(double expected
, double actual
)
224 throws AssertException
{
225 assertEquals(Double
.valueOf(expected
), Double
.valueOf(actual
));
229 * Check that 2 doubles are equals.
231 * @param errorMessage
232 * the error message to display if they differ
238 * @throws AssertException
239 * in case they differ
241 public void assertEquals(String errorMessage
, double expected
, double actual
)
242 throws AssertException
{
243 assertEquals(errorMessage
, Double
.valueOf(expected
),
244 Double
.valueOf(actual
));
248 * Check that 2 {@link List}s are equals.
250 * @param errorMessage
251 * the error message to display if they differ
257 * @throws AssertException
258 * in case they differ
260 public void assertEquals(List
<?
> expected
, List
<?
> actual
)
261 throws AssertException
{
263 assertEquals("The 2 lists don't contain the same number of items",
264 expected
.size(), actual
.size());
266 int size
= expected
.size();
267 for (int i
= 0; i
< size
; i
++) {
268 assertEquals("Line " + i
+ " (0-based) is not correct",
269 expected
.get(i
), actual
.get(i
));
274 * Check that given {@link Object} is not NULL.
276 * @param errorMessage
277 * the error message to display if it is NULL
281 * @throws AssertException
282 * in case they differ
284 public void assertNotNull(String errorMessage
, Object actual
)
285 throws AssertException
{
286 if (actual
== null) {
287 String defaultReason
= String
.format("" //
288 + "Assertion failed!%n" //
289 + "Object should not have been NULL");
291 if (errorMessage
== null) {
292 throw new AssertException(defaultReason
);
295 throw new AssertException(errorMessage
, new AssertException(
301 * Generate the default assert message for 2 different values that were
302 * supposed to be equals.
309 * @return the message
311 public static String
generateAssertMessage(Object expected
, Object actual
) {
312 return String
.format("" //
313 + "Assertion failed!%n" //
314 + "Expected value: [%s]%n" //
315 + "Actual value: [%s]", expected
, actual
);