TransactionTokenInterceptor implements HandlerInterceptor { boolean validateToken(final TransactionToken receivedToken, final TransactionTokenStore tokenStore, final TransactionTokenInfo tokenInfo) { if (receivedToken.valid() && receivedToken.getTokenName().equals( tokenInfo.getTokenName())) { String storedToken = tokenStore.getAndClear(receivedToken); if (storedToken != null && storedToken.equals(receivedToken .getTokenValue())) { return true; } } return false; } TransactionTokenInterceptor();  TransactionTokenInterceptor(int sizePerTokenName);  TransactionTokenInterceptor(TokenStringGenerator generator,
            TransactionTokenInfoStore tokenInfoStore,
            TransactionTokenStore tokenStore); @Override boolean preHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler); @Override void postHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final ModelAndView modelAndView); @Override void afterCompletion(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final Exception ex); static final String TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME; static final String NEXT_TOKEN_REQUEST_ATTRIBUTE_NAME; static final String TOKEN_REQUEST_PARAMETER; }
@Test public void testValidateToken01() { HttpSessionTransactionTokenStore tokenStore = new HttpSessionTransactionTokenStore(); TransactionToken inputToken = new TransactionToken("tokenName1", "111", "222"); tokenStore.store(inputToken); TransactionTokenInfo tokenInfo = new TransactionTokenInfo("tokenName1", TransactionTokenType.IN); boolean result = interceptor.validateToken(inputToken, tokenStore, tokenInfo); assertThat(result, is(true)); result = interceptor.validateToken(inputToken, tokenStore, tokenInfo); assertThat(result, is(false)); }
@Test public void testValidateToken02() { HttpSessionTransactionTokenStore tokenStore = mock( HttpSessionTransactionTokenStore.class); TransactionToken inputToken = new TransactionToken("tokenName1", "111", "222"); TransactionTokenInfo tokenInfo = new TransactionTokenInfo("tokenName1", TransactionTokenType.IN); when(tokenStore.getAndClear(any(TransactionToken.class))).thenReturn( "differentValue"); boolean result = interceptor.validateToken(inputToken, tokenStore, tokenInfo); assertThat(result, is(false)); }