From 70b3ae874ed56d1ce28651a45e47497356e9ecb2 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 5 May 2015 13:56:19 -0400 Subject: [PATCH] Fix locale depenency in tests --- .../validation/ResourceValidatorTest.java | 32 ++++++++++++++++--- .../ResourceValidatorDstu2Test.java | 11 ++++--- pom.xml | 4 +++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/validation/ResourceValidatorTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/validation/ResourceValidatorTest.java index 05f6a67c669..b9223fd4e2c 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/validation/ResourceValidatorTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/validation/ResourceValidatorTest.java @@ -9,10 +9,12 @@ import ca.uhn.fhir.model.primitive.DateTimeDt; import org.apache.commons.io.IOUtils; import org.hamcrest.core.StringContains; +import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.text.SimpleDateFormat; +import java.util.Locale; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; @@ -25,6 +27,7 @@ import static org.junit.Assert.fail; public class ResourceValidatorTest { private static FhirContext ourCtx = new FhirContext(); + private static Locale ourDefaultLocale; private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceValidatorTest.class); @Test @@ -45,10 +48,29 @@ public class ResourceValidatorTest { } catch (ValidationFailureException e) { ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome())); assertEquals(1, e.getOperationOutcome().getIssue().size()); - assertThat(e.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue(), containsString("Invalid content was found starting with element 'breed'")); + assertThat(e.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue(), containsString("cvc-complex-type.2.4.a")); } } - + + @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.getAvailableLocales(); + Locale newLocale = available[(int)(Math.random() * available.length)]; + Locale.setDefault(newLocale); + + ourLog.info("Tests are running in locale: " + newLocale.getDisplayName()); + } + + public static void afterClass() { + Locale.setDefault(ourDefaultLocale); + } + /** * See issue #50 */ @@ -92,7 +114,7 @@ public class ResourceValidatorTest { } catch (ValidationFailureException e) { ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome())); assertEquals(1, e.getOperationOutcome().getIssue().size()); - assertThat(e.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue(), containsString("Inv-2: A system is required if a value is provided.")); + assertThat(e.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue(), containsString("Inv-2:")); } } @@ -114,7 +136,7 @@ public class ResourceValidatorTest { OperationOutcome operationOutcome = (OperationOutcome) validationResult.getOperationOutcome(); ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome)); assertEquals(1, operationOutcome.getIssue().size()); - assertThat(operationOutcome.getIssueFirstRep().getDetails().getValue(), containsString("Inv-2: A system is required if a value is provided.")); + assertThat(operationOutcome.getIssueFirstRep().getDetails().getValue(), containsString("Inv-2:")); p.getTelecomFirstRep().setSystem(ContactSystemEnum.EMAIL); validationResult = val.validateWithResult(p); @@ -155,7 +177,7 @@ public class ResourceValidatorTest { OperationOutcome operationOutcome = (OperationOutcome) validationResult.getOperationOutcome(); ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome)); assertEquals(1, operationOutcome.getIssue().size()); - assertThat(operationOutcome.getIssueFirstRep().getDetails().getValue(), containsString("Inv-2: A system is required if a value is provided.")); + assertThat(operationOutcome.getIssueFirstRep().getDetails().getValue(), containsString("Inv-2:")); } private FhirValidator createFhirValidator() { diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/validation/ResourceValidatorDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/validation/ResourceValidatorDstu2Test.java index aec58d43a04..6bfe21b6d4c 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/validation/ResourceValidatorDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/validation/ResourceValidatorDstu2Test.java @@ -10,6 +10,7 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.text.SimpleDateFormat; +import java.util.Locale; import org.apache.commons.io.IOUtils; import org.hamcrest.core.StringContains; @@ -65,7 +66,7 @@ public class ResourceValidatorDstu2Test { ourLog.info(resultString); assertEquals(2, result.getOperationOutcome().getIssue().size()); - assertThat(resultString, StringContains.containsString("cvc-pattern-valid: Value '2000-15-31'")); + assertThat(resultString, StringContains.containsString("'2000-15-31'")); } @SuppressWarnings("deprecation") @@ -90,7 +91,7 @@ public class ResourceValidatorDstu2Test { } catch (ValidationFailureException e) { String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome()); ourLog.info(encoded); - assertThat(encoded, containsString("if there's a duration, there needs to be")); + assertThat(encoded, containsString("tim-1: aa")); } } @@ -115,7 +116,7 @@ public class ResourceValidatorDstu2Test { OperationOutcome operationOutcome = (OperationOutcome) validationResult.getOperationOutcome(); String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); ourLog.info(encoded); - assertThat(encoded, containsString("if there's a duration, there needs to be")); + assertThat(encoded, containsString("tim-1:")); } @Test @@ -157,7 +158,7 @@ public class ResourceValidatorDstu2Test { } catch (ValidationFailureException e) { ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome())); assertEquals(1, e.getOperationOutcome().getIssue().size()); - assertThat(e.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue(), containsString("Invalid content was found starting with element 'breed'")); + assertThat(e.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue(), containsString("cvc-complex-type")); } } @@ -179,7 +180,7 @@ public class ResourceValidatorDstu2Test { OperationOutcome operationOutcome = (OperationOutcome) validationResult.getOperationOutcome(); ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome)); assertEquals(1, operationOutcome.getIssue().size()); - assertThat(operationOutcome.getIssueFirstRep().getDetails(), containsString("A system is required if a value is provided.")); + assertThat(operationOutcome.getIssueFirstRep().getDetails(), containsString("cpt-2:")); p.getTelecomFirstRep().setSystem(ContactPointSystemEnum.EMAIL); validationResult = val.validateWithResult(p); diff --git a/pom.xml b/pom.xml index e8127dcf0c9..d6e9d184fee 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,10 @@ + 3.0.1