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.util.Locale;
22  
23  import javax.faces.component.StateHolder;
24  import javax.faces.convert.Converter;
25  import javax.faces.convert.NumberConverter;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
31  import org.apache.shale.test.mock.MockFacesContext;
32  
33  /**
34   * Test JSF NumberConverter
35   * @version $Name: $ ($version: $) $Date: 16-aug-2005.15:12:23 $
36   */
37  public class JsfNumberConverterTest extends NumberConverterTestCase
38  {
39    public JsfNumberConverterTest(String name)
40    {
41      super(name);
42    }
43  
44    @Override
45    protected void setUp() throws Exception
46    {
47      super.setUp();
48    }
49    
50    @Override
51    protected void tearDown() throws Exception
52    {
53      super.tearDown();
54    }
55    
56    public static Test suite()
57    {
58      return new TestSuite(JsfNumberConverterTest.class);
59    }
60    
61    @Override
62    protected NumberConverter getNumberConverter()
63    {
64      return new NumberConverter();
65    }
66  
67    @Override
68    protected void doTestStateHolderSaveRestore(
69      Converter conv1,
70      Converter conv2,
71      MockFacesContext context,
72      MockUIComponentWrapper wrapper
73      )
74    {
75      Object state = ((StateHolder)conv1).saveState(context);
76  
77      ((StateHolder)conv2).restoreState(context, state);
78  
79      boolean isEqual =  _isConvertersEqual((NumberConverter)conv1,
80                                             (NumberConverter)conv2);
81  
82      assertEquals(true, isEqual);
83      wrapper.getMock().verify();
84    }
85  
86    // This is a bad equals comparison. But this is max we can do.
87    private boolean _isConvertersEqual(
88      NumberConverter conv1,
89      NumberConverter conv2)
90    {
91      return
92      (
93        conv1.getMaxFractionDigits() == conv2.getMaxFractionDigits()  &&
94        conv1.getMaxIntegerDigits()  == conv2.getMaxIntegerDigits()   &&
95        conv1.getMinFractionDigits() ==  conv2.getMinFractionDigits() &&
96        conv1.getMinIntegerDigits()  ==  conv2.getMinIntegerDigits()  &&
97        conv1.isTransient() == conv2.isTransient() &&
98        conv1.isGroupingUsed() ==  conv2.isGroupingUsed() &&
99        conv1.isIntegerOnly()  == conv2.isIntegerOnly()   &&
100       equals(conv1.getType(), conv2.getType()) &&
101       equals(conv1.getLocale(), conv2.getLocale()) &&
102       equals(conv1.getCurrencyCode(), conv2.getCurrencyCode()) &&
103       equals(conv1.getCurrencySymbol(), conv2.getCurrencySymbol()) &&
104       equals(conv1.getPattern(), conv2.getPattern())
105     );
106   }
107 
108   @Override
109   protected void doTestEquals(
110     Converter conv1,
111     Converter conv2,
112     boolean expectedOutCome
113     )
114   {
115     NumberConverter nConv1, nConv2;
116     nConv1 = (NumberConverter) conv1;
117     nConv2 = (NumberConverter) conv2;
118     boolean isEqual =  _isConvertersEqual(nConv1, nConv2);
119     assertEquals(expectedOutCome, isEqual);
120   }
121 
122   @Override
123    protected void doTestStrictNess(
124     MockFacesContext context,
125     MockUIComponentWrapper wrapper,
126     Locale locale,
127     String inputValue)
128   {
129      NumberConverter converter = getNumberConverter();
130      converter.setLocale(locale);
131      Object obj = converter.getAsObject(context, wrapper.getUIComponent(), inputValue);
132      // JSF Converter is lenient - so it will get parsed to a valid object
133      assertEquals(true, (obj != null));
134   };
135 
136 }