LangImpl implements Lang { @Override public Locale getLocaleFromStringOrDefault(Optional<String> language) { if (language.isPresent()) { return Locale.forLanguageTag(language.get()); } else { return Locale.forLanguageTag(DEFAULT_LANGUAGE); } } @Inject  LangImpl(NinjaProperties ninjaProperties); @Override void clearLanguage(Result result); @Override Result setLanguage(String locale, Result result); @Override Optional<String> getLanguage(Context context, Optional<Result> result); @Override boolean isLanguageDirectlySupportedByThisApplication(String language); @Override Locale getLocaleFromStringOrDefault(Optional<String> language);  }
@Test public void testGetLocaleFromStringOrDefault() { when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)).thenReturn(new String[] {"en"}); Lang lang = new LangImpl(ninjaProperties); Optional<String> language = Optional.empty(); Locale locale = lang.getLocaleFromStringOrDefault(language); assertEquals(Locale.ENGLISH, locale); when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)).thenReturn(new String[] {"de", "en"}); lang = new LangImpl(ninjaProperties); language = Optional.empty(); locale = lang.getLocaleFromStringOrDefault(language); assertEquals(Locale.GERMAN, locale); when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)).thenReturn(new String[] {"de-DE", "en"}); lang = new LangImpl(ninjaProperties); language = Optional.empty(); locale = lang.getLocaleFromStringOrDefault(language); assertEquals(Locale.GERMANY, locale); }
@Test(expected = IllegalStateException.class) public void testGetLocaleFromStringOrDefaultISEWhenNoApplicationLanguageDefined() { when(ninjaProperties.getStringArray(NinjaConstant.applicationLanguages)).thenReturn(new String[] {}); Lang lang = new LangImpl(ninjaProperties); Optional<String> language = Optional.empty(); lang.getLocaleFromStringOrDefault(language); }