| 1 | package be.nikiroo.utils.test; |
| 2 | |
| 3 | /** |
| 4 | * A {@link TestCase} that can be run with {@link TestLauncher}. |
| 5 | * |
| 6 | * @author niki |
| 7 | */ |
| 8 | abstract public class TestCase { |
| 9 | /** |
| 10 | * The type of {@link Exception} used to signal a failed assertion or a |
| 11 | * force-fail. |
| 12 | * |
| 13 | * @author niki |
| 14 | */ |
| 15 | class AssertException extends Exception { |
| 16 | private static final long serialVersionUID = 1L; |
| 17 | |
| 18 | public AssertException(String reason, Exception source) { |
| 19 | super(reason, source); |
| 20 | } |
| 21 | |
| 22 | public AssertException(String reason) { |
| 23 | super(reason); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | private String name; |
| 28 | |
| 29 | /** |
| 30 | * Create a new {@link TestCase}. |
| 31 | * |
| 32 | * @param name |
| 33 | * the test name |
| 34 | */ |
| 35 | public TestCase(String name) { |
| 36 | this.name = name; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Setup the test (called before the test is run). |
| 41 | * |
| 42 | * @throws Exception |
| 43 | * in case of error |
| 44 | */ |
| 45 | public void setUp() throws Exception { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Tear-down the test (called when the test has been ran). |
| 50 | * |
| 51 | * @throws Exception |
| 52 | * in case of error |
| 53 | */ |
| 54 | public void tearDown() throws Exception { |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * The test name. |
| 59 | * |
| 60 | * @return the name |
| 61 | */ |
| 62 | public String getName() { |
| 63 | return name; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Actually do the test. |
| 68 | * |
| 69 | * @throws Exception |
| 70 | * in case of error |
| 71 | */ |
| 72 | abstract public void test() throws Exception; |
| 73 | |
| 74 | /** |
| 75 | * Force a failure. |
| 76 | * |
| 77 | * @throws AssertException |
| 78 | * every time |
| 79 | */ |
| 80 | public void fail() throws AssertException { |
| 81 | fail(null); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Force a failure. |
| 86 | * |
| 87 | * @param reason |
| 88 | * the failure reason |
| 89 | * @throws AssertException |
| 90 | * every time |
| 91 | */ |
| 92 | public void fail(String reason) throws AssertException { |
| 93 | throw new AssertException("Failed!" + // |
| 94 | reason != null ? "\n" + reason : ""); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check that 2 {@link Object}s are equals. |
| 99 | * |
| 100 | * @param expected |
| 101 | * the expected value |
| 102 | * @param actual |
| 103 | * the actual value |
| 104 | * |
| 105 | * @throws AssertException |
| 106 | * in case they differ |
| 107 | */ |
| 108 | public void assertEquals(Object expected, Object actual) |
| 109 | throws AssertException { |
| 110 | assertEquals(null, expected, actual); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Check that 2 {@link Object}s are equals. |
| 115 | * |
| 116 | * @param the |
| 117 | * error message to display if they differ |
| 118 | * @param expected |
| 119 | * the expected value |
| 120 | * @param actual |
| 121 | * the actual value |
| 122 | * |
| 123 | * @throws AssertException |
| 124 | * in case they differ |
| 125 | */ |
| 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, |
| 132 | actual)); |
| 133 | } else { |
| 134 | throw new AssertException(errorMessage, new AssertException( |
| 135 | generateAssertMessage(expected, actual))); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Check that 2 {@link Object}s are equals. |
| 142 | * |
| 143 | * @param expected |
| 144 | * the expected value |
| 145 | * @param actual |
| 146 | * the actual value |
| 147 | * |
| 148 | * @throws AssertException |
| 149 | * in case they differ |
| 150 | */ |
| 151 | public void assertEquals(long expected, long actual) throws AssertException { |
| 152 | assertEquals(new Long(expected), new Long(actual)); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Check that 2 {@link Object}s are equals. |
| 157 | * |
| 158 | * @param the |
| 159 | * error message to display if they differ |
| 160 | * @param expected |
| 161 | * the expected value |
| 162 | * @param actual |
| 163 | * the actual value |
| 164 | * |
| 165 | * @throws AssertException |
| 166 | * in case they differ |
| 167 | */ |
| 168 | public void assertEquals(String errorMessage, long expected, long actual) |
| 169 | throws AssertException { |
| 170 | assertEquals(errorMessage, new Long(expected), new Long(actual)); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Check that 2 {@link Object}s are equals. |
| 175 | * |
| 176 | * @param expected |
| 177 | * the expected value |
| 178 | * @param actual |
| 179 | * the actual value |
| 180 | * |
| 181 | * @throws AssertException |
| 182 | * in case they differ |
| 183 | */ |
| 184 | public void assertEquals(boolean expected, boolean actual) |
| 185 | throws AssertException { |
| 186 | assertEquals(new Boolean(expected), new Boolean(actual)); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Check that 2 {@link Object}s are equals. |
| 191 | * |
| 192 | * @param the |
| 193 | * error message to display if they differ |
| 194 | * @param expected |
| 195 | * the expected value |
| 196 | * @param actual |
| 197 | * the actual value |
| 198 | * |
| 199 | * @throws AssertException |
| 200 | * in case they differ |
| 201 | */ |
| 202 | public void assertEquals(String errorMessage, boolean expected, |
| 203 | boolean actual) throws AssertException { |
| 204 | assertEquals(errorMessage, new Boolean(expected), new Boolean(actual)); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Check that 2 {@link Object}s are equals. |
| 209 | * |
| 210 | * @param expected |
| 211 | * the expected value |
| 212 | * @param actual |
| 213 | * the actual value |
| 214 | * |
| 215 | * @throws AssertException |
| 216 | * in case they differ |
| 217 | */ |
| 218 | public void assertEquals(double expected, double actual) |
| 219 | throws AssertException { |
| 220 | assertEquals(new Double(expected), new Double(actual)); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Check that 2 {@link Object}s are equals. |
| 225 | * |
| 226 | * @param the |
| 227 | * error message to display if they differ |
| 228 | * @param expected |
| 229 | * the expected value |
| 230 | * @param actual |
| 231 | * the actual value |
| 232 | * |
| 233 | * @throws AssertException |
| 234 | * in case they differ |
| 235 | */ |
| 236 | public void assertEquals(String errorMessage, double expected, double actual) |
| 237 | throws AssertException { |
| 238 | assertEquals(errorMessage, new Double(expected), new Double(actual)); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Check that given {@link Object} is not NULL. |
| 243 | * |
| 244 | * @param the |
| 245 | * error message to display if it is NULL |
| 246 | * @param actual |
| 247 | * the actual value |
| 248 | * |
| 249 | * @throws AssertException |
| 250 | * in case they differ |
| 251 | */ |
| 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); |
| 258 | |
| 259 | if (errorMessage == null) { |
| 260 | throw new AssertException(defaultReason); |
| 261 | } else { |
| 262 | throw new AssertException(errorMessage, new AssertException( |
| 263 | defaultReason)); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Generate the default assert message for 2 different values that were |
| 270 | * supposed to be equals. |
| 271 | * |
| 272 | * @param expected |
| 273 | * the expected value |
| 274 | * @param actual |
| 275 | * the actual value |
| 276 | * |
| 277 | * @return the message |
| 278 | */ |
| 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); |
| 284 | } |
| 285 | } |