1
2
3
4
5
6 package mockit.internal.expectations;
7
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNull;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12
13 import org.junit.jupiter.api.BeforeEach;
14 import org.junit.jupiter.api.Test;
15
16 final class EquivalentInstancesTest {
17
18 private EquivalentInstances equivalentInstances;
19 private final Object mock1 = new Object();
20 private final Object mock2 = new Object();
21 private final Object mock3 = new Object();
22
23 @BeforeEach
24 void setUp() {
25 equivalentInstances = new EquivalentInstances();
26 }
27
28 @Test
29 void isEquivalentInstanceWhenSameObject() {
30 assertTrue(equivalentInstances.isEquivalentInstance(mock1, mock1));
31 }
32
33 @Test
34 void isEquivalentInstanceWhenDifferentObjects() {
35 assertFalse(equivalentInstances.isEquivalentInstance(mock1, mock2));
36 }
37
38 @Test
39 void isEquivalentInstanceWhenInReplacementMap() {
40 equivalentInstances.replacementMap.put(mock2, mock1);
41 assertTrue(equivalentInstances.isEquivalentInstance(mock1, mock2));
42 }
43
44 @Test
45 void isEquivalentInstanceWhenInInstanceMap() {
46 equivalentInstances.instanceMap.put(mock2, mock1);
47 assertTrue(equivalentInstances.isEquivalentInstance(mock1, mock2));
48 }
49
50 @Test
51 void isEquivalentInstanceWhenInInstanceMapReverse() {
52 equivalentInstances.instanceMap.put(mock1, mock2);
53 assertTrue(equivalentInstances.isEquivalentInstance(mock1, mock2));
54 }
55
56 @Test
57 void areNonEquivalentInstancesWhenBothNotInMaps() {
58
59 assertFalse(equivalentInstances.areNonEquivalentInstances(mock1, mock2));
60 }
61
62 @Test
63 void areNonEquivalentInstancesWhenOnlyInvokedIsInMap() {
64 equivalentInstances.instanceMap.put(mock2, mock3);
65
66
67 assertTrue(equivalentInstances.areNonEquivalentInstances(mock1, mock2));
68 }
69
70 @Test
71 void areNonEquivalentInstancesWhenInvocationInstanceIsInMap() {
72 equivalentInstances.instanceMap.put(mock1, mock3);
73
74 assertFalse(equivalentInstances.areNonEquivalentInstances(mock1, mock2));
75 }
76
77 @Test
78 void areMatchingInstancesWhenMatchInstanceTrueAndSameObject() {
79 assertTrue(equivalentInstances.areMatchingInstances(true, mock1, mock1));
80 }
81
82 @Test
83 void areMatchingInstancesWhenMatchInstanceFalseAndEmptyMap() {
84 assertTrue(equivalentInstances.areMatchingInstances(false, mock1, mock2));
85 }
86
87 @Test
88 void areMatchingInstancesWhenMatchInstanceFalseAndSameObject() {
89 equivalentInstances.instanceMap.put(mock3, mock1);
90
91 assertTrue(equivalentInstances.areMatchingInstances(false, mock1, mock2));
92 }
93
94 @Test
95 void areMatchingInstancesWhenInDifferentEquivalenceSets() {
96 equivalentInstances.instanceMap.put(mock1, mock3);
97 equivalentInstances.instanceMap.put(mock2, new Object());
98
99 assertFalse(equivalentInstances.areMatchingInstances(false, mock1, mock2));
100 }
101
102 @Test
103 void areInDifferentEquivalenceSetsWhenMock1EquivalentIsMock2() {
104 equivalentInstances.instanceMap.put(mock1, mock2);
105
106 assertTrue(equivalentInstances.areMatchingInstances(false, mock1, mock2));
107 }
108
109 @Test
110 void areInDifferentEquivalenceSetsWhenMock2EquivalentIsMock1() {
111 equivalentInstances.instanceMap.put(mock2, mock1);
112
113 assertTrue(equivalentInstances.areMatchingInstances(false, mock1, mock2));
114 }
115
116 @Test
117 void instanceMapHasMocksInSeparateEntriesReturnsFalse() {
118
119 equivalentInstances.instanceMap.put(mock1, mock3);
120
121 assertTrue(equivalentInstances.areMatchingInstances(false, mock1, mock2));
122 }
123
124 @Test
125 void instanceMapHasMocksInSeparateEntriesReturnsTrue() {
126
127 equivalentInstances.instanceMap.put(mock1, mock3);
128 equivalentInstances.instanceMap.put(mock2, new Object());
129 assertFalse(equivalentInstances.areMatchingInstances(false, mock1, mock2));
130 }
131
132 @Test
133 void getReplacementInstanceForMethodInvocationForConstructor() {
134 equivalentInstances.replacementMap.put(mock1, mock2);
135
136 assertNull(equivalentInstances.getReplacementInstanceForMethodInvocation(mock1, "<init>()V"));
137 }
138
139 @Test
140 void getReplacementInstanceForMethodInvocationForRegularMethod() {
141 equivalentInstances.replacementMap.put(mock1, mock2);
142 assertEquals(mock2, equivalentInstances.getReplacementInstanceForMethodInvocation(mock1, "doSomething()V"));
143 }
144
145 @Test
146 void getReplacementInstanceForMethodInvocationReturnsNullWhenNotInMap() {
147 assertNull(equivalentInstances.getReplacementInstanceForMethodInvocation(mock1, "doSomething()V"));
148 }
149
150 @Test
151 void isReplacementInstanceForConstructorReturnsFalse() {
152 equivalentInstances.replacementMap.put(mock1, mock2);
153 assertFalse(equivalentInstances.isReplacementInstance(mock1, "<init>()V"));
154 }
155
156 @Test
157 void isReplacementInstanceForRegularMethodWhenKeyInMap() {
158 equivalentInstances.replacementMap.put(mock1, mock2);
159 assertTrue(equivalentInstances.isReplacementInstance(mock1, "doSomething()V"));
160 }
161
162 @Test
163 void isReplacementInstanceForRegularMethodWhenValueInMap() {
164 equivalentInstances.replacementMap.put(mock3, mock1);
165 assertTrue(equivalentInstances.isReplacementInstance(mock1, "doSomething()V"));
166 }
167
168 @Test
169 void isReplacementInstanceReturnsFalseWhenNotInMap() {
170 assertFalse(equivalentInstances.isReplacementInstance(mock1, "doSomething()V"));
171 }
172 }