|
| 1 | +package org.unicode.conformance.messageformat2.icu78; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import com.ibm.icu.util.ULocale; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Locale; |
| 9 | +import java.util.TimeZone; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Rule; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.rules.TestName; |
| 15 | +import org.unicode.conformance.testtype.messageformat2.IMFInputParam; |
| 16 | +import org.unicode.conformance.testtype.messageformat2.MFInputParamDatetime; |
| 17 | +import org.unicode.conformance.testtype.messageformat2.MFInputParamObject; |
| 18 | +import org.unicode.conformance.testtype.messageformat2.MessageFormatInputJson; |
| 19 | +import org.unicode.conformance.testtype.messageformat2.MessageFormatTester; |
| 20 | + |
| 21 | +public class MessageFormatterTest { |
| 22 | + |
| 23 | + /** |
| 24 | + * The default locale used for all of our tests. Used in @Before |
| 25 | + */ |
| 26 | + protected final static Locale defaultLocale = Locale.US; |
| 27 | + |
| 28 | + /** |
| 29 | + * The default time zone for all of our tests. Used in @Before |
| 30 | + */ |
| 31 | + protected final static TimeZone defaultTimeZone = TimeZone.getTimeZone("America/Los_Angeles"); |
| 32 | + |
| 33 | + private com.ibm.icu.util.TimeZone testStartDefaultIcuTz; |
| 34 | + |
| 35 | + private TimeZone testStartDefaultJdkTz; |
| 36 | + |
| 37 | + private ULocale testStartDefaultULocale; |
| 38 | + |
| 39 | + private Locale testStartDefaultLocale; |
| 40 | + |
| 41 | + @Rule |
| 42 | + public TestName name = new TestName(); |
| 43 | + |
| 44 | + // Copying test setup behavior from ICU4J CoreTestFmwk / TestFmwk, which |
| 45 | + // ensures we pin the default locale and TZ during the test. ICU Formatters |
| 46 | + // implicitly use the system's default locale and TZ. |
| 47 | + @Before |
| 48 | + public final void setup() { |
| 49 | + // Just like TestFmwk initializes JDK TimeZone and Locale before every test, |
| 50 | + // do the same for ICU TimeZone and ULocale. |
| 51 | + ULocale.setDefault(ULocale.forLocale(defaultLocale)); |
| 52 | + com.ibm.icu.util.TimeZone.setDefault( |
| 53 | + com.ibm.icu.util.TimeZone.getTimeZone(defaultTimeZone.getID())); |
| 54 | + |
| 55 | + // Save starting timezones |
| 56 | + testStartDefaultIcuTz = com.ibm.icu.util.TimeZone.getDefault(); |
| 57 | + testStartDefaultJdkTz = TimeZone.getDefault(); |
| 58 | + |
| 59 | + // Save starting locales |
| 60 | + testStartDefaultULocale = ULocale.getDefault(); |
| 61 | + testStartDefaultLocale = Locale.getDefault(); |
| 62 | + } |
| 63 | + |
| 64 | + // Copying test teardown beahvior from ICU4J CoreTestFmwk, corresponding to |
| 65 | + // the setup work. |
| 66 | + @After |
| 67 | + public final void teardown() { |
| 68 | + String testMethodName = name.getMethodName(); |
| 69 | + |
| 70 | + // Assert that timezones are in a good state |
| 71 | + |
| 72 | + com.ibm.icu.util.TimeZone testEndDefaultIcuTz = com.ibm.icu.util.TimeZone.getDefault(); |
| 73 | + TimeZone testEndDefaultJdkTz = TimeZone.getDefault(); |
| 74 | + |
| 75 | + assertEquals("In [" + testMethodName + "] Test should keep in sync ICU & JDK TZs", |
| 76 | + testEndDefaultIcuTz.getID(), |
| 77 | + testEndDefaultJdkTz.getID()); |
| 78 | + |
| 79 | + assertEquals("In [" + testMethodName + "] Test should reset ICU default TZ", |
| 80 | + testStartDefaultIcuTz.getID(), testEndDefaultIcuTz.getID()); |
| 81 | + assertEquals("In [" + testMethodName + "] Test should reset JDK default TZ", |
| 82 | + testStartDefaultJdkTz.getID(), testEndDefaultJdkTz.getID()); |
| 83 | + |
| 84 | + // Assert that locales are in a good state |
| 85 | + |
| 86 | + ULocale testEndDefaultULocale = ULocale.getDefault(); |
| 87 | + Locale testEndDefaultLocale = Locale.getDefault(); |
| 88 | + |
| 89 | + assertEquals("In [" + testMethodName + "] Test should reset ICU ULocale", |
| 90 | + testStartDefaultULocale.toLanguageTag(), testEndDefaultULocale.toLanguageTag()); |
| 91 | + assertEquals("In [" + testMethodName + "] Test should reset JDK Locale", |
| 92 | + testStartDefaultLocale.toLanguageTag(), testEndDefaultLocale.toLanguageTag()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testGetFormattedMessage() { |
| 97 | + // Setup |
| 98 | + MessageFormatInputJson inputJson = new MessageFormatInputJson(); |
| 99 | + inputJson.label = "00001"; |
| 100 | + inputJson.locale = "en-GB"; |
| 101 | + inputJson.src = "Hello {$name}, your card expires on {$exp :datetime skeleton=yMMMdE}!"; |
| 102 | + inputJson.test_description = "Test using the ICU4J API doc example for the MessageFormatter class"; |
| 103 | + List<IMFInputParam> inputs = new ArrayList<>(); |
| 104 | + MFInputParamObject nameArg = new MFInputParamObject(); |
| 105 | + nameArg.name = "name"; |
| 106 | + nameArg.value = "John"; |
| 107 | + inputs.add(nameArg); |
| 108 | + MFInputParamDatetime expArg = new MFInputParamDatetime("exp", "2023-03-27T19:42:51"); // March 27, 2023, 7:42:51 PM |
| 109 | + expArg.name = "exp"; |
| 110 | + inputs.add(expArg); |
| 111 | + inputJson.params = inputs; |
| 112 | + |
| 113 | + // Actual |
| 114 | + String formattedString = MessageFormatTester.INSTANCE.getFormattedMessage(inputJson); |
| 115 | + |
| 116 | + // Expect & assert test |
| 117 | + String expected = "Hello John, your card expires on Mon, 27 Mar 2023, 12:42!"; |
| 118 | + assertEquals(expected, formattedString); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + // ICU 75 impl output differs from MF2 spec defined at same time point (CLDR 45) |
| 123 | + // in what to return in message for non-provided args / formatting errors |
| 124 | + @Test |
| 125 | + public void testGetFormattedMessage_usingNonProvidedArg() { |
| 126 | + // Setup |
| 127 | + MessageFormatInputJson inputJson = new MessageFormatInputJson(); |
| 128 | + inputJson.label = "00020"; |
| 129 | + inputJson.locale = "en-US"; |
| 130 | + inputJson.src = ":date"; |
| 131 | + inputJson.test_description = "Test of formatting a pattern using an input arg that isn't provided"; |
| 132 | + List<IMFInputParam> inputs = new ArrayList<>(); |
| 133 | + inputJson.params = inputs; |
| 134 | + |
| 135 | + // Actual |
| 136 | + String formattedString = MessageFormatTester.INSTANCE.getFormattedMessage(inputJson); |
| 137 | + |
| 138 | + // Expect & assert test |
| 139 | + String expected = ":date"; |
| 140 | + assertEquals(expected, formattedString); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testGetFormattedMessage_numberLiteralOperand() { |
| 145 | + // Setup |
| 146 | + MessageFormatInputJson inputJson = new MessageFormatInputJson(); |
| 147 | + inputJson.label = "00035"; |
| 148 | + inputJson.locale = "en-US"; |
| 149 | + inputJson.src = "hello {4.2 :integer}"; |
| 150 | + inputJson.test_description = "Test of formatting a pattern using an input arg that isn't provided"; |
| 151 | + List<IMFInputParam> inputs = new ArrayList<>(); |
| 152 | + inputJson.params = inputs; |
| 153 | + |
| 154 | + // Actual |
| 155 | + String formattedString = MessageFormatTester.INSTANCE.getFormattedMessage(inputJson); |
| 156 | + |
| 157 | + // Expect & assert test |
| 158 | + String expected = "hello 4"; |
| 159 | + assertEquals(expected, formattedString); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments