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  package org.apache.myfaces.trinidad.convert;
20  
21  import java.text.DecimalFormatSymbols;
22  
23  import java.util.Locale;
24  
25  import javax.faces.convert.ConverterException;
26  import javax.faces.convert.NumberConverter;
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.UIViewRoot;
29  
30  import org.apache.myfaces.trinidad.context.MockRequestContext;
31  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
32  import org.apache.myfaces.trinidadbuild.test.MockFacesContext12;
33  import org.apache.shale.test.mock.MockFacesContext;
34  import org.jmock.Mock;
35  
36  import junit.framework.Test;
37  import junit.framework.TestSuite;
38  
39  /**
40   * Test Trinidad NumberConverter
41   * @version $Name: $ ($version: $) $Date: 16-aug-2005.15:12:23 $
42   */
43  public class TrinidadNumberConverterTest extends NumberConverterTestCase
44  {
45    public TrinidadNumberConverterTest(String name)
46    {
47      super(name);
48    }
49  
50    @Override
51    protected NumberConverter getNumberConverter()
52    {
53      return new org.apache.myfaces.trinidad.convert.NumberConverter();
54    }
55  
56    @Override
57    protected void setUp() throws Exception
58    {
59      super.setUp();
60      _mafct = new MockRequestContext();
61      _mafct.setDecimalSeparator('.');
62      _mafct.setNumberGroupingSeparator(',');
63      _mafct.setCurrencyCode(null);
64    }
65  
66    @Override
67    protected void tearDown() throws Exception
68    {
69      
70      // RequestContext uses a thread local variable to hold itself and has a
71      // check in it. So you need to release, since all instances for tests
72      // are created on the same thread by Junit.
73      _mafct.release();
74      _mafct = null;
75      super.tearDown();
76    }
77  
78    public static Test suite()
79    {
80      return new TestSuite(TrinidadNumberConverterTest.class);
81    }
82    
83    @Override
84    public void testCurrencyCodeIsHonoured()
85    {
86       DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
87      _mafct.setDecimalSeparator(symbols.getMonetaryDecimalSeparator());
88      _mafct.setNumberGroupingSeparator(symbols.getGroupingSeparator());
89      super.testCurrencyCodeIsHonoured();
90    }
91  
92  //  @Override
93  //  public void testFranceLocale()
94  //  {
95  //    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRANCE);
96  //    _mafct.setDecimalSeparator(symbols.getMonetaryDecimalSeparator());
97  //    _mafct.setNumberGroupingSeparator(symbols.getGroupingSeparator());
98  //  
99  //    super.testFranceLocale();
100 //  }
101   
102 
103   public void testValueSetInRequestContextIsHonoured()
104   {
105     //ugly ?
106     _mafct.release();
107     _mafct = null;
108     _mafct = new MockRequestContext();
109     _mafct.setDecimalSeparator('*');
110     _mafct.setNumberGroupingSeparator('!');
111     _mafct.setCurrencyCode(null);
112     Mock mock = mock(UIComponent.class);
113     UIComponent component = (UIComponent) mock.proxy();
114 
115     NumberConverter conv = getNumberConverter();
116 
117     conv.setLocale(Locale.US);
118     Number inputValue =  new Double(8989.789);
119     String out = conv.getAsString(facesContext, component, inputValue);
120     assertEquals("8!989*789", out);
121 
122     mock.verify();
123   }
124 
125   @Override
126   protected void doTestStrictNess(
127     MockFacesContext context,
128     MockUIComponentWrapper wrapper,
129     Locale locale,
130     String inputValue)
131   {
132      NumberConverter converter = getNumberConverter();
133      converter.setLocale(locale);
134      context.getViewRoot().setLocale(locale);
135      try
136      {
137        // ADF Converter is not lenient.
138        converter.getAsObject(context, wrapper.getUIComponent(), inputValue);
139        fail("Expected converter exception");
140      }
141      catch (ConverterException ce)
142      {
143        ; // We expected a exception to occur
144      }
145   }
146 
147   public void testCustomMessageIsSet()
148   {
149     String[] failingValues = {"222.22.2", "3,",       "23.3.3",   "10e.04"};
150     String[] types         = {"number",   "percent",  "currency", "pattern"};
151     String[] customMessage = {"number", "percent",    "currency", "pattern"};
152 
153     for (int i = 0; i < failingValues.length ; i++)
154     {
155       MockFacesContext12 context  = new MockFacesContext12(externalContext,
156                                                            lifecycle,
157                                                            application);
158 
159       try
160       {
161         Mock mock = buildMockUIComponent(3);
162         UIComponent component = (UIComponent) mock.proxy();
163         MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
164         
165         
166         org.apache.myfaces.trinidad.convert.NumberConverter converter =
167           new org.apache.myfaces.trinidad.convert.NumberConverter();
168         
169         UIViewRoot root = facesContext.getViewRoot();
170         root.setLocale(Locale.US);
171 
172         for (int j = 0; j < 3; j++)
173         {
174           context.setViewRoot(root);
175         }
176         
177         try
178         {
179           // ADF Converter is not lenient.
180           converter.setMessageDetailConvertNumber(customMessage[0]);
181           converter.setMessageDetailConvertPercent(customMessage[1]);
182           converter.setMessageDetailConvertCurrency(customMessage[2]);
183           converter.setMessageDetailConvertPattern(customMessage[3]);
184           
185           if ("pattern".equals(types[i]))
186             converter.setPattern("##.000");
187           else
188             converter.setType(types[i]);
189 
190           Object obj = converter.getAsObject(context, component, failingValues[i]);
191           fail("Expected converter exception");
192         }
193         catch (ConverterException ce)
194         {
195           // We expected a exception to occur
196           assertEquals(ce.getFacesMessage().getDetail(), customMessage[i]);
197         }
198       }
199       finally
200       {
201         context.release();
202       }
203     }
204   }
205 
206   private MockRequestContext _mafct;
207 
208   //checkForSettingsInRequestContext - dec sep, currencyCode, NumGrpSptr
209 }