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 924 public void setReturnValue(boolean value, int minCount, int maxCount) { 925 try { 926 state.setReturnValue(value, new Range(minCount, maxCount)); 927 } catch (RuntimeExceptionWrapper e) { 928 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 929 } 930 } 931 932 949 public void setReturnValue(long value, int minCount, int maxCount) { 950 try { 951 state.setReturnValue(value, new Range(minCount, maxCount)); 952 } catch (RuntimeExceptionWrapper e) { 953 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 954 } 955 } 956 957 973 public void setReturnValue(float value, int minCount, int maxCount) { 974 try { 975 state.setReturnValue(value, new Range(minCount, maxCount)); 976 } catch (RuntimeExceptionWrapper e) { 977 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 978 } 979 } 980 981 997 public void setReturnValue(double value, int minCount, int maxCount) { 998 try { 999 state.setReturnValue(value, new Range(minCount, maxCount)); 1000 } catch (RuntimeExceptionWrapper e) { 1001 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 1002 } 1003 } 1004 1005 1024 public void setReturnValue(Object value, int minCount, int maxCount) { 1025 try { 1026 state.setReturnValue(value, new Range(minCount, maxCount)); 1027 } catch (RuntimeExceptionWrapper e) { 1028 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 1029 } 1030 } 1031 1032 1035 public static final Range ONE = new Range(1); 1036 1037 1040 public static final Range ONE_OR_MORE = new Range(1, Integer.MAX_VALUE); 1041 1042 1045 public static final Range ZERO_OR_MORE = new Range(0, Integer.MAX_VALUE); 1046 1047 1051 public static final ArgumentsMatcher EQUALS_MATCHER = new EqualsMatcher(); 1052 1053 1056 public static final ArgumentsMatcher ALWAYS_MATCHER = new AlwaysMatcher(); 1057 1058 1063 public static final ArgumentsMatcher ARRAY_MATCHER = new ArrayMatcher(); 1064 1065 1073 public void setDefaultMatcher(ArgumentsMatcher matcher) { 1074 try { 1075 state.setDefaultMatcher(matcher); 1076 } catch (RuntimeExceptionWrapper e) { 1077 throw (RuntimeException ) e.getRuntimeException().fillInStackTrace(); 1078 } 1079 } 1080 1081 1089 public void expectAndReturn(boolean ignored, boolean value) { 1090 setReturnValue(value); 1091 } 1092 1093 1100 public void expectAndReturn(long ignored, long value) { 1101 setReturnValue(value); 1102 } 1103 1104 1111 public void expectAndReturn(float ignored, float value) { 1112 setReturnValue(value); 1113 } 1114 1115 1122 public void expectAndReturn(double ignored, double value) { 1123 setReturnValue(value); 1124 } 1125 1126 1133 public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value) { 1134 setReturnValue(value); 1135 } 1136 1137 1145 public void expectAndReturn(boolean ignored, boolean value, Range range) { 1146 setReturnValue(value, range); 1147 } 1148 1149 1157 public void expectAndReturn(long ignored, long value, Range range) { 1158 setReturnValue(value, range); 1159 } 1160 1161 1169 public void expectAndReturn(float ignored, float value, Range range) { 1170 setReturnValue(value, range); 1171 } 1172 1173 1181 public void expectAndReturn(double ignored, double value, Range range) { 1182 setReturnValue(value, range); 1183 } 1184 1185 1193 public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value, 1194 Range range) { 1195 setReturnValue(value, range); 1196 } 1197 1198 1206 public void expectAndReturn(boolean ignored, boolean value, int count) { 1207 setReturnValue(value, count); 1208 } 1209 1210 1218 public void expectAndReturn(long ignored, long value, int count) { 1219 setReturnValue(value, count); 1220 } 1221 1222 1230 public void expectAndReturn(float ignored, float value, int count) { 1231 setReturnValue(value, count); 1232 } 1233 1234 1242 public void expectAndReturn(double ignored, double value, int count) { 1243 setReturnValue(value, count); 1244 } 1245 1246 1254 public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value, 1255 int count) { 1256 setReturnValue(value, count); 1257 } 1258 1259 1267 public void expectAndReturn(boolean ignored, boolean value, int min, int max) { 1268 setReturnValue(value, min, max); 1269 } 1270 1271 1279 public void expectAndReturn(long ignored, long value, int min, int max) { 1280 setReturnValue(value, min, max); 1281 } 1282 1283 1291 public void expectAndReturn(float ignored, float value, int min, int max) { 1292 setReturnValue(value, min, max); 1293 } 1294 1295 1303 public void expectAndReturn(double ignored, double value, int min, int max) { 1304 setReturnValue(value, min, max); 1305 } 1306 1307 1315 public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value, 1316 int min, int max) { 1317 setReturnValue(value, min, max); 1318 } 1319 1320 1327 public void expectAndThrow(boolean ignored, Throwable throwable) { 1328 setThrowable(throwable); 1329 } 1330 1331 1338 public void expectAndThrow(long ignored, Throwable throwable) { 1339 setThrowable(throwable); 1340 } 1341 1342 1349 public void expectAndThrow(float ignored, Throwable throwable) { 1350 setThrowable(throwable); 1351 } 1352 1353 1360 public void expectAndThrow(double ignored, Throwable throwable) { 1361 setThrowable(throwable); 1362 } 1363 1364 1371 public void expectAndThrow(Object ignored, Throwable throwable) { 1372 setThrowable(throwable); 1373 } 1374 1375 1383 public void expectAndThrow(boolean ignored, Throwable throwable, Range range) { 1384 setThrowable(throwable, range); 1385 } 1386 1387 1395 public void expectAndThrow(long ignored, Throwable throwable, Range range) { 1396 setThrowable(throwable, range); 1397 } 1398 1399 1407 public void expectAndThrow(float ignored, Throwable throwable, Range range) { 1408 setThrowable(throwable, range); 1409 } 1410 1411 1419 public void expectAndThrow(double ignored, Throwable throwable, Range range) { 1420 setThrowable(throwable, range); 1421 } 1422 1423 1431 public void expectAndThrow(Object ignored, Throwable throwable, Range range) { 1432 setThrowable(throwable, range); 1433 } 1434 1435 1443 public void expectAndThrow(boolean ignored, Throwable throwable, int count) { 1444 setThrowable(throwable, count); 1445 } 1446 1447 1455 public void expectAndThrow(long ignored, Throwable throwable, int count) { 1456 setThrowable(throwable, count); 1457 } 1458 1459 1467 public void expectAndThrow(float ignored, Throwable throwable, int count) { 1468 setThrowable(throwable, count); 1469 } 1470 1471 1479 public void expectAndThrow(double ignored, Throwable throwable, int count) { 1480 setThrowable(throwable, count); 1481 } 1482 1483 1491 public void expectAndThrow(Object ignored, Throwable throwable, int count) { 1492 setThrowable(throwable, count); 1493 } 1494 1495 1503 public void expectAndThrow(boolean ignored, Throwable throwable, int min, 1504 int max) { 1505 setThrowable(throwable, min, max); 1506 } 1507 1508 1516 public void expectAndThrow(long ignored, Throwable throwable, int min, 1517 int max) { 1518 setThrowable(throwable, min, max); 1519 } 1520 1521 1529 public void expectAndThrow(float ignored, Throwable throwable, int min, 1530 int max) { 1531 setThrowable(throwable, min, max); 1532 } 1533 1534 1542 public void expectAndThrow(double ignored, Throwable throwable, int min, 1543 int max) { 1544 setThrowable(throwable, min, max); 1545 } 1546 1547 1555 public void expectAndThrow(Object ignored, Throwable throwable, int min, 1556 int max) { 1557 setThrowable(throwable, min, max); 1558 } 1559 1560 1568 public void expectAndDefaultReturn(boolean ignored, boolean value) { 1569 setDefaultReturnValue(value); 1570 } 1571 1572 1580 public void expectAndDefaultReturn(long ignored, long value) { 1581 setDefaultReturnValue(value); 1582 } 1583 1584 1592 public void expectAndDefaultReturn(float ignored, float value) { 1593 setDefaultReturnValue(value); 1594 } 1595 1596 1604 public void expectAndDefaultReturn(double ignored, double value) { 1605 setDefaultReturnValue(value); 1606 } 1607 1608 1616 public <V1, V2 extends V1> void expectAndDefaultReturn(V1 ignored, V2 value) { 1617 setDefaultReturnValue(value); 1618 } 1619 1620 1628 public void expectAndDefaultThrow(boolean ignored, Throwable throwable) { 1629 setDefaultThrowable(throwable); 1630 } 1631 1632 1640 public void expectAndDefaultThrow(long ignored, Throwable throwable) { 1641 setDefaultThrowable(throwable); 1642 } 1643 1644 1652 public void expectAndDefaultThrow(float ignored, Throwable throwable) { 1653 setDefaultThrowable(throwable); 1654 } 1655 1656 1664 public void expectAndDefaultThrow(double ignored, Throwable throwable) { 1665 setDefaultThrowable(throwable); 1666 } 1667 1668 1676 public void expectAndDefaultThrow(Object ignored, Throwable throwable) { 1677 setDefaultThrowable(throwable); 1678 } 1679} | Popular Tags |