You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2018/05/08 12:49:09 UTC

[myfaces] 10/29: MYFACES-2970 f:convertNumber conversion is not symmetric when currencyCode and currencySymbol are used

This is an automated email from the ASF dual-hosted git repository.

deki pushed a commit to branch 1.1.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git

commit 36ba98afea9e92981cfa01371fd7262e06856bb7
Author: Leonardo Uribe <lu...@apache.org>
AuthorDate: Thu Nov 11 16:15:43 2010 +0000

    MYFACES-2970 f:convertNumber conversion is not symmetric when currencyCode and currencySymbol are used
---
 .../java/javax/faces/convert/NumberConverter.java  |  28 ++++
 .../javax/faces/convert/NumberConverterTest.java   | 156 +++++++++++++++++++++
 2 files changed, 184 insertions(+)

diff --git a/api/src/main/java/javax/faces/convert/NumberConverter.java b/api/src/main/java/javax/faces/convert/NumberConverter.java
index aaa75fb..9f15576 100755
--- a/api/src/main/java/javax/faces/convert/NumberConverter.java
+++ b/api/src/main/java/javax/faces/convert/NumberConverter.java
@@ -98,15 +98,43 @@ public class NumberConverter
             {
                 NumberFormat format = getNumberFormat(facesContext);
                 format.setParseIntegerOnly(_integerOnly);
+                
+                DecimalFormat df = (DecimalFormat)format;
+                //df.setParseBigDecimal(true);
+                DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
+                boolean changed = false;
+                if(dfs.getGroupingSeparator() == '\u00a0')
+                {
+                  dfs.setGroupingSeparator(' ');
+                  df.setDecimalFormatSymbols(dfs);
+                  changed = true;
+                }
+                
+                formatCurrency(format);
+                
                 try
                 {
                     return format.parse(value);
                 }
                 catch (ParseException e)
                 {
+                  if(changed)
+                  {
+                    dfs.setGroupingSeparator('\u00a0');
+                    df.setDecimalFormatSymbols(dfs);
+                  }
+                  try
+                  {
+                    return format.parse(value);
+                  }
+                  catch (ParseException pe)
+                  {
+
                     throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
                                                                                CONVERSION_MESSAGE_ID,
                                                                                new Object[]{uiComponent.getId(),value}), e);
+
+                  }
                 }
             }
         }
diff --git a/api/src/test/java/javax/faces/convert/NumberConverterTest.java b/api/src/test/java/javax/faces/convert/NumberConverterTest.java
new file mode 100644
index 0000000..6ea48fe
--- /dev/null
+++ b/api/src/test/java/javax/faces/convert/NumberConverterTest.java
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package javax.faces.convert;
+
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+import javax.faces.component.UIInput;
+import javax.faces.context.FacesContext;
+import java.util.Locale;
+
+public class NumberConverterTest extends AbstractJsfTestCase
+{
+    private NumberConverter mock;
+
+    public static void main(String[] args)
+    {
+        junit.textui.TestRunner.run(NumberConverterTest.class);
+    }
+
+    public NumberConverterTest(String name)
+    {
+        super(name);
+    }
+
+    public void setUp()
+    {
+        super.setUp();
+
+        mock = new NumberConverter();
+    }
+
+    public void tearDown()
+    {
+        super.tearDown();
+
+        mock = null;
+    }
+    /*
+     * temporarily comment out tests that fail, until Matthias Wessendorf has time to investigate
+     */
+    public void testFranceLocaleWithNonBreakingSpace()
+    {
+        mock.setLocale(Locale.FRANCE);
+        FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.GERMANY);
+        UIInput input = new UIInput();
+        mock.setType("currency");
+        String stringValue = mock.getAsString(facesContext, input, new Double(12345.68d));
+        Number number = (Number) mock.getAsObject(FacesContext.getCurrentInstance(), input, "12\u00a0345,68 \u20AC");
+        assertNotNull(number);
+    }
+    public void testFranceLocaleWithoutNonBreakingSpace()
+    {
+        mock.setLocale(Locale.FRANCE);
+        FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.GERMANY);
+        UIInput input = new UIInput();
+        mock.setType("currency");
+        Number number = (Number) mock.getAsObject(FacesContext.getCurrentInstance(), input, "12 345,68 \u20AC");
+        assertNotNull(number);
+    }
+    
+    /**
+     * EUR12,345.68 
+     */
+    public void testUSLocaleUsingEURCurrencyCode()
+    {
+        facesContext.getViewRoot().setLocale(Locale.US);
+        mock.setLocale(Locale.US);
+        UIInput input = new UIInput();
+        mock.setType("currency");
+        mock.setCurrencyCode("EUR");
+        Number testValue = new Double(12345.68d);
+        String stringValue = mock.getAsString(facesContext, input, testValue);
+        Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
+        assertNotNull(number);
+        assertEquals(testValue, number);
+    }
+
+    /**
+     * �12,345.68
+     */
+    public void testUKLocaleUsingEURCurrencyCode()
+    {
+        facesContext.getViewRoot().setLocale(Locale.US);
+        mock.setLocale(Locale.UK);
+        UIInput input = new UIInput();
+        mock.setType("currency");
+        mock.setCurrencyCode("EUR");
+        Number testValue = new Double(12345.68d);
+        String stringValue = mock.getAsString(facesContext, input, testValue);
+        Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
+        assertNotNull(number);
+        assertEquals(testValue, number);
+    }
+    
+    /**
+     * 12.345,68 �
+     */
+    public void testGermanyLocaleUsingEURCurrencyCode()
+    {
+        facesContext.getViewRoot().setLocale(Locale.US);
+        mock.setLocale(Locale.GERMANY);
+        UIInput input = new UIInput();
+        mock.setType("currency");
+        mock.setCurrencyCode("EUR");
+        Number testValue = new Double(12345.68d);
+        String stringValue = mock.getAsString(facesContext, input, testValue);
+        Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
+        assertNotNull(number);
+        assertEquals(testValue, number);
+    }
+    
+    public void testCurrencyPattern()
+    {
+        facesContext.getViewRoot().setLocale(Locale.US);
+        mock.setLocale(Locale.US);
+        UIInput input = new UIInput();
+        mock.setPattern("\u00A4 ###,###.###");
+        Number testValue = new Double(12345.68d);
+        String stringValue = mock.getAsString(facesContext, input, testValue);
+        Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
+        assertNotNull(number);
+        assertEquals(testValue, number);        
+    }
+
+    public void testCurrencyPattern2()
+    {
+        facesContext.getViewRoot().setLocale(Locale.US);
+        mock.setLocale(Locale.GERMANY);
+        UIInput input = new UIInput();
+        mock.setPattern("\u00A4 ###,###.###");
+        mock.setCurrencyCode("USD"); //Since currency is �, but we are using USD currency code, the output is USD 12.345,68
+        Number testValue = new Double(12345.68d);
+        String stringValue = mock.getAsString(facesContext, input, testValue);
+        Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
+        assertNotNull(number);
+        assertEquals(testValue, number);        
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
deki@apache.org.