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  {
17      private TransactionTemplate transactionTemplate;
18      private TransactionInterceptor transactionInterceptor;
19      private MethodInvocation methodInvocation;
20  
21      @Before
22      public void setUp()
23      {
24          transactionTemplate = new TransactionTemplate()
25          {
26  
27              public Object execute(TransactionCallback action)
28              {
29                  return action.doInTransaction();
30              }
31          };
32          transactionInterceptor = new TransactionInterceptor(transactionTemplate);
33          methodInvocation = mock(MethodInvocation.class);
34      }
35  
36  
37  
38      @Test
39      public void testExecute() throws IllegalAccessException, InvocationTargetException
40      {
41          transactionInterceptor.intercept(methodInvocation);
42          verify(methodInvocation).invoke();
43      }
44  
45      @Test(expected = RuntimeException.class)
46      public void testExecuteThrowRuntimeException() throws IllegalAccessException, InvocationTargetException
47      {
48          doThrow(new RuntimeException()).when(methodInvocation).invoke();
49          transactionInterceptor.intercept(methodInvocation);
50      }
51      @Test(expected = IllegalAccessException.class)
52      public void testExecuteThrowIllegalAccessException() throws IllegalAccessException, InvocationTargetException
53      {
54          doThrow(new IllegalAccessException()).when(methodInvocation).invoke();
55          transactionInterceptor.intercept(methodInvocation);
56      }
57  
58      @Test(expected = InvocationTargetException.class)
59      public void testExecuteThrowInvocationTargetException() throws IllegalAccessException, InvocationTargetException
60      {
61          doThrow(new InvocationTargetException(new RuntimeException())).when(methodInvocation).invoke();
62          transactionInterceptor.intercept(methodInvocation);
63      }
64  }