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 org.junit.Test;
6
7 import java.lang.reflect.InvocationTargetException;
8 import java.util.Arrays;
9 import java.util.concurrent.atomic.AtomicInteger;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.mock;
13
14 public class DefaultMethodInvocationTest {
15 @Test
16 public void testInvoke() throws IllegalAccessException, InvocationTargetException {
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 private final AtomicInteger counter;
32 public int before;
33 public int after;
34
35 public CountingInterceptor(AtomicInteger counter) {
36 this.counter = counter;
37 }
38
39 public void intercept(MethodInvocation invocation) throws IllegalAccessException, InvocationTargetException {
40 before = counter.getAndIncrement();
41 invocation.invoke();
42 after = counter.getAndIncrement();
43 }
44 }
45
46 }