b37ea987ffca27677a32eb5d31d05e2632974c52
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
) {
26 * Create a new {@link TestCase}.
31 public TestCase(String name
) {
36 * Setup the test (called before the test is run).
41 public void setUp() throws Exception
{
45 * Tear-down the test (called when the test has been ran).
50 public void tearDown() throws Exception
{
58 public String
getName() {
63 * Actually do the test.
68 abstract public void test() throws Exception
;
73 * @throws AssertException
76 public void fail() throws AssertException
{
85 * @throws AssertException
88 public void fail(String reason
) throws AssertException
{
89 throw new AssertException("Failed!" + //
90 reason
!= null ?
"\n" + reason
: "");
94 * Check that 2 {@link Object}s are equals.
101 * @throws AssertException
102 * in case they differ
104 public void assertEquals(Object expected
, Object actual
)
105 throws AssertException
{
106 assertEquals(null, expected
, actual
);
110 * Check that 2 {@link Object}s are equals.
113 * error message to display if they differ
119 * @throws AssertException
120 * in case they differ
122 public void assertEquals(String errorMessage
, Object expected
, Object actual
)
123 throws AssertException
{
125 if (errorMessage
== null) {
126 errorMessage
= generateAssertMessage(expected
, actual
);
129 if ((expected
== null && actual
!= null)
130 || (expected
!= null && !expected
.equals(actual
))) {
131 throw new AssertException(errorMessage
);
136 * Check that 2 {@link Object}s are equals.
143 * @throws AssertException
144 * in case they differ
146 public void assertEquals(long expected
, long actual
) throws AssertException
{
147 assertEquals(new Long(expected
), new Long(actual
));
151 * Check that 2 {@link Object}s are equals.
154 * error message to display if they differ
160 * @throws AssertException
161 * in case they differ
163 public void assertEquals(String errorMessage
, long expected
, long actual
)
164 throws AssertException
{
165 assertEquals(errorMessage
, new Long(expected
), new Long(actual
));
169 * Check that 2 {@link Object}s are equals.
176 * @throws AssertException
177 * in case they differ
179 public void assertEquals(boolean expected
, boolean actual
)
180 throws AssertException
{
181 assertEquals(new Boolean(expected
), new Boolean(actual
));
185 * Check that 2 {@link Object}s are equals.
188 * error message to display if they differ
194 * @throws AssertException
195 * in case they differ
197 public void assertEquals(String errorMessage
, boolean expected
,
198 boolean actual
) throws AssertException
{
199 assertEquals(errorMessage
, new Boolean(expected
), new Boolean(actual
));
203 * Check that 2 {@link Object}s are equals.
210 * @throws AssertException
211 * in case they differ
213 public void assertEquals(double expected
, double actual
)
214 throws AssertException
{
215 assertEquals(new Double(expected
), new Double(actual
));
219 * Check that 2 {@link Object}s are equals.
222 * error message to display if they differ
228 * @throws AssertException
229 * in case they differ
231 public void assertEquals(String errorMessage
, double expected
, double actual
)
232 throws AssertException
{
233 assertEquals(errorMessage
, new Double(expected
), new Double(actual
));
237 * Check that given {@link Object} is not NULL.
240 * error message to display if it is NULL
244 * @throws AssertException
245 * in case they differ
247 public void assertNotNull(String errorMessage
, Object actual
)
248 throws AssertException
{
249 if (actual
== null) {
250 if (errorMessage
== null) {
251 errorMessage
= String
.format("" //
252 + "Assertion failed!\n" //
253 + "Object should have been NULL: [%s]", actual
);
255 throw new AssertException(errorMessage
);
260 * Generate the default assert message for 2 different values that were
261 * supposed to be equals.
268 * @return the message
270 public static String
generateAssertMessage(Object expected
, Object actual
) {
271 return String
.format("" //
272 + "Assertion failed!\n" //
273 + "Expected value: [%s]\n" //
274 + "Actual value: [%s]", expected
, actual
);