1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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
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");
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 }