How to use method of org.jmock.internal.matcher.MethodMatcher class

Best Jmock-library code snippet using org.jmock.internal.matcher.MethodMatcher.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.internal;
2
3import org.hamcrest.Description;
4import org.hamcrest.Matcher;
5import org.hamcrest.core.IsAnything;
6import org.jmock.api.Action;
7import org.jmock.api.Expectation;
8import org.jmock.api.Invocation;
9import org.jmock.api.Invocation.ExpectationMode;
10import org.jmock.internal.matcher.MethodMatcher;
11import org.jmock.lib.action.VoidAction;
12
13import java.lang.reflect.Method;
14import java.util.ArrayList;
15import java.util.List;
16
17/** 
18 * An expectation of zero or more matching invocations.
19 * 
20 * @author npryce
21 * @author smgf
22 */
23public class InvocationExpectation implements Expectation {
24    private static ParametersMatcher ANY_PARAMETERS = new AnyParametersMatcher();
25    private Cardinality cardinality = Cardinality.ALLOWING;
26	private Matcher<?> objectMatcher = IsAnything.anything();
27	private Matcher<? super Method> methodMatcher = IsAnything.anything("<any method>");
28	private boolean methodIsKnownToBeVoid = false;
29	private ParametersMatcher parametersMatcher = ANY_PARAMETERS;
30    private Action action = new VoidAction();
31    private boolean actionIsDefault = true;
32    private List<OrderingConstraint> orderingConstraints = new ArrayList<OrderingConstraint>();
33    private List<SideEffect> sideEffects = new ArrayList<SideEffect>();
34    
35	private int invocationCount = 0;
36	
37    public void setCardinality(Cardinality cardinality) {
38        this.cardinality = cardinality;
39    }
40	
41	public void setObjectMatcher(Matcher<?> objectMatcher) {
42		this.objectMatcher = objectMatcher;
43	}
44	
45	public void setMethod(Method method) {
46	    this.methodMatcher = new MethodMatcher(method);
47	    this.methodIsKnownToBeVoid = method.getReturnType() == void.class;
48	}
49	
50	public void setMethodMatcher(Matcher<? super Method> matcher) {
51		this.methodMatcher = matcher;
52		this.methodIsKnownToBeVoid = false;
53	}
54	
55	public void setParametersMatcher(ParametersMatcher parametersMatcher) {
56		this.parametersMatcher = parametersMatcher;
57	}
58
59    public void addOrderingConstraint(OrderingConstraint orderingConstraint) {
60        orderingConstraints.add(orderingConstraint);
61    }
62
63    public void addSideEffect(SideEffect sideEffect) {
64        sideEffects.add(sideEffect);
65    }
66    
67    public void setAction(Action action) {
68        this.action = action;
69        this.actionIsDefault = false;
70    }
71    
72    public void setDefaultAction(Action action) {
73        this.action = action;
74        this.actionIsDefault = true;
75    }
76    
77    public void describeTo(Description description) {
78        if (! isSatisfied()) {
79            description.appendText("! ");
80        }
81
82        describeExpectation(description);
83    }
84
85    public void describeMismatch(Invocation invocation, Description description) {
86        describeExpectation(description);
87
88        final Object[] parameters = invocation.getParametersAsArray();
89        if (methodMatcher.matches(invocation.getInvokedMethod()) &&
90            parametersMatcher.isCompatibleWith(parameters))
91        {
92            parametersMatcher.describeMismatch(parameters, description);
93        }
94    }
95
96    private void describeExpectation(Description description) {
97        describeMethod(description);
98        parametersMatcher.describeTo(description);
99        describeSideEffects(description);
100    }
101
102    private void describeMethod(Description description) {
103        cardinality.describeTo(description);
104        description.appendText(", ");
105        if (invocationCount == 0) {
106            description.appendText("never invoked");
107        }
108        else {
109            description.appendText("already invoked ");
110            description.appendText(Formatting.times(invocationCount));
111        }
112        description.appendText(": ");
113        objectMatcher.describeTo(description);
114        description.appendText(".");
115        methodMatcher.describeTo(description);
116    }
117    
118    private void describeSideEffects(Description description) {
119        for (OrderingConstraint orderingConstraint : orderingConstraints) {
120            description.appendText("; ");
121            orderingConstraint.describeTo(description);
122        }
123        
124        if (!shouldSuppressActionDescription()) {
125            description.appendText("; ");
126            action.describeTo(description);
127        }
128        
129        for (SideEffect sideEffect : sideEffects) {
130            description.appendText("; ");
131            sideEffect.describeTo(description);
132        }
133    }
134
135    private boolean shouldSuppressActionDescription() {
136        return methodIsKnownToBeVoid && actionIsDefault;
137    }
138
139    public boolean isSatisfied() {
140        return cardinality.isSatisfied(invocationCount);
141    }
142    
143    public boolean allowsMoreInvocations() {
144        return cardinality.allowsMoreInvocations(invocationCount);
145    }
146    
147    public boolean matches(Invocation invocation) {
148		return allowsMoreInvocations()
149			&& objectMatcher.matches(invocation.getInvokedObject())
150			&& methodMatcher.matches(invocation.getInvokedMethod())
151			&& parametersMatcher.matches(invocation.getParametersAsArray())
152            && isInCorrectOrder();
153        
154	}
155    
156	private boolean isInCorrectOrder() {
157        for (OrderingConstraint constraint : orderingConstraints) {
158            if (!constraint.allowsInvocationNow()) return false;
159        }
160        return true;
161    }
162	
163    public Object invoke(Invocation invocation) throws Throwable {
164		invocationCount++;
165		performSideEffects();
166		final Object result = action.invoke(new Invocation(ExpectationMode.ASSERTING, invocation));
167        invocation.checkReturnTypeCompatibility(result);
168        return result;
169	}
170
171    private void performSideEffects() {
172        for (SideEffect sideEffect : sideEffects) {
173            sideEffect.perform();
174        }
175    }
176    
177    private static class AnyParametersMatcher extends IsAnything<Object[]> implements ParametersMatcher {
178        public AnyParametersMatcher() {
179            super("(<any parameters>)");
180        }
181
182        public boolean isCompatibleWith(Object[] parameters) {
183            return true;
184        }
185    };
186
187}
188
Full Screen
copy
1package org.jmock.internal;
2
3import java.lang.reflect.Method;
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.List;
7
8import org.hamcrest.Matcher;
9import org.jmock.Sequence;
10import org.jmock.api.Action;
11import org.jmock.api.Expectation;
12import org.jmock.api.Invocation;
13import org.jmock.internal.matcher.MethodNameMatcher;
14import org.jmock.internal.matcher.MockObjectMatcher;
15import org.jmock.internal.matcher.AllParametersMatcher;
16import org.jmock.syntax.MethodClause;
17import org.jmock.syntax.ParametersClause;
18import org.jmock.syntax.ReceiverClause;
19
20public class InvocationExpectationBuilder 
21    implements ExpectationCapture, 
22               ReceiverClause, MethodClause, ParametersClause
23{
24    private final InvocationExpectation expectation = new InvocationExpectation();
25    
26    private boolean isFullySpecified = false;
27    private boolean needsDefaultAction = true;
28    private List<Matcher<?>> capturedParameterMatchers = new ArrayList<Matcher<?>>();
29    
30    public Expectation toExpectation(Action defaultAction) {
31        if (needsDefaultAction) {
32            expectation.setDefaultAction(defaultAction);
33        }
34        
35        return expectation;
36    }
37    
38    public void setCardinality(Cardinality cardinality) {
39        expectation.setCardinality(cardinality);
40    }
41    
42    public void addParameterMatcher(Matcher<?> matcher) {
43        capturedParameterMatchers.add(matcher);
44    }
45    
46    public void addOrderingConstraint(OrderingConstraint constraint) {
47        expectation.addOrderingConstraint(constraint);
48    }
49    
50    public void addInSequenceOrderingConstraint(Sequence sequence) {
51        sequence.constrainAsNextInSequence(expectation);
52    }
53    
54    public void setAction(Action action) {
55        expectation.setAction(action);
56        needsDefaultAction = false;
57    }
58    
59    public void addSideEffect(SideEffect sideEffect) {
60        expectation.addSideEffect(sideEffect);
61    }
62    
63    private <T> T captureExpectedObject(T mockObject) {
64        if (!(mockObject instanceof CaptureControl)) {
65            throw new IllegalArgumentException("can only set expectations on mock objects");
66        }
67        
68        expectation.setObjectMatcher(new MockObjectMatcher(mockObject));
69        isFullySpecified = true;
70        
71        Object capturingImposter = ((CaptureControl)mockObject).captureExpectationTo(this);
72        
73        return asMockedType(mockObject, capturingImposter);
74    }
75    
76    // Damn you Java generics! Damn you to HELL!
77    @SuppressWarnings("unchecked")
78    private <T> T asMockedType(@SuppressWarnings("unused") T mockObject, 
79                               Object capturingImposter) 
80    {
81        return (T) capturingImposter;
82    }
83    
84    public void createExpectationFrom(Invocation invocation) {
85        expectation.setMethod(invocation.getInvokedMethod());
86        
87        if (capturedParameterMatchers.isEmpty()) {
88            expectation.setParametersMatcher(new AllParametersMatcher(invocation.getParametersAsArray()));
89        }
90        else {
91            checkParameterMatcherCount(invocation);
92            expectation.setParametersMatcher(new AllParametersMatcher(capturedParameterMatchers));
93        }
94    }
95    
96    private void checkParameterMatcherCount(Invocation invocation) {
97        if (capturedParameterMatchers.size() != invocation.getParameterCount()) {
98            throw new IllegalArgumentException("not all parameters were given explicit matchers: either all parameters must be specified by matchers or all must be specified by values, you cannot mix matchers and values");
99        }
100    }
101    
102    public void checkWasFullySpecified() {
103        if (!isFullySpecified) {
104            throw new IllegalStateException("expectation was not fully specified");
105        }
106    }
107
108    /* 
109     * Syntactic sugar
110     */
111    
112    public <T> T of(T mockObject) {
113        return captureExpectedObject(mockObject);
114    }
115
116    public MethodClause of(Matcher<?> objectMatcher) {
117        expectation.setObjectMatcher(objectMatcher);
118        isFullySpecified = true;
119        return this;
120    }
121
122    public ParametersClause method(Matcher<Method> methodMatcher) {
123        expectation.setMethodMatcher(methodMatcher);
124        return this;
125    }
126    
127    public ParametersClause method(String nameRegex) {
128        return method(new MethodNameMatcher(nameRegex));
129    }
130    
131    public void with(Matcher<?>... parameterMatchers) {
132        expectation.setParametersMatcher(new AllParametersMatcher(Arrays.asList(parameterMatchers)));
133    }
134    
135    public void withNoArguments() {
136        with();
137    }
138}
139
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)