From b84150ff5e5543338809a2580799d40cb7540d27 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 21 Jun 2016 09:53:24 -0400 Subject: [PATCH] Randomly set locale after each test in order to make sure we aren't dependent on a specific environment --- .../main/java/ca/uhn/fhir/util/TestUtil.java | 23 ++++++++++++++++++- .../uhn/fhir/rest/server/CreateDstu3Test.java | 1 - .../model/BaseDateTimeTypeDstu3Test.java | 20 ---------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TestUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TestUtil.java index ad368f6b42a..3fcae4ede7d 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TestUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TestUtil.java @@ -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"); + } + + } } diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateDstu3Test.java index 1a3d6420d59..d66be30c148 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateDstu3Test.java @@ -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; diff --git a/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/model/BaseDateTimeTypeDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/model/BaseDateTimeTypeDstu3Test.java index 937b51e3840..f23d8c477b8 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/model/BaseDateTimeTypeDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/model/BaseDateTimeTypeDstu3Test.java @@ -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()); - } - }