View Javadoc

1   package com.atlassian.plugins.rest.common.interceptor.impl;
2   
3   import com.atlassian.plugin.module.ContainerAccessor;
4   import com.atlassian.plugin.module.ContainerManagedPlugin;
5   import com.atlassian.plugins.rest.common.interceptor.ResourceInterceptor;
6   import com.atlassian.plugins.rest.common.interceptor.impl.test.ClassResource;
7   import com.atlassian.plugins.rest.common.interceptor.impl.test.MethodResource;
8   import com.atlassian.plugins.rest.common.interceptor.impl.test.MyInterceptor;
9   import com.atlassian.plugins.rest.common.interceptor.impl.test.PackageResource;
10  import junit.framework.TestCase;
11  
12  import java.util.List;
13  
14  import static org.mockito.Mockito.mock;
15  import static org.mockito.Mockito.when;
16  
17  public class InterceptorChainBuilderTest extends TestCase {
18      public void testMethodChain() throws NoSuchMethodException {
19          verifyChain(MethodResource.class);
20      }
21  
22      public void testClassChain() throws NoSuchMethodException {
23          verifyChain(ClassResource.class);
24      }
25  
26      public void testPackageChain() throws NoSuchMethodException {
27          verifyChain(PackageResource.class);
28      }
29  
30      public void testDefaultChain() throws NoSuchMethodException {
31          ContainerManagedPlugin plugin = mock(ContainerManagedPlugin.class);
32          InterceptorChainBuilder builder = new InterceptorChainBuilder(plugin);
33          List<ResourceInterceptor> interceptors = builder.getResourceInterceptorsForMethod(Object.class.getMethod("hashCode"));
34          assertNotNull(interceptors);
35          assertEquals(0, interceptors.size());
36      }
37  
38      private void verifyChain(Class resourceClass)
39              throws NoSuchMethodException {
40          ContainerAccessor containerAccessor = mock(ContainerAccessor.class);
41          when(containerAccessor.createBean(MyInterceptor.class)).thenReturn(new MyInterceptor());
42          ContainerManagedPlugin plugin = mock(ContainerManagedPlugin.class);
43          when(plugin.getContainerAccessor()).thenReturn(containerAccessor);
44          InterceptorChainBuilder builder = new InterceptorChainBuilder(plugin);
45          List<ResourceInterceptor> interceptors = builder.getResourceInterceptorsForMethod(resourceClass.getMethod("run"));
46          assertNotNull(interceptors);
47          assertEquals(1, interceptors.size());
48          assertTrue("Interceptor was " + interceptors.get(0), interceptors.get(0) instanceof MyInterceptor);
49      }
50  
51  
52  }