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(new Long(expected
), new Long(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
, new Long(expected
), new Long(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(new Boolean(expected
), new Boolean(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
, new Boolean(expected
), new Boolean(actual
));
210 * Check that 2 {@link Object}s are equals.
217 * @throws AssertException
218 * in case they differ
220 public void assertEquals(double expected
, double actual
)
221 throws AssertException
{
222 assertEquals(new Double(expected
), new Double(actual
));
226 * Check that 2 {@link Object}s are equals.
228 * @param errorMessage
229 * the error message to display if they differ
235 * @throws AssertException
236 * in case they differ
238 public void assertEquals(String errorMessage
, double expected
, double actual
)
239 throws AssertException
{
240 assertEquals(errorMessage
, new Double(expected
), new Double(actual
));
244 * Check that given {@link Object} is not NULL.
246 * @param errorMessage
247 * the error message to display if it is NULL
251 * @throws AssertException
252 * in case they differ
254 public void assertNotNull(String errorMessage
, Object actual
)
255 throws AssertException
{
256 if (actual
== null) {
257 String defaultReason
= String
.format("" //
258 + "Assertion failed!\n" //
259 + "Object should have been NULL: [%s]", actual
);
261 if (errorMessage
== null) {
262 throw new AssertException(defaultReason
);
265 throw new AssertException(errorMessage
, new AssertException(
271 * Generate the default assert message for 2 different values that were
272 * supposed to be equals.
279 * @return the message
281 public static String
generateAssertMessage(Object expected
, Object actual
) {
282 return String
.format("" //
283 + "Assertion failed!\n" //
284 + "Expected value: [%s]\n" //
285 + "Actual value: [%s]", expected
, actual
);