View Javadoc

1   /*
2    * $Id: MockIntegrationBeansAnnotationProcessor.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * Will process any fields on an object with the {@link org.ibeans.annotation.MockIntegrationBean} annotation, inserting
31   * a Mockito Mock object.  This is only used for testing.
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      {
41      }
42  
43      public void setMuleContext(MuleContext muleContext)
44      {
45          this.plugin = new MuleIBeansPlugin(muleContext);
46      }
47  
48      public Object process(Object object)
49      {
50          List<AnnotationMetaData> annos = AnnotationUtils.getFieldAnnotations(object.getClass(), MockIntegrationBean.class);
51  
52          if (annos.size() > 0)
53          {
54              for (AnnotationMetaData data : annos)
55              {
56                  Field field = (Field) data.getMember();
57                  field.setAccessible(true);
58                  Object mockito = Mockito.mock(field.getType(), field.getName());
59                  try
60                  {
61                      //InvocationHandler handler = new MockIBeanHandler(field.getType(), muleContext, mockito);
62                      InvocationHandler handler = new MockIntegrationBeanInvocationHandler(field.getType(), plugin, mockito);
63  
64                      Object mock = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{field.getType(), MockIBean.class}, handler);
65  
66                      field.set(object, mock);
67                  }
68                  catch (Exception e)
69                  {
70                      throw new RuntimeException(e);
71                  }
72              }
73          }
74          return object;
75      }
76  }