View Javadoc

1   package com.atlassian.plugins.rest.common.interceptor.impl;
2   
3   import com.atlassian.plugins.rest.common.interceptor.MethodInvocation;
4   import com.atlassian.plugins.rest.common.interceptor.ResourceInterceptor;
5   import junit.framework.TestCase;
6   
7   import java.lang.reflect.InvocationTargetException;
8   import java.util.Arrays;
9   import java.util.concurrent.atomic.AtomicInteger;
10  
11  import static org.mockito.Mockito.mock;
12  
13  public class DefaultMethodInvocationTest extends TestCase
14  {
15      public void testInvoke() throws IllegalAccessException, InvocationTargetException
16      {
17          AtomicInteger counter = new AtomicInteger();
18          CountingInterceptor interceptor1 = new CountingInterceptor(counter);
19          CountingInterceptor interceptor2 = new CountingInterceptor(counter);
20          DefaultMethodInvocation inv = new DefaultMethodInvocation(null, null, null, Arrays.<ResourceInterceptor>asList(
21                  interceptor1, interceptor2, mock(ResourceInterceptor.class)), new Object[0]);
22  
23          inv.invoke();
24          assertEquals(0, interceptor1.before);
25          assertEquals(1, interceptor2.before);
26          assertEquals(2, interceptor2.after);
27          assertEquals(3, interceptor1.after);
28      }
29  
30      private static class CountingInterceptor implements ResourceInterceptor
31      {
32          private final AtomicInteger counter;
33          public int before;
34          public int after;
35  
36          public CountingInterceptor(AtomicInteger counter)
37          {
38              this.counter = counter;
39          }
40  
41          public void intercept(MethodInvocation invocation) throws IllegalAccessException, InvocationTargetException
42          {
43              before = counter.getAndIncrement();
44              invocation.invoke();
45              after = counter.getAndIncrement();
46          }
47      }
48  
49  }