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

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

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.matcher;
2
3import java.util.List;
4
5import org.hamcrest.Matcher;
6import org.hamcrest.collection.IsArray;
7import org.hamcrest.core.IsEqual;
8
9public class ParametersMatcher extends IsArray<Object> {
10    public ParametersMatcher(Object[] expectedValues) {
11        super(equalMatchersFor(expectedValues));
12    }
13    
14    @SuppressWarnings("unchecked")
15    private static Matcher<Object>[] equalMatchersFor(Object[] expectedValues) {
16        Matcher<Object>[] matchers = new Matcher[expectedValues.length];
17        for (int i = 0; i < expectedValues.length; i++) {
18            matchers[i] = new IsEqual<Object>(expectedValues[i]);
19        }
20        return matchers;
21    }
22    
23    @SuppressWarnings("unchecked")
24    public ParametersMatcher(List<Matcher<?>> parameterMatchers) {
25        super(parameterMatchers.toArray(new Matcher[0]));
26    }
27
28    @Override
29    protected String descriptionStart() {
30        return "(";
31    }
32    
33    @Override
34    protected String descriptionEnd() {
35        return ")";
36    }
37}
38
Full Screen
copy
1package org.jmock.internal.matcher;
2
3import static java.util.Arrays.asList;
4
5import java.util.List;
6
7import org.hamcrest.Description;
8import org.hamcrest.Matcher;
9import org.hamcrest.TypeSafeDiagnosingMatcher;
10import org.hamcrest.core.IsEqual;
11import org.jmock.internal.ParametersMatcher;
12
13public class AllParametersMatcher extends TypeSafeDiagnosingMatcher<Object[]>  implements ParametersMatcher {
14    private final Matcher<Object>[] elementMatchers;
15
16    public AllParametersMatcher(Object[] expectedValues) {
17        this.elementMatchers =  equalMatchersFor(expectedValues);
18    }
19    
20    @SuppressWarnings("unchecked")
21    public AllParametersMatcher(List<Matcher<?>> parameterMatchers) {
22        this.elementMatchers = parameterMatchers.toArray(new Matcher[0]);
23    }
24
25    public boolean isCompatibleWith(Object[] parameters) {
26        return elementMatchers.length == parameters.length;
27    }
28
29    @Override
30    public boolean matchesSafely(Object[] parameters, Description mismatch) {
31        return matchesNumberOfParameters(parameters, mismatch)
32            && matchesParameters(parameters, mismatch);
33    }
34
35    private boolean matchesNumberOfParameters(Object[] parameters, Description mismatch) {
36        if (elementMatchers.length != parameters.length) {
37            mismatch.appendText("wrong number of parameters: ")
38                    .appendValue(parameters);
39            return false;
40        }
41        return true;
42    }
43
44    private boolean matchesParameters(Object[] parameters, Description mismatch) {
45        boolean result = true;
46        for (int i = 0; i < parameters.length; i++) {
47            result &= matchesParameter(parameters[i], elementMatchers[i], mismatch, i);
48        }
49        return result;
50    }
51
52    private boolean matchesParameter(final Object value, final Matcher<Object> matcher, Description mismatch, int index) {
53        mismatch.appendText("\n      parameter " + index + " ");
54        final boolean parameterMatches = matcher.matches(value);
55        if (parameterMatches) {
56            mismatch.appendText("matched: ").appendDescriptionOf(matcher);
57        } else {
58            mismatch.appendText("did not match: ")
59                .appendDescriptionOf(matcher)
60                .appendText(", because ");
61            matcher.describeMismatch(value, mismatch);
62        }
63        return parameterMatches;
64    }
65
66    public void describeTo(Description description) {
67        description.appendList("(", ", ",")", asList(elementMatchers));
68    }
69    
70    @SuppressWarnings("unchecked")
71    private static Matcher<Object>[] equalMatchersFor(Object[] expectedValues) {
72        Matcher<Object>[] matchers = new Matcher[expectedValues.length];
73        for (int i = 0; i < expectedValues.length; i++) {
74            matchers[i] = new IsEqual<Object>(expectedValues[i]);
75        }
76        return matchers;
77    }
78
79}
80
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)