View Javadoc
1   package org.pojomatic.test;
2   
3   import org.pojomatic.Pojomatic;
4   import org.pojomatic.NoPojomaticPropertiesException;
5   
6   /**
7    * This class is not meant to be a part of the public API.
8    */
9   public class AssertUtils {
10  
11    /**
12     * Determines if two objects are both null or are equal according to
13     * {@link Object#equals(Object)}.
14     *
15     * @param first the first object to compare
16     * @param second the second object to compare
17     * @return {@code true} if both objects are null,
18     * or {@code first} is non-null and {@code first.equals(second)},
19     * {@code false} otherwise
20     * @see Object#equals(Object)
21     */
22    public static boolean equal(Object first, Object second) {
23      return first == null ? second == null : first.equals(second);
24    }
25  
26    /**
27     * Asserts that two objects are either both null or are equal according to
28     * {@link Object#equals(Object)}. If not, an {@code AssertionError} is thrown. If the objects are
29     * not equal, but the types of two objects are compatible for equality, then the differences as
30     * determined by {@link Pojomatic#diff(Object, Object)} are included in the failure message.
31     *
32     * @param message the message to add if the assertion fails
33     * @param expected will be displayed first if the assertion fails
34     * @param actual will be displayed second if the assertion fails
35     * @throws AssertionError if the objects are not equal. {@link AssertionError#getMessage()} will
36     * include information about the differences
37     */
38    public static void assertEquals(String message, Object expected, Object actual) {
39      if (!equal(expected, actual)) {
40        if (expected == null) {
41          throw new AssertionError(
42            makeBuilder(message).append("expected is null, but actual is ").append(actual));
43        }
44        if (actual == null) {
45          throw new AssertionError(
46            makeBuilder(message).append("actual is null, but expected is ").append(expected));
47        }
48        try {
49          if (Pojomatic.areCompatibleForEquals(expected.getClass(), actual.getClass())) {
50            throw new AssertionError(appendStandardEqualityMessage(
51              makeBuilder(message).append("differences between expected and actual:")
52              .append(Pojomatic.diff(expected, actual))
53                .append(" ("), expected, actual).append(")").toString());
54          }
55        }
56        catch (NoPojomaticPropertiesException e) {}
57        throw new AssertionError(
58          appendStandardEqualityMessage(makeBuilder(message), expected, actual).toString());
59        }
60    }
61  
62    private static StringBuilder appendStandardEqualityMessage(
63      StringBuilder builder, Object expected, Object actual) {
64      return builder
65        .append("expected:<").append(expected).append("> but was:<").append(actual).append(">");
66    }
67  
68    private static StringBuilder makeBuilder(String message) {
69      return message == null ? new StringBuilder() : new StringBuilder(message).append(" ");
70    }
71  
72    private AssertUtils() {}
73  
74  }