Randomly set locale after each test in order to make sure we aren't

dependent on a specific environment
This commit is contained in:
James Agnew 2016-06-21 09:53:24 -04:00
parent 0d65ec3f4e
commit b84150ff5e
3 changed files with 22 additions and 22 deletions

View File

@ -23,6 +23,7 @@ package ca.uhn.fhir.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Locale;
public class TestUtil {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TestUtil.class);
@ -34,7 +35,6 @@ public class TestUtil {
* tons of memory being used by the end and the JVM crashes in Travis. Manually clearing all of the
* static fields seems to solve this.
*/
@CoverageIgnore
public static void clearAllStaticFieldsForUnitTest() {
Class<?> theType;
@ -64,6 +64,27 @@ public class TestUtil {
}
}
}
/*
* Set some system properties randomly after each test.. this is kind of hackish,
* but it helps us make sure we don't have any tests that depend on a particular
* environment
*/
Locale[] available = { Locale.CANADA, Locale.GERMANY, Locale.TAIWAN };
Locale newLocale = available[(int) (Math.random() * available.length)];
Locale.setDefault(newLocale);
ourLog.info("Tests are running in locale: " + newLocale.getDisplayName());
if (Math.random() < 0.5) {
ourLog.info("Tests are using WINDOWS line endings and ISO-8851-1");
System.setProperty("file.encoding", "ISO-8859-1");
System.setProperty("line.separator", "\r\n");
} else {
ourLog.info("Tests are using UNIX line endings and UTF-8");
System.setProperty("file.encoding", "UTF-8");
System.setProperty("line.separator", "\n");
}
}
}

View File

@ -31,7 +31,6 @@ import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.annotation.ConditionalUrlParam;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;

View File

@ -20,7 +20,6 @@ import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.util.TestUtil;
public class BaseDateTimeTypeDstu3Test {
private static Locale ourDefaultLocale;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseDateTimeTypeDstu3Test.class);
private SimpleDateFormat myDateInstantParser;
@ -209,23 +208,4 @@ public class BaseDateTimeTypeDstu3Test {
assertThat(human, containsString("12"));
}
public static void afterClass() {
Locale.setDefault(ourDefaultLocale);
}
@BeforeClass
public static void beforeClass() {
/*
* We cache the default locale, but temporarily set it to a random value during this test. This helps ensure
* that there are no language specific dependencies in the test.
*/
ourDefaultLocale = Locale.getDefault();
Locale[] available = { Locale.CANADA, Locale.GERMANY, Locale.TAIWAN };
Locale newLocale = available[(int) (Math.random() * available.length)];
Locale.setDefault(newLocale);
ourLog.info("Tests are running in locale: " + newLocale.getDisplayName());
}
}