### Question:
ExpiringUsernameAuthenticationToken extends UsernamePasswordAuthenticationToken { @Override public boolean isAuthenticated() { if (tokenExpiration != null && new Date().compareTo(tokenExpiration) >= 0) { return false; } else { return super.isAuthenticated(); } } ExpiringUsernameAuthenticationToken(Object principal, Object credentials);  ExpiringUsernameAuthenticationToken(Date tokenExpiration, Object principal, Object credentials, Collection<GrantedAuthority> authorities); @Override boolean isAuthenticated(); Date getTokenExpiration();  }### Answer:
@Test public void testExpirationNull() throws Exception { ExpiringUsernameAuthenticationToken t = new ExpiringUsernameAuthenticationToken(null, null, null, new LinkedList<GrantedAuthority>()); assertTrue(t.isAuthenticated()); }
@Test public void testExpirationFuture() throws Exception { Date future = new DateTime().plusHours(2).toDate(); ExpiringUsernameAuthenticationToken t = new ExpiringUsernameAuthenticationToken(future, null, null, new LinkedList<GrantedAuthority>()); assertTrue(t.isAuthenticated()); }
@Test public void testExpirationFutureChange() throws Exception { Date future = new DateTime().plusMillis(300).toDate(); ExpiringUsernameAuthenticationToken t = new ExpiringUsernameAuthenticationToken(future, null, null, new LinkedList<GrantedAuthority>()); assertTrue(t.isAuthenticated()); synchronized (this) { wait(600); } assertFalse(t.isAuthenticated()); }
@Test public void testNonAuthenticatedToken() throws Exception { ExpiringUsernameAuthenticationToken t = new ExpiringUsernameAuthenticationToken(null, null); assertFalse(t.isAuthenticated()); }