CloseThreadMonitor implements Runnable { public void run() { try { this.connectionHandle.setThreadWatch(Thread.currentThread()); this.threadToMonitor.join(this.closeConnectionWatchTimeout); if (!this.connectionHandle.isClosed() && this.threadToMonitor.equals(this.connectionHandle.getThreadUsingConnection()) ){ logger.error(this.stackTrace); } } catch (Exception e) { if (this.connectionHandle != null){ this.connectionHandle.setThreadWatch(null); } } } CloseThreadMonitor(Thread threadToMonitor, ConnectionHandle connectionHandle, String stackTrace, long closeConnectionWatchTimeout); void run();  }
@Test public void testConnectionCorrectlyClosed() throws InterruptedException{ mockThread.join(); expect(mockConnection.isClosed()).andReturn(true).once(); replay(mockConnection, mockLogger, mockThread); testClass.run(); verify(mockConnection, mockLogger, mockThread); }
@Test public void testConnectionNotClosed() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ mockLogger.error((String)anyObject()); expectLastCall().once(); expect(mockConnection.isClosed()).andReturn(false).once(); expect(mockConnection.getThreadUsingConnection()).andReturn(mockThread).once(); replay(mockConnection, mockLogger); testClass.run(); verify(mockConnection, mockLogger); }
@Test public void testConnectionInterrupted() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InterruptedException{ expect(mockConnection.isClosed()).andThrow(new RuntimeException()).once(); replay(mockConnection, mockLogger); testClass.run(); verify(mockConnection, mockLogger); }