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 javax.faces.component.StateHolder;
22  import javax.faces.convert.Converter;
23  import javax.faces.convert.DateTimeConverter;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
29  import org.apache.shale.test.mock.MockFacesContext;
30  
31  public class JsfDateTimeConverterTest extends DateTimeConverterTestCase
32  {
33    public JsfDateTimeConverterTest(String name)
34    {
35      super(name);
36    }
37    
38    @Override
39    protected void setUp() throws Exception
40    {
41      super.setUp();
42    }
43    
44    @Override
45    protected void tearDown() throws Exception
46    {
47      super.tearDown();
48    }
49    
50    public static Test suite()
51    {
52      return new TestSuite(JsfDateTimeConverterTest.class);
53    }
54    
55    @Override
56    protected void doTestStateHolderSaveRestore(
57      Converter conv1,
58      Converter conv2,
59      MockFacesContext context,
60      MockUIComponentWrapper wrapper
61      )
62    {
63      DateTimeConverter dtConv1, dtConv2;
64      dtConv1 = (DateTimeConverter)conv1;
65      dtConv2 = (DateTimeConverter)conv2;
66  
67      Object state = ((StateHolder)conv1).saveState(context);
68  
69      ((StateHolder)conv2).restoreState(context, state);
70  
71      boolean isEqual =  _isConvertersEqual(dtConv1, dtConv2);
72  
73      assertEquals(true, isEqual);
74      wrapper.getMock().verify();
75    }
76  
77    @Override
78    protected void doTestEquals(
79      Converter conv1,
80      Converter conv2,
81      boolean expectedOutCome
82      )
83    {
84      DateTimeConverter dtConv1, dtConv2;
85      dtConv1 = (DateTimeConverter)conv1;
86      dtConv2 = (DateTimeConverter)conv2;
87      boolean isEqual =  _isConvertersEqual(dtConv1, dtConv2);
88      assertEquals(expectedOutCome, isEqual);
89    }
90  
91    @Override
92    protected DateTimeConverter getDateTimeConverter()
93    {
94      return new DateTimeConverter();
95    }
96  
97  
98    @Override
99    protected void setSecondaryPattern(
100     DateTimeConverter converter,
101     String secondaryPattern
102     )
103   {
104     // Do nothing there is nothing called secondary pattern in JSF world DTC
105     return;
106   }
107 
108   private boolean _equals(
109     Object o1,
110     Object o2
111     )
112   {
113     return ( o1 == o2 || (o1 != null && o1.equals(o2)));
114   }
115 
116   private boolean _isConvertersEqual(
117     DateTimeConverter conv1,
118     DateTimeConverter conv2
119     )
120   {
121     return
122       ( conv1.isTransient() == conv2.isTransient()
123          && _equals(conv1.getDateStyle(), conv2.getDateStyle())
124          && _equals(conv1.getLocale(), conv2.getLocale())
125          && _equals(conv1.getPattern(), conv2.getPattern())
126          && _equals(conv1.getTimeStyle(), conv2.getTimeStyle())
127          && _equals(conv1.getTimeZone(), conv2.getTimeZone())
128          && _equals(conv1.getType(), conv2.getType())
129       );
130   }
131 
132 }