636
|
1 |
package org.junit;
|
|
2 |
|
|
3 |
import java.lang.reflect.Array;
|
|
4 |
|
|
5 |
import org.hamcrest.Description;
|
|
6 |
import org.hamcrest.Matcher;
|
|
7 |
import org.hamcrest.StringDescription;
|
|
8 |
import org.junit.internal.ArrayComparisonFailure;
|
|
9 |
|
|
10 |
/**
|
|
11 |
* A set of assertion methods useful for writing tests. Only failed assertions
|
|
12 |
* are recorded. These methods can be used directly:
|
|
13 |
* <code>Assert.assertEquals(...)</code>, however, they read better if they
|
|
14 |
* are referenced through static import:<br/>
|
|
15 |
*
|
|
16 |
* <pre>
|
|
17 |
* import static org.junit.Assert.*;
|
|
18 |
* ...
|
|
19 |
* assertEquals(...);
|
|
20 |
* </pre>
|
|
21 |
*
|
|
22 |
* @see AssertionError
|
|
23 |
*/
|
|
24 |
public class Assert {
|
|
25 |
/**
|
|
26 |
* Protect constructor since it is a static only class
|
|
27 |
*/
|
|
28 |
protected Assert() {
|
|
29 |
}
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Asserts that a condition is true. If it isn't it throws an
|
|
33 |
* {@link AssertionError} with the given message.
|
|
34 |
*
|
|
35 |
* @param message
|
|
36 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
37 |
* okay)
|
|
38 |
* @param condition
|
|
39 |
* condition to be checked
|
|
40 |
*/
|
|
41 |
static public void assertTrue(String message, boolean condition) {
|
|
42 |
if (!condition)
|
|
43 |
fail(message);
|
|
44 |
}
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Asserts that a condition is true. If it isn't it throws an
|
|
48 |
* {@link AssertionError} without a message.
|
|
49 |
*
|
|
50 |
* @param condition
|
|
51 |
* condition to be checked
|
|
52 |
*/
|
|
53 |
static public void assertTrue(boolean condition) {
|
|
54 |
assertTrue(null, condition);
|
|
55 |
}
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Asserts that a condition is false. If it isn't it throws an
|
|
59 |
* {@link AssertionError} with the given message.
|
|
60 |
*
|
|
61 |
* @param message
|
|
62 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
63 |
* okay)
|
|
64 |
* @param condition
|
|
65 |
* condition to be checked
|
|
66 |
*/
|
|
67 |
static public void assertFalse(String message, boolean condition) {
|
|
68 |
assertTrue(message, !condition);
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Asserts that a condition is false. If it isn't it throws an
|
|
73 |
* {@link AssertionError} without a message.
|
|
74 |
*
|
|
75 |
* @param condition
|
|
76 |
* condition to be checked
|
|
77 |
*/
|
|
78 |
static public void assertFalse(boolean condition) {
|
|
79 |
assertFalse(null, condition);
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Fails a test with the given message.
|
|
84 |
*
|
|
85 |
* @param message
|
|
86 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
87 |
* okay)
|
|
88 |
* @see AssertionError
|
|
89 |
*/
|
|
90 |
static public void fail(String message) {
|
|
91 |
throw new AssertionError(message == null ? "" : message);
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Fails a test with no message.
|
|
96 |
*/
|
|
97 |
static public void fail() {
|
|
98 |
fail(null);
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Asserts that two objects are equal. If they are not, an
|
|
103 |
* {@link AssertionError} is thrown with the given message. If
|
|
104 |
* <code>expected</code> and <code>actual</code> are <code>null</code>,
|
|
105 |
* they are considered equal.
|
|
106 |
*
|
|
107 |
* @param message
|
|
108 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
109 |
* okay)
|
|
110 |
* @param expected
|
|
111 |
* expected value
|
|
112 |
* @param actual
|
|
113 |
* actual value
|
|
114 |
*/
|
|
115 |
static public void assertEquals(String message, Object expected,
|
|
116 |
Object actual) {
|
|
117 |
if (expected == null && actual == null)
|
|
118 |
return;
|
|
119 |
if (expected != null && isEquals(expected, actual))
|
|
120 |
return;
|
|
121 |
else if (expected instanceof String && actual instanceof String) {
|
|
122 |
String cleanMessage= message == null ? "" : message;
|
|
123 |
throw new ComparisonFailure(cleanMessage, (String) expected,
|
|
124 |
(String) actual);
|
|
125 |
} else
|
|
126 |
failNotEquals(message, expected, actual);
|
|
127 |
}
|
|
128 |
|
|
129 |
private static boolean isEquals(Object expected, Object actual) {
|
|
130 |
return expected.equals(actual);
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Asserts that two objects are equal. If they are not, an
|
|
135 |
* {@link AssertionError} without a message is thrown. If
|
|
136 |
* <code>expected</code> and <code>actual</code> are <code>null</code>,
|
|
137 |
* they are considered equal.
|
|
138 |
*
|
|
139 |
* @param expected
|
|
140 |
* expected value
|
|
141 |
* @param actual
|
|
142 |
* the value to check against <code>expected</code>
|
|
143 |
*/
|
|
144 |
static public void assertEquals(Object expected, Object actual) {
|
|
145 |
assertEquals(null, expected, actual);
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Asserts that two object arrays are equal. If they are not, an
|
|
150 |
* {@link AssertionError} is thrown with the given message. If
|
|
151 |
* <code>expecteds</code> and <code>actuals</code> are <code>null</code>,
|
|
152 |
* they are considered equal.
|
|
153 |
*
|
|
154 |
* @param message
|
|
155 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
156 |
* okay)
|
|
157 |
* @param expecteds
|
|
158 |
* Object array or array of arrays (multi-dimensional array) with
|
|
159 |
* expected values.
|
|
160 |
* @param actuals
|
|
161 |
* Object array or array of arrays (multi-dimensional array) with
|
|
162 |
* actual values
|
|
163 |
*/
|
|
164 |
public static void assertArrayEquals(String message, Object[] expecteds,
|
|
165 |
Object[] actuals) throws ArrayComparisonFailure {
|
|
166 |
internalArrayEquals(message, expecteds, actuals);
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Asserts that two object arrays are equal. If they are not, an
|
|
171 |
* {@link AssertionError} is thrown. If <code>expected</code> and
|
|
172 |
* <code>actual</code> are <code>null</code>, they are considered
|
|
173 |
* equal.
|
|
174 |
*
|
|
175 |
* @param expecteds
|
|
176 |
* Object array or array of arrays (multi-dimensional array) with
|
|
177 |
* expected values
|
|
178 |
* @param actuals
|
|
179 |
* Object array or array of arrays (multi-dimensional array) with
|
|
180 |
* actual values
|
|
181 |
*/
|
|
182 |
public static void assertArrayEquals(Object[] expecteds, Object[] actuals) {
|
|
183 |
assertArrayEquals(null, expecteds, actuals);
|
|
184 |
}
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Asserts that two byte arrays are equal. If they are not, an
|
|
188 |
* {@link AssertionError} is thrown with the given message.
|
|
189 |
*
|
|
190 |
* @param message
|
|
191 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
192 |
* okay)
|
|
193 |
* @param expecteds
|
|
194 |
* byte array with expected values.
|
|
195 |
* @param actuals
|
|
196 |
* byte array with actual values
|
|
197 |
*/
|
|
198 |
public static void assertArrayEquals(String message, byte[] expecteds,
|
|
199 |
byte[] actuals) throws ArrayComparisonFailure {
|
|
200 |
internalArrayEquals(message, expecteds, actuals);
|
|
201 |
}
|
|
202 |
|
|
203 |
/**
|
|
204 |
* Asserts that two byte arrays are equal. If they are not, an
|
|
205 |
* {@link AssertionError} is thrown.
|
|
206 |
*
|
|
207 |
* @param expecteds
|
|
208 |
* byte array with expected values.
|
|
209 |
* @param actuals
|
|
210 |
* byte array with actual values
|
|
211 |
*/
|
|
212 |
public static void assertArrayEquals(byte[] expecteds, byte[] actuals) {
|
|
213 |
assertArrayEquals(null, expecteds, actuals);
|
|
214 |
}
|
|
215 |
|
|
216 |
/**
|
|
217 |
* Asserts that two char arrays are equal. If they are not, an
|
|
218 |
* {@link AssertionError} is thrown with the given message.
|
|
219 |
*
|
|
220 |
* @param message
|
|
221 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
222 |
* okay)
|
|
223 |
* @param expecteds
|
|
224 |
* char array with expected values.
|
|
225 |
* @param actuals
|
|
226 |
* char array with actual values
|
|
227 |
*/
|
|
228 |
public static void assertArrayEquals(String message, char[] expecteds,
|
|
229 |
char[] actuals) throws ArrayComparisonFailure {
|
|
230 |
internalArrayEquals(message, expecteds, actuals);
|
|
231 |
}
|
|
232 |
|
|
233 |
/**
|
|
234 |
* Asserts that two char arrays are equal. If they are not, an
|
|
235 |
* {@link AssertionError} is thrown.
|
|
236 |
*
|
|
237 |
* @param expecteds
|
|
238 |
* char array with expected values.
|
|
239 |
* @param actuals
|
|
240 |
* char array with actual values
|
|
241 |
*/
|
|
242 |
public static void assertArrayEquals(char[] expecteds, char[] actuals) {
|
|
243 |
assertArrayEquals(null, expecteds, actuals);
|
|
244 |
}
|
|
245 |
|
|
246 |
/**
|
|
247 |
* Asserts that two short arrays are equal. If they are not, an
|
|
248 |
* {@link AssertionError} is thrown with the given message.
|
|
249 |
*
|
|
250 |
* @param message
|
|
251 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
252 |
* okay)
|
|
253 |
* @param expecteds
|
|
254 |
* short array with expected values.
|
|
255 |
* @param actuals
|
|
256 |
* short array with actual values
|
|
257 |
*/
|
|
258 |
public static void assertArrayEquals(String message, short[] expecteds,
|
|
259 |
short[] actuals) throws ArrayComparisonFailure {
|
|
260 |
internalArrayEquals(message, expecteds, actuals);
|
|
261 |
}
|
|
262 |
|
|
263 |
/**
|
|
264 |
* Asserts that two short arrays are equal. If they are not, an
|
|
265 |
* {@link AssertionError} is thrown.
|
|
266 |
*
|
|
267 |
* @param expecteds
|
|
268 |
* short array with expected values.
|
|
269 |
* @param actuals
|
|
270 |
* short array with actual values
|
|
271 |
*/
|
|
272 |
public static void assertArrayEquals(short[] expecteds, short[] actuals) {
|
|
273 |
assertArrayEquals(null, expecteds, actuals);
|
|
274 |
}
|
|
275 |
|
|
276 |
/**
|
|
277 |
* Asserts that two int arrays are equal. If they are not, an
|
|
278 |
* {@link AssertionError} is thrown with the given message.
|
|
279 |
*
|
|
280 |
* @param message
|
|
281 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
282 |
* okay)
|
|
283 |
* @param expecteds
|
|
284 |
* int array with expected values.
|
|
285 |
* @param actuals
|
|
286 |
* int array with actual values
|
|
287 |
*/
|
|
288 |
public static void assertArrayEquals(String message, int[] expecteds,
|
|
289 |
int[] actuals) throws ArrayComparisonFailure {
|
|
290 |
internalArrayEquals(message, expecteds, actuals);
|
|
291 |
}
|
|
292 |
|
|
293 |
/**
|
|
294 |
* Asserts that two int arrays are equal. If they are not, an
|
|
295 |
* {@link AssertionError} is thrown.
|
|
296 |
*
|
|
297 |
* @param expecteds
|
|
298 |
* int array with expected values.
|
|
299 |
* @param actuals
|
|
300 |
* int array with actual values
|
|
301 |
*/
|
|
302 |
public static void assertArrayEquals(int[] expecteds, int[] actuals) {
|
|
303 |
assertArrayEquals(null, expecteds, actuals);
|
|
304 |
}
|
|
305 |
|
|
306 |
/**
|
|
307 |
* Asserts that two long arrays are equal. If they are not, an
|
|
308 |
* {@link AssertionError} is thrown with the given message.
|
|
309 |
*
|
|
310 |
* @param message
|
|
311 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
312 |
* okay)
|
|
313 |
* @param expecteds
|
|
314 |
* long array with expected values.
|
|
315 |
* @param actuals
|
|
316 |
* long array with actual values
|
|
317 |
*/
|
|
318 |
public static void assertArrayEquals(String message, long[] expecteds,
|
|
319 |
long[] actuals) throws ArrayComparisonFailure {
|
|
320 |
internalArrayEquals(message, expecteds, actuals);
|
|
321 |
}
|
|
322 |
|
|
323 |
/**
|
|
324 |
* Asserts that two long arrays are equal. If they are not, an
|
|
325 |
* {@link AssertionError} is thrown.
|
|
326 |
*
|
|
327 |
* @param expecteds
|
|
328 |
* long array with expected values.
|
|
329 |
* @param actuals
|
|
330 |
* long array with actual values
|
|
331 |
*/
|
|
332 |
public static void assertArrayEquals(long[] expecteds, long[] actuals) {
|
|
333 |
assertArrayEquals(null, expecteds, actuals);
|
|
334 |
}
|
|
335 |
|
|
336 |
/**
|
|
337 |
* Asserts that two object arrays are equal. If they are not, an
|
|
338 |
* {@link AssertionError} is thrown with the given message. If
|
|
339 |
* <code>expecteds</code> and <code>actuals</code> are <code>null</code>,
|
|
340 |
* they are considered equal.
|
|
341 |
*
|
|
342 |
* @param message
|
|
343 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
344 |
* okay)
|
|
345 |
* @param expecteds
|
|
346 |
* Object array or array of arrays (multi-dimensional array) with
|
|
347 |
* expected values.
|
|
348 |
* @param actuals
|
|
349 |
* Object array or array of arrays (multi-dimensional array) with
|
|
350 |
* actual values
|
|
351 |
*/
|
|
352 |
private static void internalArrayEquals(String message, Object expecteds,
|
|
353 |
Object actuals) throws ArrayComparisonFailure {
|
|
354 |
if (expecteds == actuals)
|
|
355 |
return;
|
|
356 |
String header= message == null ? "" : message + ": ";
|
|
357 |
if (expecteds == null)
|
|
358 |
fail(header + "expected array was null");
|
|
359 |
if (actuals == null)
|
|
360 |
fail(header + "actual array was null");
|
|
361 |
int actualsLength= Array.getLength(actuals);
|
|
362 |
int expectedsLength= Array.getLength(expecteds);
|
|
363 |
if (actualsLength != expectedsLength)
|
|
364 |
fail(header + "array lengths differed, expected.length="
|
|
365 |
+ expectedsLength + " actual.length=" + actualsLength);
|
|
366 |
|
|
367 |
for (int i= 0; i < expectedsLength; i++) {
|
|
368 |
Object expected= Array.get(expecteds, i);
|
|
369 |
Object actual= Array.get(actuals, i);
|
|
370 |
if (isArray(expected) && isArray(actual)) {
|
|
371 |
try {
|
|
372 |
internalArrayEquals(message, expected, actual);
|
|
373 |
} catch (ArrayComparisonFailure e) {
|
|
374 |
e.addDimension(i);
|
|
375 |
throw e;
|
|
376 |
}
|
|
377 |
} else
|
|
378 |
try {
|
|
379 |
assertEquals(expected, actual);
|
|
380 |
} catch (AssertionError e) {
|
|
381 |
throw new ArrayComparisonFailure(header, e, i);
|
|
382 |
}
|
|
383 |
}
|
|
384 |
}
|
|
385 |
|
|
386 |
private static boolean isArray(Object expected) {
|
|
387 |
return expected != null && expected.getClass().isArray();
|
|
388 |
}
|
|
389 |
|
|
390 |
/**
|
|
391 |
* Asserts that two doubles or floats are equal to within a positive delta.
|
|
392 |
* If they are not, an {@link AssertionError} is thrown with the given
|
|
393 |
* message. If the expected value is infinity then the delta value is
|
|
394 |
* ignored. NaNs are considered equal:
|
|
395 |
* <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
|
|
396 |
*
|
|
397 |
* @param message
|
|
398 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
399 |
* okay)
|
|
400 |
* @param expected
|
|
401 |
* expected value
|
|
402 |
* @param actual
|
|
403 |
* the value to check against <code>expected</code>
|
|
404 |
* @param delta
|
|
405 |
* the maximum delta between <code>expected</code> and
|
|
406 |
* <code>actual</code> for which both numbers are still
|
|
407 |
* considered equal.
|
|
408 |
*/
|
|
409 |
static public void assertEquals(String message, double expected,
|
|
410 |
double actual, double delta) {
|
|
411 |
if (Double.compare(expected, actual) == 0)
|
|
412 |
return;
|
|
413 |
if (!(Math.abs(expected - actual) <= delta))
|
|
414 |
failNotEquals(message, new Double(expected), new Double(actual));
|
|
415 |
}
|
|
416 |
|
|
417 |
/**
|
|
418 |
* Asserts that two longs are equal. If they are not, an
|
|
419 |
* {@link AssertionError} is thrown.
|
|
420 |
*
|
|
421 |
* @param expected
|
|
422 |
* expected long value.
|
|
423 |
* @param actual
|
|
424 |
* actual long value
|
|
425 |
*/
|
|
426 |
static public void assertEquals(long expected, long actual) {
|
|
427 |
assertEquals(null, expected, actual);
|
|
428 |
}
|
|
429 |
|
|
430 |
/**
|
|
431 |
* Asserts that two longs are equal. If they are not, an
|
|
432 |
* {@link AssertionError} is thrown with the given message.
|
|
433 |
*
|
|
434 |
* @param message
|
|
435 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
436 |
* okay)
|
|
437 |
* @param expected
|
|
438 |
* long expected value.
|
|
439 |
* @param actual
|
|
440 |
* long actual value
|
|
441 |
*/
|
|
442 |
static public void assertEquals(String message, long expected, long actual) {
|
|
443 |
assertEquals(message, (Long) expected, (Long) actual);
|
|
444 |
}
|
|
445 |
|
|
446 |
/**
|
|
447 |
* @deprecated Use
|
|
448 |
* <code>assertEquals(double expected, double actual, double epsilon)</code>
|
|
449 |
* instead
|
|
450 |
*/
|
|
451 |
@Deprecated
|
|
452 |
static public void assertEquals(double expected, double actual) {
|
|
453 |
assertEquals(null, expected, actual);
|
|
454 |
}
|
|
455 |
|
|
456 |
/**
|
|
457 |
* @deprecated Use
|
|
458 |
* <code>assertEquals(String message, double expected, double actual, double epsilon)</code>
|
|
459 |
* instead
|
|
460 |
*/
|
|
461 |
@Deprecated
|
|
462 |
static public void assertEquals(String message, double expected,
|
|
463 |
double actual) {
|
|
464 |
fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers");
|
|
465 |
}
|
|
466 |
|
|
467 |
/**
|
|
468 |
* Asserts that two doubles or floats are equal to within a positive delta.
|
|
469 |
* If they are not, an {@link AssertionError} is thrown. If the expected
|
|
470 |
* value is infinity then the delta value is ignored.NaNs are considered
|
|
471 |
* equal: <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
|
|
472 |
*
|
|
473 |
* @param expected
|
|
474 |
* expected value
|
|
475 |
* @param actual
|
|
476 |
* the value to check against <code>expected</code>
|
|
477 |
* @param delta
|
|
478 |
* the maximum delta between <code>expected</code> and
|
|
479 |
* <code>actual</code> for which both numbers are still
|
|
480 |
* considered equal.
|
|
481 |
*/
|
|
482 |
static public void assertEquals(double expected, double actual, double delta) {
|
|
483 |
assertEquals(null, expected, actual, delta);
|
|
484 |
}
|
|
485 |
|
|
486 |
/**
|
|
487 |
* Asserts that an object isn't null. If it is an {@link AssertionError} is
|
|
488 |
* thrown with the given message.
|
|
489 |
*
|
|
490 |
* @param message
|
|
491 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
492 |
* okay)
|
|
493 |
* @param object
|
|
494 |
* Object to check or <code>null</code>
|
|
495 |
*/
|
|
496 |
static public void assertNotNull(String message, Object object) {
|
|
497 |
assertTrue(message, object != null);
|
|
498 |
}
|
|
499 |
|
|
500 |
/**
|
|
501 |
* Asserts that an object isn't null. If it is an {@link AssertionError} is
|
|
502 |
* thrown.
|
|
503 |
*
|
|
504 |
* @param object
|
|
505 |
* Object to check or <code>null</code>
|
|
506 |
*/
|
|
507 |
static public void assertNotNull(Object object) {
|
|
508 |
assertNotNull(null, object);
|
|
509 |
}
|
|
510 |
|
|
511 |
/**
|
|
512 |
* Asserts that an object is null. If it is not, an {@link AssertionError}
|
|
513 |
* is thrown with the given message.
|
|
514 |
*
|
|
515 |
* @param message
|
|
516 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
517 |
* okay)
|
|
518 |
* @param object
|
|
519 |
* Object to check or <code>null</code>
|
|
520 |
*/
|
|
521 |
static public void assertNull(String message, Object object) {
|
|
522 |
assertTrue(message, object == null);
|
|
523 |
}
|
|
524 |
|
|
525 |
/**
|
|
526 |
* Asserts that an object is null. If it isn't an {@link AssertionError} is
|
|
527 |
* thrown.
|
|
528 |
*
|
|
529 |
* @param object
|
|
530 |
* Object to check or <code>null</code>
|
|
531 |
*/
|
|
532 |
static public void assertNull(Object object) {
|
|
533 |
assertNull(null, object);
|
|
534 |
}
|
|
535 |
|
|
536 |
/**
|
|
537 |
* Asserts that two objects refer to the same object. If they are not, an
|
|
538 |
* {@link AssertionError} is thrown with the given message.
|
|
539 |
*
|
|
540 |
* @param message
|
|
541 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
542 |
* okay)
|
|
543 |
* @param expected
|
|
544 |
* the expected object
|
|
545 |
* @param actual
|
|
546 |
* the object to compare to <code>expected</code>
|
|
547 |
*/
|
|
548 |
static public void assertSame(String message, Object expected, Object actual) {
|
|
549 |
if (expected == actual)
|
|
550 |
return;
|
|
551 |
failNotSame(message, expected, actual);
|
|
552 |
}
|
|
553 |
|
|
554 |
/**
|
|
555 |
* Asserts that two objects refer to the same object. If they are not the
|
|
556 |
* same, an {@link AssertionError} without a message is thrown.
|
|
557 |
*
|
|
558 |
* @param expected
|
|
559 |
* the expected object
|
|
560 |
* @param actual
|
|
561 |
* the object to compare to <code>expected</code>
|
|
562 |
*/
|
|
563 |
static public void assertSame(Object expected, Object actual) {
|
|
564 |
assertSame(null, expected, actual);
|
|
565 |
}
|
|
566 |
|
|
567 |
/**
|
|
568 |
* Asserts that two objects do not refer to the same object. If they do
|
|
569 |
* refer to the same object, an {@link AssertionError} is thrown with the
|
|
570 |
* given message.
|
|
571 |
*
|
|
572 |
* @param message
|
|
573 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
574 |
* okay)
|
|
575 |
* @param unexpected
|
|
576 |
* the object you don't expect
|
|
577 |
* @param actual
|
|
578 |
* the object to compare to <code>unexpected</code>
|
|
579 |
*/
|
|
580 |
static public void assertNotSame(String message, Object unexpected,
|
|
581 |
Object actual) {
|
|
582 |
if (unexpected == actual)
|
|
583 |
failSame(message);
|
|
584 |
}
|
|
585 |
|
|
586 |
/**
|
|
587 |
* Asserts that two objects do not refer to the same object. If they do
|
|
588 |
* refer to the same object, an {@link AssertionError} without a message is
|
|
589 |
* thrown.
|
|
590 |
*
|
|
591 |
* @param unexpected
|
|
592 |
* the object you don't expect
|
|
593 |
* @param actual
|
|
594 |
* the object to compare to <code>unexpected</code>
|
|
595 |
*/
|
|
596 |
static public void assertNotSame(Object unexpected, Object actual) {
|
|
597 |
assertNotSame(null, unexpected, actual);
|
|
598 |
}
|
|
599 |
|
|
600 |
static private void failSame(String message) {
|
|
601 |
String formatted= "";
|
|
602 |
if (message != null)
|
|
603 |
formatted= message + " ";
|
|
604 |
fail(formatted + "expected not same");
|
|
605 |
}
|
|
606 |
|
|
607 |
static private void failNotSame(String message, Object expected,
|
|
608 |
Object actual) {
|
|
609 |
String formatted= "";
|
|
610 |
if (message != null)
|
|
611 |
formatted= message + " ";
|
|
612 |
fail(formatted + "expected same:<" + expected + "> was not:<" + actual
|
|
613 |
+ ">");
|
|
614 |
}
|
|
615 |
|
|
616 |
static private void failNotEquals(String message, Object expected,
|
|
617 |
Object actual) {
|
|
618 |
fail(format(message, expected, actual));
|
|
619 |
}
|
|
620 |
|
|
621 |
static String format(String message, Object expected, Object actual) {
|
|
622 |
String formatted= "";
|
|
623 |
if (message != null && !message.equals(""))
|
|
624 |
formatted= message + " ";
|
|
625 |
String expectedString= String.valueOf(expected);
|
|
626 |
String actualString= String.valueOf(actual);
|
|
627 |
if (expectedString.equals(actualString))
|
|
628 |
return formatted + "expected: "
|
|
629 |
+ formatClassAndValue(expected, expectedString)
|
|
630 |
+ " but was: " + formatClassAndValue(actual, actualString);
|
|
631 |
else
|
|
632 |
return formatted + "expected:<" + expectedString + "> but was:<"
|
|
633 |
+ actualString + ">";
|
|
634 |
}
|
|
635 |
|
|
636 |
private static String formatClassAndValue(Object value, String valueString) {
|
|
637 |
String className= value == null ? "null" : value.getClass().getName();
|
|
638 |
return className + "<" + valueString + ">";
|
|
639 |
}
|
|
640 |
|
|
641 |
/**
|
|
642 |
* Asserts that two object arrays are equal. If they are not, an
|
|
643 |
* {@link AssertionError} is thrown with the given message. If
|
|
644 |
* <code>expecteds</code> and <code>actuals</code> are <code>null</code>,
|
|
645 |
* they are considered equal.
|
|
646 |
*
|
|
647 |
* @param message
|
|
648 |
* the identifying message for the {@link AssertionError} (<code>null</code>
|
|
649 |
* okay)
|
|
650 |
* @param expecteds
|
|
651 |
* Object array or array of arrays (multi-dimensional array) with
|
|
652 |
* expected values.
|
|
653 |
* @param actuals
|
|
654 |
* Object array or array of arrays (multi-dimensional array) with
|
|
655 |
* actual values
|
|
656 |
* @deprecated use assertArrayEquals
|
|
657 |
*/
|
|
658 |
@Deprecated
|
|
659 |
public static void assertEquals(String message, Object[] expecteds,
|
|
660 |
Object[] actuals) {
|
|
661 |
assertArrayEquals(message, expecteds, actuals);
|
|
662 |
}
|
|
663 |
|
|
664 |
/**
|
|
665 |
* Asserts that two object arrays are equal. If they are not, an
|
|
666 |
* {@link AssertionError} is thrown. If <code>expected</code> and
|
|
667 |
* <code>actual</code> are <code>null</code>, they are considered
|
|
668 |
* equal.
|
|
669 |
*
|
|
670 |
* @param expecteds
|
|
671 |
* Object array or array of arrays (multi-dimensional array) with
|
|
672 |
* expected values
|
|
673 |
* @param actuals
|
|
674 |
* Object array or array of arrays (multi-dimensional array) with
|
|
675 |
* actual values
|
|
676 |
* @deprecated use assertArrayEquals
|
|
677 |
*/
|
|
678 |
@Deprecated
|
|
679 |
public static void assertEquals(Object[] expecteds, Object[] actuals) {
|
|
680 |
assertArrayEquals(expecteds, actuals);
|
|
681 |
}
|
|
682 |
|
|
683 |
/**
|
|
684 |
* Asserts that <code>actual</code> satisfies the condition specified by
|
|
685 |
* <code>matcher</code>. If not, an {@link AssertionError} is thrown with
|
|
686 |
* information about the matcher and failing value. Example:
|
|
687 |
*
|
|
688 |
* <pre>
|
|
689 |
* assertThat(0, is(1)); // fails:
|
|
690 |
* // failure message:
|
|
691 |
* // expected: is <1>
|
|
692 |
* // got value: <0>
|
|
693 |
* assertThat(0, is(not(1))) // passes
|
|
694 |
* </pre>
|
|
695 |
*
|
|
696 |
* @param <T>
|
|
697 |
* the static type accepted by the matcher (this can flag obvious
|
|
698 |
* compile-time problems such as {@code assertThat(1, is("a"))}
|
|
699 |
* @param actual
|
|
700 |
* the computed value being compared
|
|
701 |
* @param matcher
|
|
702 |
* an expression, built of {@link Matcher}s, specifying allowed
|
|
703 |
* values
|
|
704 |
*
|
|
705 |
* @see org.hamcrest.CoreMatchers
|
|
706 |
* @see org.junit.matchers.JUnitMatchers
|
|
707 |
*/
|
|
708 |
public static <T> void assertThat(T actual, Matcher<T> matcher) {
|
|
709 |
assertThat("", actual, matcher);
|
|
710 |
}
|
|
711 |
|
|
712 |
/**
|
|
713 |
* Asserts that <code>actual</code> satisfies the condition specified by
|
|
714 |
* <code>matcher</code>. If not, an {@link AssertionError} is thrown with
|
|
715 |
* the reason and information about the matcher and failing value. Example:
|
|
716 |
*
|
|
717 |
* <pre>
|
|
718 |
* :
|
|
719 |
* assertThat("Help! Integers don't work", 0, is(1)); // fails:
|
|
720 |
* // failure message:
|
|
721 |
* // Help! Integers don't work
|
|
722 |
* // expected: is <1>
|
|
723 |
* // got value: <0>
|
|
724 |
* assertThat("Zero is one", 0, is(not(1))) // passes
|
|
725 |
* </pre>
|
|
726 |
*
|
|
727 |
* @param reason
|
|
728 |
* additional information about the error
|
|
729 |
* @param <T>
|
|
730 |
* the static type accepted by the matcher (this can flag obvious
|
|
731 |
* compile-time problems such as {@code assertThat(1, is("a"))}
|
|
732 |
* @param actual
|
|
733 |
* the computed value being compared
|
|
734 |
* @param matcher
|
|
735 |
* an expression, built of {@link Matcher}s, specifying allowed
|
|
736 |
* values
|
|
737 |
*
|
|
738 |
* @see org.hamcrest.CoreMatchers
|
|
739 |
* @see org.junit.matchers.JUnitMatchers
|
|
740 |
*/
|
|
741 |
public static <T> void assertThat(String reason, T actual,
|
|
742 |
Matcher<T> matcher) {
|
|
743 |
if (!matcher.matches(actual)) {
|
|
744 |
Description description= new StringDescription();
|
|
745 |
description.appendText(reason);
|
|
746 |
description.appendText("\nExpected: ");
|
|
747 |
matcher.describeTo(description);
|
|
748 |
description.appendText("\n got: ").appendValue(actual)
|
|
749 |
.appendText("\n");
|
|
750 |
throw new java.lang.AssertionError(description.toString());
|
|
751 |
}
|
|
752 |
}
|
|
753 |
}
|