### Question:
TokenHandlerInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { internalPreProcess((HandlerMethod) handler, request); return true; } else { return true; } } TokenHandlerInterceptor(); @Override boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler);  }### Answer:
@Test public void Tokenアノテーションを指定してsessionにトークンを保存する() throws Exception { TokenManager manager = new TokenManager(); manager.afterPropertiesSet(); TokenHandlerInterceptor tokenHandler = new TokenHandlerInterceptor(); Field managerField = tokenHandler.getClass().getDeclaredField("manager"); managerField.setAccessible(true); managerField.set(tokenHandler, manager); tokenHandler.preHandle(request, response, tokenSaveHanlder); assertThat(manager.getToken(request, Controllers.SCOPE_SESSION), is(notNullValue())); }
@Test public void Tokenアノテーションを指定してトークンのチェックを行う() throws Exception { TokenManager manager = new TokenManager(); manager.afterPropertiesSet(); TokenHandlerInterceptor tokenHandler = new TokenHandlerInterceptor(); Field managerField = tokenHandler.getClass().getDeclaredField("manager"); managerField.setAccessible(true); managerField.set(tokenHandler, manager); tokenHandler.preHandle(request, response, tokenSaveHanlder); String requestedToken = manager.getToken(request, Controllers.SCOPE_SESSION); request.setParameter(TokenManager.SESSION_TOKEN_PARAMETER_NAME, requestedToken); try { tokenHandler.preHandle(request, response, tokenCheckHanlder); } catch (InvalidTokenException e) { fail("トークンチェックに失敗"); } assertThat(manager.getToken(request, Controllers.SCOPE_SESSION), is(nullValue())); }
@Test public void Tokenアノテーションを指定してリセットをオフにしトークンのチェックを行う() throws Exception { TokenManager manager = new TokenManager(); manager.afterPropertiesSet(); TokenHandlerInterceptor tokenHandler = new TokenHandlerInterceptor(); Field managerField = tokenHandler.getClass().getDeclaredField("manager"); managerField.setAccessible(true); managerField.set(tokenHandler, manager); tokenHandler.preHandle(request, response, tokenSaveHanlder); String requestedToken = manager.getToken(request, Controllers.SCOPE_SESSION); request.setParameter(TokenManager.SESSION_TOKEN_PARAMETER_NAME, requestedToken); try { tokenHandler.preHandle(request, response, tokenResetAfterCheck); } catch (InvalidTokenException e) { fail("トークンチェックに失敗"); } assertThat(manager.getToken(request, Controllers.SCOPE_SESSION), is(notNullValue())); }
@Test(expected = InvalidTokenException.class) public void Tokenチェックに失敗する() throws Exception { TokenManager manager = new TokenManager(); manager.afterPropertiesSet(); TokenHandlerInterceptor tokenHandler = new TokenHandlerInterceptor(); Field managerField = tokenHandler.getClass().getDeclaredField("manager"); managerField.setAccessible(true); managerField.set(tokenHandler, manager); tokenHandler.preHandle(request, response, tokenSaveHanlder); String requestedToken = manager.getToken(request, Controllers.SCOPE_SESSION); request.setParameter(TokenManager.SESSION_TOKEN_PARAMETER_NAME, requestedToken); tokenHandler.preHandle(request, response, tokenSaveHanlder); tokenHandler.preHandle(request, response, tokenCheckHanlder); }
@Test public void Tokenアノテーションによって注釈されていない場合トークンが生成されない() throws Exception { TokenManager manager = new TokenManager(); manager.afterPropertiesSet(); TokenHandlerInterceptor tokenHandler = new TokenHandlerInterceptor(); Field managerField = tokenHandler.getClass().getDeclaredField("manager"); managerField.setAccessible(true); managerField.set(tokenHandler, manager); boolean result = tokenHandler.preHandle(request, response, nothing); assertThat(result, is(true)); String token = manager.getToken(request, Controllers.SCOPE_SESSION); assertThat(token, is(nullValue())); }