1 package com.mockobjects.dynamic; 2 3 import java.lang.reflect.InvocationHandler ; 4 import java.lang.reflect.Method ; 5 import java.lang.reflect.Proxy ; 6 7 import junit.framework.AssertionFailedError; 8 9 import com.mockobjects.Verifiable; 10 import com.mockobjects.constraint.Constraint; 11 12 public class Mock implements InvocationHandler , Verifiable { 13 private String name; 14 private Object proxy; 15 private CallFactory callFactory; 16 private CallableAddable callSequence; 17 18 public Mock(CallFactory callFactory, CallableAddable callableAddable, Class mockedClass, String name) { 19 this.name = name; 20 this.callFactory = callFactory; 21 this.callSequence = callableAddable; 22 this.proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class [] { mockedClass }, this); 23 } 24 25 public Mock(Class mockedClass, String nonDefaultName) { 26 this(new DefaultCallFactory(), new CallBag(), mockedClass, nonDefaultName); 27 } 28 29 public Mock(Class mockedClass) { 30 this(mockedClass, mockNameFromClass(mockedClass)); 31 } 32 33 public void reset() { 34 this.callSequence.reset(); 35 } 36 37 public static String mockNameFromClass(Class c) { 38 return "mock" + className(c); 39 } 40 41 public static String className(Class c) { 42 String name = c.getName(); 43 int dotIndex = name.lastIndexOf('.'); 44 45 if (dotIndex >= 0) { 46 return name.substring(dotIndex + 1); 47 } else { 48 return name; 49 } 50 } 51 52 private ConstraintMatcher createConstraintMatcher(Object constraintArg) { 53 56 if (constraintArg instanceof Constraint[]) { 57 return new FullConstraintMatcher((Constraint[])constraintArg); 59 } else if (constraintArg instanceof Constraint) { 60 return C.args((Constraint)constraintArg); 62 } else { 63 return C.args(C.eq(constraintArg)); 65 } 66 } 67 68 public String getMockName() { 69 return this.name; 70 } 71 72 public String toString() { 73 return this.name; 74 } 75 76 public Object proxy() { 77 return this.proxy; 78 } 79 80 public Object invoke(Object proxy, Method method, Object [] args) 81 throws Throwable { 82 try { 83 if (isCheckingEqualityOnProxy(method, args)) { 84 return new Boolean (args[0] == this.proxy); 85 } else if (isMockNameGetter(method, args)) { 86 return this.getMockName(); 87 } else { 88 return callSequence.call(this, method.getName(), (args == null ? new Object [0] : args)); 89 } 90 } catch (AssertionFailedError ex) { 91 throw new AssertionFailedError(name + ": " + ex.getMessage()); 92 } 93 } 94 95 private boolean isCheckingEqualityOnProxy(Method method, Object [] args) { 96 return (method.getName().equals("equals")) && (args.length == 1) && (Proxy.isProxyClass(args[0].getClass())); 97 } 98 99 private boolean isMockNameGetter(Method method, Object [] args) { 100 return (method.getName().equals("getMockName")) && (args.length == 0); 101 } 102 103 public void verify() { 104 try { 105 callSequence.verify(); 106 } catch (AssertionFailedError ex) { 107 throw new AssertionFailedError(name + ": " + ex.getMessage()); 108 } 109 } 110 111 public void expect(String methodName) { 112 expect(methodName, C.NO_ARGS); 113 } 114 115 public void expect(String methodName, Object singleEqualArg) { 116 expect(methodName, createConstraintMatcher(singleEqualArg)); 117 } 118 119 public void expect(String methodName, ConstraintMatcher args) { 120 callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createVoidStub()))); 121 } 122 123 public void expectAndReturn(String methodName, Object result) { 124 this.expectAndReturn(methodName, C.NO_ARGS, result); 125 } 126 127 public void expectAndReturn(String methodName, boolean result) { 128 this.expectAndReturn(methodName, new Boolean (result)); 129 } 130 131 public void expectAndReturn(String methodName, int result) { 132 this.expectAndReturn(methodName, new Integer (result)); 133 } 134 135 public void expectAndReturn(String methodName, Object singleEqualArg, Object result) { 136 this.expectAndReturn(methodName, createConstraintMatcher(singleEqualArg), result); 137 } 138 139 public void expectAndReturn(String methodName, Object singleEqualArg, boolean result) { 140 this.expectAndReturn(methodName, singleEqualArg, new Boolean (result)); 141 } 142 143 public void expectAndReturn(String methodName, Object singleEqualArg, int result) { 144 this.expectAndReturn(methodName, singleEqualArg, new Integer (result)); 145 } 146 147 public void expectAndReturn(String methodName, ConstraintMatcher args, Object result) { 148 callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createReturnStub(result)))); 149 } 150 151 public void expectAndReturn(String methodName, ConstraintMatcher args, boolean result) { 152 this.expectAndReturn(methodName, args, new Boolean (result)); 153 } 154 155 public void expectAndReturn(String methodName, ConstraintMatcher args, int result) { 156 this.expectAndReturn(methodName, args, new Integer (result)); 157 } 158 159 public void expectAndThrow(String methodName, Throwable exception) { 160 this.expectAndThrow(methodName, C.NO_ARGS, exception); 161 } 162 163 public void expectAndThrow(String methodName, Object singleEqualArg, Throwable exception) { 164 this.expectAndThrow(methodName, createConstraintMatcher(singleEqualArg), exception); 165 } 166 167 public void expectAndThrow(String methodName, ConstraintMatcher args, Throwable exception) { 168 callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, callFactory.createThrowStub(exception)))); 169 } 170 171 public void matchAndReturn(String methodName, Object result) { 172 this.matchAndReturn(methodName, C.NO_ARGS, result); 173 } 174 175 public void matchAndReturn(String methodName, boolean result) { 176 this.matchAndReturn(methodName, new Boolean (result)); 177 } 178 179 public void matchAndReturn(String methodName, int result) { 180 this.matchAndReturn(methodName, new Integer (result)); 181 } 182 183 public void matchAndReturn(String methodName, Object singleEqualArg, Object result) { 184 this.matchAndReturn(methodName, createConstraintMatcher(singleEqualArg), result); 185 } 186 187 public void matchAndReturn(String methodName, boolean singleEqualArg, Object result) { 188 this.matchAndReturn(methodName, new Boolean (singleEqualArg), result); 189 } 190 191 public void matchAndReturn(String methodName, int singleEqualArg, Object result) { 192 this.matchAndReturn(methodName, new Integer (singleEqualArg), result); 193 } 194 195 public void matchAndReturn(String methodName, Object singleEqualArg, boolean result) { 196 this.matchAndReturn(methodName, singleEqualArg, new Boolean (result)); 197 } 198 199 public void matchAndReturn(String methodName, Object singleEqualArg, int result) { 200 this.matchAndReturn(methodName, singleEqualArg, new Integer (result)); 201 } 202 203 public void matchAndReturn(String methodName, ConstraintMatcher args, Object result) { 204 callSequence.addMatch(callFactory.createCallSignature(methodName, args, callFactory.createReturnStub(result))); 205 } 206 207 public void matchAndReturn(String methodName, ConstraintMatcher args, boolean result) { 208 this.matchAndReturn(methodName, args, new Boolean (result)); 209 } 210 211 public void matchAndReturn(String methodName, ConstraintMatcher args, int result) { 212 this.matchAndReturn(methodName, args, new Integer (result)); 213 } 214 215 public void matchAndThrow(String methodName, Throwable throwable) { 216 this.matchAndThrow(methodName, C.NO_ARGS, throwable); 217 } 218 219 public void matchAndThrow(String methodName, Object singleEqualArg, Throwable throwable) { 220 this.matchAndThrow(methodName, createConstraintMatcher(singleEqualArg), throwable); 221 } 222 223 public void matchAndThrow(String methodName, boolean singleEqualArg, Throwable throwable) { 224 this.matchAndThrow(methodName,new Boolean (singleEqualArg), throwable); 225 } 226 227 public void matchAndThrow(String methodName, int singleEqualArg, Throwable throwable) { 228 this.matchAndThrow(methodName,new Integer (singleEqualArg), throwable); 229 } 230 231 public void matchAndThrow(String methodName, ConstraintMatcher args, Throwable throwable) { 232 callSequence.addMatch(callFactory.createCallSignature(methodName, args, callFactory.createThrowStub(throwable))); 233 } 234 235 237 public void expect(String methodName, CallSequence deprecated) { 238 throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); 239 } 240 241 243 public void expectAndReturn(String methodName, CallSequence deprecated, Object result) { 244 throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); 245 } 246 247 249 public void expectAndThrow(String methodName, CallSequence deprecated, Throwable throwable) { 250 throw new AssertionFailedError("method is deprecated! Use: new OrderedMock() instead..."); 251 } 252 253 255 public void expectVoid(String methodName, ConstraintMatcher args) { 256 this.expect(methodName, args); 257 } 258 259 261 public void expectVoid(String methodName, Object equalArg) { 262 this.expect(methodName,equalArg); 263 } 264 265 267 public void expectVoid(String methodName) { 268 this.expect(methodName); 269 } 270 271 273 public void expectNotCalled(String methodName) { 274 } 275 } 276 | Popular Tags |