Skip to content

Commit

Permalink
Fixed bug in Locale jdk where Locale is created with invalid tag. Fil…
Browse files Browse the repository at this point in the history
…tering out values with invalid locale
  • Loading branch information
gastontulipani authored and naponce committed Jun 10, 2019
1 parent 7852a6b commit 1838596
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.appdirect.sdk.vendorFields.converter;

import java.beans.PropertyEditorSupport;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Locale;
Expand All @@ -18,6 +19,7 @@ public void setAsText(final String text) throws IllegalArgumentException {
.stream()
.sorted(Comparator.comparing(Locale.LanguageRange::getWeight).reversed())
.map(localeRange -> Locale.forLanguageTag(localeRange.getRange()))
.filter(locale -> Arrays.asList(Locale.getAvailableLocales()).contains(locale))
.collect(Collectors.toList()));
} catch (IllegalArgumentException | NullPointerException e) {
throw new PropertyEditorSupportException("Failed to serialize Locale from Accept-Language header with value=%s", text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void setup() {

@Test
public void testOneLocale() {
String localeToParse = "us";
String localeToParse = "en";
List<Locale> expected = Collections.singletonList(Locale.forLanguageTag(localeToParse));

localeConverter.setAsText(localeToParse);
Expand Down Expand Up @@ -80,6 +80,15 @@ public void testMultipleLocales_orderedByWeight() {
assertThat(localeConverter.getValue()).isEqualTo(expected);
}

@Test
public void testInvalidLocale_isNotIncludedInList() {
String localeToParse = "us";

localeConverter.setAsText(localeToParse);

assertThat(localeConverter.getValue()).isEqualTo(Collections.emptyList());
}

@Test
public void testNull_throwsPropertyEditorSupportException() {
String localeToParse = null;
Expand Down

0 comments on commit 1838596

Please sign in to comment.