View Javadoc

1   package com.atlassian.plugins.rest.common.transaction;
2   
3   import com.atlassian.plugins.rest.common.interceptor.MethodInvocation;
4   import com.atlassian.sal.api.transaction.TransactionCallback;
5   import com.atlassian.sal.api.transaction.TransactionTemplate;
6   import org.junit.Before;
7   import org.junit.Test;
8   
9   import java.lang.reflect.InvocationTargetException;
10  
11  import static org.mockito.Mockito.doThrow;
12  import static org.mockito.Mockito.mock;
13  import static org.mockito.Mockito.verify;
14  
15  public class TransactionInterceptorTest {
16      private TransactionTemplate transactionTemplate;
17      private TransactionInterceptor transactionInterceptor;
18      private MethodInvocation methodInvocation;
19  
20      @Before
21      public void setUp() {
22          transactionTemplate = new TransactionTemplate() {
23  
24              public Object execute(TransactionCallback action) {
25                  return action.doInTransaction();
26              }
27          };
28          transactionInterceptor = new TransactionInterceptor(transactionTemplate);
29          methodInvocation = mock(MethodInvocation.class);
30      }
31  
32  
33      @Test
34      public void testExecute() throws IllegalAccessException, InvocationTargetException {
35          transactionInterceptor.intercept(methodInvocation);
36          verify(methodInvocation).invoke();
37      }
38  
39      @Test(expected = RuntimeException.class)
40      public void testExecuteThrowRuntimeException() throws IllegalAccessException, InvocationTargetException {
41          doThrow(new RuntimeException()).when(methodInvocation).invoke();
42          transactionInterceptor.intercept(methodInvocation);
43      }
44  
45      @Test(expected = IllegalAccessException.class)
46      public void testExecuteThrowIllegalAccessException() throws IllegalAccessException, InvocationTargetException {
47          doThrow(new IllegalAccessException()).when(methodInvocation).invoke();
48          transactionInterceptor.intercept(methodInvocation);
49      }
50  
51      @Test(expected = InvocationTargetException.class)
52      public void testExecuteThrowInvocationTargetException() throws IllegalAccessException, InvocationTargetException {
53          doThrow(new InvocationTargetException(new RuntimeException())).when(methodInvocation).invoke();
54          transactionInterceptor.intercept(methodInvocation);
55      }
56  }