490edbb4ec9d47db7f2c24cf21630ac303dc7044
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 public void setUp() throws Exception
{
49 * Tear-down the test (called when the test has been ran).
54 public void tearDown() throws Exception
{
62 public String
getName() {
67 * Actually do the test.
72 abstract public void test() throws Exception
;
77 * @throws AssertException
80 public void fail() throws AssertException
{
89 * @throws AssertException
92 public void fail(String reason
) throws AssertException
{
93 throw new AssertException("Failed!" + //
94 reason
!= null ?
"\n" + reason
: "");
98 * Check that 2 {@link Object}s are equals.
105 * @throws AssertException
106 * in case they differ
108 public void assertEquals(Object expected
, Object actual
)
109 throws AssertException
{
110 assertEquals(null, expected
, actual
);
114 * Check that 2 {@link Object}s are equals.
116 * @param errorMessage
117 * the error message to display if they differ
123 * @throws AssertException
124 * in case they differ
126 public void assertEquals(String errorMessage
, Object expected
, Object actual
)
127 throws AssertException
{
128 if ((expected
== null && actual
!= null)
129 || (expected
!= null && !expected
.equals(actual
))) {
130 if (errorMessage
== null) {
131 throw new AssertException(generateAssertMessage(expected
,
135 throw new AssertException(errorMessage
, new AssertException(
136 generateAssertMessage(expected
, actual
)));
141 * Check that 2 {@link Object}s are equals.
148 * @throws AssertException
149 * in case they differ
151 public void assertEquals(long expected
, long actual
) throws AssertException
{
152 assertEquals(new Long(expected
), new Long(actual
));
156 * Check that 2 {@link Object}s are equals.
158 * @param errorMessage
159 * the error message to display if they differ
165 * @throws AssertException
166 * in case they differ
168 public void assertEquals(String errorMessage
, long expected
, long actual
)
169 throws AssertException
{
170 assertEquals(errorMessage
, new Long(expected
), new Long(actual
));
174 * Check that 2 {@link Object}s are equals.
181 * @throws AssertException
182 * in case they differ
184 public void assertEquals(boolean expected
, boolean actual
)
185 throws AssertException
{
186 assertEquals(new Boolean(expected
), new Boolean(actual
));
190 * Check that 2 {@link Object}s are equals.
192 * @param errorMessage
193 * the error message to display if they differ
199 * @throws AssertException
200 * in case they differ
202 public void assertEquals(String errorMessage
, boolean expected
,
203 boolean actual
) throws AssertException
{
204 assertEquals(errorMessage
, new Boolean(expected
), new Boolean(actual
));
208 * Check that 2 {@link Object}s are equals.
215 * @throws AssertException
216 * in case they differ
218 public void assertEquals(double expected
, double actual
)
219 throws AssertException
{
220 assertEquals(new Double(expected
), new Double(actual
));
224 * Check that 2 {@link Object}s are equals.
226 * @param errorMessage
227 * the error message to display if they differ
233 * @throws AssertException
234 * in case they differ
236 public void assertEquals(String errorMessage
, double expected
, double actual
)
237 throws AssertException
{
238 assertEquals(errorMessage
, new Double(expected
), new Double(actual
));
242 * Check that given {@link Object} is not NULL.
244 * @param errorMessage
245 * the error message to display if it is NULL
249 * @throws AssertException
250 * in case they differ
252 public void assertNotNull(String errorMessage
, Object actual
)
253 throws AssertException
{
254 if (actual
== null) {
255 String defaultReason
= String
.format("" //
256 + "Assertion failed!\n" //
257 + "Object should have been NULL: [%s]", actual
);
259 if (errorMessage
== null) {
260 throw new AssertException(defaultReason
);
263 throw new AssertException(errorMessage
, new AssertException(
269 * Generate the default assert message for 2 different values that were
270 * supposed to be equals.
277 * @return the message
279 public static String
generateAssertMessage(Object expected
, Object actual
) {
280 return String
.format("" //
281 + "Assertion failed!\n" //
282 + "Expected value: [%s]\n" //
283 + "Actual value: [%s]", expected
, actual
);