struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (WW-4871) Decimal converters should avoid loss of user's data caused by rounding
Date Thu, 09 Nov 2017 17:06:01 GMT

    [ https://issues.apache.org/jira/browse/WW-4871?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16246051#comment-16246051
] 

ASF GitHub Bot commented on WW-4871:
------------------------------------

lukaszlenart closed pull request #173: WW-4871 Fixes StringConverter and improves it's tests
URL: https://github.com/apache/struts/pull/173
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 d8895e8c2..97669438a 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
@@ -65,10 +65,15 @@ protected String convertToString(Locale locale, Object value) {
         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);
+            // TODO: delete this variable and corresponding if statement when jdk fixed java.text.NumberFormat.format's
behavior with Float
+            Object fixedValue = value;
+            if (BigDecimal.class.isInstance(value) || Double.class.isInstance(value) || Float.class.isInstance(value))
{
+                format.setMaximumFractionDigits(Integer.MAX_VALUE);
+                if (Float.class.isInstance(value)) {
+                    fixedValue = Double.valueOf(value.toString());
+                }
             }
-            return format.format(value);
+            return format.format(fixedValue);
         } else {
             return Objects.toString(value, null);
         }
diff --git a/core/src/test/java/com/opensymphony/xwork2/conversion/impl/NumberConverterTest.java
b/core/src/test/java/com/opensymphony/xwork2/conversion/impl/NumberConverterTest.java
index 4ffa684ac..27ea51aac 100644
--- a/core/src/test/java/com/opensymphony/xwork2/conversion/impl/NumberConverterTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/conversion/impl/NumberConverterTest.java
@@ -3,6 +3,7 @@
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.SimpleFooAction;
 import com.opensymphony.xwork2.XWorkTestCase;
