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  
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.Date;
27  import java.util.Locale;
28  import java.util.TimeZone;
29  import java.text.SimpleDateFormat;
30  
31  import junit.framework.Test;
32  
33  public class DateTimeConverterTest extends AbstractJsfTestCase
34  {
35      private DateTimeConverter mock;
36  
37      public static void main(String[] args)
38      {
39          junit.textui.TestRunner.run(DateTimeConverterTest.class);
40      }
41  
42      public DateTimeConverterTest(String name)
43      {
44          super(name);
45      }
46  
47      protected void setUp() throws Exception
48      {
49          super.setUp();
50  
51          mock = new DateTimeConverter();
52          mock.setTimeZone(TimeZone.getDefault());
53          FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.GERMANY);
54  
55      }
56  
57      protected void tearDown() throws Exception
58      {
59          super.tearDown();
60  
61          mock = null;
62      }
63  
64      /*
65      * Test method for 'javax.faces.component.UIComponentBase.getAsObject()'
66      */
67      public void testGetAsObject()
68      {
69  
70          UIInput input = new UIInput();
71  
72          mock.setPattern("MM/dd/yyyy");
73  
74          //should trow a ConverterException
75          try
76          {
77              mock.getAsObject(FacesContext.getCurrentInstance(),input,"15/15/15");
78  
79              assertTrue("this date should not be parsable - and it is, so this is wrong.",false);
80          }
81          catch (ConverterException e)
82          {
83  
84          }
85  
86          //should not trow a ConverterException
87          try
88          {
89              Date date = (Date) mock.getAsObject(FacesContext.getCurrentInstance(),input,"12/01/01");
90  
91              SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy");
92              format.setTimeZone(TimeZone.getDefault());
93  
94              String str = format.format(date);
95  
96              assertEquals("12/01/01",str);
97  
98              format = new SimpleDateFormat("MM/dd/yyyy");
99              format.setTimeZone(TimeZone.getDefault());
100 
101             str = format.format(date);
102 
103             assertEquals("12/01/0001",str);            
104         }
105         catch (ConverterException e)
106         {
107             assertTrue("this date should not be parsable - and it is, so this is wrong.",false);
108         }
109     }
110 }