/
CachedInteractionTest.java
168 lines (125 loc) · 5.85 KB
/
CachedInteractionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package fitnesse.slim.fixtureInteraction;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Collections;
import fitnesse.slim.test.TableTableIncFirstCol;
import fitnesse.testsystems.slim.SlimTestContext;
import fitnesse.testsystems.slim.Table;
import fitnesse.testsystems.slim.tables.SlimTable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class CachedInteractionTest {
@Spy
private CachedInteraction interaction;
@Test
public void canFindClass() {
Class<? extends CachedInteractionTest> myClass = this.getClass();
Class<?> clazz = interaction.getClass(myClass.getName());
assertEquals(myClass, clazz);
verify(interaction, times(1)).handleClassCacheMiss(myClass.getName());
// 2nd call
clazz = interaction.getClass(myClass.getName());
assertEquals(myClass, clazz);
// cache hit on 2nd call
verify(interaction, times(1)).handleClassCacheMiss(myClass.getName());
}
@Test
public void canDealWithNoClassFound() {
String className = "IDontExist";
Class<?> clazz = interaction.getClass(className);
assertNull(clazz);
// 2nd call
clazz = interaction.getClass(className);
assertNull(clazz);
// cache hit on 2nd call
verify(interaction, times(1)).handleClassCacheMiss(className);
}
@Test
public void canFindConstructor() throws NoSuchMethodException {
Class<? extends CachedInteractionTest> myClass = this.getClass();
Object[] args = new Object[0];
Constructor<?> constructor = interaction.getConstructor(myClass, args);
assertEquals(myClass.getConstructor(), constructor);
verify(interaction, times(1)).handleConstructorCacheMiss(myClass, args);
// call 2nd time
constructor = interaction.getConstructor(myClass, args);
assertEquals(myClass.getConstructor(), constructor);
// cache hit on 2nd call
verify(interaction, times(1)).handleConstructorCacheMiss(myClass, args);
Constructor<?> constructor2 = interaction.getConstructor(SlimTable.class, new Object[3]);
assertEquals(SlimTable.class.getConstructor(Table.class, String.class, SlimTestContext.class), constructor2);
}
@Test
public void canFindConstructorWithArgs() throws NoSuchMethodException {
Constructor<?> constructor2 = interaction.getConstructor(SlimTable.class, new Object[3]);
assertEquals(SlimTable.class.getConstructor(Table.class, String.class, SlimTestContext.class), constructor2);
}
@Test
public void canDealWithNoConstructor() throws NoSuchMethodException {
Constructor<?> constructor2 = interaction.getConstructor(SlimTable.class, new Object[0]);
assertNull(constructor2);
constructor2 = interaction.getConstructor(SlimTable.class, new Object[0]);
assertNull(constructor2);
}
@Test
public void canFindMethod() {
String findMethod = "canFindMethod";
Method method = interaction.findMatchingMethod(findMethod, this);
assertEquals(findMethod, method.getName());
assertEquals(getClass(), method.getDeclaringClass());
verify(interaction, times(1)).handleMethodCacheMiss(findMethod, this, new Object[0]);
//2nd call
method = interaction.findMatchingMethod(findMethod, this);
assertEquals(findMethod, method.getName());
assertEquals(getClass(), method.getDeclaringClass());
// cache hit, no 2nd call
verify(interaction, times(1)).handleMethodCacheMiss(findMethod, this, new Object[0]);
}
@Test
public void canFindMethodWithArguments() {
String findMethod = "createInstance";
DefaultInteraction instance = new DefaultInteraction();
Method method = interaction.findMatchingMethod(findMethod, instance, null, null, null);
assertEquals(findMethod, method.getName());
assertEquals(DefaultInteraction.class, method.getDeclaringClass());
verify(interaction, times(1)).handleMethodCacheMiss(findMethod, instance, new Object[3]);
//2nd call
method = interaction.findMatchingMethod(findMethod, instance, null, null, null);
assertEquals(findMethod, method.getName());
assertEquals(DefaultInteraction.class, method.getDeclaringClass());
// cache hit, no 2nd call
verify(interaction, times(1)).handleMethodCacheMiss(findMethod, instance, new Object[3]);
}
@Test
public void canFindMethodWithSameSimpleClassName() {
String findMethod = "doTable";
TableTableIncFirstCol instance = new TableTableIncFirstCol();
fitnesse.slim.test.statementexecutorconsumer.TableTableIncFirstCol consInstance =
new fitnesse.slim.test.statementexecutorconsumer.TableTableIncFirstCol();
Method method = interaction.findMatchingMethod(findMethod, instance, Collections.emptyList());
assertEquals(findMethod, method.getName());
assertEquals("Method returned is defined by the wrong class (i.e. not the class of the instance passed)",
instance.getClass(), method.getDeclaringClass());
//2nd call
Method consMethod = interaction.findMatchingMethod(findMethod, consInstance, Collections.emptyList());
assertEquals(findMethod, consMethod.getName());
assertEquals("Method returned is defined by the wrong class (i.e. not the class of the instance passed)",
consInstance.getClass(), consMethod.getDeclaringClass());
}
@Test
public void canDealWithNoMethod() {
String findMethod = "addChildTable";
Method method = interaction.findMatchingMethod(findMethod, SlimTable.class, 3);
assertNull(method);
verify(interaction, times(1)).handleMethodCacheMiss(findMethod, SlimTable.class, new Integer[] { 3 });
// 2nd call cache hit
method = interaction.findMatchingMethod(findMethod, SlimTable.class, 3);
assertNull(method);
verify(interaction, times(1)).handleMethodCacheMiss(findMethod, SlimTable.class, new Integer[] { 3 });
}
}