1 /* 2 * Copyright (c) 2013, 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 assertLessThan(lhs, rhs, null); 72 } 73 74 /** 75 * Asserts that {@code lhs} is less than {@code rhs}. 76 * 77 * @param lhs The left hand side of the comparison. 78 * @param rhs The right hand side of the comparison. 79 * @param msg A description of the assumption. 80 * @throws RuntimeException if the assertion isn't valid. 81 */ 82 public static <T extends Comparable<T>>void assertLessThan(T lhs, T rhs, String msg) { 83 assertTrue(compare(lhs, rhs, msg) < 0, getMessage(lhs, rhs, "<", msg)); 84 } 85 86 /** 87 * Shorthand for {@link #assertLessThanOrEqual(T, T)}. 88 * 89 * @see #assertLessThanOrEqual(T, T) 90 */ 91 public static <T extends Comparable<T>> void assertLTE(T lhs, T rhs) { 92 assertLessThanOrEqual(lhs, rhs); 93 } 94 95 /** 96 * Shorthand for {@link #assertLessThanOrEqual(T, T, String)}. 97 * 98 * @see #assertLessThanOrEqual(T, T, String) 99 */ 100 public static <T extends Comparable<T>> void assertLTE(T lhs, T rhs, String msg) { 101 assertLessThanOrEqual(lhs, rhs, msg); 102 } 103 104 /** 105 * Calls {@link #assertLessThanOrEqual(T, T, String)} with a default message. 106 * 107 * @see #assertLessThanOrEqual(T, T, String) 108 */ 109 public static <T extends Comparable<T>> void assertLessThanOrEqual(T lhs, T rhs) { 110 assertLessThanOrEqual(lhs, rhs, null); 111 } 112 113 /** 114 * Asserts that {@code lhs} is less than or equal to {@code rhs}. 115 * 116 * @param lhs The left hand side of the comparison. 117 * @param rhs The right hand side of the comparison. 118 * @param msg A description of the assumption. 119 * @throws RuntimeException if the assertion isn't valid. 120 */ 121 public static <T extends Comparable<T>> void assertLessThanOrEqual(T lhs, T rhs, String msg) { 122 assertTrue(compare(lhs, rhs, msg) <= 0, getMessage(lhs, rhs, "<=", msg)); 123 } 124 125 /** 126 * Shorthand for {@link #assertEquals(T, T)}. 127 * 128 * @see #assertEquals(T, T) 129 */ 130 public static void assertEQ(Object lhs, Object rhs) { 131 assertEquals(lhs, rhs); 132 } 133 134 /** 135 * Shorthand for {@link #assertEquals(T, T, String)}. 136 * 137 * @see #assertEquals(T, T, String) 138 */ 139 public static void assertEQ(Object lhs, Object rhs, String msg) { 140 assertEquals(lhs, rhs, msg); 141 } 142 143 /** 144 * Calls {@link #assertEquals(T, T, String)} with a default message. 145 * 146 * @see #assertEquals(T, T, String) 147 */ 148 public static void assertEquals(Object lhs, Object rhs) { 149 assertEquals(lhs, rhs, null); 150 } 151 152 /** 153 * Asserts that {@code lhs} is equal to {@code rhs}. 154 * 155 * @param lhs The left hand side of the comparison. 156 * @param rhs The right hand side of the comparison. 157 * @param msg A description of the assumption. 158 * @throws RuntimeException if the assertion isn't valid. 159 */ 160 public static void assertEquals(Object lhs, Object rhs, String msg) { 161 if (lhs == null) { 162 if (rhs != null) { 163 error(msg); 164 } 165 } else { 166 assertTrue(lhs.equals(rhs), getMessage(lhs, rhs, "==", msg)); 167 } 168 } 169 170 /** 171 * Shorthand for {@link #assertGreaterThanOrEqual(T, T)}. 172 * 173 * @see #assertGreaterThanOrEqual(T, T) 174 */ 175 public static <T extends Comparable<T>> void assertGTE(T lhs, T rhs) { 176 assertGreaterThanOrEqual(lhs, rhs); 177 } 178 179 /** 180 * Shorthand for {@link #assertGreaterThanOrEqual(T, T, String)}. 181 * 182 * @see #assertGreaterThanOrEqual(T, T, String) 183 */ 184 public static <T extends Comparable<T>> void assertGTE(T lhs, T rhs, String msg) { 185 assertGreaterThanOrEqual(lhs, rhs, msg); 186 } 187 188 /** 189 * Calls {@link #assertGreaterThanOrEqual(T, T, String)} with a default message. 190 * 191 * @see #assertGreaterThanOrEqual(T, T, String) 192 */ 193 public static <T extends Comparable<T>> void assertGreaterThanOrEqual(T lhs, T rhs) { 194 assertGreaterThanOrEqual(lhs, rhs, null); 195 } 196 197 /** 198 * Asserts that {@code lhs} is greater than or equal to {@code rhs}. 199 * 200 * @param lhs The left hand side of the comparison. 201 * @param rhs The right hand side of the comparison. 202 * @param msg A description of the assumption. 203 * @throws RuntimeException if the assertion isn't valid. 204 */ 205 public static <T extends Comparable<T>> void assertGreaterThanOrEqual(T lhs, T rhs, String msg) { 206 assertTrue(compare(lhs, rhs, msg) >= 0, getMessage(lhs, rhs, ">=", msg)); 207 } 208 209 /** 210 * Shorthand for {@link #assertGreaterThan(T, T)}. 211 * 212 * @see #assertGreaterThan(T, T) 213 */ 214 public static <T extends Comparable<T>> void assertGT(T lhs, T rhs) { 215 assertGreaterThan(lhs, rhs); 216 } 217 218 /** 219 * Shorthand for {@link #assertGreaterThan(T, T, String)}. 220 * 221 * @see #assertGreaterThan(T, T, String) 222 */ 223 public static <T extends Comparable<T>> void assertGT(T lhs, T rhs, String msg) { 224 assertGreaterThan(lhs, rhs, msg); 225 } 226 227 /** 228 * Calls {@link #assertGreaterThan(T, T, String)} with a default message. 229 * 230 * @see #assertGreaterThan(T, T, String) 231 */ 232 public static <T extends Comparable<T>> void assertGreaterThan(T lhs, T rhs) { 233 assertGreaterThan(lhs, rhs, null); 234 } 235 236 /** 237 * Asserts that {@code lhs} is greater than {@code rhs}. 238 * 239 * @param lhs The left hand side of the comparison. 240 * @param rhs The right hand side of the comparison. 241 * @param msg A description of the assumption. 242 * @throws RuntimeException if the assertion isn't valid. 243 */ 244 public static <T extends Comparable<T>> void assertGreaterThan(T lhs, T rhs, String msg) { 245 assertTrue(compare(lhs, rhs, msg) > 0, getMessage(lhs, rhs, ">", msg)); 246 } 247 248 /** 249 * Shorthand for {@link #assertNotEquals(T, T)}. 250 * 251 * @see #assertNotEquals(T, T) 252 */ 253 public static void assertNE(Object lhs, Object rhs) { 254 assertNotEquals(lhs, rhs); 255 } 256 257 /** 258 * Shorthand for {@link #assertNotEquals(T, T, String)}. 259 * 260 * @see #assertNotEquals(T, T, String) 261 */ 262 public static void assertNE(Object lhs, Object rhs, String msg) { 263 assertNotEquals(lhs, rhs, msg); 264 } 265 266 /** 267 * Calls {@link #assertNotEquals(T, T, String)} with a default message. 268 * 269 * @see #assertNotEquals(T, T, String) 270 */ 271 public static void assertNotEquals(Object lhs, Object rhs) { 272 assertNotEquals(lhs, rhs, null); 273 } 274 275 /** 276 * Asserts that {@code lhs} is not equal to {@code rhs}. 277 * 278 * @param lhs The left hand side of the comparison. 279 * @param rhs The right hand side of the comparison. 280 * @param msg A description of the assumption. 281 * @throws RuntimeException if the assertion isn't valid. 282 */ 283 public static void assertNotEquals(Object lhs, Object rhs, String msg) { 284 if (lhs == null) { 285 if (rhs == null) { 286 error(msg); 287 } 288 } else { 289 assertFalse(lhs.equals(rhs), getMessage(lhs, rhs,"!=", msg)); 290 } 291 } 292 293 /** 294 * Calls {@link #assertNull(Object, String)} with a default message. 295 * 296 * @see #assertNull(Object, String) 297 */ 298 public static void assertNull(Object o) { 299 assertNull(o, "Expected " + format(o) + " to be null"); 300 } 301 302 /** 303 * Asserts that {@code o} is null. 304 * 305 * @param o The reference assumed to be null. 306 * @param msg A description of the assumption. 307 * @throws RuntimeException if the assertion isn't valid. 308 */ 309 public static void assertNull(Object o, String msg) { 310 assertEquals(o, null, msg); 311 } 312 313 /** 314 * Calls {@link #assertNotNull(Object, String)} with a default message. 315 * 316 * @see #assertNotNull(Object, String) 317 */ 318 public static void assertNotNull(Object o) { 319 assertNotNull(o, "Expected non null reference"); 320 } 321 322 /** 323 * Asserts that {@code o} is <i>not</i> null. 324 * 325 * @param o The reference assumed <i>not</i> to be null, 326 * @param msg A description of the assumption. 327 * @throws RuntimeException if the assertion isn't valid. 328 */ 329 public static void assertNotNull(Object o, String msg) { 330 assertNotEquals(o, null, msg); 331 } 332 333 /** 334 * Calls {@link #assertFalse(boolean, String)} with a default message. 335 * 336 * @see #assertFalse(boolean, String) 337 */ 338 public static void assertFalse(boolean value) { 339 assertFalse(value, "Expected value to be false"); 340 } 341 342 /** 343 * Asserts that {@code value} is {@code false}. 344 * 345 * @param value The value assumed to be false. 346 * @param msg A description of the assumption. 347 * @throws RuntimeException if the assertion isn't valid. 348 */ 349 public static void assertFalse(boolean value, String msg) { 350 assertTrue(!value, msg); 351 } 352 353 /** 354 * Calls {@link #assertTrue(boolean, String)} with a default message. 355 * 356 * @see #assertTrue(boolean, String) 357 */ 358 public static void assertTrue(boolean value) { 359 assertTrue(value, "Expected value to be true"); 360 } 361 362 /** 363 * Asserts that {@code value} is {@code true}. 364 * 365 * @param value The value assumed to be true. 366 * @param msg A description of the assumption. 367 * @throws RuntimeException if the assertion isn't valid. 368 */ 369 public static void assertTrue(boolean value, String msg) { 370 if (!value) { 371 error(msg); 372 } 373 } 374 375 /** 376 * Asserts that two strings are equal. 377 * 378 * If strings are not equals, then exception message 379 * will contain {@code msg} followed by list of mismatched lines. 380 * 381 * @param str1 First string to compare. 382 * @param str2 Second string to compare. 383 * @param msg A description of the assumption. 384 * @throws RuntimeException if strings are not equal. 385 */ 386 public static void assertStringsEqual(String str1, String str2, 387 String msg) { 388 String lineSeparator = System.getProperty("line.separator"); 389 String str1Lines[] = str1.split(lineSeparator); 390 String str2Lines[] = str2.split(lineSeparator); 391 392 int minLength = Math.min(str1Lines.length, str2Lines.length); 393 String longestStringLines[] = ((str1Lines.length == minLength) ? 394 str2Lines : str1Lines); 395 396 boolean stringsAreDifferent = false; 397 398 StringBuilder messageBuilder = new StringBuilder(msg); 399 400 messageBuilder.append("\n"); 401 402 for (int line = 0; line < minLength; line++) { 403 if (!str1Lines[line].equals(str2Lines[line])) { 404 messageBuilder.append(String. 405 format("[line %d] '%s' differs " + 406 "from '%s'\n", 407 line, 408 str1Lines[line], 409 str2Lines[line])); 410 stringsAreDifferent = true; 411 } 412 } 413 414 if (minLength < longestStringLines.length) { 415 String stringName = ((longestStringLines == str1Lines) ? 416 "first" : "second"); 417 messageBuilder.append(String.format("Only %s string contains " + 418 "following lines:\n", 419 stringName)); 420 stringsAreDifferent = true; 421 for(int line = minLength; line < longestStringLines.length; line++) { 422 messageBuilder.append(String. 423 format("[line %d] '%s'", line, 424 longestStringLines[line])); 425 } 426 } 427 428 if (stringsAreDifferent) { 429 error(messageBuilder.toString()); 430 } 431 } 432 433 private static <T extends Comparable<T>> int compare(T lhs, T rhs, String msg) { 434 assertNotNull(lhs, msg); 435 assertNotNull(rhs, msg); 436 return lhs.compareTo(rhs); 437 } 438 439 private static String format(Object o) { 440 return o == null? "null" : o.toString(); 441 } 442 443 private static void error(String msg) { 444 throw new RuntimeException(msg); 445 } 446 447 private static String getMessage(Object lhs, Object rhs, String op, String msg) { 448 return (msg == null ? "" : msg + " ") + "(assert failed: " + format(lhs) + " " + op + " " + format(rhs) + ")"; 449 } 450 } 451