View Javadoc
1   package net.trajano.commons.testing.test;
2   
3   import static org.junit.Assert.fail;
4   
5   import java.util.UUID;
6   import java.util.concurrent.Callable;
7   import java.util.concurrent.ThreadLocalRandom;
8   
9   import org.junit.Assert;
10  import org.junit.Test;
11  
12  import net.trajano.commons.testing.EqualsTestUtil;
13  
14  /**
15   * Tests {@link EqualsClassTestUtil}.
16   *
17   * @author Archimedes Trajano
18   */
19  public class EqualsTestUtilTest {
20  
21      private static final class MissingEquals {
22  
23          private final int i;
24  
25          public MissingEquals(final int i) {
26  
27              this.i = i;
28          }
29  
30      }
31  
32      private static final class MissingHashCode {
33  
34          private final int i;
35  
36          public MissingHashCode(final int i) {
37  
38              this.i = i;
39          }
40  
41          @Override
42          public boolean equals(final Object obj) {
43  
44              if (this == obj) {
45                  return true;
46              }
47              if (obj == null) {
48                  return false;
49              }
50              if (getClass() != obj.getClass()) {
51                  return false;
52              }
53              final MissingHashCode other = (MissingHashCode) obj;
54              if (i != other.i) {
55                  return false;
56              }
57              return true;
58          }
59      }
60  
61      @Test
62      public void testFailureWithMissingEqualsObject() throws Exception {
63  
64          try {
65              final MissingEquals i1 = new MissingEquals(1);
66              final MissingEquals i2 = new MissingEquals(1);
67              Assert.assertEquals(i1.i, i2.i);
68              EqualsTestUtil.assertEqualsImplementedCorrectly(i1, i2);
69              throw new RuntimeException("failure");
70          } catch (final AssertionError e) {
71          }
72      }
73  
74      @Test
75      public void testFailureWithMissingHashcodeObject() throws Exception {
76  
77          try {
78              final MissingHashCode i1 = new MissingHashCode(1);
79              final MissingHashCode i2 = new MissingHashCode(1);
80              EqualsTestUtil.assertEqualsImplementedCorrectly(i1, i2);
81              fail();
82          } catch (final AssertionError e) {
83          }
84      }
85  
86      @Test
87      public void testFailureWithRandom() throws Exception {
88  
89          try {
90              final Integer i1 = ThreadLocalRandom.current()
91                      .nextInt();
92              final Integer i2 = ThreadLocalRandom.current()
93                      .nextInt();
94              EqualsTestUtil.assertEqualsImplementedCorrectly(i1, i2);
95              fail();
96          } catch (final AssertionError e) {
97          }
98      }
99  
100     @Test
101     public void testWithBuilders() throws Exception {
102 
103         EqualsTestUtil.assertEqualsImplementedCorrectly(new Callable<UUID>() {
104 
105             @Override
106             public UUID call() throws Exception {
107 
108                 return UUID.fromString("c2d8c5c5-5202-44ff-89f1-93d596721df6");
109             }
110 
111         });
112     }
113 
114     @Test
115     public void testWithOneObjects() throws Exception {
116 
117         final UUID u1 = UUID.fromString("c2d8c5c5-5202-44ff-89f1-93d596721df6");
118         EqualsTestUtil.assertEqualsImplementedCorrectly(u1);
119     }
120 
121     @Test
122     public void testWithTwoBuilders() throws Exception {
123 
124         EqualsTestUtil.assertEqualsImplementedCorrectly(new Callable<UUID>() {
125 
126             @Override
127             public UUID call() throws Exception {
128 
129                 return UUID.fromString("c2d8c5c5-5202-44ff-89f1-93d596721df6");
130             }
131 
132         }, new Callable<UUID>() {
133 
134             @Override
135             public UUID call() throws Exception {
136 
137                 return UUID.fromString("c2d8c5c5-5202-44ff-89f1-93d596721df6");
138             }
139 
140         });
141     }
142 
143     @Test
144     public void testWithTwoObjects() throws Exception {
145 
146         final UUID u1 = UUID.fromString("c2d8c5c5-5202-44ff-89f1-93d596721df6");
147         final UUID u2 = UUID.fromString("c2d8c5c5-5202-44ff-89f1-93d596721df6");
148         EqualsTestUtil.assertEqualsImplementedCorrectly(u1, u2);
149     }
150 
151 }