Merge pull request #1845 from hapifhir/do-20241204-npe-in-decimaltype
Fix NPE in DecimalType
This commit is contained in:
commit
34efd6aa1d
|
@ -34,6 +34,7 @@ import java.math.MathContext;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,7 +79,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public DecimalType(String theValue) {
|
public DecimalType(String theValue) {
|
||||||
setValue(new BigDecimal(theValue));
|
setValueAsString(theValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -148,6 +149,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
setValue(new BigDecimal(theValue));
|
setValue(new BigDecimal(theValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setValueAsString(String theString) {
|
||||||
|
if (StringUtils.isBlank(theString)) {
|
||||||
|
setValue((BigDecimal) null);
|
||||||
|
} else {
|
||||||
|
setValue(new BigDecimal(theString));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DecimalType copy() {
|
public DecimalType copy() {
|
||||||
return new DecimalType(getValue());
|
return new DecimalType(getValue());
|
||||||
|
|
|
@ -3,6 +3,11 @@ package org.hl7.fhir.dstu2.model;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
class DecimalTypeNullTest {
|
class DecimalTypeNullTest {
|
||||||
|
|
||||||
|
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
|
||||||
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
||||||
Assertions.assertNull(copyDecimal.getValue());
|
Assertions.assertNull(copyDecimal.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of((String)null),
|
||||||
|
Arguments.of(""),
|
||||||
|
Arguments.of(" ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideEmptyOrNullStringsConstructor")
|
||||||
|
void testNullValue(String theStringValue) {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
Assertions.assertNull(nullDecimal.getValue());
|
||||||
|
Assertions.assertNull(nullDecimal.asStringValue());
|
||||||
|
|
||||||
|
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
|
||||||
|
Assertions.assertNull(anotherNullDecimal.getValue());
|
||||||
|
Assertions.assertNull(anotherNullDecimal.asStringValue());
|
||||||
|
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -33,6 +33,7 @@ import java.math.BigDecimal;
|
||||||
import java.math.MathContext;
|
import java.math.MathContext;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
||||||
|
|
||||||
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
||||||
|
@ -79,7 +80,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public DecimalType(String theValue) {
|
public DecimalType(String theValue) {
|
||||||
setValue(new BigDecimal(theValue));
|
setValueAsString(theValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -149,6 +150,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
setValue(new BigDecimal(theValue));
|
setValue(new BigDecimal(theValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setValueAsString(String theString) {
|
||||||
|
if (StringUtils.isBlank(theString)) {
|
||||||
|
setValue((BigDecimal) null);
|
||||||
|
} else {
|
||||||
|
setValue(new BigDecimal(theString));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a new value using a long
|
* Sets a new value using a long
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
package org.hl7.fhir.dstu2016may.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
class DecimalTypeNullTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test null value toString()")
|
||||||
|
void testToString() {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
System.out.println("Value -> " + nullDecimal);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test null value equalsDeep()")
|
||||||
|
void equalsDeep() {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
DecimalType validDecimal = new DecimalType("3.14");
|
||||||
|
Assertions.assertFalse(nullDecimal.equalsDeep(validDecimal));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test null value equalsShallow()")
|
||||||
|
void equalsShallow() {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
DecimalType validDecimal = new DecimalType("3.14");
|
||||||
|
Assertions.assertFalse(nullDecimal.equalsShallow(validDecimal));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test null value copy()")
|
||||||
|
void copy() {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
DecimalType copyDecimal = nullDecimal.copy();
|
||||||
|
Assertions.assertNull(copyDecimal.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test null value typedCopy()")
|
||||||
|
void typedCopy() {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
||||||
|
Assertions.assertNull(copyDecimal.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of((String)null),
|
||||||
|
Arguments.of(""),
|
||||||
|
Arguments.of(" ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideEmptyOrNullStringsConstructor")
|
||||||
|
void testNullValue(String theStringValue) {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
Assertions.assertNull(nullDecimal.getValue());
|
||||||
|
Assertions.assertNull(nullDecimal.asStringValue());
|
||||||
|
|
||||||
|
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
|
||||||
|
Assertions.assertNull(anotherNullDecimal.getValue());
|
||||||
|
Assertions.assertNull(anotherNullDecimal.asStringValue());
|
||||||
|
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,33 +1,33 @@
|
||||||
package org.hl7.fhir.dstu3.model;
|
package org.hl7.fhir.dstu3.model;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Copyright (c) 2011+, HL7, Inc.
|
Copyright (c) 2011+, HL7, Inc.
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without modification,
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
are permitted provided that the following conditions are met:
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright notice, this
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
list of conditions and the following disclaimer.
|
list of conditions and the following disclaimer.
|
||||||
* Redistributions in binary form must reproduce the above copyright notice,
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
this list of conditions and the following disclaimer in the documentation
|
this list of conditions and the following disclaimer in the documentation
|
||||||
and/or other materials provided with the distribution.
|
and/or other materials provided with the distribution.
|
||||||
* Neither the name of HL7 nor the names of its contributors may be used to
|
* Neither the name of HL7 nor the names of its contributors may be used to
|
||||||
endorse or promote products derived from this software without specific
|
endorse or promote products derived from this software without specific
|
||||||
prior written permission.
|
prior written permission.
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
POSSIBILITY OF SUCH DAMAGE.
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@ import java.math.BigDecimal;
|
||||||
import java.math.MathContext;
|
import java.math.MathContext;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
||||||
|
|
||||||
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
||||||
|
@ -81,7 +82,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public DecimalType(String theValue) {
|
public DecimalType(String theValue) {
|
||||||
setValue(new BigDecimal(theValue));
|
setValueAsString(theValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -151,6 +152,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
setValue(BigDecimal.valueOf(theValue));
|
setValue(BigDecimal.valueOf(theValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setValueAsString(String theString) {
|
||||||
|
if (StringUtils.isBlank(theString)) {
|
||||||
|
setValue((BigDecimal) null);
|
||||||
|
} else {
|
||||||
|
setValue(new BigDecimal(theString));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a new value using a long
|
* Sets a new value using a long
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -3,6 +3,11 @@ package org.hl7.fhir.dstu3.model;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
class DecimalTypeNullTest {
|
class DecimalTypeNullTest {
|
||||||
|
|
||||||
|
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
|
||||||
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
||||||
Assertions.assertNull(copyDecimal.getValue());
|
Assertions.assertNull(copyDecimal.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of((String)null),
|
||||||
|
Arguments.of(""),
|
||||||
|
Arguments.of(" ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideEmptyOrNullStringsConstructor")
|
||||||
|
void testNullValue(String theStringValue) {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
Assertions.assertNull(nullDecimal.getValue());
|
||||||
|
Assertions.assertNull(nullDecimal.asStringValue());
|
||||||
|
|
||||||
|
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
|
||||||
|
Assertions.assertNull(anotherNullDecimal.getValue());
|
||||||
|
Assertions.assertNull(anotherNullDecimal.asStringValue());
|
||||||
|
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -33,6 +33,7 @@ import java.math.BigDecimal;
|
||||||
import java.math.MathContext;
|
import java.math.MathContext;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
||||||
|
|
||||||
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
||||||
|
@ -150,8 +151,13 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValueAsString(String theString) {
|
public void setValueAsString(String theString) {
|
||||||
setValue(new BigDecimal(theString));
|
if (StringUtils.isBlank(theString)) {
|
||||||
setRepresentation(theString);
|
setValue((BigDecimal) null);
|
||||||
|
setRepresentation(null);
|
||||||
|
} else {
|
||||||
|
setValue(new BigDecimal(theString));
|
||||||
|
setRepresentation(theString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3,6 +3,11 @@ package org.hl7.fhir.r4.model;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
class DecimalTypeNullTest {
|
class DecimalTypeNullTest {
|
||||||
|
|
||||||
|
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
|
||||||
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
||||||
Assertions.assertNull(copyDecimal.getValue());
|
Assertions.assertNull(copyDecimal.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of((String)null),
|
||||||
|
Arguments.of(""),
|
||||||
|
Arguments.of(" ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideEmptyOrNullStringsConstructor")
|
||||||
|
void testNullValue(String theStringValue) {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
Assertions.assertNull(nullDecimal.getValue());
|
||||||
|
Assertions.assertNull(nullDecimal.asStringValue());
|
||||||
|
|
||||||
|
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
|
||||||
|
Assertions.assertNull(anotherNullDecimal.getValue());
|
||||||
|
Assertions.assertNull(anotherNullDecimal.asStringValue());
|
||||||
|
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -33,6 +33,7 @@ import java.math.BigDecimal;
|
||||||
import java.math.MathContext;
|
import java.math.MathContext;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
||||||
|
|
||||||
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
||||||
|
@ -150,8 +151,13 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValueAsString(String theString) {
|
public void setValueAsString(String theString) {
|
||||||
setValue(new BigDecimal(theString));
|
if (StringUtils.isBlank(theString)) {
|
||||||
setRepresentation(theString);
|
setValue((BigDecimal) null);
|
||||||
|
setRepresentation(null);
|
||||||
|
} else {
|
||||||
|
setValue(new BigDecimal(theString));
|
||||||
|
setRepresentation(theString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
package org.hl7.fhir.r4b.model;
|
package org.hl7.fhir.r4b.model;
|
||||||
|
|
||||||
import org.hl7.fhir.r4b.model.DecimalType;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
class DecimalTypeNullTest {
|
class DecimalTypeNullTest {
|
||||||
|
|
||||||
|
@ -45,4 +49,27 @@ class DecimalTypeNullTest {
|
||||||
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
||||||
Assertions.assertNull(copyDecimal.getValue());
|
Assertions.assertNull(copyDecimal.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of((String)null),
|
||||||
|
Arguments.of(""),
|
||||||
|
Arguments.of(" ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideEmptyOrNullStringsConstructor")
|
||||||
|
void testNullValue(String theStringValue) {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
Assertions.assertNull(nullDecimal.getValue());
|
||||||
|
Assertions.assertNull(nullDecimal.asStringValue());
|
||||||
|
|
||||||
|
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
|
||||||
|
Assertions.assertNull(anotherNullDecimal.getValue());
|
||||||
|
Assertions.assertNull(anotherNullDecimal.asStringValue());
|
||||||
|
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -35,6 +35,7 @@ import java.math.BigDecimal;
|
||||||
import java.math.MathContext;
|
import java.math.MathContext;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
|
||||||
|
|
||||||
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
||||||
|
@ -166,8 +167,13 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValueAsString(String theString) {
|
public void setValueAsString(String theString) {
|
||||||
setValue(new BigDecimal(theString));
|
if (StringUtils.isBlank(theString)) {
|
||||||
setRepresentation(theString);
|
setValue((BigDecimal) null);
|
||||||
|
setRepresentation(null);
|
||||||
|
} else {
|
||||||
|
setValue(new BigDecimal(theString));
|
||||||
|
setRepresentation(theString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -184,7 +190,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
|
||||||
/**
|
/**
|
||||||
* A parser can provide a literal representation for the decimal value that preserves
|
* A parser can provide a literal representation for the decimal value that preserves
|
||||||
* the presented form.
|
* the presented form.
|
||||||
*
|
* <p/>
|
||||||
* All sorts of bad things can happen if this method is used to set the string representation
|
* All sorts of bad things can happen if this method is used to set the string representation
|
||||||
* to anything other than what was parsed into the actual value. Don't do that
|
* to anything other than what was parsed into the actual value. Don't do that
|
||||||
*
|
*
|
||||||
|
|
|
@ -3,6 +3,12 @@ package org.hl7.fhir.r5.model;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
class DecimalTypeNullTest {
|
class DecimalTypeNullTest {
|
||||||
|
|
||||||
|
@ -44,4 +50,27 @@ class DecimalTypeNullTest {
|
||||||
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
|
||||||
Assertions.assertNull(copyDecimal.getValue());
|
Assertions.assertNull(copyDecimal.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of((String)null),
|
||||||
|
Arguments.of(""),
|
||||||
|
Arguments.of(" ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideEmptyOrNullStringsConstructor")
|
||||||
|
void testNullValue(String theStringValue) {
|
||||||
|
DecimalType nullDecimal = new DecimalType();
|
||||||
|
Assertions.assertNull(nullDecimal.getValue());
|
||||||
|
Assertions.assertNull(nullDecimal.asStringValue());
|
||||||
|
|
||||||
|
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
|
||||||
|
Assertions.assertNull(anotherNullDecimal.getValue());
|
||||||
|
Assertions.assertNull(anotherNullDecimal.asStringValue());
|
||||||
|
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
|
||||||
|
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue