Skip to content

Commit

Permalink
Pin the default time zone and locale to make ICU formatting tests mor…
Browse files Browse the repository at this point in the history
…e resilient
  • Loading branch information
echeran authored and mradbourne committed Mar 12, 2024
1 parent 1ca9227 commit 1bb65d5
Showing 1 changed file with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import static org.junit.Assert.assertEquals;

import com.ibm.icu.util.ULocale;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.unicode.conformance.testtype.messageformat2.MFInputArg;
import org.unicode.conformance.testtype.messageformat2.MFInputArgType;
import org.unicode.conformance.testtype.messageformat2.MFTestSubType;
Expand All @@ -14,6 +21,78 @@

public class MessageFormatterTest {

/**
* The default locale used for all of our tests. Used in @Before
*/
protected final static Locale defaultLocale = Locale.US;

/**
* The default time zone for all of our tests. Used in @Before
*/
protected final static TimeZone defaultTimeZone = TimeZone.getTimeZone("America/Los_Angeles");

private com.ibm.icu.util.TimeZone testStartDefaultIcuTz;

private java.util.TimeZone testStartDefaultJdkTz;

private com.ibm.icu.util.ULocale testStartDefaultULocale;

private java.util.Locale testStartDefaultLocale;

@Rule
public TestName name = new TestName();

// Copying test setup behavior from ICU4J CoreTestFmwk / TestFmwk, which
// ensures we pin the default locale and TZ during the test. ICU Formatters
// implicitly use the system's default locale and TZ.
@Before
public final void setup() {
// Just like TestFmwk initializes JDK TimeZone and Locale before every test,
// do the same for ICU TimeZone and ULocale.
ULocale.setDefault(ULocale.forLocale(defaultLocale));
com.ibm.icu.util.TimeZone.setDefault(
com.ibm.icu.util.TimeZone.getTimeZone(defaultTimeZone.getID()));

// Save starting timezones
testStartDefaultIcuTz = com.ibm.icu.util.TimeZone.getDefault();
testStartDefaultJdkTz = java.util.TimeZone.getDefault();

// Save starting locales
testStartDefaultULocale = com.ibm.icu.util.ULocale.getDefault();
testStartDefaultLocale = java.util.Locale.getDefault();
}

// Copying test teardown beahvior from ICU4J CoreTestFmwk, corresponding to
// the setup work.
@After
public final void teardown() {
String testMethodName = name.getMethodName();

// Assert that timezones are in a good state

com.ibm.icu.util.TimeZone testEndDefaultIcuTz = com.ibm.icu.util.TimeZone.getDefault();
java.util.TimeZone testEndDefaultJdkTz = java.util.TimeZone.getDefault();

assertEquals("In [" + testMethodName + "] Test should keep in sync ICU & JDK TZs",
testEndDefaultIcuTz.getID(),
testEndDefaultJdkTz.getID());

assertEquals("In [" + testMethodName + "] Test should reset ICU default TZ",
testStartDefaultIcuTz.getID(), testEndDefaultIcuTz.getID());
assertEquals("In [" + testMethodName + "] Test should reset JDK default TZ",
testStartDefaultJdkTz.getID(), testEndDefaultJdkTz.getID());

// Assert that locales are in a good state

com.ibm.icu.util.ULocale testEndDefaultULocale = com.ibm.icu.util.ULocale.getDefault();
java.util.Locale testEndDefaultLocale = java.util.Locale.getDefault();

assertEquals("In [" + testMethodName + "] Test should reset ICU ULocale",
testStartDefaultULocale.toLanguageTag(), testEndDefaultULocale.toLanguageTag());
assertEquals("In [" + testMethodName + "] Test should reset JDK Locale",
testStartDefaultLocale.toLanguageTag(), testEndDefaultLocale.toLanguageTag());
}

@Test
public void testGetFormattedMessage() {
// Setup
Expand Down

0 comments on commit 1bb65d5

Please sign in to comment.