TransactionAwareDirContextInvocationHandler implements
        InvocationHandler { void doCloseConnection(DirContext context, ContextSource contextSource) throws javax.naming.NamingException { DirContextHolder transactionContextHolder = (DirContextHolder) TransactionSynchronizationManager .getResource(contextSource); if (transactionContextHolder == null || transactionContextHolder.getCtx() != context) { log.debug("Closing context"); context.close(); } else { log.debug("Leaving transactional context open"); } } TransactionAwareDirContextInvocationHandler(DirContext target,
            ContextSource contextSource); Object invoke(Object proxy, Method method, Object[] args);  }
@Test public void testDoCloseConnection_NoTransaction() throws NamingException { tested.doCloseConnection(dirContextMock, contextSourceMock); verify(dirContextMock).close(); }
@Test public void testDoCloseConnection_ActiveTransaction() throws NamingException { TransactionSynchronizationManager.bindResource(contextSourceMock, holder); verifyNoMoreInteractions(dirContextMock); tested.doCloseConnection(dirContextMock, contextSourceMock); }
@Test public void testDoCloseConnection_NotTransactionalContext() throws NamingException { TransactionSynchronizationManager.bindResource(contextSourceMock, holder); DirContext dirContextMock2 = mock(DirContext.class); tested.doCloseConnection(dirContextMock2, contextSourceMock); verify(dirContextMock2).close(); }