View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.faces.convert;
21  
22  import org.apache.shale.test.base.AbstractJsfTestCase;
23  
24  import javax.faces.component.UIInput;
25  import javax.faces.context.FacesContext;
26  import java.util.Locale;
27  
28  public class NumberConverterTest extends AbstractJsfTestCase
29  {
30      private NumberConverter mock;
31  
32      public static void main(String[] args)
33      {
34          junit.textui.TestRunner.run(NumberConverterTest.class);
35      }
36  
37      public NumberConverterTest(String name)
38      {
39          super(name);
40      }
41  
42      protected void setUp() throws Exception
43      {
44          super.setUp();
45  
46          mock = new NumberConverter();
47      }
48  
49      protected void tearDown() throws Exception
50      {
51          super.tearDown();
52  
53          mock = null;
54      }
55      /*
56       * temporarily comment out tests that fail, until Matthias Wessendorf has time to investigate
57       */
58      public void testFranceLocaleWithNonBreakingSpace()
59      {
60          mock.setLocale(Locale.FRANCE);
61          FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.GERMANY);
62          UIInput input = new UIInput();
63          mock.setType("currency");
64          String stringValue = mock.getAsString(facesContext, input, new Double(12345.68d));
65          Number number = (Number) mock.getAsObject(FacesContext.getCurrentInstance(), input, "12\u00a0345,68 \u20AC");
66          assertNotNull(number);
67      }
68      public void testFranceLocaleWithoutNonBreakingSpace()
69      {
70          mock.setLocale(Locale.FRANCE);
71          FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.GERMANY);
72          UIInput input = new UIInput();
73          mock.setType("currency");
74          Number number = (Number) mock.getAsObject(FacesContext.getCurrentInstance(), input, "12 345,68 \u20AC");
75          assertNotNull(number);
76      }
77      
78      /**
79       * EUR12,345.68 
80       */
81      public void testUSLocaleUsingEURCurrencyCode()
82      {
83          facesContext.getViewRoot().setLocale(Locale.US);
84          mock.setLocale(Locale.US);
85          UIInput input = new UIInput();
86          mock.setType("currency");
87          mock.setCurrencyCode("EUR");
88          Number testValue = 12345.68d;
89          String stringValue = mock.getAsString(facesContext, input, testValue);
90          Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
91          assertNotNull(number);
92          assertEquals(testValue, number);
93      }
94  
95      /**
96       * �12,345.68
97       */
98      public void testUKLocaleUsingEURCurrencyCode()
99      {
100         facesContext.getViewRoot().setLocale(Locale.US);
101         mock.setLocale(Locale.UK);
102         UIInput input = new UIInput();
103         mock.setType("currency");
104         mock.setCurrencyCode("EUR");
105         Number testValue = 12345.68d;
106         String stringValue = mock.getAsString(facesContext, input, testValue);
107         Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
108         assertNotNull(number);
109         assertEquals(testValue, number);
110     }
111     
112     /**
113      * 12.345,68 �
114      */
115     public void testGermanyLocaleUsingEURCurrencyCode()
116     {
117         facesContext.getViewRoot().setLocale(Locale.US);
118         mock.setLocale(Locale.GERMANY);
119         UIInput input = new UIInput();
120         mock.setType("currency");
121         mock.setCurrencyCode("EUR");
122         Number testValue = 12345.68d;
123         String stringValue = mock.getAsString(facesContext, input, testValue);
124         Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
125         assertNotNull(number);
126         assertEquals(testValue, number);
127     }
128     
129     public void testCurrencyPattern()
130     {
131         facesContext.getViewRoot().setLocale(Locale.US);
132         mock.setLocale(Locale.US);
133         UIInput input = new UIInput();
134         mock.setPattern("\u00A4 ###,###.###");
135         Number testValue = 12345.68d;
136         String stringValue = mock.getAsString(facesContext, input, testValue);
137         Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
138         assertNotNull(number);
139         assertEquals(testValue, number);        
140     }
141 
142     public void testCurrencyPattern2()
143     {
144         facesContext.getViewRoot().setLocale(Locale.US);
145         mock.setLocale(Locale.GERMANY);
146         UIInput input = new UIInput();
147         mock.setPattern("\u00A4 ###,###.###");
148         mock.setCurrencyCode("USD"); //Since currency is �, but we are using USD currency code, the output is USD 12.345,68
149         Number testValue = 12345.68d;
150         String stringValue = mock.getAsString(facesContext, input, testValue);
151         Number number = (Number) mock.getAsObject(facesContext, input, stringValue);
152         assertNotNull(number);
153         assertEquals(testValue, number);        
154     }
155 
156 }