View Javadoc
1   package org.pojomatic.test;
2   
3   import static org.junit.Assert.*;
4   
5   import org.junit.Test;
6   import org.pojomatic.Pojomatic;
7   import org.pojomatic.junit.PojomaticAssert;
8   
9   /**
10   * Tests assertion methods such as assertEquals(Object, Object) for correctness of assertions.
11   */
12  public abstract class AssertTest {
13  
14  
15    /**
16     * Only the unit under test should throw {@link AssertionError}, so no assertions are allowed
17     * to be thrown from within the implementation of this method.
18     *
19     * @param expected the expected object
20     * @param actual the actual object
21     */
22    protected abstract void performAssertEquals(Object expected, Object actual);
23  
24    /**
25     * Only the unit under test should throw {@link AssertionError}, so no assertions are allowed
26     * to be thrown from within the implementation of this method.
27     *
28     * @param expected the expected object
29     * @param actual the actual object
30     * @param message the messaage to include with the assertion
31     */
32    protected abstract void performAssertEquals(Object expected, Object actual, String message);
33  
34    private void performAssertEquals(
35      Object expected, Object actual, String message, String expectedMessage) {
36      try {
37        performAssertEquals(expected, actual, message);
38        fail("exception expected");
39      }
40      catch (AssertionError e) {
41        assertEquals(expectedMessage, e.getMessage());
42      }
43    }
44  
45    @Test
46    public final void assertEqualsWhenEqual() {
47      performAssertEquals(new Container(3), new Container(3), "message");
48    }
49  
50    @Test
51    public final void assertEqualsWhenEqualNoMessage() {
52      performAssertEquals(new Container(3), new Container(3));
53    }
54  
55    @Test
56    public final void assertEqualsBothNull() {
57      performAssertEquals(null, null, null);
58    }
59  
60    @Test
61    public final void assertEqualsBothNullNoMessage() {
62      performAssertEquals(null, null);
63    }
64  
65    @Test
66    public final void assertEqualsNullExpected() {
67      performAssertEquals(
68        null, new Container(null), null,
69        "expected is null, but actual is Container{test: {null}}");
70    }
71  
72    @Test
73    public final void assertEqualsNullExpectedNoMessage() {
74      performAssertEquals(
75        null, new Container(null), null,
76        "expected is null, but actual is Container{test: {null}}");
77    }
78  
79    @Test
80    public final void assertEqualsNullActual() {
81      performAssertEquals(
82        new Container(null), null, null, "actual is null, but expected is Container{test: {null}}");
83    }
84  
85    /**
86     * Tests that {@link PojomaticAssert#assertEqualsWithDiff(Object, Object)}
87     * uses {@link Object#equals(Object)} instead of {@link Pojomatic#equals(Object, Object)}.
88     */
89    @Test
90    public final void assertEqualsViaInheritedEquals() {
91      //create objects which are never equal via Object.equals(Object), but are equal via
92      //Pojomatic.equals(Object, Object)
93      OnlyPojomaticEqual first = new OnlyPojomaticEqual();
94      OnlyPojomaticEqual second = new OnlyPojomaticEqual();
95      performAssertEquals(first, second, null,
96        "differences between expected and actual:no differences (expected:<toString> but was:<toString>)");
97    }
98  
99    @Test
100   public final void assertEqualsNoMessage() {
101     try {
102       performAssertEquals(new Container("foo"), new Container("bar"));
103     }
104     catch (AssertionError e) {
105       assertEquals("differences between expected and actual:[test: {foo} versus {bar}]" +
106           " (expected:<Container{test: {foo}}> but was:<Container{test: {bar}}>)", e.getMessage());
107     }
108   }
109 
110   @Test
111   public final void assertEqualsNullMessage() {
112     performAssertEquals(
113       new Container("foo"), new Container("bar"), null,
114       "differences between expected and actual:[test: {foo} versus {bar}]" +
115         " (expected:<Container{test: {foo}}> but was:<Container{test: {bar}}>)");
116   }
117 
118   @Test
119   public final void assertEqualsMessage2() {
120     String first = "foo";
121     String second = "bar";
122     performAssertEquals(
123       new Container(first), new Container(second), null,
124       "differences between expected and actual:[test: {foo} versus {bar}]" +
125         " (expected:<Container{test: {foo}}> but was:<Container{test: {bar}}>)");
126   }
127 
128   @Test
129   public final void assertEqualsCustomMessage() {
130     performAssertEquals(
131       new Container("foo"), new Container("bar"), "hello",
132       "hello differences between expected and actual:[test: {foo} versus {bar}]" +
133         " (expected:<Container{test: {foo}}> but was:<Container{test: {bar}}>)");
134   }
135 
136   @Test
137   public final void assertEqualsNonPojomatic() {
138     performAssertEquals("string a", "string b", null, "expected:<string a> but was:<string b>");
139   }
140 
141   @Test
142   public final void assertEqualsNonComparable() {
143     performAssertEquals(new Container("foo"), new DifferentPojo("foo"), null,
144       "expected:<Container{test: {foo}}> but was:<DifferentPojo{test: {foo}}>");
145   }
146 }