| | |
| | | package org.forgerock.opendj.ldap; |
| | | |
| | | import static org.fest.assertions.Assertions.assertThat; |
| | | import static org.fest.assertions.Fail.fail; |
| | | import static org.forgerock.opendj.ldap.Connections.newFixedConnectionPool; |
| | | import static org.forgerock.opendj.ldap.ErrorResultException.newErrorResult; |
| | | import static org.forgerock.opendj.ldap.responses.Responses.newBindResult; |
| | | import static org.mockito.Matchers.any; |
| | | import static org.mockito.Matchers.anyBoolean; |
| | | import static org.mockito.Matchers.eq; |
| | | import static org.mockito.Matchers.isA; |
| | | import static org.mockito.Mockito.doAnswer; |
| | | import static org.mockito.Mockito.mock; |
| | | import static org.mockito.Mockito.times; |
| | |
| | | * @throws Exception |
| | | * If an unexpected error occurred. |
| | | */ |
| | | @Test(enabled = false) |
| | | @Test |
| | | public void testConnectionEventListenerClose() throws Exception { |
| | | final ConnectionFactory factory = mockConnectionFactory(mock(Connection.class)); |
| | | final Connection pooledConnection = mock(Connection.class); |
| | | when(pooledConnection.isValid()).thenReturn(true); |
| | | final ConnectionFactory factory = mockConnectionFactory(pooledConnection); |
| | | final ConnectionPool pool = newFixedConnectionPool(factory, 1); |
| | | final Connection connection = pool.getConnection(); |
| | | final ConnectionEventListener listener = mock(ConnectionEventListener.class); |
| | |
| | | * @throws Exception |
| | | * If an unexpected error occurred. |
| | | */ |
| | | @Test(enabled = false) |
| | | @Test |
| | | public void testConnectionEventListenerError() throws Exception { |
| | | final Connection badConnection = mock(Connection.class); |
| | | when(badConnection.bind(any(BindRequest.class))).thenThrow( |
| | | newErrorResult(newBindResult(ResultCode.CLIENT_SIDE_SERVER_DOWN))); |
| | | final ConnectionFactory factory = mockConnectionFactory(badConnection); |
| | | final ConnectionPool pool = newFixedConnectionPool(factory, 2); |
| | | final List<ConnectionEventListener> listeners = new LinkedList<ConnectionEventListener>(); |
| | | final Connection mockConnection = mockConnection(listeners); |
| | | final ConnectionFactory factory = mockConnectionFactory(mockConnection); |
| | | final ConnectionPool pool = newFixedConnectionPool(factory, 1); |
| | | final Connection connection = pool.getConnection(); |
| | | final ConnectionEventListener listener = mock(ConnectionEventListener.class); |
| | | connection.addConnectionEventListener(listener); |
| | | try { |
| | | connection.bind(Requests.newSimpleBindRequest("cn=test", "password".toCharArray())); |
| | | fail("Expected connection error"); |
| | | } catch (final ErrorResultException e) { |
| | | assertThat(e.getResult().getResultCode()).isEqualTo(ResultCode.CLIENT_SIDE_SERVER_DOWN); |
| | | } finally { |
| | | connection.close(); |
| | | } |
| | | verify(listener).handleConnectionError(eq(false), any(ConnectionException.class)); |
| | | verify(listener).handleConnectionClosed(); |
| | | assertThat(listeners).hasSize(1); |
| | | listeners.get(0).handleConnectionError(false, |
| | | newErrorResult(ResultCode.CLIENT_SIDE_SERVER_DOWN)); |
| | | verify(listener, times(0)).handleConnectionClosed(); |
| | | verify(listener).handleConnectionError(eq(false), isA(ConnectionException.class)); |
| | | verify(listener, times(0)).handleUnsolicitedNotification(any(ExtendedResult.class)); |
| | | connection.close(); |
| | | assertThat(listeners).hasSize(0); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws Exception |
| | | * If an unexpected error occurred. |
| | | */ |
| | | @Test(enabled = false) |
| | | @Test |
| | | public void testConnectionEventListenerUnsolicitedNotification() throws Exception { |
| | | final List<ConnectionEventListener> listeners = new LinkedList<ConnectionEventListener>(); |
| | | final Connection mockConnection = mock(Connection.class); |
| | | |
| | | // Handle listener registration / deregistration in mock connection. |
| | | doAnswer(new Answer<Void>() { |
| | | @Override |
| | | public Void answer(final InvocationOnMock invocation) throws Throwable { |
| | | final ConnectionEventListener listener = |
| | | (ConnectionEventListener) invocation.getArguments()[0]; |
| | | listeners.add(listener); |
| | | return null; |
| | | } |
| | | }).when(mockConnection).addConnectionEventListener(any(ConnectionEventListener.class)); |
| | | |
| | | doAnswer(new Answer<Void>() { |
| | | @Override |
| | | public Void answer(final InvocationOnMock invocation) throws Throwable { |
| | | final ConnectionEventListener listener = |
| | | (ConnectionEventListener) invocation.getArguments()[0]; |
| | | listeners.remove(listener); |
| | | return null; |
| | | } |
| | | }).when(mockConnection).removeConnectionEventListener(any(ConnectionEventListener.class)); |
| | | |
| | | final Connection mockConnection = mockConnection(listeners); |
| | | final ConnectionFactory factory = mockConnectionFactory(mockConnection); |
| | | final ConnectionPool pool = newFixedConnectionPool(factory, 1); |
| | | final Connection connection = pool.getConnection(); |
| | |
| | | pool.close(); |
| | | } |
| | | |
| | | private Connection mockConnection(final List<ConnectionEventListener> listeners) { |
| | | final Connection mockConnection = mock(Connection.class); |
| | | |
| | | // Handle listener registration / deregistration in mock connection. |
| | | doAnswer(new Answer<Void>() { |
| | | @Override |
| | | public Void answer(final InvocationOnMock invocation) throws Throwable { |
| | | final ConnectionEventListener listener = |
| | | (ConnectionEventListener) invocation.getArguments()[0]; |
| | | listeners.add(listener); |
| | | return null; |
| | | } |
| | | }).when(mockConnection).addConnectionEventListener(any(ConnectionEventListener.class)); |
| | | |
| | | doAnswer(new Answer<Void>() { |
| | | @Override |
| | | public Void answer(final InvocationOnMock invocation) throws Throwable { |
| | | final ConnectionEventListener listener = |
| | | (ConnectionEventListener) invocation.getArguments()[0]; |
| | | listeners.remove(listener); |
| | | return null; |
| | | } |
| | | }).when(mockConnection).removeConnectionEventListener(any(ConnectionEventListener.class)); |
| | | |
| | | return mockConnection; |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | private ConnectionFactory mockConnectionFactory(final Connection first, |
| | | final Connection... remaining) throws ErrorResultException { |