View Javadoc
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   * <p>Unit tests for {@link ConnectionHandler}.</p>
18   */
19  public class ConnectionHandlerTest {
20  
21  
22      @Rule
23      public ExpectedException exception = ExpectedException.none();
24  
25      /**
26       * <p>Test that public methods can be invoked when the delegate is an instance of a private class.</p>
27       *
28       * @see <a href="https://studio.atlassian.com/browse/AO-329">AO-329</a>
29       */
30      @Test
31      public void testPrivateDelegate() throws Exception {
32          ConnectionHandler.newInstance(PrivateConnectionFactory.newPrivateConnection()).createStatement();
33      }
34  
35  
36      /**
37       * @throws Exception because it's a test :P
38       * @see <a href="https://ecosystem.atlassian.net/browse/AO-364">AO-364</a>
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  }