1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.ibeans.config; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.context.MuleContextAware; |
14 | |
import org.mule.api.registry.InjectProcessor; |
15 | |
import org.mule.module.ibeans.spi.MuleIBeansPlugin; |
16 | |
import org.mule.util.annotation.AnnotationMetaData; |
17 | |
import org.mule.util.annotation.AnnotationUtils; |
18 | |
|
19 | |
import java.lang.reflect.Field; |
20 | |
import java.lang.reflect.InvocationHandler; |
21 | |
import java.lang.reflect.Proxy; |
22 | |
import java.util.List; |
23 | |
|
24 | |
import org.ibeans.annotation.MockIntegrationBean; |
25 | |
import org.ibeans.impl.test.MockIBean; |
26 | |
import org.ibeans.impl.test.MockIntegrationBeanInvocationHandler; |
27 | |
import org.mockito.Mockito; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class MockIntegrationBeansAnnotationProcessor implements InjectProcessor, MuleContextAware |
34 | |
{ |
35 | |
public static final String NAME = "_mockIntegrationBeanProcessor"; |
36 | |
|
37 | |
private MuleIBeansPlugin plugin; |
38 | |
|
39 | |
public MockIntegrationBeansAnnotationProcessor() |
40 | 0 | { |
41 | 0 | } |
42 | |
|
43 | |
public void setMuleContext(MuleContext muleContext) |
44 | |
{ |
45 | 0 | this.plugin = new MuleIBeansPlugin(muleContext); |
46 | 0 | } |
47 | |
|
48 | |
public Object process(Object object) |
49 | |
{ |
50 | 0 | List<AnnotationMetaData> annos = AnnotationUtils.getFieldAnnotations(object.getClass(), MockIntegrationBean.class); |
51 | |
|
52 | 0 | if (annos.size() > 0) |
53 | |
{ |
54 | 0 | for (AnnotationMetaData data : annos) |
55 | |
{ |
56 | 0 | Field field = (Field) data.getMember(); |
57 | 0 | field.setAccessible(true); |
58 | 0 | Object mockito = Mockito.mock(field.getType(), field.getName()); |
59 | |
try |
60 | |
{ |
61 | |
|
62 | 0 | InvocationHandler handler = new MockIntegrationBeanInvocationHandler(field.getType(), plugin, mockito); |
63 | |
|
64 | 0 | Object mock = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{field.getType(), MockIBean.class}, handler); |
65 | |
|
66 | 0 | field.set(object, mock); |
67 | |
} |
68 | 0 | catch (Exception e) |
69 | |
{ |
70 | 0 | throw new RuntimeException(e); |
71 | 0 | } |
72 | 0 | } |
73 | |
} |
74 | 0 | return object; |
75 | |
} |
76 | |
} |