Add ICU4J dependency + initial tests

This commit is contained in:
dotasek 2022-10-27 16:02:44 -04:00
parent cc38567c93
commit cd5c9efcec
3 changed files with 113 additions and 1 deletions

View File

@ -80,6 +80,12 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>72.1</version>
</dependency>
<!-- JUnit Jupiter --> <!-- JUnit Jupiter -->
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>

View File

@ -1,5 +1,6 @@
package org.hl7.fhir.utilities.i18n; package org.hl7.fhir.utilities.i18n;
import com.ibm.icu.text.PluralRules;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -12,6 +13,11 @@ import java.nio.charset.StandardCharsets;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class I18nBaseTest { class I18nBaseTest {
public static final String BAD_STRING_ARG = "THIS_DOES_NOT_EXIST"; public static final String BAD_STRING_ARG = "THIS_DOES_NOT_EXIST";
@ -40,6 +46,38 @@ class I18nBaseTest {
Assertions.assertEquals(form.format(testArgs), result); Assertions.assertEquals(form.format(testArgs), result);
} }
@Test
@DisplayName("Test pluralization works without initializing Locale.")
void testFormatMessagePluralWithoutInitLocale() {
I18nTestClass testClass = new I18nTestClass();
//Answer value must be of the type {1}
String resultOne = testClass.formatMessagePL(1, I18nConstants.QUESTIONNAIRE_QR_ITEM_WRONGTYPE_PLURAL);
assertThat(resultOne, containsString("be of the type"));
//Answer value must be one of the {0} types {1}
String resultMany = testClass.formatMessagePL(3, I18nConstants.QUESTIONNAIRE_QR_ITEM_WRONGTYPE_PLURAL);
assertThat(resultMany, containsString("one of the 3 types "));
}
@Test
@DisplayName("Test pluralization works without initializing Locale.")
void testFormatMessagePluralWithInitLocale() {
I18nTestClass testClass = new I18nTestClass();
//ResourceBundle loadedBundle = ResourceBundle.getBundle("Messages", Locale.GERMAN);
testClass.setLocale(Locale.GERMAN);
//Answer value muss einer der Typen {1} sein
String resultOne = testClass.formatMessagePL(1, I18nConstants.QUESTIONNAIRE_QR_ITEM_WRONGTYPE_PLURAL);
assertThat(resultOne, containsString("Answer value muss einer der Typen {1} sein"));
//?
String resultMany = testClass.formatMessagePL(3, I18nConstants.QUESTIONNAIRE_QR_ITEM_WRONGTYPE_PLURAL);
assertThat(resultMany, containsString("one of the 3 types "));
}
@Test @Test
@DisplayName("Assert no string modification is done when no match is found.") @DisplayName("Assert no string modification is done when no match is found.")
void testFormatMessageForNonExistentMessage() { void testFormatMessageForNonExistentMessage() {
@ -68,7 +106,7 @@ class I18nBaseTest {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
// System.out.println("Searching for umlauts -> " + line); // System.out.println("Searching for umlauts -> " + line);
Assertions.assertFalse(stringContainsItemFromList(line, UMLAUTS)); assertFalse(stringContainsItemFromList(line, UMLAUTS));
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -80,4 +118,20 @@ class I18nBaseTest {
public static boolean stringContainsItemFromList(String inputStr, String[] items) { public static boolean stringContainsItemFromList(String inputStr, String[] items) {
return Arrays.stream(items).anyMatch(inputStr::contains); return Arrays.stream(items).anyMatch(inputStr::contains);
} }
@Test
void pluralKeysCompleteAndValid() {
ResourceBundle loadedBundle = ResourceBundle.getBundle("Messages", Locale.GERMAN);
PluralRules pluralRules = PluralRules.forLocale(Locale.GERMANY);
for (String key : loadedBundle.keySet()) {
String[] keyComponent = key.split("_");
assertFalse(keyComponent[keyComponent.length - 1].equalsIgnoreCase("PLURAL"), "Invalid use of PLURAL keyword for key: " + key);
if (keyComponent.length > 2
&& keyComponent[keyComponent.length - 2].equalsIgnoreCase("PLURAL")) {
assertTrue(pluralRules.getKeywords().contains(keyComponent[keyComponent.length - 1]));
}
}
}
} }

View File

@ -0,0 +1,52 @@
package org.hl7.fhir.utilities.i18n;
import com.ibm.icu.text.PluralRules;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ICU4JTests {
static final String[] EN_KEYWORDS = {
PluralRules.KEYWORD_ONE,
PluralRules.KEYWORD_OTHER
};
@Test
void getEnLocalePlurals() {
final Set<String> keywords = getPluralKeywords(Locale.ENGLISH);
assertEquals(keywords, new HashSet<String>(Arrays.asList(EN_KEYWORDS)));
}
static final String[] DE_KEYWORDS = {
PluralRules.KEYWORD_ONE,
PluralRules.KEYWORD_OTHER
};
@Test
void getDeLocalePlurals() {
final Set<String> keywords = getPluralKeywords(Locale.GERMAN);
assertEquals(keywords, new HashSet<String>(Arrays.asList(DE_KEYWORDS)));
}
static final String[] NL_KEYWORDS = {
PluralRules.KEYWORD_ONE,
PluralRules.KEYWORD_OTHER
};
@Test
void getNlLocalePlurals() {
final Set<String> keywords = getPluralKeywords(Locale.forLanguageTag("nl"));
assertEquals(keywords, new HashSet<String>(Arrays.asList(NL_KEYWORDS)));
}
private static Set<String> getPluralKeywords(Locale locale) {
final PluralRules pluralRules = PluralRules.forLocale(locale);
return pluralRules.getKeywords();
}
}