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