+import org.apache.commons.lang3.StringUtils;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -48,11 +49,13 @@ public void testStringToBigDecimalConversionPL() throws Exception {
         Map<String, Object> context = new HashMap<>();
         context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
 
-        // when
-        Object value = converter.convertValue(context, null, null, null, "1234,4567", BigDecimal.class);
+        // when a bit bigger than double
+        String aBitBiggerThanDouble = "17976931348623157" + StringUtils.repeat('0', 291)
+ "1,"
+                + StringUtils.repeat('0', 324) + "49";
+        Object value = converter.convertValue(context, null, null, null, aBitBiggerThanDouble,
BigDecimal.class);
 
-        // then
-        assertEquals(BigDecimal.valueOf(1234.4567), value);
+        // then does not lose integer and fraction digits
+        assertEquals(new BigDecimal(aBitBiggerThanDouble.substring(0, 309) + "." + aBitBiggerThanDouble.substring(310)),
value);
     }
 
     public void testStringToBigDecimalConversionWithDotsPL() throws Exception {
@@ -87,11 +90,19 @@ public void testStringToDoubleConversionPL() throws Exception {
         Map<String, Object> context = new HashMap<>();
         context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
 
-        // when
-        Object value = converter.convertValue(context, null, null, null, "1234,4567", Double.class);
+        // when has max fraction digits
+        Object value = converter.convertValue(context, null, null, null,
+                "0," + StringUtils.repeat('0', 323) + "49", Double.class);
 
-        // then
-        assertEquals(1234.4567, value);
+        // then does not lose fraction digits
+        assertEquals(Double.MIN_VALUE, value);
+
+        // when has max integer digits
+        value = converter.convertValue(context, null, null, null,
+                "17976931348623157" + StringUtils.repeat('0', 292) + ",0", Double.class);
+
+        // then does not lose integer digits
+        assertEquals(Double.MAX_VALUE, value);
     }
 
     public void testStringToDoubleConversionWithDotsPL() throws Exception {
@@ -113,11 +124,19 @@ public void testStringToFloatConversionPL() throws Exception {
         Map<String, Object> context = new HashMap<>();
         context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
 
-        // when
-        Object value = converter.convertValue(context, null, null, null, "1234,4567", Float.class);
+        // when has max fraction digits
+        Object value = converter.convertValue(context, null, null, null,
+                "0," + StringUtils.repeat('0', 44) + "1401298464324817", Float.class);
 
-        // then
-        assertEquals(1234.4567F, value);
+        // then does not lose fraction digits
+        assertEquals(Float.MIN_VALUE, value);
+
+        // when has max integer digits
+        value = converter.convertValue(context, null, null, null,
+                "34028234663852886" + StringUtils.repeat('0', 22) + ",0", Float.class);
+
+        // then does not lose integer digits
+        assertEquals(Float.MAX_VALUE, value);
     }
 
     public void testStringToFloatConversionWithDotsPL() throws Exception {
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
index 27fbb6bc5..62f4ef2a2 100644
--- a/core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java
@@ -1,6 +1,7 @@
 package com.opensymphony.xwork2.conversion.impl;
 
 import com.opensymphony.xwork2.ActionContext;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.struts2.StrutsInternalTestCase;
 
 import java.math.BigDecimal;
@@ -17,10 +18,10 @@ public void testIntegerToStringConversionPL() throws Exception {
         context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
 
         // when
-        Object value = converter.convertValue(context, null, null, null, 234, null);
+        Object value = converter.convertValue(context, null, null, null, Integer.MIN_VALUE,
null);
 
         // then
-        assertEquals("234", value);
+        assertEquals("" + Integer.MIN_VALUE, value);
     }
 
     public void testDoubleToStringConversionPL() throws Exception {
@@ -29,11 +30,48 @@ public void testDoubleToStringConversionPL() throws Exception {
         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);
+        // when has max fraction digits
+        Object value = converter.convertValue(context, null, null, null, Double.MIN_VALUE,
null);
 
-        // then
-        assertEquals("234,12", value);
+        // then does not lose fraction digits
+        assertEquals("0," + StringUtils.repeat('0', 323) + "49", value);
+
+        // when has max integer digits
+        value = converter.convertValue(context, null, null, null, Double.MAX_VALUE, null);
+
+        // then does not lose integer digits
+        assertEquals("17976931348623157" + StringUtils.repeat('0', 292), value);
+
+        // when cannot be represented exactly with a finite binary number
+        value = converter.convertValue(context, null, null, null, 0.1d, null);
+
+        // then produce the shortest decimal representation that can unambiguously identify
the true value of the floating-point number
+        assertEquals("0,1", value);
+    }
+
+    public void testFloatToStringConversionPL() throws Exception {
+        // given
+        StringConverter converter = new StringConverter();
+        Map<String, Object> context = new HashMap<>();
+        context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
+
+        // when has max fraction digits
+        Object value = converter.convertValue(context, null, null, null, Float.MIN_VALUE,
null);
+
+        // then does not lose fraction digits
+        assertEquals("0," + StringUtils.repeat('0', 44) + "14", value);
+
+        // when has max integer digits
+        value = converter.convertValue(context, null, null, null, Float.MAX_VALUE, null);
+
+        // then does not lose integer digits
+        assertEquals("34028235" + StringUtils.repeat('0', 31), value);
+
+        // when cannot be represented exactly with a finite binary number
+        value = converter.convertValue(context, null, null, null, 0.1f, null);
+
+        // then produce the shortest decimal representation that can unambiguously identify
the true value of the floating-point number
+        assertEquals("0,1", value);
     }
 
     public void testBigDecimalToStringConversionPL() throws Exception {
@@ -42,11 +80,14 @@ public void testBigDecimalToStringConversionPL() throws Exception {
         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);
+        // when a bit bigger than double
+        String aBitBiggerThanDouble = "17976931348623157" + StringUtils.repeat('0', 291)
+ "1."
+                + StringUtils.repeat('0', 324) + "49";
+        Object value = converter.convertValue(context, null, null, null,
+                new BigDecimal(aBitBiggerThanDouble), null);
 
-        // then
-        assertEquals("234,12", value);
+        // then does not lose integer and fraction digits
+        assertEquals(aBitBiggerThanDouble.substring(0, 309) + "," + aBitBiggerThanDouble.substring(310),
value);
     }
 
     public void testStringArrayToStringConversion() {
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 8969696c4..0ab1740b6 100644
--- a/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java
@@ -269,7 +269,7 @@ public void testArrayOfDoubles() throws Exception {
         assertTrue(context.hasFieldErrors());
         assertEquals(1, context.getFieldErrors().size());
         assertEquals(1, context.getFieldErrors().get("doubleArray").size());
-        assertEquals("Value 15.0 not in scope!", context.getFieldErrors().get("doubleArray").get(0));
+        assertEquals("Value 15 not in scope!", context.getFieldErrors().get("doubleArray").get(0));
     }
 
     public void testCollectionOfDoubles() throws Exception {
@@ -290,7 +290,7 @@ public void testCollectionOfDoubles() throws Exception {
         assertTrue(context.hasFieldErrors());
         assertEquals(1, context.getFieldErrors().size());
         assertEquals(1, context.getFieldErrors().get("doubleCollection").size());
-        assertEquals("Value 15.0 not in scope!", context.getFieldErrors().get("doubleCollection").get(0));
+        assertEquals("Value 15 not in scope!", context.getFieldErrors().get("doubleCollection").get(0));
     }
 
     @Override
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 4ebbd3033..91f11f621 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.0</option>
-    <option value="foo" selected="selected">2.0</option>
+    <option value="hello" selected="selected">1</option>
+    <option value="foo" selected="selected">2</option>
     <option value="&lt;cat&gt;">1.5</option>
 </select>
 <inputtype="hidden" id="__multiselect_collection" name="__multiselect_collection" value=""/>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Decimal converters should avoid loss of user's data caused by rounding
> ----------------------------------------------------------------------
>
>                 Key: WW-4871
>                 URL: https://issues.apache.org/jira/browse/WW-4871
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 2.5.12, 2.5.13
>            Reporter: Yasser Zamani
>             Fix For: 2.5.14
>
>
> For example in following jsp:
> {code:xml}
> <s:form>
> <s:hidden name="myDecimal"/>
> <s:submit/>
> </s:form>
> {code}
> User loses decimal digits when posts back because of converter's locale based rounding.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message