1 package be
.nikiroo
.utils
.test
;
4 * A {@link TestCase} that can be run with {@link TestLauncher}.
8 abstract public class TestCase
{
10 * The type of {@link Exception} used to signal a failed assertion or a
15 class AssertException
extends Exception
{
16 private static final long serialVersionUID
= 1L;
18 public AssertException(String reason
, Exception source
) {
19 super(reason
, source
);
22 public AssertException(String reason
) {
30 * Create a new {@link TestCase}.
35 public TestCase(String name
) {
40 * Setup the test (called before the test is run).
45 @SuppressWarnings("unused")
46 public void setUp() throws Exception
{
50 * Tear-down the test (called when the test has been ran).
55 @SuppressWarnings("unused")
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 {@link Object}s 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 {@link Object}s 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 {@link Object}s 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 {@link Object}s 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 {@link Object}s 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 {@link Object}s 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 given {@link Object} is not NULL.
248 * @param errorMessage
249 * the error message to display if it is NULL
253 * @throws AssertException
254 * in case they differ
256 public void assertNotNull(String errorMessage
, Object actual
)
257 throws AssertException
{
258 if (actual
== null) {
259 String defaultReason
= String
.format("" //
260 + "Assertion failed!%n" //
261 + "Object should not have been NULL");
263 if (errorMessage
== null) {
264 throw new AssertException(defaultReason
);
267 throw new AssertException(errorMessage
, new AssertException(
273 * Generate the default assert message for 2 different values that were
274 * supposed to be equals.
281 * @return the message
283 public static String
generateAssertMessage(Object expected
, Object actual
) {
284 return String
.format("" //
285 + "Assertion failed!%n" //
286 + "Expected value: [%s]%n" //
287 + "Actual value: [%s]", expected
, actual
);