|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package org.easymock.internal; |
|
6 |
| |
|
7 |
| import java.util.ArrayList; |
|
8 |
| import java.util.LinkedList; |
|
9 |
| import java.util.List; |
|
10 |
| |
|
11 |
| public class ResultList { |
|
12 |
| |
|
13 |
| private int callCount; |
|
14 |
| |
|
15 |
| private LinkedList<Range> ranges = new LinkedList<Range>(); |
|
16 |
| |
|
17 |
| private List<Result> results = new ArrayList<Result>(); |
|
18 |
| |
|
19 |
218
| void add(Result result, Range range) {
|
|
20 |
218
| if (!ranges.isEmpty()) {
|
|
21 |
26
| Range lastRange = ranges.getLast();
|
|
22 |
26
| if (!lastRange.hasFixedCount())
|
|
23 |
2
| throw new RuntimeExceptionWrapper(
|
|
24 |
| new IllegalStateException( |
|
25 |
| "last method called on mock already has a non-fixed count set.")); |
|
26 |
| } |
|
27 |
216
| ranges.add(range);
|
|
28 |
216
| results.add(result);
|
|
29 |
| } |
|
30 |
| |
|
31 |
249
| Result next() {
|
|
32 |
249
| int currentPosition = 0;
|
|
33 |
249
| for (int i = 0; i < ranges.size(); i++) {
|
|
34 |
309
| Range interval = ranges.get(i);
|
|
35 |
309
| if (interval.hasOpenCount()) {
|
|
36 |
29
| callCount += 1;
|
|
37 |
29
| return getResult(i);
|
|
38 |
| } |
|
39 |
280
| currentPosition += interval.getMaximum();
|
|
40 |
280
| if (currentPosition > callCount) {
|
|
41 |
205
| callCount += 1;
|
|
42 |
205
| return getResult(i);
|
|
43 |
| } |
|
44 |
| } |
|
45 |
15
| return null;
|
|
46 |
| } |
|
47 |
| |
|
48 |
234
| private Result getResult(int i) {
|
|
49 |
234
| return results.get(i);
|
|
50 |
| } |
|
51 |
| |
|
52 |
215
| boolean hasValidCallCount() {
|
|
53 |
215
| return getMainInterval().contains(getCallCount());
|
|
54 |
| } |
|
55 |
| |
|
56 |
32
| String getMessage() {
|
|
57 |
32
| return getMessage(getCallCount());
|
|
58 |
| } |
|
59 |
| |
|
60 |
61
| String getMessage(int count) {
|
|
61 |
61
| return getMainInterval().expectedAndActual(count);
|
|
62 |
| } |
|
63 |
| |
|
64 |
276
| private Range getMainInterval() {
|
|
65 |
276
| int min = 0;
|
|
66 |
276
| int max = 0;
|
|
67 |
| |
|
68 |
276
| for (Range interval : ranges) {
|
|
69 |
328
| min += interval.getMinimum();
|
|
70 |
328
| if (interval.hasOpenCount() || max == Integer.MAX_VALUE) {
|
|
71 |
30
| max = Integer.MAX_VALUE;
|
|
72 |
| } else { |
|
73 |
298
| max += interval.getMaximum();
|
|
74 |
| } |
|
75 |
| } |
|
76 |
| |
|
77 |
276
| return new Range(min, max);
|
|
78 |
| } |
|
79 |
| |
|
80 |
276
| public int getCallCount() {
|
|
81 |
276
| return callCount;
|
|
82 |
| } |
|
83 |
| } |