### Question:
LocaleUtils { public static String getUserLocaleAsString(final HttpServletRequest request) { String locale = getLocaleFromRequestURL(request); if (locale == null) { locale = getStandardizedLocaleFromCookie(request); } if (locale == null) { String browserLocale = getLocaleFromBrowser(request); locale = browserLocale != null ? browserLocale : DEFAULT_LOCALE; } return locale; }  static String getUserLocaleAsString(final HttpServletRequest request); static boolean isLocaleSupportedInPortal(String locale); static boolean canLocaleBeReducedToSupportedLocale(String locale, String supportedLocale); static String getStandardizedLocaleFromCookie(final HttpServletRequest request); static String getLocaleFromCookies(HttpServletRequest request); static String getLocaleFromBrowser(final HttpServletRequest request); static Locale getUserLocale(final HttpServletRequest request); static String getLocaleFromRequestURL(final HttpServletRequest request); static void addOrReplaceLocaleCookieResponse(final HttpServletResponse response, final String locale); static final String LOCALE_PARAM; @Deprecated
    static final String PORTAL_LOCALE_PARAM; static final String DEFAULT_LOCALE; static final String LOCALE_COOKIE_NAME; }### Answer:
@Test public void testLocalCanBeRetrieveFromCookie() throws Exception { Cookie[] cookies = { new Cookie("aCookie", "value"), new Cookie(LocaleUtils.LOCALE_COOKIE_NAME, "fr"), new Cookie("aNullCookie", null), }; doReturn(cookies).when(request).getCookies(); String locale = LocaleUtils.getUserLocaleAsString(request); assertEquals("fr", locale); }
@Test public void testANullCookieResultsWithBrowserLocale() throws Exception { doReturn(Locale.CANADA_FRENCH).when(request).getLocale(); String locale = LocaleUtils.getUserLocaleAsString(request); assertEquals(Locale.FRENCH.toString(), locale); }
@Test public void testANullCookieAndBrowserLocaleResultsWithDefaultLocale() throws Exception { String locale = LocaleUtils.getUserLocaleAsString(request); assertEquals("en", locale); }
@Test public void testWeCanRetrieveLocaleFromCookie() throws Exception { Cookie[] cookies = { new Cookie(LocaleUtils.LOCALE_COOKIE_NAME, "en_US"), }; doReturn(cookies).when(request).getCookies(); String locale = LocaleUtils.getUserLocaleAsString(request); assertEquals("en", locale); }
@Test public void testWeCanRetrieveLocaleFromRequest() throws Exception { Cookie[] cookies = { new Cookie(LocaleUtils.LOCALE_COOKIE_NAME, "en_US"), }; doReturn(cookies).when(request).getCookies(); String locale = LocaleUtils.getUserLocaleAsString(request); assertEquals("en", locale); }
@Test public void testAnInvalidCookieResultsWithDefaultLocale() throws Exception { Cookie[] cookies = { new Cookie(LocaleUtils.LOCALE_COOKIE_NAME, "weirdvalue"), }; String locale = LocaleUtils.getUserLocaleAsString(request); assertEquals("en", locale); }
@Test public void testAnInvalidLocaleInRequestResultsWithDefaultLocale() throws Exception { doReturn("weirdvalue").when(request).getParameter(LocaleUtils.LOCALE_PARAM); String locale = LocaleUtils.getUserLocaleAsString(request); assertEquals("en", locale); }