Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
../../../img/srcFileCovDistChart10.png 0% of files have more coverage
68   219   35   3,09
20   147   0,51   22
22     1,59  
1    
 
  MockBuilder       Line # 37 68 0% 35 0 100% 1.0
 
  (72)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
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   
17    package org.easymock.internal;
18   
19    import java.lang.reflect.Constructor;
20    import java.lang.reflect.Method;
21    import java.util.HashSet;
22    import java.util.Set;
23   
24    import org.easymock.*;
25   
26    /**
27    * Default implementation of IMockBuilder.
28    * <p>
29    * <i>The original idea and part of the code where contributed by Rodrigo
30    * Damazio and Bruno Fonseca at <a href="http://www.google.com">Google</a></i>
31    *
32    * @param <T>
33    * type of the mock created
34    *
35    * @author Henri Tremblay
36    */
 
37    public class MockBuilder<T> implements IMockBuilder<T> {
38   
39    private final Class<T> toMock;
40   
41    private final Set<Method> mockedMethods = new HashSet<Method>();
42   
43    private Constructor<T> constructor;
44   
45    private ConstructorArgs constructorArgs;
46   
47    private final EasyMockSupport support;
48   
 
49  72 toggle public MockBuilder(final Class<T> toMock) {
50  72 this(toMock, null);
51    }
52   
53    /**
54    * Used by EasyMockSupport to allow the mock registration in the list of
55    * controls
56    *
57    * @param toMock
58    * The class of the mock to create
59    * @param support
60    * The EasyMockSupport used to create mocks. Null if none
61    */
 
62  88 toggle public MockBuilder(final Class<T> toMock, final EasyMockSupport support) {
63  88 this.toMock = toMock;
64  88 this.support = support;
65    }
66   
 
67  22 toggle public IMockBuilder<T> addMockedMethod(final Method method) {
68  22 mockedMethods.add(method);
69  22 return this;
70    }
71   
 
72  46 toggle public IMockBuilder<T> addMockedMethod(final String methodName) {
73  46 final Method m = ReflectionUtils.findMethod(toMock, methodName);
74  46 if (m == null) {
75  2 throw new IllegalArgumentException("Method not found (or private): " + methodName);
76    }
77  44 mockedMethods.add(m);
78  44 return this;
79    }
80   
 
81  8 toggle public IMockBuilder<T> addMockedMethod(final String methodName, final Class<?>... parameterTypes) {
82  8 final Method m = ReflectionUtils.findMethod(toMock, methodName, parameterTypes);
83  8 if (m == null) {
84  2 throw new IllegalArgumentException("Method not found (or private): " + methodName);
85    }
86  6 mockedMethods.add(m);
87  6 return this;
88    }
89   
 
90  4 toggle public IMockBuilder<T> addMockedMethods(final String... methodNames) {
91  4 for (final String methodName : methodNames) {
92  8 addMockedMethod(methodName);
93    }
94  4 return this;
95    }
96   
 
97  2 toggle public IMockBuilder<T> addMockedMethods(final Method... methods) {
98  2 for (final Method method : methods) {
99  4 addMockedMethod(method);
100    }
101  2 return this;
102    }
103   
 
104  2 toggle @SuppressWarnings("unchecked")
105    public IMockBuilder<T> withConstructor(final Constructor<?> constructor) {
106  2 checkConstructorNotInitialized();
107  2 this.constructor = (Constructor<T>) constructor;
108  2 return this;
109    }
110   
 
111  2 toggle @SuppressWarnings("unchecked")
112    public IMockBuilder<T> withConstructor(final ConstructorArgs constructorArgs) {
113  2 checkConstructorNotInitialized();
114  2 this.constructor = (Constructor<T>) constructorArgs.getConstructor();
115  2 this.constructorArgs = constructorArgs;
116  2 return this;
117    }
118   
 
119  6 toggle public IMockBuilder<T> withConstructor() {
120  6 checkConstructorNotInitialized();
121  6 try {
122  6 constructor = ReflectionUtils.getConstructor(toMock);
123    } catch (final NoSuchMethodException e) {
124  2 throw new IllegalArgumentException("No empty constructor can be found", e);
125    }
126  4 constructorArgs = new ConstructorArgs(constructor);
127  4 return this;
128    }
129   
 
130  10 toggle public IMockBuilder<T> withConstructor(final Object... initArgs) {
131  10 checkConstructorNotInitialized();
132  10 try {
133  10 constructor = ReflectionUtils.getConstructor(toMock, initArgs);
134    } catch (final NoSuchMethodException e) {
135  2 throw new IllegalArgumentException("No constructor matching arguments can be found", e);
136    }
137  8 constructorArgs = new ConstructorArgs(constructor, initArgs);
138  8 return this;
139    }
140   
 
141  18 toggle public IMockBuilder<T> withConstructor(final Class<?>... argTypes) {
142  18 checkConstructorNotInitialized();
143   
144  16 try {
145  16 constructor = toMock.getDeclaredConstructor(argTypes);
146    } catch (final NoSuchMethodException e) {
147  2 throw new IllegalArgumentException("No constructor matching arguments can be found", e);
148    }
149  14 return this;
150    }
151   
 
152  16 toggle public IMockBuilder<T> withArgs(final Object... initArgs) {
153  16 if (constructor == null) {
154  2 throw new IllegalStateException(
155    "Trying to define constructor arguments without first setting their type.");
156    }
157  14 if (constructorArgs != null) {
158  2 throw new IllegalStateException("Trying to define the constructor arguments more than once.");
159    }
160   
161  12 constructorArgs = new ConstructorArgs(constructor, initArgs);
162  12 return this;
163    }
164   
 
165  4 toggle public T createMock(final IMocksControl control) {
166  4 return createMock(null, control);
167    }
168   
 
169  38 toggle public T createMock() {
170  38 return createMock((String) null);
171    }
172   
 
173  8 toggle public T createNiceMock() {
174  8 return createNiceMock(null);
175    }
176   
 
177  6 toggle public T createStrictMock() {
178  6 return createStrictMock(null);
179    }
180   
 
181  70 toggle @SuppressWarnings("deprecation")
182    public T createMock(final String name, final IMocksControl control) {
183    // Create a mock with no default {@code withConstructor} was not called.
184  70 if (constructor == null) {
185  44 return control.createMock(name, toMock, mockedMethods.toArray(new Method[mockedMethods.size()]));
186    }
187   
188    // If the constructor is defined, so must be its arguments
189  26 if (constructorArgs == null) {
190  2 throw new IllegalStateException("Picked a constructor but didn't pass arguments to it");
191    }
192   
193  24 return control.createMock(name, toMock, constructorArgs, mockedMethods
194    .toArray(new Method[mockedMethods.size()]));
195    }
196   
 
197  42 toggle public T createMock(final String name) {
198  42 final IMocksControl control = (support == null ? EasyMock.createControl() : support.createControl());
199  42 return createMock(name, control);
200    }
201   
 
202  12 toggle public T createNiceMock(final String name) {
203  12 final IMocksControl control = (support == null ? EasyMock.createNiceControl() : support
204    .createNiceControl());
205  12 return createMock(name, control);
206    }
207   
 
208  10 toggle public T createStrictMock(final String name) {
209  10 final IMocksControl control = (support == null ? EasyMock.createStrictControl() : support
210    .createStrictControl());
211  10 return createMock(name, control);
212    }
213   
 
214  38 toggle private void checkConstructorNotInitialized() {
215  38 if (constructor != null) {
216  2 throw new IllegalStateException("Trying to define the constructor call more than once.");
217    }
218    }
219    }