How to use method of org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher class

Best Jmock-library code snippet using org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher.

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.jmock.lib.concurrent;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Iterator;
6
7import org.hamcrest.Description;
8import org.hamcrest.SelfDescribing;
9import org.jmock.api.Expectation;
10import org.jmock.api.ExpectationError;
11import org.jmock.api.Invocation;
12import org.jmock.api.InvocationDispatcher;
13import org.jmock.internal.StateMachine;
14
15public class UnsynchronisedInvocationDispatcher implements InvocationDispatcher {
16    private final Collection<Expectation> expectations;
17    private final Collection<StateMachine> stateMachines;
18
19    public UnsynchronisedInvocationDispatcher() {
20        expectations = new ArrayList<Expectation>();
21        stateMachines = new ArrayList<StateMachine>();
22    }
23
24    public UnsynchronisedInvocationDispatcher(Collection<Expectation> theExpectations, Collection<StateMachine> theStateMachines) {
25        expectations = theExpectations;
26        stateMachines = theStateMachines;
27    }
28
29    /* (non-Javadoc)
30     * @see org.jmock.internal.InvocationDispatcher#newStateMachine(java.lang.String)
31     */
32    public StateMachine newStateMachine(String name) {
33        StateMachine stateMachine = new StateMachine(name);
34        stateMachines.add(stateMachine);
35        return stateMachine;
36    }
37
38    /* (non-Javadoc)
39     * @see org.jmock.internal.InvocationDispatcher#add(org.jmock.api.Expectation)
40     */
41    public void add(Expectation expectation) {
42        expectations.add(expectation);
43    }
44
45    /* (non-Javadoc)
46     * @see org.jmock.internal.InvocationDispatcher#describeTo(org.hamcrest.Description)
47     */
48    public void describeTo(Description description) {
49        describe(description, expectations);
50    }
51
52    /* (non-Javadoc)
53     * @see org.jmock.internal.InvocationDispatcher#describeMismatch(org.jmock.api.Invocation, org.hamcrest.Description)
54     */
55    public void describeMismatch(Invocation invocation, Description description) {
56        describe(description, describedWith(expectations, invocation));
57    }
58
59    private Iterable<SelfDescribing> describedWith(Iterable<Expectation> expectations, final Invocation invocation) {
60        final Iterator<Expectation> iterator = expectations.iterator();
61        return new Iterable<SelfDescribing>() {
62            public Iterator<SelfDescribing> iterator() {
63                return new Iterator<SelfDescribing>() {
64                    public boolean hasNext() {
65                        return iterator.hasNext();
66                    }
67
68                    public SelfDescribing next() {
69                        return new SelfDescribing() {
70                            public void describeTo(Description description) {
71                                iterator.next().describeMismatch(invocation, description);
72                            }
73                        };
74                    }
75
76                    public void remove() {
77                        iterator.remove();
78                    }
79                };
80            }
81        };
82    }
83
84    private void describe(Description description, Iterable<? extends SelfDescribing> selfDescribingExpectations) {
85        if (expectations.isEmpty()) {
86            description.appendText("no expectations specified: did you...\n" +
87                    " - forget to start an expectation with a cardinality clause?\n" +
88                    " - call a mocked method to specify the parameter of an expectation?");
89        } else {
90            description.appendList("expectations:\n  ", "\n  ", "", selfDescribingExpectations);
91            if (!stateMachines.isEmpty()) {
92                description.appendList("\nstates:\n  ", "\n  ", "", stateMachines);
93            }
94        }
95    }
96
97    /* (non-Javadoc)
98     * @see org.jmock.internal.InvocationDispatcher#isSatisfied()
99     */
100    public boolean isSatisfied() {
101        for (Expectation expectation : expectations) {
102            if (!expectation.isSatisfied()) {
103                return false;
104            }
105        }
106        return true;
107    }
108
109    /* (non-Javadoc)
110     * @see org.jmock.internal.InvocationDispatcher#dispatch(org.jmock.api.Invocation)
111     */
112    public Object dispatch(Invocation invocation) throws Throwable {
113        for (Expectation expectation : expectations) {
114            if (expectation.matches(invocation)) {
115                return expectation.invoke(invocation);
116            }
117        }
118
119        throw ExpectationError.unexpected("unexpected invocation", invocation);
120    }
121
122}
123
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)