Merge pull request #2979 from cainj13/localeNpeFix

make locale retrieval null-safe
This commit is contained in:
Stian Thorgersen 2016-07-01 12:33:36 +02:00 committed by GitHub
commit fa312fb3db
2 changed files with 38 additions and 13 deletions

View file

@ -135,21 +135,23 @@ public class LocaleHelper {
private static Locale findLocale(Set<String> supportedLocales, String... localeStrings) { private static Locale findLocale(Set<String> supportedLocales, String... localeStrings) {
for (String localeString : localeStrings) { for (String localeString : localeStrings) {
Locale result = null; if (localeString != null) {
Locale search = Locale.forLanguageTag(localeString); Locale result = null;
for (String languageTag : supportedLocales) { Locale search = Locale.forLanguageTag(localeString);
Locale locale = Locale.forLanguageTag(languageTag); for (String languageTag : supportedLocales) {
if (locale.getLanguage().equals(search.getLanguage())) { Locale locale = Locale.forLanguageTag(languageTag);
if (locale.getCountry().equals("") && result == null) { if (locale.getLanguage().equals(search.getLanguage())) {
result = locale; if (locale.getCountry().equals("") && result == null) {
} result = locale;
if (locale.getCountry().equals(search.getCountry())) { }
return locale; if (locale.getCountry().equals(search.getCountry())) {
return locale;
}
} }
} }
} if (result != null) {
if (result != null) { return result;
return result; }
} }
} }
return null; return null;

View file

@ -0,0 +1,23 @@
package org.keycloak.services.util;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.nullValue;
public class LocaleHelperTest {
@Test
public void shouldNotExceptionOnNullLocaleAttributeItem() throws Exception {
final Method method = LocaleHelper.class.getDeclaredMethod("findLocale", Set.class, String[].class);
method.setAccessible(true);
Locale foundLocale = (Locale) method.invoke(null, Stream.of("en", "es", "fr").collect(Collectors.toSet()), new String[]{null});
assertThat(foundLocale, nullValue());
}
}