### Question:
SitePreferenceHandlerInterceptor extends HandlerInterceptorAdapter { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { sitePreferenceHandler.handleSitePreference(request, response); return true; } SitePreferenceHandlerInterceptor();  SitePreferenceHandlerInterceptor(SitePreferenceHandler sitePreferenceHandler); boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler);  }### Answer:
@Test public void preHandleDefault() throws Exception { SitePreferenceHandlerInterceptor interceptor = new SitePreferenceHandlerInterceptor(); boolean result = interceptor.preHandle(request, response, null); assertNull(SitePreferenceUtils.getCurrentSitePreference(request)); assertTrue(result); }
@Test public void preHandleCustomNormal() throws Exception { SitePreferenceHandler handler = new SitePreferenceHandler() { public SitePreference handleSitePreference(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("currentSitePreference", SitePreference.NORMAL); return SitePreference.NORMAL; } }; SitePreferenceHandlerInterceptor interceptor = new SitePreferenceHandlerInterceptor(handler); boolean result = interceptor.preHandle(request, response, null); assertEquals(SitePreference.NORMAL, request.getAttribute("currentSitePreference")); assertTrue(result); }
@Test public void preHandleCustomMobile() throws Exception { SitePreferenceHandler handler = new SitePreferenceHandler() { public SitePreference handleSitePreference(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("currentSitePreference", SitePreference.MOBILE); return SitePreference.MOBILE; } }; SitePreferenceHandlerInterceptor interceptor = new SitePreferenceHandlerInterceptor(handler); boolean result = interceptor.preHandle(request, response, null); assertEquals(SitePreference.MOBILE, request.getAttribute("currentSitePreference")); assertTrue(result); }
@Test public void preHandleCustomTablet() throws Exception { SitePreferenceHandler handler = new SitePreferenceHandler() { public SitePreference handleSitePreference(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("currentSitePreference", SitePreference.TABLET); return SitePreference.TABLET; } }; SitePreferenceHandlerInterceptor interceptor = new SitePreferenceHandlerInterceptor(handler); boolean result = interceptor.preHandle(request, response, null); assertEquals(SitePreference.TABLET, request.getAttribute("currentSitePreference")); assertTrue(result); }