1 package com.atlassian.activeobjects.spi;
2
3 import com.atlassian.activeobjects.spi.factory.PrivateConnectionFactory;
4 import org.hamcrest.CoreMatchers;
5 import org.junit.Rule;
6 import org.junit.Test;
7 import org.junit.rules.ExpectedException;
8
9 import java.sql.Connection;
10 import java.sql.SQLException;
11
12 import static org.mockito.ArgumentMatchers.anyString;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16
17
18
19 public class ConnectionHandlerTest {
20
21
22 @Rule
23 public ExpectedException exception = ExpectedException.none();
24
25
26
27
28
29
30 @Test
31 public void testPrivateDelegate() throws Exception {
32 ConnectionHandler.newInstance(PrivateConnectionFactory.newPrivateConnection()).createStatement();
33 }
34
35
36
37
38
39
40 @Test
41 public void testIfExceptionsAreHandledCorrectly() throws Exception {
42 final Connection connectionMock = mock(Connection.class);
43 final ConnectionHandler.Closeable closeableMock = mock(ConnectionHandler.Closeable.class);
44
45 final SQLException expectedException = new SQLException("An exception that should be re-thrown by proxy object");
46 when(connectionMock.prepareStatement(anyString())).thenThrow(expectedException);
47 final Connection connectionHandler = ConnectionHandler.newInstance(connectionMock, closeableMock);
48 exception.expect(SQLException.class);
49 exception.expectMessage(CoreMatchers.equalTo("An exception that should be re-thrown by proxy object"));
50 connectionHandler.prepareStatement("this call should throw SQLException");
51 }
52
53 }