|
1 |
| package com.agical.rmock.core.expectation; |
|
2 |
| |
|
3 |
| import com.agical.rmock.core.Expectation; |
|
4 |
| import com.agical.rmock.core.InvocationHandler; |
|
5 |
| import com.agical.rmock.core.MethodHandle; |
|
6 |
| import com.agical.rmock.core.ProxyFactory; |
|
7 |
| import com.agical.rmock.core.SectionProvider; |
|
8 |
| import com.agical.rmock.core.action.ReturnAction; |
|
9 |
| import com.agical.rmock.core.match.Expression; |
|
10 |
| import com.agical.rmock.core.match.constraint.EqualsConstraint; |
|
11 |
| import com.agical.rmock.core.match.multiplicity.MultiplicityFactory; |
|
12 |
| import com.agical.rmock.core.util.InterfaceMethodHandle; |
|
13 |
| import com.agical.rmock.core.util.NamingUtils; |
|
14 |
| |
|
15 |
| |
|
16 |
| class InvocationRecorder implements InvocationHandler { |
|
17 |
| private Expectation last; |
|
18 |
| private ProxyFactory proxyFactory; |
|
19 |
| private final MultiplicityFactory M = new MultiplicityFactory(); |
|
20 |
| private SectionProvider sectionProvider; |
|
21 |
| private NamingUtils namingUtils = new NamingUtils(); |
|
22 |
| |
|
23 |
1708
| public void setSectionProvider(SectionProvider sectionProvider) {
|
|
24 |
1708
| this.sectionProvider = sectionProvider;
|
|
25 |
| } |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
1659
| public void setProxyFactory(ProxyFactory proxyFactory) {
|
|
31 |
1659
| this.proxyFactory = proxyFactory;
|
|
32 |
| } |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
751
| public Object invocation(String id, Class objectType, String method, Object[] arguments, MethodHandle methodHandle) throws Exception {
|
|
39 |
751
| ExpectationImpl expectation = new ExpectationImpl();
|
|
40 |
751
| expectation.setMultiplicity(M.once());
|
|
41 |
751
| expectation.setId(id);
|
|
42 |
751
| expectation.setMethod(method);
|
|
43 |
| |
|
44 |
751
| Expression[] argumentExpressions = new Expression[arguments.length];
|
|
45 |
751
| for (int i = 0; i < arguments.length; i++) {
|
|
46 |
532
| Object argument = arguments[i];
|
|
47 |
532
| argumentExpressions[i] = new EqualsConstraint(argument);
|
|
48 |
| } |
|
49 |
751
| expectation.setArguments(argumentExpressions);
|
|
50 |
| |
|
51 |
751
| sectionProvider.getCurrentSection().add(expectation);
|
|
52 |
751
| last = expectation;
|
|
53 |
| |
|
54 |
751
| Class returnType = methodHandle.getReturnType();
|
|
55 |
751
| Object returnValue = null;
|
|
56 |
751
| if( returnType.isInterface() ) {
|
|
57 |
99
| Object object = createAutoMockReturnAction(id, method, expectation, returnType);
|
|
58 |
99
| return object;
|
|
59 |
| } else { |
|
60 |
| |
|
61 |
652
| InterfaceMethodHandle interfaceMethodHandle = new InterfaceMethodHandle(id, method, null, returnType, proxyFactory);
|
|
62 |
652
| returnValue = interfaceMethodHandle.invoke(arguments);
|
|
63 |
652
| expectation.setAction(new ReturnAction(returnValue));
|
|
64 |
| } |
|
65 |
| |
|
66 |
652
| return returnValue;
|
|
67 |
| } |
|
68 |
| |
|
69 |
99
| private Object createAutoMockReturnAction(String id, String method, ExpectationImpl expectation, Class returnType) {
|
|
70 |
99
| Object object = proxyFactory.createInterfaceProxy( returnType, namingUtils.createNameFromMethod(method, returnType)+"$");
|
|
71 |
99
| expectation.setAction(new ReturnAction(object));
|
|
72 |
99
| return object;
|
|
73 |
| } |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
513
| public Expectation getLast() {
|
|
79 |
513
| return last;
|
|
80 |
| } |
|
81 |
| } |