1   package org.pojomatic.test;
2   
3   import org.pojomatic.Pojomatic;
4   import org.pojomatic.NoPojomaticPropertiesException;
5   
6   
7   
8   
9   public class AssertUtils {
10  
11    
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22    public static boolean equal(Object first, Object second) {
23      return first == null ? second == null : first.equals(second);
24    }
25  
26    
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
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  }