|
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 Results { |
|
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 |
385
| public void add(Result result, Range range) {
|
|
20 |
385
| if (!ranges.isEmpty()) {
|
|
21 |
39
| Range lastRange = ranges.getLast();
|
|
22 |
39
| 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 |
383
| ranges.add(range);
|
|
28 |
383
| results.add(result);
|
|
29 |
| } |
|
30 |
| |
|
31 |
431
| public Result next() {
|
|
32 |
431
| int currentPosition = 0;
|
|
33 |
431
| for (int i = 0; i < ranges.size(); i++) {
|
|
34 |
513
| Range interval = ranges.get(i);
|
|
35 |
513
| if (interval.hasOpenCount()) {
|
|
36 |
52
| callCount += 1;
|
|
37 |
52
| return results.get(i);
|
|
38 |
| } |
|
39 |
461
| currentPosition += interval.getMaximum();
|
|
40 |
461
| if (currentPosition > callCount) {
|
|
41 |
348
| callCount += 1;
|
|
42 |
348
| return results.get(i);
|
|
43 |
| } |
|
44 |
| } |
|
45 |
31
| return null;
|
|
46 |
| } |
|
47 |
| |
|
48 |
941
| public boolean hasValidCallCount() {
|
|
49 |
941
| return getMainInterval().contains(getCallCount());
|
|
50 |
| } |
|
51 |
| |
|
52 |
203
| @Override
|
|
53 |
| public String toString() { |
|
54 |
203
| return getMainInterval().expectedAndActual(getCallCount());
|
|
55 |
| } |
|
56 |
| |
|
57 |
1144
| private Range getMainInterval() {
|
|
58 |
1144
| int min = 0, max = 0;
|
|
59 |
| |
|
60 |
1144
| for (Range interval : ranges) {
|
|
61 |
1282
| min += interval.getMinimum();
|
|
62 |
1282
| if (interval.hasOpenCount() || max == Integer.MAX_VALUE) {
|
|
63 |
142
| max = Integer.MAX_VALUE;
|
|
64 |
| } else { |
|
65 |
1140
| max += interval.getMaximum();
|
|
66 |
| } |
|
67 |
| } |
|
68 |
| |
|
69 |
1144
| return new Range(min, max);
|
|
70 |
| } |
|
71 |
| |
|
72 |
1144
| public int getCallCount() {
|
|
73 |
1144
| return callCount;
|
|
74 |
| } |
|
75 |
| } |