001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.easymock;
017:
018: import static org.easymock.EasyMock.reportMatcher;
019: import org.unitils.core.Unitils;
020: import org.unitils.core.UnitilsException;
021: import org.unitils.easymock.annotation.Mock;
022: import org.unitils.easymock.util.*;
023: import org.unitils.reflectionassert.ReflectionComparatorMode;
024: import static org.unitils.reflectionassert.ReflectionComparatorMode.IGNORE_DEFAULTS;
025: import static org.unitils.reflectionassert.ReflectionComparatorMode.LENIENT_ORDER;
026:
027: /**
028: * Utility facade for handling EasyMock things such as replay or manually creating a mock.
029: *
030: * @author Tim Ducheyne
031: * @author Filip Neven
032: */
033: public class EasyMockUnitils {
034:
035: /**
036: * Expects the given object argument but uses a reflection argument matcher to compare
037: * the given value with the actual value during the test. The comparator modes are set to
038: * ignore defaults and lenient order.
039: * <p/>
040: * Same as refEq with ignore defaults and lenient order as comparator modes.
041: *
042: * @param <T> The type of the object to compare with
043: * @param object the value
044: * @return null
045: */
046: public static <T> T lenEq(T object) {
047: return refEq(object, IGNORE_DEFAULTS, LENIENT_ORDER);
048: }
049:
050: /**
051: * Expects the given object argument but uses a reflection argument matcher with the given comparator modes
052: * to compare the given value with the actual value during the test.
053: *
054: * @param <T> the type of the object to compare with
055: * @param object the value
056: * @param modes the comparator modes
057: * @return null
058: */
059: public static <T> T refEq(T object,
060: ReflectionComparatorMode... modes) {
061: ReflectionArgumentMatcher<T> reflectionArgumentMatcher = new ReflectionArgumentMatcher<T>(
062: object, modes);
063: reportMatcher(reflectionArgumentMatcher);
064: return object;
065: }
066:
067: /**
068: * Creates a regular EasyMock mock object of the given type.
069: * <p/>
070: * Same as {@link #createRegularMock(Class,InvocationOrder,Calls)} with a default invocation order
071: * and default calls value. These defaults can be set in the unitils configuration.
072: * <p/>
073: * An instance of the mock control is stored, so that it can be set to the replay/verify state when
074: * {@link #replay()} or {@link #verify()} is called.
075: *
076: * @param <T> the type of the mock
077: * @param mockType the type of the mock, not null
078: * @return a mock for the given class or interface, not null
079: */
080: public static <T> T createRegularMock(Class<T> mockType) {
081: return createRegularMock(mockType, InvocationOrder.DEFAULT,
082: Calls.DEFAULT);
083: }
084:
085: /**
086: * Creates a regular EasyMock mock object of the given type.
087: * <p/>
088: * An instance of the mock control is stored, so that it can be set to the replay/verify state when
089: * {@link #replay()} or {@link #verify()} is called.
090: *
091: * @param <T> the type of the mock
092: * @param mockType the class type for the mock, not null
093: * @param invocationOrder the order setting, not null
094: * @param calls the calls setting, not null
095: * @return a mock for the given class or interface, not null
096: */
097: public static <T> T createRegularMock(Class<T> mockType,
098: InvocationOrder invocationOrder, Calls calls) {
099: return getEasyMockModule().createRegularMock(mockType,
100: invocationOrder, calls);
101: }
102:
103: /**
104: * Creates a lenient mock object of the given type. The {@link org.unitils.easymock.util.LenientMocksControl} is used
105: * for creating the mock.
106: * <p/>
107: * Same as {@link #createMock(Class,InvocationOrder,Calls,Order,Dates,Defaults)} with a default invocation order,
108: * default calls, default order, default dates and default defaults value. These defaults can be set in the
109: * unitils configuration.
110: * <p/>
111: * An instance of the mock control is stored, so that it can be set to the replay/verify state when
112: * {@link #replay()} or {@link #verify()} is called.
113: *
114: * @param <T> the type of the mock
115: * @param mockType the type of the mock, not null
116: * @return a mock for the given class or interface, not null
117: */
118: public static <T> T createMock(Class<T> mockType) {
119: return createMock(mockType, InvocationOrder.DEFAULT,
120: Calls.DEFAULT, Order.DEFAULT, Dates.DEFAULT,
121: Defaults.DEFAULT);
122: }
123:
124: /**
125: * Creates a lenient mock object of the given type. The {@link org.unitils.easymock.util.LenientMocksControl} is used
126: * for creating the mock.
127: * <p/>
128: * Same as {@link #createMock(Class,InvocationOrder,Calls,Order,Dates,Defaults)} with a default invocation order,
129: * default calls, default order, default dates and default defaults value. These defaults can be set in the
130: * unitils configuration.
131: * <p/>
132: * An instance of the mock control is stored, so that it can be set to the replay/verify state when
133: * {@link #replay()} or {@link #verify()} is called.
134: *
135: * @param <T> the type of the mock
136: * @param mockType the type of the mock, not null
137: * @param invocationOrder the invocation order setting, not null
138: * @param calls the calls setting, not null
139: * @param order the order setting, not null
140: * @param dates the dates setting, not null
141: * @param defaults the defaults setting, not null
142: * @return a mock for the given class or interface, not null
143: */
144: public static <T> T createMock(Class<T> mockType,
145: InvocationOrder invocationOrder, Calls calls, Order order,
146: Dates dates, Defaults defaults) {
147: return getEasyMockModule().createMock(mockType,
148: invocationOrder, calls, order, dates, defaults);
149: }
150:
151: /**
152: * Unit tests should call this method after having set their recorded expected behavior on the mock objects.
153: * <p/>
154: * This method makes sure EasyMock's replay method is called on every mock object that was supplied to the
155: * fields annotated with {@link org.unitils.easymock.annotation.Mock}, or directly created by the
156: * {@link #createRegularMock(Class,InvocationOrder,Calls)} and
157: * {@link #createMock(Class,InvocationOrder,Calls,Order,Dates,Defaults)} methods.
158: * <p/>
159: * After each test, the expected behavior is verified automatically, or explicitly by calling {@link #verify()}.
160: */
161: public static void replay() {
162: getEasyMockModule().replay();
163: }
164:
165: /**
166: * Unit tests can call this method to check whether all recorded expected behavior was actually observed during
167: * the test.
168: * <p/>
169: * This method makes sure {@link org.easymock.internal.MocksControl#verify} method is called for every mock mock object
170: * that was injected to a field annotated with {@link Mock}, or directly created by the
171: * {@link #createRegularMock(Class,InvocationOrder,Calls)} or
172: * {@link #createMock(Class,InvocationOrder,Calls,Order,Dates,Defaults)} methods.
173: * <p/>
174: * By default, the expected behavior is verified automatically. This can be disabled however by setting the property
175: * EasyMockModule.autoVerifyAfterTest.enabled to false. In that case, verification can also be performed explicitly
176: * by calling this method.
177: */
178: public static void verify() {
179: getEasyMockModule().verify();
180: }
181:
182: /**
183: * Gets the instance EasyMockModule that is registered in the modules repository.
184: * This instance implements the actual behavior of the static methods in this class, such as {@link #replay()}.
185: * This way, other implementations can be plugged in, while keeping the simplicity of using static methods.
186: *
187: * @return the instance, not null
188: * @throws UnitilsException when no such module could be found
189: */
190: private static EasyMockModule getEasyMockModule() {
191: Unitils unitils = Unitils.getInstance();
192: EasyMockModule module = unitils.getModulesRepository()
193: .getModuleOfType(EasyMockModule.class);
194: if (module == null) {
195: throw new UnitilsException(
196: "Unable to find an instance of an EasyMockModule in the modules repository.");
197: }
198: return module;
199: }
200:
201: }
|