• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2003-2009 OFFIS, Henri Tremblay
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.easymock;
17 
18 import java.util.ArrayList;
19 import java.util.List;
20 
21 /**
22  * Helper class to be used to keep tracks of mocks easily. See EasyMock
23  * documentation and SupportTest sample
24  */
25 public class EasyMockSupport {
26 
27     /** List of all controls created */
28     protected final List<IMocksControl> controls = new ArrayList<IMocksControl>(
29             5);
30 
31     /**
32      * Creates a mock object that implements the given interface, order checking
33      * is enabled by default.
34      *
35      * @param <T>
36      *            the interface that the mock object should implement.
37      * @param toMock
38      *            the class of the interface that the mock object should
39      *            implement.
40      * @return the mock object.
41      */
createStrictMock(Class<T> toMock)42     public <T> T createStrictMock(Class<T> toMock) {
43         return createStrictControl().createMock(toMock);
44     }
45 
46     /**
47      * Creates a mock object that implements the given interface, order checking
48      * is enabled by default.
49      *
50      * @param name
51      *            the name of the mock object.
52      * @param toMock
53      *            the class of the interface that the mock object should
54      *            implement.
55      * @param <T>
56      *            the interface that the mock object should implement.
57      * @return the mock object.
58      * @throws IllegalArgumentException
59      *             if the name is not a valid Java identifier.
60      */
createStrictMock(String name, Class<T> toMock)61     public <T> T createStrictMock(String name, Class<T> toMock) {
62         return createStrictControl().createMock(name, toMock);
63     }
64 
65     /**
66      * Creates a mock object that implements the given interface, order checking
67      * is disabled by default.
68      *
69      * @param <T>
70      *            the interface that the mock object should implement.
71      * @param toMock
72      *            the class of the interface that the mock object should
73      *            implement.
74      * @return the mock object.
75      */
createMock(Class<T> toMock)76     public <T> T createMock(Class<T> toMock) {
77         return createControl().createMock(toMock);
78     }
79 
80     /**
81      * Creates a mock object that implements the given interface, order checking
82      * is disabled by default.
83      *
84      * @param name
85      *            the name of the mock object.
86      * @param toMock
87      *            the class of the interface that the mock object should
88      *            implement.
89      *
90      * @param <T>
91      *            the interface that the mock object should implement.
92      * @return the mock object.
93      * @throws IllegalArgumentException
94      *             if the name is not a valid Java identifier.
95      */
createMock(String name, Class<T> toMock)96     public <T> T createMock(String name, Class<T> toMock) {
97         return createControl().createMock(name, toMock);
98     }
99 
100     /**
101      * Creates a mock object that implements the given interface, order checking
102      * is disabled by default, and the mock object will return <code>0</code>,
103      * <code>null</code> or <code>false</code> for unexpected invocations.
104      *
105      * @param <T>
106      *            the interface that the mock object should implement.
107      * @param toMock
108      *            the class of the interface that the mock object should
109      *            implement.
110      * @return the mock object.
111      */
createNiceMock(Class<T> toMock)112     public <T> T createNiceMock(Class<T> toMock) {
113         return createNiceControl().createMock(toMock);
114     }
115 
116     /**
117      * Creates a mock object that implements the given interface, order checking
118      * is disabled by default, and the mock object will return <code>0</code>,
119      * <code>null</code> or <code>false</code> for unexpected invocations.
120      *
121      * @param name
122      *            the name of the mock object.
123      * @param toMock
124      *            the class of the interface that the mock object should
125      *            implement.
126      *
127      * @param <T>
128      *            the interface that the mock object should implement.
129      * @return the mock object.
130      * @throws IllegalArgumentException
131      *             if the name is not a valid Java identifier.
132      */
createNiceMock(String name, Class<T> toMock)133     public <T> T createNiceMock(String name, Class<T> toMock) {
134         return createNiceControl().createMock(name, toMock);
135     }
136 
137     /**
138      * Creates a control, order checking is enabled by default.
139      *
140      * @return the control.
141      */
createStrictControl()142     public IMocksControl createStrictControl() {
143         IMocksControl ctrl = EasyMock.createStrictControl();
144         controls.add(ctrl);
145         return ctrl;
146     }
147 
148     /**
149      * Creates a control, order checking is disabled by default.
150      *
151      * @return the control.
152      */
createControl()153     public IMocksControl createControl() {
154         IMocksControl ctrl = EasyMock.createControl();
155         controls.add(ctrl);
156         return ctrl;
157     }
158 
159     /**
160      * Creates a control, order checking is disabled by default, and the mock
161      * objects created by this control will return <code>0</code>,
162      * <code>null</code> or <code>false</code> for unexpected invocations.
163      *
164      * @return the control.
165      */
createNiceControl()166     public IMocksControl createNiceControl() {
167         IMocksControl ctrl = EasyMock.createNiceControl();
168         controls.add(ctrl);
169         return ctrl;
170     }
171 
172     /**
173      * Switches all registered mock objects (more exactly: the controls of the
174      * mock objects) to replay mode. For details, see the EasyMock
175      * documentation.
176      */
replayAll()177     public void replayAll() {
178         for (IMocksControl c : controls) {
179             c.replay();
180         }
181     }
182 
183     /**
184      * Resets all registered mock objects (more exactly: the controls of the
185      * mock objects). For details, see the EasyMock documentation.
186      */
resetAll()187     public void resetAll() {
188         for (IMocksControl c : controls) {
189             c.reset();
190         }
191     }
192 
193     /**
194      * Verifies all registered mock objects (more exactly: the controls of the
195      * mock objects).
196      */
verifyAll()197     public void verifyAll() {
198         for (IMocksControl c : controls) {
199             c.verify();
200         }
201     }
202 
203     /**
204      * Resets all registered mock objects (more exactly: the controls of the
205      * mock objects) and turn them to a mock with nice behavior. For details,
206      * see the EasyMock documentation.
207      */
resetAllToNice()208     public void resetAllToNice() {
209         for (IMocksControl c : controls) {
210             c.resetToNice();
211         }
212     }
213 
214     /**
215      * Resets all registered mock objects (more exactly: the controls of the
216      * mock objects) and turn them to a mock with default behavior. For details,
217      * see the EasyMock documentation.
218      */
resetAllToDefault()219     public void resetAllToDefault() {
220         for (IMocksControl c : controls) {
221             c.resetToDefault();
222         }
223     }
224 
225     /**
226      * Resets all registered mock objects (more exactly: the controls of the
227      * mock objects) and turn them to a mock with strict behavior. For details,
228      * see the EasyMock documentation.
229      */
resetAllToStrict()230     public void resetAllToStrict() {
231         for (IMocksControl c : controls) {
232             c.resetToStrict();
233         }
234     }
235 
236 }
237