KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > classextension > MockClassControl


1 /*
2  * Copyright (c) 2003-2004 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.classextension;
6
7 import java.lang.reflect.Method JavaDoc;
8
9 import org.easymock.MockControl;
10 import org.easymock.classextension.internal.ClassProxyFactory;
11 import org.easymock.internal.IBehaviorFactory;
12
13 /**
14  * Instances of <code>MockClassControl</code> control the behavior of their
15  * associated mock objects. For more information, see the EasyMock
16  * documentation.
17  *
18  * @see <a HREF="http://www.easymock.org/">Easymock </a>
19  */

20 public class MockClassControl extends MockControl {
21
22     /**
23      * Creates a mock control object for the specified class or interface. The
24      * {@link MockClassControl}and its associated mock object will not check
25      * the order of expected method calls. An unexpected method call on the mock
26      * object will lead to an <code>AssertionFailedError</code>.
27      *
28      * @param classToMock
29      * the class to mock.
30      * @return the mock control (which is a {@link MockClassControl}instance)
31      */

32     public static MockControl createControl(Class JavaDoc classToMock) {
33         if (classToMock.isInterface()) {
34             return MockControl.createControl(classToMock);
35         }
36         return new MockClassControl(classToMock, UNORDERED_BEHAVIOR_FACTORY);
37     }
38
39     /**
40      * Same as {@link #createControl(Class)}but allows to pass a list of
41      * methods to mock. All the other methods won't be. It means that if these
42      * methods are called, their real code will be executed.
43      *
44      * @param classToMock
45      * the class to mock
46      * @param mockedMethods
47      * Methods to be mocked. If null, all methods will be mocked.
48      * @return the mock control
49      */

50     public static MockClassControl createControl(Class JavaDoc classToMock,
51             Method JavaDoc[] mockedMethods) {
52         return new MockClassControl(classToMock, mockedMethods,
53                 UNORDERED_BEHAVIOR_FACTORY);
54     }
55
56     /**
57      * Creates a mock control object for the specified class or interface. The
58      * {@link MockClassControl}and its associated mock object will check the
59      * order of expected method calls. An unexpected method call on the mock
60      * object will lead to an <code>AssertionFailedError</code>.
61      *
62      * @param classToMock
63      * the class to mock.
64      * @return the mock control (which is a {@link MockClassControl}instance)
65      */

66     public static MockControl createStrictControl(Class JavaDoc classToMock) {
67         if (classToMock.isInterface()) {
68             return MockControl.createStrictControl(classToMock);
69         }
70         return new MockClassControl(classToMock, ORDERED_BEHAVIOR_FACTORY);
71     }
72
73     /**
74      * Same as {@link #createStrictControl(Class)}but allows to pass a list of
75      * methods to mock. All the other methods won't be. It means that if these
76      * methods are called, their real code will be executed.
77      *
78      * @param classToMock
79      * the class to mock
80      * @param mockedMethods
81      * Methods to be mocked. If null, all methods will be mocked.
82      * @return the mock control
83      */

84     public static MockClassControl createStrictControl(Class JavaDoc classToMock,
85             Method JavaDoc[] mockedMethods) {
86         return new MockClassControl(classToMock, mockedMethods,
87                 ORDERED_BEHAVIOR_FACTORY);
88     }
89
90     /**
91      * Creates a mock control object for the specified class or interface. The
92      * {@link MockClassControl}and its associated mock object will check not
93      * the order of expected method calls. An unexpected method call on the mock
94      * object will return an empty value (0, null, false).
95      *
96      * @param classToMock
97      * the class to mock.
98      * @return the mock control (which is a {@link MockClassControl}instance)
99      */

100     public static MockControl createNiceControl(Class JavaDoc classToMock) {
101         if (classToMock.isInterface()) {
102             return MockControl.createNiceControl(classToMock);
103         }
104         return new MockClassControl(classToMock, NICE_BEHAVIOR_FACTORY);
105     }
106
107     /**
108      * Same as {@link #createNiceControl(Class, Method[])}but allows to pass a
109      * list of methods to mock. All the other methods won't be. It means that if
110      * these methods are called, their real code will be executed.
111      *
112      * @param classToMock
113      * the class to mock
114      * @param mockedMethods
115      * Methods to be mocked. If null, all methods will be mocked.
116      * @return the mock control
117      */

118     public static MockClassControl createNiceControl(Class JavaDoc classToMock,
119             Method JavaDoc[] mockedMethods) {
120         return new MockClassControl(classToMock, mockedMethods,
121                 NICE_BEHAVIOR_FACTORY);
122     }
123
124     /**
125      * @deprecated No need to pick a constructor anymore. Constructor arguments
126      * are now ignored. Just use {@link #createControl(Class)}
127      */

128     public static MockClassControl createControl(Class JavaDoc classToMock,
129             Class JavaDoc[] constructorTypes, Object JavaDoc[] constructorArgs) {
130         return new MockClassControl(classToMock, UNORDERED_BEHAVIOR_FACTORY);
131     }
132
133     /**
134      * @deprecated No need to pick a constructor anymore. Constructor arguments
135      * are now ignored. Just use
136      * {@link #createControl(Class, Method[])}
137      */

138     public static MockClassControl createControl(Class JavaDoc classToMock,
139             Class JavaDoc[] constructorTypes, Object JavaDoc[] constructorArgs,
140             Method JavaDoc[] mockedMethods) {
141         return new MockClassControl(classToMock, mockedMethods,
142                 UNORDERED_BEHAVIOR_FACTORY);
143     }
144
145     /**
146      * @deprecated No need to pick a constructor anymore. Constructor arguments
147      * are now ignored. Just use {@link #createStrictControl(Class)}
148      */

149     public static MockClassControl createStrictControl(Class JavaDoc classToMock,
150             Class JavaDoc[] constructorTypes, Object JavaDoc[] constructorArgs) {
151         return new MockClassControl(classToMock, ORDERED_BEHAVIOR_FACTORY);
152     }
153
154     /**
155      * @deprecated No need to pick a constructor anymore. Constructor arguments
156      * are now ignored. Just use
157      * {@link #createStrictControl(Class, Method[])}
158      */

159     public static MockClassControl createStrictControl(Class JavaDoc classToMock,
160             Class JavaDoc[] constructorTypes, Object JavaDoc[] constructorArgs,
161             Method JavaDoc[] mockedMethods) {
162         return new MockClassControl(classToMock, mockedMethods,
163                 ORDERED_BEHAVIOR_FACTORY);
164     }
165
166     /**
167      * @deprecated No need to pick a constructor anymore. Constructor arguments
168      * are now ignored. Just use {@link #createNiceControl(Class)}
169      */

170     public static MockClassControl createNiceControl(Class JavaDoc classToMock,
171             Class JavaDoc[] constructorTypes, Object JavaDoc[] constructorArgs) {
172         return new MockClassControl(classToMock, NICE_BEHAVIOR_FACTORY);
173     }
174
175     /**
176      * @deprecated No need to pick a constructor anymore. Constructor arguments
177      * are now ignored. Just use
178      * {@link #createNiceControl(Class, Method[])}
179      */

180     public static MockClassControl createNiceControl(Class JavaDoc classToMock,
181             Class JavaDoc[] constructorTypes, Object JavaDoc[] constructorArgs,
182             Method JavaDoc[] mockedMethods) {
183         return new MockClassControl(classToMock, mockedMethods,
184                 NICE_BEHAVIOR_FACTORY);
185     }
186
187     private MockClassControl(Class JavaDoc classToMock, Method JavaDoc[] mockedMethods,
188             IBehaviorFactory behaviorFactory) {
189         super(classToMock, new ClassProxyFactory(mockedMethods),
190                 behaviorFactory);
191     }
192
193     private MockClassControl(Class JavaDoc classToMock, IBehaviorFactory behaviorFactory) {
194         this(classToMock, null, behaviorFactory);
195     }
196 }
197
Popular Tags