From cd5c9efcec5d615ac102c4da00097a47c6cfee88 Mon Sep 17 00:00:00 2001 From: dotasek Date: Thu, 27 Oct 2022 16:02:44 -0400 Subject: [PATCH] Add ICU4J dependency + initial tests --- org.hl7.fhir.utilities/pom.xml | 6 ++ .../hl7/fhir/utilities/i18n/I18nBaseTest.java | 56 ++++++++++++++++++- .../hl7/fhir/utilities/i18n/ICU4JTests.java | 52 +++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/ICU4JTests.java diff --git a/org.hl7.fhir.utilities/pom.xml b/org.hl7.fhir.utilities/pom.xml index d7ad5b9ec..f66076b91 100644 --- a/org.hl7.fhir.utilities/pom.xml +++ b/org.hl7.fhir.utilities/pom.xml @@ -80,6 +80,12 @@ true + + com.ibm.icu + icu4j + 72.1 + + org.junit.jupiter diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/I18nBaseTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/I18nBaseTest.java index 989b6cd5a..ceea6d3cc 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/I18nBaseTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/I18nBaseTest.java @@ -1,5 +1,6 @@ package org.hl7.fhir.utilities.i18n; +import com.ibm.icu.text.PluralRules; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,6 +13,11 @@ import java.nio.charset.StandardCharsets; import java.text.MessageFormat; 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 { public static final String BAD_STRING_ARG = "THIS_DOES_NOT_EXIST"; @@ -40,6 +46,38 @@ class I18nBaseTest { 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 @DisplayName("Assert no string modification is done when no match is found.") void testFormatMessageForNonExistentMessage() { @@ -68,7 +106,7 @@ class I18nBaseTest { String line; while ((line = reader.readLine()) != null) { // System.out.println("Searching for umlauts -> " + line); - Assertions.assertFalse(stringContainsItemFromList(line, UMLAUTS)); + assertFalse(stringContainsItemFromList(line, UMLAUTS)); } } catch (IOException e) { e.printStackTrace(); @@ -80,4 +118,20 @@ class I18nBaseTest { public static boolean stringContainsItemFromList(String inputStr, String[] items) { 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])); + } + } + } + } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/ICU4JTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/ICU4JTests.java new file mode 100644 index 000000000..86e63815f --- /dev/null +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/i18n/ICU4JTests.java @@ -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 keywords = getPluralKeywords(Locale.ENGLISH); + assertEquals(keywords, new HashSet(Arrays.asList(EN_KEYWORDS))); + } + + static final String[] DE_KEYWORDS = { + PluralRules.KEYWORD_ONE, + PluralRules.KEYWORD_OTHER + }; + + @Test + void getDeLocalePlurals() { + final Set keywords = getPluralKeywords(Locale.GERMAN); + assertEquals(keywords, new HashSet(Arrays.asList(DE_KEYWORDS))); + } + + static final String[] NL_KEYWORDS = { + PluralRules.KEYWORD_ONE, + PluralRules.KEYWORD_OTHER + }; + + @Test + void getNlLocalePlurals() { + final Set keywords = getPluralKeywords(Locale.forLanguageTag("nl")); + assertEquals(keywords, new HashSet(Arrays.asList(NL_KEYWORDS))); + } + + private static Set getPluralKeywords(Locale locale) { + final PluralRules pluralRules = PluralRules.forLocale(locale); + return pluralRules.getKeywords(); + } +}