1 | package spiffy.junit; |
2 | |
3 | import java.util.Collection; |
4 | |
5 | import junit.framework.Assert; |
6 | |
7 | /** |
8 | * This class provides essential assertions missing from JUnit in order to create clear JUnit test cases. |
9 | * |
10 | * @author Kasper B. Graversen |
11 | */ |
12 | public class AssertHelper extends Assert { |
13 | |
14 | /** |
15 | * Check if a collection is empty |
16 | * |
17 | * @param collection |
18 | */ |
19 | public static <T> void assertEmpty(final String message, final Collection<T> collection) { |
20 | if( collection == null ) { |
21 | fail(message + "[collection is null]"); |
22 | } |
23 | if( collection.size() != 0 ) { |
24 | fail("Collection " + collection + " is not empty!"); |
25 | } |
26 | } |
27 | |
28 | /** |
29 | * Check if two arrays element for element are equal. If not an AssertionFailedError is thrown. When both argument |
30 | * arrays are <tt>null</tt>, the method silently returns. |
31 | */ |
32 | public static void assertEquals(final String message, final byte[] expected, final byte[] actual) { |
33 | if( expected == null && actual == null ) { return; } |
34 | if( expected == null || actual == null ) { |
35 | fail(message + " [Expected: " + expected + ", actual " + actual + ']'); |
36 | } |
37 | assertEquals(message + " [Length]", expected.length, actual.length); |
38 | for( int i = 0; i < expected.length; i++ ) { |
39 | assertEquals(message + " [" + i + ']', expected[i], actual[i]); |
40 | } |
41 | } |
42 | |
43 | /** |
44 | * Check if two arrays element for element are equal. If not an AssertionFailedError is thrown. When both argument |
45 | * arrays are <tt>null</tt>, the method silently returns. |
46 | */ |
47 | public static void assertEquals(final String message, final int[] expected, final int[] actual) { |
48 | if( expected == null && actual == null ) { return; } |
49 | if( expected == null || actual == null ) { |
50 | fail(message + " [Expected: " + expected + ", actual " + actual + ']'); |
51 | } |
52 | assertEquals(message + " [Length]", expected.length, actual.length); |
53 | for( int i = 0; i < expected.length; i++ ) { |
54 | assertEquals(message + " [" + i + ']', expected[i], actual[i]); |
55 | } |
56 | } |
57 | |
58 | /** |
59 | * Check if two arrays element for element are equal. If not an AssertionFailedError is thrown. When both argument |
60 | * arrays are <tt>null</tt>, the method silently returns. |
61 | */ |
62 | public static void assertEquals(final String message, final Object[] expected, final Object[] actual) { |
63 | if( expected == null && actual == null ) { return; } |
64 | if( expected == null || actual == null ) { |
65 | fail(message + " [Expected: " + expected + ", actual " + actual + ']'); |
66 | } |
67 | assertEquals(message + " [Length]", expected.length, actual.length); |
68 | for( int i = 0; i < expected.length; i++ ) { |
69 | assertEquals(message + " [" + i + ']', expected[i], actual[i]); |
70 | } |
71 | } |
72 | |
73 | /** |
74 | * check that the actual value is greater than the expected |
75 | */ |
76 | public static void assertGreaterThan(final String message, final double expectedGreaterThan, final double actual) { |
77 | if( expectedGreaterThan >= actual ) { |
78 | fail(message + ". Actual not greater than expected, " + expectedGreaterThan + " >= " + actual); |
79 | } |
80 | } |
81 | |
82 | /** |
83 | * check that the actual value is greater than the expected |
84 | */ |
85 | public static void assertGreaterThan(final String message, final int expectedGreaterThan, final int actual) { |
86 | if( expectedGreaterThan >= actual ) { |
87 | fail(message + ". Actual not greater than expected, " + expectedGreaterThan + " >= " + actual); |
88 | } |
89 | } |
90 | |
91 | public static void assertNotEquals(final int expected, final int actual) { |
92 | if( expected == actual ) { |
93 | fail("The two values are equal! Expected: " + expected + " actual: " + actual); |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * check that the actual value is not the expected |
99 | */ |
100 | public static void assertNotEquals(final String message, final double expected, final double actual) { |
101 | if( expected == actual ) { |
102 | fail(message + ". The two values are equal! Expected: " + expected + " actual: " + actual); |
103 | } |
104 | } |
105 | |
106 | /** |
107 | * check that the actual value is not the expected |
108 | */ |
109 | public static void assertNotEquals(final String message, final int expected, final int actual) { |
110 | if( expected == actual ) { |
111 | fail(message + ". The two values are equal! Expected: " + expected + " actual: " + actual); |
112 | } |
113 | } |
114 | |
115 | } |