Fix overflow error for quantities stored as doubles. Closes #2244.

This commit is contained in:
Antonin Delpeuch 2019-12-13 15:23:24 +01:00
parent cc5498a42a
commit 1f708637b6
4 changed files with 25 additions and 3 deletions

View File

@ -61,7 +61,7 @@ public class WbQuantityExpr implements WbExpression<QuantityValue> {
@Override @Override
public QuantityValue evaluate(ExpressionContext ctxt) public QuantityValue evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException { throws SkipSchemaExpressionException {
StringValue amount = getLanguageExpr().evaluate(ctxt); StringValue amount = getAmountExpr().evaluate(ctxt);
// we know the amount is nonnull, nonempty here // we know the amount is nonnull, nonempty here
BigDecimal parsedAmount = null; BigDecimal parsedAmount = null;
@ -99,7 +99,7 @@ public class WbQuantityExpr implements WbExpression<QuantityValue> {
} }
@JsonProperty("amount") @JsonProperty("amount")
public WbExpression<? extends StringValue> getLanguageExpr() { public WbExpression<? extends StringValue> getAmountExpr() {
return amountExpr; return amountExpr;
} }

View File

@ -59,7 +59,7 @@ public class WbStringVariable extends WbVariableExpr<StringValue> {
if (!cell.value.toString().isEmpty()) { if (!cell.value.toString().isEmpty()) {
String stringValue = cell.value.toString(); String stringValue = cell.value.toString();
if (cell.value instanceof Double && ((Double)cell.value) % 1 == 0) { if (cell.value instanceof Double && ((Double)cell.value) % 1 == 0) {
stringValue = Integer.toString(((Double)cell.value).intValue()); stringValue = Long.toString(((Double)cell.value).longValue());
} }
return Datamodel.makeStringValue(stringValue.trim()); return Datamodel.makeStringValue(stringValue.trim());
} }

View File

@ -46,6 +46,12 @@ public class WbQuantityExprTest extends WbExpressionTest<QuantityValue> {
setRow("4.00"); setRow("4.00");
evaluatesTo(Datamodel.makeQuantityValue(new BigDecimal("4.00"), null, null, "1"), exprWithoutUnit); evaluatesTo(Datamodel.makeQuantityValue(new BigDecimal("4.00"), null, null, "1"), exprWithoutUnit);
} }
@Test
public void testOverflow() {
setRow(14341937500d);
evaluatesTo(Datamodel.makeQuantityValue(new BigDecimal("14341937500"), null, null, "1"), exprWithoutUnit);
}
@Test @Test
public void testInvalidAmountWithoutUnit() { public void testInvalidAmountWithoutUnit() {

View File

@ -69,6 +69,22 @@ public class WbStringVariableTest extends WbVariableTest<StringValue> {
public void testDoubleInteger() { public void testDoubleInteger() {
evaluatesTo(Datamodel.makeStringValue("45"), new Cell(45.0,null)); evaluatesTo(Datamodel.makeStringValue("45"), new Cell(45.0,null));
} }
/**
* Test that large doubles are correctly converted to strings
*/
@Test
public void testLargeDouble() {
evaluatesTo(Datamodel.makeStringValue("14341937500"), new Cell(14341937500d, null));
}
/**
* Test that large doubles are correctly converted to strings
*/
@Test
public void testLong() {
evaluatesTo(Datamodel.makeStringValue("14341937500"), new Cell(14341937500L, null));
}
@Test @Test