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.validator;
20  
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  
25  import javax.faces.application.FacesMessage;
26  import javax.faces.convert.DateTimeConverter;
27  import javax.faces.validator.ValidatorException;
28  import javax.faces.component.UIComponent;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  
33  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
34  import org.apache.shale.test.mock.MockApplication;
35  import org.jmock.Mock;
36  
37  /**
38   * Unit tests for DateRestrictionValidator.
39   *
40   */
41  public class DateRestrictionValidatorTest extends ValidatorTestCase
42  {
43    /**
44     * Creates a new DateTimeRangeValidatorTest.
45     *
46     * @param testName  the unit test name
47     */
48    public DateRestrictionValidatorTest(
49      String testName)
50    {
51      super(testName);
52    }
53    
54    @Override
55    protected void setUp() throws Exception
56    {
57      super.setUp();
58    }
59    
60    @Override
61    protected void tearDown() throws Exception
62    {
63      super.tearDown();
64    }
65    
66    public static Test suite()
67    {
68      return new TestSuite(DateRestrictionValidatorTest.class);
69    }
70  
71    /**
72     * Tests that null returns immediately.
73     *
74     * @throws ValidatorException  when test fails
75     */
76    public void testNull() throws ValidatorException
77    {
78      DateRestrictionValidator validator = new DateRestrictionValidator();
79  
80      Mock mock = buildMockUIComponent();
81      UIComponent component = (UIComponent) mock.proxy();
82      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
83      doTestNull(facesContext, wrapper, validator);
84    }
85  
86     /**
87     * Test when context is set to null
88     */
89    public void testNullContext()
90    {
91      Mock mock = buildMockUIComponent();
92      UIComponent component = (UIComponent) mock.proxy();
93      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
94      DateRestrictionValidator validator = new DateRestrictionValidator();
95  
96      doTestNullContext(wrapper, validator);
97    }
98  
99    /**
100    * Test null value for component.
101    */
102   public void testNullComponent()
103   {
104     DateRestrictionValidator validator = new DateRestrictionValidator();
105 
106     doTestNullComponent(facesContext, validator);
107   }
108 
109   /**
110    * Tests that non Date objects throw a ValidationException.
111    */
112   public void testNonDate()
113   {
114     DateRestrictionValidator validator = new DateRestrictionValidator();
115 
116     Mock mock = buildMockUIComponent();
117     UIComponent component = (UIComponent) mock.proxy();
118     
119     mock.stubs().method("getId").will(returnValue("test"));
120     try
121     {
122       setFacesContext(facesContext);
123       validator.validate(facesContext, component, "not-a-date");
124       fail("ValidatorException not thrown");
125     }
126     catch (IllegalArgumentException iae)
127     {
128       // pass
129     }
130     finally
131     {
132       setFacesContext(null);
133     }
134     mock.verify();
135   }
136 
137   protected void setMockCreateConverter(MockApplication app)
138   {
139     DateTimeConverter dtc = new DateTimeConverter();
140     app.addConverter(DateTimeConverter.CONVERTER_ID, dtc.getClass().getName());
141   }
142   
143   /**
144    * Tests that invalid day, like Fri or Sun cause a ValidationException.
145    */
146   public void testInvalidDaysOfWeek()
147   {
148     DateRestrictionValidator validator = new DateRestrictionValidator();
149     validator.setInvalidDaysOfWeek(new String[] {"mon", "Sun"});
150 
151     Mock mock = buildMockUIComponent();
152     UIComponent component = (UIComponent) mock.proxy();
153     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
154     setMockLabelForComponent(wrapper);
155     
156     SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
157     Date dateToValidate = null;
158     
159     try
160     {
161       dateToValidate = sdf.parse("30.10.2006");
162     }
163     catch (ParseException e1)
164     {
165     }
166 
167     setMockCreateConverter(application);
168     try
169     {
170       setFacesContext(facesContext);
171       validator.validate(facesContext, component, dateToValidate);
172       fail("ValidatorException not thrown");
173     }
174     catch (ValidatorException e)
175     {
176       // pass
177 //      String msg = e.getFacesMessage().getDetail();
178 //      assertEquals(msg, "max set");
179       
180       FacesMessage msg = e.getFacesMessage();
181       assertNotNull(msg);
182       
183     }
184     finally
185     {
186       setFacesContext(null);
187     }
188   }
189 
190   /**
191    * Tests that invalid day, like Fri or Sun cause a ValidationException.
192    */
193   public void testInvalidMonth()
194   {
195     
196     DateRestrictionValidator validator = new DateRestrictionValidator();
197     validator.setInvalidMonths(new String[] {"Jan", "Oct"});
198 
199     Mock mock = buildMockUIComponent();
200     UIComponent component = (UIComponent) mock.proxy();
201     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
202     setMockLabelForComponent(wrapper);
203     
204     SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
205     Date dateToValidate = null;
206     
207     try
208     {
209       dateToValidate = sdf.parse("30.10.2006");
210     }
211     catch (ParseException e1)
212     {
213     }
214 
215     setMockCreateConverter(application);
216     try
217     {
218       setFacesContext(facesContext);
219       validator.validate(facesContext, component, dateToValidate);
220       fail("ValidatorException not thrown");
221     }
222     catch (ValidatorException e)
223     {
224       // pass
225 //      String msg = e.getFacesMessage().getDetail();
226 //      assertEquals(msg, "max set");
227       
228       FacesMessage msg = e.getFacesMessage();
229       assertNotNull(msg);
230       
231     }
232     finally
233     {
234       setFacesContext(null);
235     }
236   }
237 
238   /**
239    * Tests that invalid day, like Fri or Sun cause a ValidationException.
240    */
241   public void testInvalidDays()
242   {
243     
244     DateRestrictionValidator validator = new DateRestrictionValidator();
245     TestDateListProvider tdlp = new TestDateListProvider();
246     validator.setInvalidDays(tdlp);
247 
248     Mock mock = buildMockUIComponent();
249     UIComponent component = (UIComponent) mock.proxy();
250     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
251     setMockLabelForComponent(wrapper);
252     
253     SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
254     Date dateToValidate = null;
255     
256     try
257     {
258       dateToValidate = sdf.parse("15.05.2007");
259     }
260     catch (ParseException e1)
261     {
262     }
263 
264     setMockCreateConverter(application);
265     try
266     {
267       setFacesContext(facesContext);
268       validator.validate(facesContext, component, dateToValidate);
269       fail("ValidatorException not thrown");
270     }
271     catch (ValidatorException e)
272     {
273       // pass
274 //      String msg = e.getFacesMessage().getDetail();
275 //      assertEquals(msg, "max set");
276       
277       FacesMessage msg = e.getFacesMessage();
278       assertNotNull(msg);
279       
280     }
281     finally
282     {
283       setFacesContext(null);
284     }
285   }
286 
287   public void testStateHolderSaveRestore()
288   {
289     DateRestrictionValidator originalValidator = new DateRestrictionValidator();
290     
291     originalValidator.setInvalidDaysOfWeek(new String[]{"MO", "DI"});
292     originalValidator.setInvalidMonths(new String[]{"MO", "DI"});
293 
294     originalValidator.setMessageDetailInvalidDays("min");
295     originalValidator.setMessageDetailInvalidDaysOfWeek("max");
296     originalValidator.setMessageDetailInvalidMonths("not in range");
297 
298     Mock mock = buildMockUIComponent();
299     UIComponent component = (UIComponent) mock.proxy();
300     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
301 
302     DateRestrictionValidator restoredValidator = new DateRestrictionValidator();
303 
304     doTestStateHolderSaveRestore(originalValidator, restoredValidator,
305                                  facesContext, wrapper);
306   }
307 
308 }