1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package com.oracle.java.testlibrary; 25 26 /** 27 * Asserts that can be used for verifying assumptions in tests. 28 * 29 * An assertion will throw a {@link RuntimeException} if the assertion isn't 30 * valid. All the asserts can be imported into a test by using a static 31 * import: 32 * 33 * <pre> 34 * {@code 35 * import static com.oracle.java.testlibrary.Asserts.*; 36 * } 37 * 38 * Always provide a message describing the assumption if the line number of the 39 * failing assertion isn't enough to understand why the assumption failed. For 40 * example, if the assertion is in a loop or in a method that is called 41 * multiple times, then the line number won't provide enough context to 42 * understand the failure. 43 * </pre> 44 */ 45 public class Asserts { 46 47 /** 48 * Shorthand for {@link #assertLessThan(T, T)}. 49 * 50 * @see #assertLessThan(T, T) 51 */ 52 public static <T extends Comparable<T>> void assertLT(T lhs, T rhs) { 53 assertLessThan(lhs, rhs); 54 } 55 56 /** 57 * Shorthand for {@link #assertLessThan(T, T, String)}. 58 * 59 * @see #assertLessThan(T, T, String) 60 */ 61 public static <T extends Comparable<T>> void assertLT(T lhs, T rhs, String msg) { 62 assertLessThan(lhs, rhs, msg); 63 } 64 65 /** 66 * Calls {@link #assertLessThan(T, T, String)} with a default message. 67 * 68 * @see #assertLessThan(T, T, String) 69 */ 70 public static <T extends Comparable<T>> void assertLessThan(T lhs, T rhs) { 71 String msg = "Expected that " + format(lhs) + " < " + format(rhs); 72 assertLessThan(lhs, rhs, msg); 73 } 74 75 /** 76 * Asserts that {@code lhs} is less than {@code rhs}. 77 * 78 * @param lhs The left hand side of the comparison. 79 * @param rhs The right hand side of the comparison. 80 * @param msg A description of the assumption. 81 * @throws RuntimeException if the assertion isn't valid. 82 */ 83 public static <T extends Comparable<T>>void assertLessThan(T lhs, T rhs, String msg) { 84 assertTrue(compare(lhs, rhs, msg) < 0, msg); 85 } 86 87 /** 88 * Shorthand for {@link #assertLessThanOrEqual(T, T)}. 89 * 90 * @see #assertLessThanOrEqual(T, T) 91 */ 92 public static <T extends Comparable<T>> void assertLTE(T lhs, T rhs) { 93 assertLessThanOrEqual(lhs, rhs); 94 } 95 96 /** 97 * Shorthand for {@link #assertLessThanOrEqual(T, T, String)}. 98 * 99 * @see #assertLessThanOrEqual(T, T, String) 100 */ 101 public static <T extends Comparable<T>> void assertLTE(T lhs, T rhs, String msg) { 102 assertLessThanOrEqual(lhs, rhs, msg); 103 } 104 105 /** 106 * Calls {@link #assertLessThanOrEqual(T, T, String)} with a default message. 107 * 108 * @see #assertLessThanOrEqual(T, T, String) 109 */ 110 public static <T extends Comparable<T>> void assertLessThanOrEqual(T lhs, T rhs) { 111 String msg = "Expected that " + format(lhs) + " <= " + format(rhs); 112 assertLessThanOrEqual(lhs, rhs, msg); 113 } 114 115 /** 116 * Asserts that {@code lhs} is less than or equal to {@code rhs}. 117 * 118 * @param lhs The left hand side of the comparison. 119 * @param rhs The right hand side of the comparison. 120 * @param msg A description of the assumption. 121 * @throws RuntimeException if the assertion isn't valid. 122 */ 123 public static <T extends Comparable<T>> void assertLessThanOrEqual(T lhs, T rhs, String msg) { 124 assertTrue(compare(lhs, rhs, msg) <= 0, msg); 125 } 126 127 /** 128 * Shorthand for {@link #assertEquals(T, T)}. 129 * 130 * @see #assertEquals(T, T) 131 */ 132 public static void assertEQ(Object lhs, Object rhs) { 133 assertEquals(lhs, rhs); 134 } 135 136 /** 137 * Shorthand for {@link #assertEquals(T, T, String)}. 138 * 139 * @see #assertEquals(T, T, String) 140 */ 141 public static void assertEQ(Object lhs, Object rhs, String msg) { 142 assertEquals(lhs, rhs, msg); 143 } 144 145 /** 146 * Calls {@link #assertEquals(T, T, String)} with a default message. 147 * 148 * @see #assertEquals(T, T, String) 149 */ 150 public static void assertEquals(Object lhs, Object rhs) { 151 String msg = "Expected " + format(lhs) + " to equal " + format(rhs); 152 assertEquals(lhs, rhs, msg); 153 } 154 155 /** 156 * Asserts that {@code lhs} is equal to {@code rhs}. 157 * 158 * @param lhs The left hand side of the comparison. 159 * @param rhs The right hand side of the comparison. 160 * @param msg A description of the assumption. 161 * @throws RuntimeException if the assertion isn't valid. 162 */ 163 public static void assertEquals(Object lhs, Object rhs, String msg) { 164 if (lhs == null) { 165 if (rhs != null) { 166 error(msg); 167 } 168 } else { 169 assertTrue(lhs.equals(rhs), msg); 170 } 171 } 172 173 /** 174 * Shorthand for {@link #assertGreaterThanOrEqual(T, T)}. 175 * 176 * @see #assertGreaterThanOrEqual(T, T) 177 */ 178 public static <T extends Comparable<T>> void assertGTE(T lhs, T rhs) { 179 assertGreaterThanOrEqual(lhs, rhs); 180 } 181 182 /** 183 * Shorthand for {@link #assertGreaterThanOrEqual(T, T, String)}. 184 * 185 * @see #assertGreaterThanOrEqual(T, T, String) 186 */ 187 public static <T extends Comparable<T>> void assertGTE(T lhs, T rhs, String msg) { 188 assertGreaterThanOrEqual(lhs, rhs, msg); 189 } 190 191 /** 192 * Calls {@link #assertGreaterThanOrEqual(T, T, String)} with a default message. 193 * 194 * @see #assertGreaterThanOrEqual(T, T, String) 195 */ 196 public static <T extends Comparable<T>> void assertGreaterThanOrEqual(T lhs, T rhs) { 197 String msg = "Expected that " + format(lhs) + " >= " + format(rhs); 198 assertGreaterThanOrEqual(lhs, rhs, msg); 199 } 200 201 /** 202 * Asserts that {@code lhs} is greater than or equal to {@code rhs}. 203 * 204 * @param lhs The left hand side of the comparison. 205 * @param rhs The right hand side of the comparison. 206 * @param msg A description of the assumption. 207 * @throws RuntimeException if the assertion isn't valid. 208 */ 209 public static <T extends Comparable<T>> void assertGreaterThanOrEqual(T lhs, T rhs, String msg) { 210 assertTrue(compare(lhs, rhs, msg) >= 0, msg); 211 } 212 213 /** 214 * Shorthand for {@link #assertGreaterThan(T, T)}. 215 * 216 * @see #assertGreaterThan(T, T) 217 */ 218 public static <T extends Comparable<T>> void assertGT(T lhs, T rhs) { 219 assertGreaterThan(lhs, rhs); 220 } 221 222 /** 223 * Shorthand for {@link #assertGreaterThan(T, T, String)}. 224 * 225 * @see #assertGreaterThan(T, T, String) 226 */ 227 public static <T extends Comparable<T>> void assertGT(T lhs, T rhs, String msg) { 228 assertGreaterThan(lhs, rhs, msg); 229 } 230 231 /** 232 * Calls {@link #assertGreaterThan(T, T, String)} with a default message. 233 * 234 * @see #assertGreaterThan(T, T, String) 235 */ 236 public static <T extends Comparable<T>> void assertGreaterThan(T lhs, T rhs) { 237 String msg = "Expected that " + format(lhs) + " > " + format(rhs); 238 assertGreaterThan(lhs, rhs, msg); 239 } 240 241 /** 242 * Asserts that {@code lhs} is greater than {@code rhs}. 243 * 244 * @param lhs The left hand side of the comparison. 245 * @param rhs The right hand side of the comparison. 246 * @param msg A description of the assumption. 247 * @throws RuntimeException if the assertion isn't valid. 248 */ 249 public static <T extends Comparable<T>> void assertGreaterThan(T lhs, T rhs, String msg) { 250 assertTrue(compare(lhs, rhs, msg) > 0, msg); 251 } 252 253 /** 254 * Shorthand for {@link #assertNotEquals(T, T)}. 255 * 256 * @see #assertNotEquals(T, T) 257 */ 258 public static void assertNE(Object lhs, Object rhs) { 259 assertNotEquals(lhs, rhs); 260 } 261 262 /** 263 * Shorthand for {@link #assertNotEquals(T, T, String)}. 264 * 265 * @see #assertNotEquals(T, T, String) 266 */ 267 public static void assertNE(Object lhs, Object rhs, String msg) { 268 assertNotEquals(lhs, rhs, msg); 269 } 270 271 /** 272 * Calls {@link #assertNotEquals(T, T, String)} with a default message. 273 * 274 * @see #assertNotEquals(T, T, String) 275 */ 276 public static void assertNotEquals(Object lhs, Object rhs) { 277 String msg = "Expected " + format(lhs) + " to not equal " + format(rhs); 278 assertNotEquals(lhs, rhs, msg); 279 } 280 281 /** 282 * Asserts that {@code lhs} is not equal to {@code rhs}. 283 * 284 * @param lhs The left hand side of the comparison. 285 * @param rhs The right hand side of the comparison. 286 * @param msg A description of the assumption. 287 * @throws RuntimeException if the assertion isn't valid. 288 */ 289 public static void assertNotEquals(Object lhs, Object rhs, String msg) { 290 if (lhs == null) { 291 if (rhs == null) { 292 error(msg); 293 } 294 } else { 295 assertFalse(lhs.equals(rhs), msg); 296 } 297 } 298 299 /** 300 * Calls {@link #assertNull(Object, String)} with a default message. 301 * 302 * @see #assertNull(Object, String) 303 */ 304 public static void assertNull(Object o) { 305 assertNull(o, "Expected " + format(o) + " to be null"); 306 } 307 308 /** 309 * Asserts that {@code o} is null. 310 * 311 * @param o The reference assumed to be null. 312 * @param msg A description of the assumption. 313 * @throws RuntimeException if the assertion isn't valid. 314 */ 315 public static void assertNull(Object o, String msg) { 316 assertEquals(o, null, msg); 317 } 318 319 /** 320 * Calls {@link #assertNotNull(Object, String)} with a default message. 321 * 322 * @see #assertNotNull(Object, String) 323 */ 324 public static void assertNotNull(Object o) { 325 assertNotNull(o, "Expected non null reference"); 326 } 327 328 /** 329 * Asserts that {@code o} is <i>not</i> null. 330 * 331 * @param o The reference assumed <i>not</i> to be null, 332 * @param msg A description of the assumption. 333 * @throws RuntimeException if the assertion isn't valid. 334 */ 335 public static void assertNotNull(Object o, String msg) { 336 assertNotEquals(o, null, msg); 337 } 338 339 /** 340 * Calls {@link #assertFalse(boolean, String)} with a default message. 341 * 342 * @see #assertFalse(boolean, String) 343 */ 344 public static void assertFalse(boolean value) { 345 assertFalse(value, "Expected value to be false"); 346 } 347 348 /** 349 * Asserts that {@code value} is {@code false}. 350 * 351 * @param value The value assumed to be false. 352 * @param msg A description of the assumption. 353 * @throws RuntimeException if the assertion isn't valid. 354 */ 355 public static void assertFalse(boolean value, String msg) { 356 assertTrue(!value, msg); 357 } 358 359 /** 360 * Calls {@link #assertTrue(boolean, String)} with a default message. 361 * 362 * @see #assertTrue(boolean, String) 363 */ 364 public static void assertTrue(boolean value) { 365 assertTrue(value, "Expected value to be true"); 366 } 367 368 /** 369 * Asserts that {@code value} is {@code true}. 370 * 371 * @param value The value assumed to be true. 372 * @param msg A description of the assumption. 373 * @throws RuntimeException if the assertion isn't valid. 374 */ 375 public static void assertTrue(boolean value, String msg) { 376 if (!value) { 377 error(msg); 378 } 379 } 380 381 /** 382 * Asserts that two strings are equal. 383 * 384 * If strings are not equals, then exception message 385 * will contain {@code msg} followed by list of mismatched lines. 386 * 387 * @param str1 First string to compare. 388 * @param str2 Second string to compare. 389 * @param msg A description of the assumption. 390 * @throws RuntimeException if strings are not equal. 391 */ 392 public static void assertStringsEqual(String str1, String str2, 393 String msg) { 394 String lineSeparator = System.getProperty("line.separator"); 395 String str1Lines[] = str1.split(lineSeparator); 396 String str2Lines[] = str2.split(lineSeparator); 397 398 int minLength = Math.min(str1Lines.length, str2Lines.length); 399 String longestStringLines[] = ((str1Lines.length == minLength) ? 400 str2Lines : str1Lines); 401 402 boolean stringsAreDifferent = false; 403 404 StringBuilder messageBuilder = new StringBuilder(msg); 405 406 messageBuilder.append("\n"); 407 408 for (int line = 0; line < minLength; line++) { 409 if (!str1Lines[line].equals(str2Lines[line])) { 410 messageBuilder.append(String. 411 format("[line %d] '%s' differs " + 412 "from '%s'\n", 413 line, 414 str1Lines[line], 415 str2Lines[line])); 416 stringsAreDifferent = true; 417 } 418 } 419 420 if (minLength < longestStringLines.length) { 421 String stringName = ((longestStringLines == str1Lines) ? 422 "first" : "second"); 423 messageBuilder.append(String.format("Only %s string contains " + 424 "following lines:\n", 425 stringName)); 426 stringsAreDifferent = true; 427 for(int line = minLength; line < longestStringLines.length; line++) { 428 messageBuilder.append(String. 429 format("[line %d] '%s'", line, 430 longestStringLines[line])); 431 } 432 } 433 434 if (stringsAreDifferent) { 435 error(messageBuilder.toString()); 436 } 437 } 438 439 /** 440 * Fail the test directly 441 * @throws RuntimeException anyway. 442 */ 443 public static void fail() { 444 error("Failed"); 445 } 446 447 /** 448 * Fails a test with the given message. 449 * 450 * @param msg A description of the failure. 451 * 452 */ 453 public static void fail(String msg) { 454 error(msg); 455 } 456 457 private static <T extends Comparable<T>> int compare(T lhs, T rhs, String msg) { 458 assertNotNull(lhs, msg); 459 assertNotNull(rhs, msg); 460 return lhs.compareTo(rhs); 461 } 462 463 private static String format(Object o) { 464 return o == null? "null" : o.toString(); 465 } 466 467 private static void error(String msg) { 468 throw new RuntimeException(msg); 469 } 470 471 }