1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.easymock.tests2; |
18 |
|
|
19 |
|
import static org.easymock.EasyMock.*; |
20 |
|
import static org.junit.Assert.*; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.List; |
24 |
|
import java.util.concurrent.*; |
25 |
|
|
26 |
|
import org.easymock.internal.AssertionErrorWrapper; |
27 |
|
import org.easymock.internal.MocksBehavior; |
28 |
|
import org.easymock.tests.IMethods; |
29 |
|
import org.junit.Test; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
@author |
37 |
|
|
|
|
| 98,7% |
Uncovered Elements: 1 (78) |
Complexity: 13 |
Complexity Density: 0,19 |
|
38 |
|
public class ThreadingTest { |
39 |
|
|
40 |
|
private static final int THREAD_COUNT = 10; |
41 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0,1 |
1
PASS
|
|
42 |
2
|
@Test... |
43 |
|
public void testThreadSafe() throws Throwable { |
44 |
|
|
45 |
2
|
final IMethods mock = createMock(IMethods.class); |
46 |
2
|
expect(mock.oneArg("test")).andReturn("result").times(THREAD_COUNT); |
47 |
|
|
48 |
2
|
replay(mock); |
49 |
|
|
50 |
2
|
final Callable<String> replay = new Callable<String>() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
51 |
20
|
public String call() throws Exception {... |
52 |
20
|
return mock.oneArg("test"); |
53 |
|
} |
54 |
|
}; |
55 |
|
|
56 |
2
|
final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT); |
57 |
|
|
58 |
2
|
final List<Callable<String>> tasks = Collections.nCopies(THREAD_COUNT, replay); |
59 |
|
|
60 |
2
|
final List<Future<String>> results = service.invokeAll(tasks); |
61 |
|
|
62 |
2
|
for (final Future<String> future : results) { |
63 |
20
|
assertEquals("result", future.get()); |
64 |
|
} |
65 |
|
|
66 |
2
|
verify(mock); |
67 |
|
} |
68 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 2 |
Complexity Density: 0,12 |
1
PASS
|
|
69 |
2
|
@Test... |
70 |
|
public void testThreadNotSafe() throws Throwable { |
71 |
|
|
72 |
2
|
final IMethods mock = createMock(IMethods.class); |
73 |
2
|
expect(mock.oneArg("test")).andReturn("result").times(THREAD_COUNT); |
74 |
|
|
75 |
2
|
makeThreadSafe(mock, false); |
76 |
|
|
77 |
2
|
checkIsUsedInOneThread(mock, true); |
78 |
|
|
79 |
2
|
replay(mock); |
80 |
|
|
81 |
2
|
final Callable<String> replay = new Callable<String>() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
20
|
public String call() throws Exception {... |
83 |
20
|
return mock.oneArg("test"); |
84 |
|
} |
85 |
|
}; |
86 |
|
|
87 |
2
|
final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT); |
88 |
|
|
89 |
2
|
final List<Callable<String>> tasks = Collections.nCopies(THREAD_COUNT, replay); |
90 |
|
|
91 |
2
|
final List<Future<String>> results = service.invokeAll(tasks); |
92 |
|
|
93 |
2
|
boolean exceptionThrown = false; |
94 |
|
|
95 |
2
|
for (final Future<String> future : results) { |
96 |
20
|
try { |
97 |
20
|
assertEquals("result", future.get()); |
98 |
|
} catch (final ExecutionException e) { |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
18
|
assertTrue(e.getCause().getMessage().startsWith( |
103 |
|
"\n Mock isn't supposed to be called from multiple threads. Last: ")); |
104 |
18
|
exceptionThrown = true; |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
2
|
assertTrue(exceptionThrown); |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0,14 |
1
PASS
|
|
111 |
2
|
@Test... |
112 |
|
public void testMockUsedCorrectly() { |
113 |
2
|
final IMethods mock = createMock(IMethods.class); |
114 |
2
|
expect(mock.oneArg("test")).andReturn("result").times(2); |
115 |
|
|
116 |
2
|
checkIsUsedInOneThread(mock, true); |
117 |
|
|
118 |
2
|
replay(mock); |
119 |
|
|
120 |
2
|
mock.oneArg("test"); |
121 |
2
|
mock.oneArg("test"); |
122 |
|
|
123 |
2
|
verify(mock); |
124 |
|
} |
125 |
|
|
|
|
| 92,3% |
Uncovered Elements: 1 (13) |
Complexity: 2 |
Complexity Density: 0,15 |
1
PASS
|
|
126 |
2
|
@Test... |
127 |
|
public void testChangeDefault() throws Throwable { |
128 |
2
|
final String previousThreadSafetyCheck = setEasyMockProperty(ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT, |
129 |
|
Boolean.TRUE.toString()); |
130 |
2
|
final String previousThreadSafe = setEasyMockProperty(NOT_THREAD_SAFE_BY_DEFAULT, Boolean.TRUE |
131 |
|
.toString()); |
132 |
2
|
try { |
133 |
2
|
final MocksBehavior behavior = new MocksBehavior(true); |
134 |
2
|
assertFalse(behavior.isThreadSafe()); |
135 |
|
|
136 |
2
|
final Thread t = new Thread() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
2
|
@Override... |
138 |
|
public void run() { |
139 |
2
|
behavior.checkThreadSafety(); |
140 |
|
} |
141 |
|
}; |
142 |
2
|
t.start(); |
143 |
2
|
t.join(); |
144 |
2
|
try { |
145 |
2
|
behavior.checkThreadSafety(); |
146 |
0
|
fail("Shouldn't work"); |
147 |
|
} catch (final AssertionErrorWrapper e) { |
148 |
|
|
149 |
|
} |
150 |
|
|
151 |
|
} finally { |
152 |
2
|
setEasyMockProperty(ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT, previousThreadSafetyCheck); |
153 |
2
|
setEasyMockProperty(NOT_THREAD_SAFE_BY_DEFAULT, previousThreadSafe); |
154 |
|
} |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
1
PASS
|
|
157 |
2
|
@Test... |
158 |
|
public void testRecordingInMultipleThreads() throws InterruptedException, ExecutionException { |
159 |
|
|
160 |
2
|
final Callable<String> replay = new Callable<String>() { |
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
|
161 |
20
|
public String call() throws Exception {... |
162 |
20
|
final IMethods mock = createMock(IMethods.class); |
163 |
20
|
expect(mock.oneArg("test")).andReturn("result"); |
164 |
|
|
165 |
20
|
replay(mock); |
166 |
|
|
167 |
20
|
final String s = mock.oneArg("test"); |
168 |
|
|
169 |
20
|
verify(mock); |
170 |
|
|
171 |
20
|
return s; |
172 |
|
} |
173 |
|
}; |
174 |
|
|
175 |
2
|
final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT); |
176 |
|
|
177 |
2
|
final List<Callable<String>> tasks = Collections.nCopies(THREAD_COUNT, replay); |
178 |
|
|
179 |
2
|
final List<Future<String>> results = service.invokeAll(tasks); |
180 |
|
|
181 |
2
|
for (final Future<String> future : results) { |
182 |
20
|
assertEquals("result", future.get()); |
183 |
|
} |
184 |
|
} |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0,29 |
1
PASS
|
|
186 |
2
|
@SuppressWarnings("unchecked")... |
187 |
|
@Test |
188 |
|
public void testCleanupAfterFailureInRecordPhase() { |
189 |
2
|
Comparable<String> mock = createNiceMock(Comparable.class); |
190 |
|
|
191 |
|
|
192 |
2
|
try { |
193 |
2
|
expect(mock.equals(eq(mock))).andReturn(true); |
194 |
|
} catch (final IllegalStateException e) { |
195 |
|
|
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
2
|
mock = createNiceMock(Comparable.class); |
201 |
2
|
expect(mock.compareTo((String) isNull())).andReturn(1); |
202 |
2
|
replay(mock); |
203 |
2
|
assertEquals(1, mock.compareTo(null)); |
204 |
|
} |
205 |
|
} |