fooobar.com/questions/1108744/..., , , - ( ). , JodaTime Mockito.
, (. fooobar.com/questions/71263/...). :
import org.joda.time.DateTime;
public interface Clock {
public DateTime getCurrentDateTime() ;
}
:
import org.joda.time.DateTime;
public class JodaClock implements Clock {
@Override
public DateTime getCurrentDateTime() {
return new DateTime();
}
}
SessionManager:
SessionManager(ConnectionManager connMgr, SessionGenerator sessionGen,
ObjectFactory factory, Clock clock) {
, , ( "t" testClear... "", , ...):
@Test
public void testClearSessionsMaintainsSessionsUnlessLastAccessTimeIsOverThreshold() {
final String sessionId = "test";
final Clock mockClock = mock(Clock.class);
when(mockClock.getCurrentDateTime()).thenReturn(getNow());
SessionManager sessionMgr = getSessionManager(connMgr,
sessionGen, factory, mockClock);
createSession(sessionMgr, sessionId);
sessionMgr.clearSessions(defaultTimeout);
assertNotNull(sessionMgr.getSession(sessionId));
when(mockClock.getCurrentDateTime()).thenReturn(getExpired());
sessionMgr.clearSessions(defaultTimeout);
assertNull(sessionMgr.getSession(sessionId));
}
, Session.setAccessTime() testOnlyExpiredSessionsared(), , , . fooobar.com/questions/269194/... SessionManager.clearSessions(), , SessionManager, DBSession.
From:
if (session.getAccessTime().before(cal.getTime())) {
To:
if (session.isExpired(expireTime)) {
mockSession ( Jayan fooobar.com/questions/1108744/...)
@Test
public void testOnlyOldSessionsCleared() {
final String sessionId = "test";
final String sessionId2 = "test2";
ObjectFactory mockFactory = spy(factory);
SessionManager sm = factory.createSessionManager(connMgr, sessionGen,
mockFactory, clock);
NPIISession session = factory.createNPIISession(null, clock);
NPIISession mockSession = spy(session);
doReturn(true).when(mockSession).isExpired((DateTime) anyObject());
doReturn(mockSession).when(mockFactory).createDBSession(
(Connection) anyObject(), eq(clock));
createSession(sm, sessionId);
reset(mockFactory);
createSession(sm, sessionId2);
assertNotNull(sm.getSession(sessionId));
assertNotNull(sm.getSession(sessionId2));
sm.clearSessions(defaultTimeout);
assertNull(sm.getSession(sessionId));
assertNotNull(sm.getSession(sessionId2));
}
Thanks to everyone for helping with this. Please let me know if you see any problems with the changes.