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