1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package org.easymock.internal; |
6 |
| |
7 |
| import java.util.*; |
8 |
| |
9 |
| import org.easymock.IArgumentMatcher; |
10 |
| import org.easymock.internal.matchers.And; |
11 |
| import org.easymock.internal.matchers.Not; |
12 |
| import org.easymock.internal.matchers.Or; |
13 |
| |
14 |
| public class LastControl { |
15 |
| private static final WeakHashMap<Thread, MocksControl> threadToControl = new WeakHashMap<Thread, MocksControl>(); |
16 |
| |
17 |
| private static final WeakHashMap<Thread, Stack<Object[]>> threadToCurrentArguments = new WeakHashMap<Thread, Stack<Object[]>>(); |
18 |
| |
19 |
| private static final WeakHashMap<Thread, Stack<IArgumentMatcher>> threadToArgumentMatcherStack = new WeakHashMap<Thread, Stack<IArgumentMatcher>>(); |
20 |
| |
21 |
1268
| public static synchronized void reportLastControl(MocksControl control) {
|
22 |
1268
| if (control != null) {
|
23 |
436
| threadToControl.put(Thread.currentThread(), control);
|
24 |
| } else { |
25 |
832
| threadToControl.remove(Thread.currentThread());
|
26 |
| } |
27 |
| } |
28 |
| |
29 |
417
| public static synchronized MocksControl lastControl() {
|
30 |
417
| return threadToControl.get(Thread.currentThread());
|
31 |
| } |
32 |
| |
33 |
147
| public static synchronized void reportMatcher(IArgumentMatcher matcher) {
|
34 |
147
| Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get(Thread
|
35 |
| .currentThread()); |
36 |
147
| if (stack == null) {
|
37 |
115
| stack = new Stack<IArgumentMatcher>();
|
38 |
115
| threadToArgumentMatcherStack.put(Thread.currentThread(), stack);
|
39 |
| } |
40 |
147
| stack.push(matcher);
|
41 |
| } |
42 |
| |
43 |
807
| public static synchronized List<IArgumentMatcher> pullMatchers() {
|
44 |
807
| Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get(Thread
|
45 |
| .currentThread()); |
46 |
807
| if (stack == null) {
|
47 |
693
| return null;
|
48 |
| } |
49 |
114
| threadToArgumentMatcherStack.remove(Thread.currentThread());
|
50 |
114
| return new ArrayList<IArgumentMatcher>(stack);
|
51 |
| } |
52 |
| |
53 |
11
| public static synchronized void reportAnd(int count) {
|
54 |
11
| Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get(Thread
|
55 |
| .currentThread()); |
56 |
11
| assertState(stack != null, "no matchers found.");
|
57 |
11
| stack.push(new And(popLastArgumentMatchers(count)));
|
58 |
| } |
59 |
| |
60 |
14
| public static synchronized void reportNot() {
|
61 |
14
| Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get(Thread
|
62 |
| .currentThread()); |
63 |
14
| assertState(stack != null, "no matchers found.");
|
64 |
13
| stack.push(new Not(popLastArgumentMatchers(1).get(0)));
|
65 |
| } |
66 |
| |
67 |
36
| private static List<IArgumentMatcher> popLastArgumentMatchers(int count) {
|
68 |
36
| Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get(Thread
|
69 |
| .currentThread()); |
70 |
36
| assertState(stack != null, "no matchers found.");
|
71 |
36
| assertState(stack.size() >= count, "" + count + " matchers expected, "
|
72 |
| + stack.size() + " recorded."); |
73 |
35
| List<IArgumentMatcher> result = new LinkedList<IArgumentMatcher>();
|
74 |
35
| result.addAll(stack.subList(stack.size() - count, stack.size()));
|
75 |
35
| for (int i = 0; i < count; i++) {
|
76 |
57
| stack.pop();
|
77 |
| } |
78 |
35
| return result;
|
79 |
| } |
80 |
| |
81 |
109
| private static void assertState(boolean toAssert, String message) {
|
82 |
109
| if (!toAssert) {
|
83 |
2
| threadToArgumentMatcherStack.remove(Thread.currentThread());
|
84 |
2
| throw new IllegalStateException(message);
|
85 |
| } |
86 |
| } |
87 |
| |
88 |
12
| public static void reportOr(int count) {
|
89 |
12
| Stack<IArgumentMatcher> stack = threadToArgumentMatcherStack.get(Thread
|
90 |
| .currentThread()); |
91 |
12
| assertState(stack != null, "no matchers found.");
|
92 |
12
| stack.push(new Or(popLastArgumentMatchers(count)));
|
93 |
| } |
94 |
| |
95 |
11
| public static Object[] getCurrentArguments() {
|
96 |
11
| Stack<Object[]> stack = threadToCurrentArguments.get(Thread
|
97 |
| .currentThread()); |
98 |
11
| if (stack == null || stack.empty()) {
|
99 |
1
| return null;
|
100 |
| } |
101 |
10
| return stack.lastElement();
|
102 |
| } |
103 |
| |
104 |
510
| public static void pushCurrentArguments(Object[] args) {
|
105 |
510
| Stack<Object[]> stack = threadToCurrentArguments.get(Thread
|
106 |
| .currentThread()); |
107 |
510
| if (stack == null) {
|
108 |
1
| stack = new Stack<Object[]>();
|
109 |
1
| threadToCurrentArguments.put(Thread.currentThread(), stack);
|
110 |
| } |
111 |
510
| stack.push(args);
|
112 |
| } |
113 |
| |
114 |
510
| public static void popCurrentArguments() {
|
115 |
510
| Stack<Object[]> stack = threadToCurrentArguments.get(Thread
|
116 |
| .currentThread()); |
117 |
510
| stack.pop();
|
118 |
| } |
119 |
| } |