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