WW-3171 Uses proper number of digits when formatting double
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/229afea6 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/229afea6 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/229afea6 Branch: refs/heads/master Commit: 229afea64e77c2dba9eec62b2c339e9fc92c9ec7 Parents: 20eced9 Author: Lukasz Lenart <[email protected]> Authored: Fri May 12 15:58:54 2017 +0200 Committer: Lukasz Lenart <[email protected]> Committed: Fri May 12 15:58:54 2017 +0200 ---------------------------------------------------------------------- .../xwork2/conversion/impl/StringConverter.java | 10 ++-- .../conversion/impl/StringConverterTest.java | 52 ++++++++++++++++++++ .../DoubleRangeFieldValidatorTest.java | 12 ++--- .../apache/struts2/views/jsp/ui/Select-3.txt | 4 +- 4 files changed, 67 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/229afea6/core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java index 5ffa18e..61d2516 100644 --- a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java +++ b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java @@ -4,9 +4,8 @@ import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Array; import java.lang.reflect.Member; +import java.math.BigDecimal; import java.text.DateFormat; -import java.text.Format; -import java.text.MessageFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -62,11 +61,16 @@ public class StringConverter extends DefaultTypeConverter { } protected String convertToString(Locale locale, Object value) { - if (value.getClass().isAssignableFrom(Number.class)) { + if (Number.class.isInstance(value)) { NumberFormat format = NumberFormat.getNumberInstance(locale); + format.setGroupingUsed(false); + if (Double.class.isInstance(value) || BigDecimal.class.isInstance(value)) { + format.setMinimumFractionDigits(1); + } return format.format(value); } else { return String.valueOf(value); } } + } http://git-wip-us.apache.org/repos/asf/struts/blob/229afea6/core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java b/core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java new file mode 100644 index 0000000..c275669 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java @@ -0,0 +1,52 @@ +package com.opensymphony.xwork2.conversion.impl; + +import com.opensymphony.xwork2.ActionContext; +import org.apache.struts2.StrutsInternalTestCase; + +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +public class StringConverterTest extends StrutsInternalTestCase { + + public void testIntegerToStringConversionPL() throws Exception { + // given + StringConverter converter = new StringConverter(); + Map<String, Object> context = new HashMap<>(); + context.put(ActionContext.LOCALE, new Locale("pl", "PL")); + + // when + Object value = converter.convertValue(context, null, null, null, 234, null); + + // then + assertEquals("234", value); + } + + public void testDoubleToStringConversionPL() throws Exception { + // given + StringConverter converter = new StringConverter(); + Map<String, Object> context = new HashMap<>(); + context.put(ActionContext.LOCALE, new Locale("pl", "PL")); + + // when + Object value = converter.convertValue(context, null, null, null, 234.12, null); + + // then + assertEquals("234,12", value); + } + + public void testBigDecimalToStringConversionPL() throws Exception { + // given + StringConverter converter = new StringConverter(); + Map<String, Object> context = new HashMap<>(); + context.put(ActionContext.LOCALE, new Locale("pl", "PL")); + + // when + Object value = converter.convertValue(context, null, null, null, BigDecimal.valueOf(234.12), null); + + // then + assertEquals("234,12", value); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts/blob/229afea6/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java index 68b1a82..8969696 100644 --- a/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java @@ -36,7 +36,7 @@ public class DoubleRangeFieldValidatorTest extends XWorkTestCase { //Explicitly set an out-of-range double for DoubleRangeValidatorTest Map<String, Object> context = new HashMap<>(); HashMap<String, Object> params = new HashMap<>(); - params.put("percentage", 100.0123d); + params.put("percentage", 100.12); context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, context); @@ -50,8 +50,8 @@ public class DoubleRangeFieldValidatorTest extends XWorkTestCase { assertEquals(1, errorMessages.size()); String errorMessage = errorMessages.get(0); - assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage); - assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage); + assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage); + assertEquals("percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage); } public void testRangeValidationNoError() throws Exception { @@ -192,7 +192,7 @@ public class DoubleRangeFieldValidatorTest extends XWorkTestCase { //Explicitly set an out-of-range double for DoubleRangeValidatorTest Map<String, Object> context = new HashMap<>(); HashMap<String, Object> params = new HashMap<>(); - params.put("percentage", 100.0123d); + params.put("percentage", 100.12); context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.EXPRESSION_VALIDATION_ACTION, null, context); @@ -205,8 +205,8 @@ public class DoubleRangeFieldValidatorTest extends XWorkTestCase { assertEquals(1, errorMessages.size()); String errorMessage = errorMessages.get(0); - assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage); - assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage); + assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage); + assertEquals("percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage); } public void testExpressionParams() throws Exception { http://git-wip-us.apache.org/repos/asf/struts/blob/229afea6/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-3.txt ---------------------------------------------------------------------- diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-3.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-3.txt index 91f11f6..4ebbd30 100644 --- a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-3.txt +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-3.txt @@ -1,8 +1,8 @@ <tr> <td class="tdLabel"><label for="collection" class="label">mylabel:</label></td> <td class="tdInput"><select name="collection" id="collection" title="mytitle" multiple="multiple" onmousedown="alert('onmousedown');" onmouseup="alert('onmouseup');" onmouseover="alert('onmouseover');" onmousemove="alert('onmousemove');" onmouseout="alert('onmouseout');"> - <option value="hello" selected="selected">1</option> - <option value="foo" selected="selected">2</option> + <option value="hello" selected="selected">1.0</option> + <option value="foo" selected="selected">2.0</option> <option value="<cat>">1.5</option> </select> <inputtype="hidden" id="__multiselect_collection" name="__multiselect_collection" value=""/>
