Source Code Cross Referenced for EasyMockUnitils.java in  » Testing » unitils » org » unitils » easymock » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » unitils » org.unitils.easymock 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.