KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > translator > TestDateTranslator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form.translator;
16
17 import java.util.Calendar JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import org.apache.tapestry.valid.ValidationConstraint;
22 import org.apache.tapestry.valid.ValidatorException;
23
24 /**
25  * Test case for {@link DateTranslator}.
26  *
27  * @author Paul Ferraro
28  * @since 4.0
29  */

30 public class TestDateTranslator extends TranslatorTestCase
31 {
32     private DateTranslator _translator = new DateTranslator();
33     private Calendar JavaDoc _calendar = Calendar.getInstance();
34
35     /**
36      * @see junit.framework.TestCase#setUp()
37      */

38     protected void setUp() throws Exception JavaDoc
39     {
40         _calendar.clear();
41     }
42
43     private Date JavaDoc buildDate(int year, int month, int day)
44     {
45         _calendar.set(Calendar.YEAR, year);
46         _calendar.set(Calendar.MONTH, month);
47         _calendar.set(Calendar.DATE, day);
48         
49         return _calendar.getTime();
50     }
51     
52     public void testDefaultFormat()
53     {
54         testFormat(buildDate(1976, Calendar.OCTOBER, 29), "10/29/1976");
55     }
56     
57     public void testCustomFormat()
58     {
59         _translator.setPattern("yyyy-MM-dd");
60         
61         testFormat(buildDate(1976, Calendar.OCTOBER, 29), "1976-10-29");
62     }
63     
64     public void testFormat(Date JavaDoc date, String JavaDoc expected)
65     {
66         _component.getPage();
67         _componentControl.setReturnValue(_page);
68         
69         _page.getLocale();
70         _pageControl.setReturnValue(Locale.US);
71         
72         replay();
73         
74         String JavaDoc result = _translator.format(_component, date);
75         
76         assertEquals(expected, result);
77
78         verify();
79     }
80
81     public void testNullFormat()
82     {
83         replay();
84         
85         String JavaDoc result = _translator.format(_component, null);
86         
87         assertEquals("", result);
88
89         verify();
90     }
91
92     public void testDefaultParse()
93     {
94         testParse("10/29/1976", buildDate(1976, Calendar.OCTOBER, 29));
95     }
96     
97     public void testCustomParse()
98     {
99         _translator.setPattern("yyyy-MM-dd");
100         
101         testParse("1976-10-29", buildDate(1976, Calendar.OCTOBER, 29));
102     }
103     
104     public void testTrimmedParse()
105     {
106         _translator.setTrim(true);
107         
108         testParse(" 10/29/1976 ", buildDate(1976, Calendar.OCTOBER, 29));
109     }
110     
111     public void testEmptyParse()
112     {
113         replay();
114         
115         try
116         {
117             Date JavaDoc result = (Date JavaDoc) _translator.parse(_component, "");
118
119             assertEquals(null, result);
120         }
121         catch (ValidatorException e)
122         {
123             unreachable();
124         }
125         finally
126         {
127             verify();
128         }
129     }
130
131     private void testParse(String JavaDoc date, Date JavaDoc expected)
132     {
133         _component.getPage();
134         _componentControl.setReturnValue(_page);
135         
136         _page.getLocale();
137         _pageControl.setReturnValue(Locale.US);
138         
139         replay();
140         
141         try
142         {
143             Date JavaDoc result = (Date JavaDoc) _translator.parse(_component, date);
144
145             assertEquals(expected, result);
146         }
147         catch (ValidatorException e)
148         {
149             unreachable();
150         }
151         finally
152         {
153             verify();
154         }
155     }
156     
157     public void testFailedParseDefaultMessage()
158     {
159         testFailedParse("Invalid date format for Field Name. Format is MM/DD/YYYY.");
160     }
161     
162     public void testFailedParseCustomMessage()
163     {
164         String JavaDoc message = "Field Name is an invalid date.";
165         
166         _translator.setMessage(message);
167         
168         testFailedParse(message);
169     }
170
171     private void testFailedParse(String JavaDoc message)
172     {
173         _component.getPage();
174         _componentControl.setReturnValue(_page);
175
176         _page.getLocale();
177         _pageControl.setReturnValue(Locale.US);
178         
179         _component.getPage();
180         _componentControl.setReturnValue(_page);
181
182         _page.getLocale();
183         _pageControl.setReturnValue(Locale.US);
184         
185         _component.getDisplayName();
186         _componentControl.setReturnValue("Field Name");
187         
188         replay();
189         
190         try
191         {
192             System.out.println(_translator.parse(_component, "Bad-Date"));
193             
194             unreachable();
195         }
196         catch (ValidatorException e)
197         {
198             assertEquals(message, e.getMessage());
199             assertEquals(ValidationConstraint.DATE_FORMAT, e.getConstraint());
200         }
201         finally
202         {
203             verify();
204         }
205     }
206     
207     public void testRenderContribution()
208     {
209         replay();
210         
211         _translator.renderContribution(null, _cycle, null, _component);
212         
213         verify();
214     }
215     
216     public void testTrimRenderContribution()
217     {
218         _translator.setTrim(true);
219         trim();
220         
221         replay();
222         
223         _translator.renderContribution(null, _cycle, null, _component);
224         
225         verify();
226     }
227 }
228
Popular Tags