1 package com.atlassian.sal.core.rdbms;
2
3 import com.atlassian.sal.api.rdbms.ConnectionCallback;
4 import com.atlassian.sal.spi.HostConnectionAccessor;
5 import org.junit.Before;
6 import org.junit.Rule;
7 import org.junit.rules.ExpectedException;
8 import org.mockito.Mock;
9 import org.mockito.MockitoAnnotations;
10 import org.mockito.invocation.InvocationOnMock;
11 import org.mockito.stubbing.Answer;
12
13 import java.sql.Connection;
14
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Mockito.when;
17
18 public class TestDefaultTransactionalExecutorBase {
19 @Rule
20 public ExpectedException expectedException = ExpectedException.none();
21
22 protected DefaultTransactionalExecutor defaultTransactionalExecutor;
23
24 @Mock
25 protected HostConnectionAccessor hostConnectionAccessor;
26 @Mock
27 protected Connection connection;
28 @Mock
29 protected ConnectionCallback<Object> callback;
30 @Mock
31 protected Object result;
32
33 protected WrappedConnection wrappedConnection;
34
35 protected Throwable callbackThrows;
36
37 protected static class CallbackException extends RuntimeException {
38 public CallbackException(final String message) {
39 super(message);
40 }
41 }
42
43 @Before
44 public void before() {
45 MockitoAnnotations.initMocks(this);
46
47 defaultTransactionalExecutor = new DefaultTransactionalExecutor(hostConnectionAccessor, false, false);
48
49 callbackThrows = null;
50
51
52 when(callback.execute(any(WrappedConnection.class))).thenAnswer(new Answer<Object>() {
53 @Override
54 public Object answer(final InvocationOnMock invocation) throws Throwable {
55 wrappedConnection = (WrappedConnection) invocation.getArguments()[0];
56 if (callbackThrows != null) {
57 throw callbackThrows;
58 } else {
59 return result;
60 }
61 }
62 });
63 }
64 }