View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.model.resolvers;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.model.InvocationResult;
11  import org.mule.tck.junit4.AbstractMuleTestCase;
12  
13  import java.lang.reflect.Method;
14  
15  import junit.framework.AssertionFailedError;
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.mockito.Mockito.mock;
20  
21  public class EntryPointResolverMethodCacheTestCase extends AbstractMuleTestCase
22  {
23      
24      private static final String METHOD = "aMethod";
25      
26      @Test
27      public void testMethodCaching() throws Exception
28      {
29          Method method = this.getClass().getMethod(METHOD, new Class[] { String.class});
30          Method anotherMethod = AnotherClass.class.getMethod(METHOD, new Class[] { String.class});
31  
32          MuleEventContext eventContext = mock(MuleEventContext.class);
33          MockEntryPointResolver epResolver = new MockEntryPointResolver();
34  
35          epResolver.addMethodByName(this, method, eventContext);
36          Method method1 = epResolver.getMethodByName(this, METHOD, eventContext);
37          assertEquals(method, method1);
38          assertEquals(this.getClass(), method1.getDeclaringClass());
39  
40          AnotherClass anotherObject = new AnotherClass();
41          epResolver.addMethodByName(anotherObject, anotherMethod, eventContext);
42          Method anotherMethod1 = epResolver.getMethodByName(anotherObject, METHOD, eventContext);
43          assertEquals(anotherMethod, anotherMethod1);
44          assertEquals(AnotherClass.class, anotherMethod.getDeclaringClass());
45  
46      }
47      
48      public void aMethod(String payload)
49      {
50          // this method exists only for being cached in the test
51      }
52      
53      private static class MockEntryPointResolver extends AbstractEntryPointResolver
54      {
55          public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
56          {
57              throw new AssertionFailedError("do not invoke this method");
58          }
59      }
60  
61      private static class AnotherClass
62      {
63          public void aMethod(String payload)
64          {
65              // this method exists only for being cached in the test
66          }
67      }
68      
69  }
70  
71