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 public void testInvoke() throws IllegalAccessException, InvocationTargetException {
15 AtomicInteger counter = new AtomicInteger();
16 CountingInterceptor interceptor1 = new CountingInterceptor(counter);
17 CountingInterceptor interceptor2 = new CountingInterceptor(counter);
18 DefaultMethodInvocation inv = new DefaultMethodInvocation(null, null, null, Arrays.<ResourceInterceptor>asList(
19 interceptor1, interceptor2, mock(ResourceInterceptor.class)), new Object[0]);
20
21 inv.invoke();
22 assertEquals(0, interceptor1.before);
23 assertEquals(1, interceptor2.before);
24 assertEquals(2, interceptor2.after);
25 assertEquals(3, interceptor1.after);
26 }
27
28 private static class CountingInterceptor implements ResourceInterceptor {
29 private final AtomicInteger counter;
30 public int before;
31 public int after;
32
33 public CountingInterceptor(AtomicInteger counter) {
34 this.counter = counter;
35 }
36
37 public void intercept(MethodInvocation invocation) throws IllegalAccessException, InvocationTargetException {
38 before = counter.getAndIncrement();
39 invocation.invoke();
40 after = counter.getAndIncrement();
41 }
42 }
43
44 }