| 1 5 package org.easymock; 6 7 import java.lang.reflect.InvocationHandler ; 8 import java.lang.reflect.Method ; 9 10 import junit.framework.AssertionFailedError; 11 12 import org.easymock.internal.AlwaysMatcher; 13 import org.easymock.internal.ArrayMatcher; 14 import org.easymock.internal.AssertionFailedErrorWrapper; 15 import org.easymock.internal.EqualsMatcher; 16 import org.easymock.internal.IBehavior; 17 import org.easymock.internal.IBehaviorFactory; 18 import org.easymock.internal.IMockControlState; 19 import org.easymock.internal.IProxyFactory; 20 import org.easymock.internal.JavaProxyFactory; 21 import org.easymock.internal.NiceBehavior; 22 import org.easymock.internal.ObjectMethodsFilter; 23 import org.easymock.internal.OrderedBehavior; 24 import org.easymock.internal.Range; 25 import org.easymock.internal.RecordState; 26 import org.easymock.internal.ReplayState; 27 import org.easymock.internal.RuntimeExceptionWrapper; 28 import org.easymock.internal.ThrowableWrapper; 29 import org.easymock.internal.UnorderedBehavior; 30 31 35 public class MockControl<T> { 36 private IMockControlState state; 37 38 private T mock; 39 40 private IBehavior behavior; 41 42 private IBehaviorFactory behaviorFactory; 43 44 48 protected static final IBehaviorFactory NICE_BEHAVIOR_FACTORY = new IBehaviorFactory() { 49 public IBehavior createBehavior() { 50 return new NiceBehavior(); 51 } 52 }; 53 54 58 protected static final IBehaviorFactory ORDERED_BEHAVIOR_FACTORY = new IBehaviorFactory() { 59 public IBehavior createBehavior() { 60 return new OrderedBehavior(); 61 } 62 }; 63 64 68 protected static final IBehaviorFactory UNORDERED_BEHAVIOR_FACTORY = new IBehaviorFactory() { 69 public IBehavior createBehavior() { 70 return new UnorderedBehavior(); 71 } 72 }; 73 74 84 public static <T> MockControl<T> createControl(Class <T> toMock) { 85 return new MockControl<T>(toMock, new JavaProxyFactory<T>(), 86 UNORDERED_BEHAVIOR_FACTORY); 87 } 88 89 99 public static <T> MockControl<T> createStrictControl(Class <T> toMock) { 100 return new MockControl<T>(toMock, new JavaProxyFactory<T>(), 101 ORDERED_BEHAVIOR_FACTORY); 102 } 103 104 114 public static <T> MockControl<T> createNiceControl(Class <T> toMock) { 115 return new MockControl<T>(toMock, new JavaProxyFactory<T>(), 116 NICE_BEHAVIOR_FACTORY); 117 } 118 119 131 protected MockControl(Class <T> toMock, IProxyFactory<T> proxyFactory, 132 IBehaviorFactory behaviorFactory) { 133 mock = proxyFactory.createProxy(toMock, new ObjectMethodsFilter( 134 createDelegator())); 135 this.behaviorFactory = behaviorFactory; 136 reset(); 137 } 138 139 private InvocationHandler createDelegator() { 140 return new InvocationHandler () { 141 public Object invoke(Object proxy, Method method, Object [] args) 142 throws Throwable { 143 try { 144 return state.invoke(proxy, method, args); 145 } catch (RuntimeExceptionWrapper e) { 146 throw e.getRuntimeException().fillInStackTrace(); 147 } catch (AssertionFailedErrorWrapper e) { 148 throw e.getAssertionFailedError().fillInStackTrace(); 149 } catch (ThrowableWrapper t) { 150 throw t.getThrowable().fillInStackTrace(); 151 } 152 } 153 }; 154 } 155 156 161 public T getMock() { 162 return mock; 163 } 164 165 169 public final void reset() { 170 behavior = behaviorFactory.createBehavior(); 171 state = new RecordState(behavior); 172 } 173 174 181 public void replay() { 182 try { 183 state.replay(); 184 state = new ReplayState(behavior); 185 } catch (RuntimeExceptionWrapper e) { 186 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 187 } 188 } 189 190 199 public void verify() { 200 try { 201 state.verify(); 202 } catch (RuntimeExceptionWrapper e) { 203 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 204 } catch (AssertionFailedErrorWrapper e) { 205 throw (AssertionFailedError) e.getAssertionFailedError() 206 .fillInStackTrace(); 207 } 208 } 209 210 219 public void setVoidCallable() { 220 try { 221 state.setVoidCallable(MockControl.ONE); 222 } catch (RuntimeExceptionWrapper e) { 223 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 224 } 225 } 226 227 242 public void setThrowable(Throwable throwable) { 243 try { 244 state.setThrowable(throwable, MockControl.ONE); 245 } catch (RuntimeExceptionWrapper e) { 246 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 247 } 248 } 249 250 261 public void setReturnValue(boolean value) { 262 try { 263 state.setReturnValue(value, MockControl.ONE); 264 } catch (RuntimeExceptionWrapper e) { 265 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 266 } 267 } 268 269 281 public void setReturnValue(long value) { 282 try { 283 state.setReturnValue(value, MockControl.ONE); 284 } catch (RuntimeExceptionWrapper e) { 285 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 286 } 287 } 288 289 300 public void setReturnValue(float value) { 301 try { 302 state.setReturnValue(value, MockControl.ONE); 303 } catch (RuntimeExceptionWrapper e) { 304 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 305 } 306 } 307 308 319 public void setReturnValue(double value) { 320 try { 321 state.setReturnValue(value, MockControl.ONE); 322 } catch (RuntimeExceptionWrapper e) { 323 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 324 } 325 } 326 327 341 public void setReturnValue(Object value) { 342 try { 343 state.setReturnValue(value, MockControl.ONE); 344 } catch (RuntimeExceptionWrapper e) { 345 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 346 } 347 } 348 349 360 public void setVoidCallable(int times) { 361 try { 362 state.setVoidCallable(new Range(times)); 363 } catch (RuntimeExceptionWrapper e) { 364 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 365 } 366 } 367 368 385 public void setThrowable(Throwable throwable, int times) { 386 try { 387 state.setThrowable(throwable, new Range(times)); 388 } catch (RuntimeExceptionWrapper e) { 389 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 390 } 391 } 392 393 406 public void setReturnValue(boolean value, int times) { 407 try { 408 state.setReturnValue(value, new Range(times)); 409 } catch (RuntimeExceptionWrapper e) { 410 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 411 } 412 } 413 414 427 public void setReturnValue(double value, int times) { 428 try { 429 state.setReturnValue(value, new Range(times)); 430 } catch (RuntimeExceptionWrapper e) { 431 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 432 } 433 } 434 435 448 public void setReturnValue(float value, int times) { 449 try { 450 state.setReturnValue(value, new Range(times)); 451 } catch (RuntimeExceptionWrapper e) { 452 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 453 } 454 } 455 456 470 public void setReturnValue(long value, int times) { 471 try { 472 state.setReturnValue(value, new Range(times)); 473 } catch (RuntimeExceptionWrapper e) { 474 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 475 } 476 } 477 478 494 public void setReturnValue(Object value, int times) { 495 try { 496 state.setReturnValue(value, new Range(times)); 497 } catch (RuntimeExceptionWrapper e) { 498 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 499 } 500 } 501 502 519 public void setVoidCallable(Range range) { 520 try { 521 state.setVoidCallable(range); 522 } catch (RuntimeExceptionWrapper e) { 523 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 524 } 525 } 526 527 550 public void setThrowable(Throwable throwable, Range range) { 551 try { 552 state.setThrowable(throwable, range); 553 } catch (RuntimeExceptionWrapper e) { 554 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 555 } 556 } 557 558 577 public void setReturnValue(boolean value, Range range) { 578 try { 579 state.setReturnValue(value, range); 580 } catch (RuntimeExceptionWrapper e) { 581 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 582 } 583 } 584 585 604 public void setReturnValue(double value, Range range) { 605 try { 606 state.setReturnValue(value, range); 607 } catch (RuntimeExceptionWrapper e) { 608 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 609 } 610 } 611 612 631 public void setReturnValue(float value, Range range) { 632 try { 633 state.setReturnValue(value, range); 634 } catch (RuntimeExceptionWrapper e) { 635 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 636 } 637 } 638 639 659 public void setReturnValue(long value, Range range) { 660 try { 661 state.setReturnValue(value, range); 662 } catch (RuntimeExceptionWrapper e) { 663 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 664 } 665 } 666 667 689 public void setReturnValue(Object value, Range range) { 690 try { 691 state.setReturnValue(value, range); 692 } catch (RuntimeExceptionWrapper e) { 693 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 694 } 695 } 696 697 706 public void setDefaultVoidCallable() { 707 try { 708 state.setDefaultVoidCallable(); 709 } catch (RuntimeExceptionWrapper e) { 710 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 711 } 712 } 713 714 730 public void setDefaultThrowable(Throwable throwable) { 731 try { 732 state.setDefaultThrowable(throwable); 733 } catch (RuntimeExceptionWrapper e) { 734 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 735 } 736 } 737 738 750 public void setDefaultReturnValue(boolean value) { 751 try { 752 state.setDefaultReturnValue(value); 753 } catch (RuntimeExceptionWrapper e) { 754 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 755 } 756 } 757 758 771 public void setDefaultReturnValue(long value) { 772 try { 773 state.setDefaultReturnValue(value); 774 } catch (RuntimeExceptionWrapper e) { 775 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 776 } 777 } 778 779 791 public void setDefaultReturnValue(float value) { 792 try { 793 state.setDefaultReturnValue(value); 794 } catch (RuntimeExceptionWrapper e) { 795 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 796 } 797 } 798 799 811 public void setDefaultReturnValue(double value) { 812 try { 813 state.setDefaultReturnValue(value); 814 } catch (RuntimeExceptionWrapper e) { 815 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 816 } 817 } 818 819 834 public void setDefaultReturnValue(Object value) { 835 try { 836 state.setDefaultReturnValue(value); 837 } catch (RuntimeExceptionWrapper e) { 838 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 839 } 840 } 841 842 850 public void setMatcher(ArgumentsMatcher matcher) { 851 try { 852 state.setMatcher(matcher); 853 } catch (RuntimeExceptionWrapper e) { 854 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 855 } 856 } 857 858 872 public void setVoidCallable(int minCount, int maxCount) { 873 try { 874 state.setVoidCallable(new Range(minCount, maxCount)); 875 } catch (RuntimeExceptionWrapper e) { 876 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 877 } 878 } 879 880 900 public void setThrowable(Throwable throwable, int minCount, int maxCount) { 901 try { 902 state.setThrowable(throwable, new Range(minCount, maxCount)); 903 } catch (RuntimeExceptionWrapper e) { 904 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 905 } 906 } 907 908 |