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/main/java/org/hl7/fhir/dstu3/model/DecimalType.java b/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/DecimalType.java index daab918558d..844572a8c01 100644 --- a/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/DecimalType.java +++ b/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/DecimalType.java @@ -74,7 +74,7 @@ public class DecimalType extends PrimitiveType implements Comparable * Constructor */ public DecimalType(long theValue) { - setValue(new BigDecimal(theValue)); + setValue(theValue); } /** @@ -148,21 +148,21 @@ public class DecimalType extends PrimitiveType implements Comparable * Sets a new value using an integer */ public void setValueAsInteger(int theValue) { - setValue(new BigDecimal(theValue)); + setValue(BigDecimal.valueOf(theValue)); } /** * Sets a new value using a long */ public void setValue(long theValue) { - setValue(new BigDecimal(theValue)); + setValue(BigDecimal.valueOf(theValue)); } /** * Sets a new value using a double */ public void setValue(double theValue) { - setValue(new BigDecimal(theValue)); + setValue(BigDecimal.valueOf(theValue)); } @Override diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/model/DecimalTypeTest.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/model/DecimalTypeTest.java new file mode 100644 index 00000000000..ad96ca890fe --- /dev/null +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/model/DecimalTypeTest.java @@ -0,0 +1,29 @@ +package ca.uhn.fhir.model; + +import static org.junit.Assert.*; + +import org.hl7.fhir.dstu3.model.DecimalType; +import org.junit.Test; + +public class DecimalTypeTest { + + @Test + public void testDoubleValue() { + DecimalType d = new DecimalType(1.2D); + assertEquals("1.2", d.getValueAsString()); + + d = new DecimalType(); + d.setValue(1.2D); + assertEquals("1.2", d.getValueAsString()); + + d = new DecimalType(); + d.setValue(10); + assertEquals("10", d.getValueAsString()); + + d = new DecimalType(); + d.setValue(10L); + assertEquals("10", d.getValueAsString()); + + } + +} 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 af26f5a3aab..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; @@ -55,6 +54,7 @@ public class CreateDstu3Test { */ @Test public void testCreateWithInvalidContent() throws Exception { + HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient"); httpPost.setEntity(new StringEntity("FOO", ContentType.parse("application/xml+fhir; charset=utf-8"))); HttpResponse status = ourClient.execute(httpPost); @@ -65,8 +65,9 @@ public class CreateDstu3Test { ourLog.info("Response was:\n{}", responseContent); assertEquals(400, status.getStatusLine().getStatusCode()); - String expected = ""; - assertEquals(expected, responseContent); + + assertThat(responseContent, containsString(" + + DecimalType used BigDecimal constructor instead of valueOf method to + create a BigDecimal from a double, resulting in weird floating point + conversions. Thanks to Craig McClendon for reporting! +