-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make locale parsing less lenient. (#26361)
The `locale` field of `date` fields accepts almost any string and unknown locales are simply ignored, which is trappy. We should fail on unknown languages or countries. This commit also makes `-` an accepted separator in addition to `_` since `-` is the recommended separator (https://tools.ietf.org/html/rfc5646#section-2.1). `_` is probably still worth supporting since it is the separator used by `Locale#toString()`.
- Loading branch information
Showing
5 changed files
with
155 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
core/src/test/java/org/elasticsearch/common/util/LocaleUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.common.util; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.hamcrest.Matchers; | ||
|
||
import java.util.Locale; | ||
|
||
public class LocaleUtilsTests extends ESTestCase { | ||
|
||
public void testIllegalLang() { | ||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, | ||
() -> LocaleUtils.parse("yz")); | ||
assertThat(e.getMessage(), Matchers.containsString("Unknown language: yz")); | ||
|
||
e = expectThrows(IllegalArgumentException.class, | ||
() -> LocaleUtils.parse("yz-CA")); | ||
assertThat(e.getMessage(), Matchers.containsString("Unknown language: yz")); | ||
} | ||
|
||
public void testIllegalCountry() { | ||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, | ||
() -> LocaleUtils.parse("en-YZ")); | ||
assertThat(e.getMessage(), Matchers.containsString("Unknown country: YZ")); | ||
|
||
e = expectThrows(IllegalArgumentException.class, | ||
() -> LocaleUtils.parse("en-YZ-foobar")); | ||
assertThat(e.getMessage(), Matchers.containsString("Unknown country: YZ")); | ||
} | ||
|
||
public void testIllegalNumberOfParts() { | ||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, | ||
() -> LocaleUtils.parse("en-US-foo-bar")); | ||
assertThat(e.getMessage(), Matchers.containsString("Locales can have at most 3 parts but got 4")); | ||
} | ||
|
||
public void testUnderscores() { | ||
Locale locale1 = LocaleUtils.parse("fr_FR"); | ||
Locale locale2 = LocaleUtils.parse("fr-FR"); | ||
assertEquals(locale2, locale1); | ||
} | ||
|
||
public void testSimple() { | ||
assertEquals(Locale.FRENCH, LocaleUtils.parse("fr")); | ||
assertEquals(Locale.FRANCE, LocaleUtils.parse("fr-FR")); | ||
assertEquals(Locale.ROOT, LocaleUtils.parse("root")); | ||
assertEquals(Locale.ROOT, LocaleUtils.parse("")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters