### Question:
JSSessionLocaleResolver implements LocaleResolver { public Locale resolveLocale(HttpServletRequest request) { String sessionAttribute = JasperServerConstImpl.getUserLocaleSessionAttr(); Locale locale =null; try { locale= (Locale)request.getSession().getAttribute(sessionAttribute); } catch(Exception e){} finally { if (locale == null) { locale = getLocaleFromCookies(request); } if (locale == null) { locale = request.getLocale(); } } return locale; }  Locale resolveLocale(HttpServletRequest request); void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale);  }### Answer:
@Test public void ensureLocaleGetFromSessionIfPresentInSession() throws Exception { HttpServletRequest requestMock = mock(HttpServletRequest.class); HttpSession sessionMock = mock(HttpSession.class); when(sessionMock.getAttribute(eq(JasperServerConstImpl.getUserLocaleSessionAttr()))).thenReturn(Locale.GERMAN); when(requestMock.getSession()).thenReturn(sessionMock); when(requestMock.getLocale()).thenReturn(Locale.US); Locale locale = localeResolver.resolveLocale(requestMock); assertEquals(Locale.GERMAN, locale); }
@Test public void ensureLocaleGetFromCookiesIfNotPresentInSession() throws Exception { HttpServletRequest requestMock = mock(HttpServletRequest.class); HttpSession sessionMock = mock(HttpSession.class); when(requestMock.getSession()).thenReturn(sessionMock); when(requestMock.getLocale()).thenReturn(Locale.US); Cookie cookie = mock(Cookie.class); when(cookie.getName()).thenReturn(JasperServerConstImpl.getUserLocaleSessionAttr()); when(cookie.getValue()).thenReturn("zh_CN"); Cookie[] cookies = new Cookie[] {cookie}; when(requestMock.getCookies()).thenReturn(cookies); Locale locale = localeResolver.resolveLocale(requestMock); assertEquals("zh_CN", locale.toString()); }
@Test public void ensureLocaleGetFromRequestIfNotPresentInSessionAndInCookies() throws Exception { HttpServletRequest requestMock = mock(HttpServletRequest.class); HttpSession sessionMock = mock(HttpSession.class); when(requestMock.getSession()).thenReturn(sessionMock); when(requestMock.getLocale()).thenReturn(Locale.UK); Locale locale = localeResolver.resolveLocale(requestMock); assertEquals(Locale.UK, locale); }