How to use Formatting class of org.jmock.internal package

Best Jmock-library code snippet using org.jmock.internal.Formatting

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.test.unit.internal;
2
3import org.jmock.internal.Formatting;
4
5import junit.framework.TestCase;
6
7
8public class FormattingTests extends TestCase {
9    public void testFormatsTimesAsSingularAndPlural() {
10        assertEquals("0 times", Formatting.times(0));
11        assertEquals("1 time", Formatting.times(1));
12        assertEquals("2 times", Formatting.times(2));
13        assertEquals("99 times", Formatting.times(99));
14    }
15}
16
Full Screen
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

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

...Most popular Stackoverflow questions on Formatting

    No relevant questions found for this class 😞

Most used methods in Formatting

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)