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.util.Date;
22  
23  import javax.faces.convert.DateTimeConverter;
24  import javax.faces.validator.ValidatorException;
25  import javax.faces.component.UIComponent;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
31  import org.apache.shale.test.mock.MockApplication;
32  import org.jmock.Mock;
33  
34  /**
35   * Unit tests for DateTimeRangeValidator.
36   *
37   */
38  public class DateTimeRangeValidatorTest extends ValidatorTestCase
39  {
40    /**
41     * Creates a new DateTimeRangeValidatorTest.
42     *
43     * @param testName  the unit test name
44     */
45    public DateTimeRangeValidatorTest(
46      String testName)
47    {
48      super(testName);
49    }
50    
51    @Override
52    protected void setUp() throws Exception
53    {
54      super.setUp();
55    }
56    
57    @Override
58    protected void tearDown() throws Exception
59    {
60      super.tearDown();
61    }
62    
63    public static Test suite()
64    {
65      return new TestSuite(DateTimeRangeValidatorTest.class);
66    }
67  
68    /**
69     * Tests that null returns immediately.
70     *
71     * @throws ValidatorException  when test fails
72     */
73    public void testNull() throws ValidatorException
74    {
75      DateTimeRangeValidator validator = new DateTimeRangeValidator();
76  
77      Mock mock = buildMockUIComponent();
78      UIComponent component = (UIComponent) mock.proxy();
79      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
80      doTestNull(facesContext, wrapper, validator);
81    }
82  
83     /**
84     * Test when context is set to null
85     */
86    public void testNullContext()
87    {
88      Mock mock = buildMockUIComponent();
89      UIComponent component = (UIComponent) mock.proxy();
90      MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
91      DateTimeRangeValidator validator = new DateTimeRangeValidator();
92  
93      doTestNullContext(wrapper, validator);
94    }
95  
96    /**
97     * Test null value for component.
98     */
99    public void testNullComponent()
100   {
101     DateTimeRangeValidator validator = new DateTimeRangeValidator();
102 
103     doTestNullComponent(facesContext, validator);
104   }
105 
106   /**
107    * Tests that non Date objects throw a ValidationException.
108    */
109   public void testNonDate()
110   {
111     DateTimeRangeValidator validator = new DateTimeRangeValidator();
112 
113     Mock mock = buildMockUIComponent();
114     UIComponent component = (UIComponent) mock.proxy();
115     
116     mock.stubs().method("getId").will(returnValue("test"));
117     try
118     {
119       setFacesContext(facesContext);
120       validator.validate(facesContext, component, "not-a-date");
121       fail("ValidatorException not thrown");
122     }
123     catch (IllegalArgumentException iae)
124     {
125       // pass
126     }
127     finally
128     {
129       setFacesContext(null);
130     }
131     mock.verify();
132   }
133 
134   /**
135    * Tests that dates before the maximum date are valid.
136    *
137    * @throws ValidatorException  when test fails
138    */
139   public void testBeforeMaximumDate() throws ValidatorException
140   {
141     long millis = System.currentTimeMillis();
142     DateTimeRangeValidator validator = new DateTimeRangeValidator();
143     validator.setMaximum(new Date(millis));
144 
145     Mock mock = buildMockUIComponent();
146     UIComponent component = (UIComponent) mock.proxy();
147 
148     try
149     {
150       setFacesContext(facesContext);
151       validator.validate(facesContext, component, new Date(millis - 1));
152     }
153     finally
154     {
155       setFacesContext(null);
156     }
157     mock.verify();
158   }
159 
160   protected void setMockCreateConverter(MockApplication app)
161   {
162     DateTimeConverter dtc = new DateTimeConverter();
163     app.addConverter(DateTimeConverter.CONVERTER_ID, dtc.getClass().getName());
164   }
165 
166   /**
167    * Tests that dates after the maximum date cause a ValidationException.
168    */
169   public void testAfterMaximumDate()
170   {
171     long millis = System.currentTimeMillis();
172     DateTimeRangeValidator validator = new DateTimeRangeValidator();
173     validator.setMaximum(new Date(millis));
174 
175     Mock mock = buildMockUIComponent();
176     UIComponent component = (UIComponent) mock.proxy();
177     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
178     setMockLabelForComponent(wrapper);
179 
180     setMockCreateConverter(application);
181     try
182     {
183       setFacesContext(facesContext);
184       validator.setMessageDetailMaximum("max set");
185       validator.validate(facesContext, component, new Date(millis + 1));
186       fail("ValidatorException not thrown");
187     }
188     catch (ValidatorException e)
189     {
190       // pass
191       String msg = e.getFacesMessage().getDetail();
192       assertEquals(msg, "max set");
193     }
194     finally
195     {
196       setFacesContext(null);
197     }
198 
199     mock.verify();
200   }
201 
202   /**
203    * Tests that dates after the minimum date are valid.
204    *
205    * @throws ValidatorException  when test fails
206    */
207   public void testAfterMinimumDate() throws ValidatorException
208   {
209     long millis = System.currentTimeMillis();
210     DateTimeRangeValidator validator = new DateTimeRangeValidator();
211     validator.setMinimum(new Date(millis));
212 
213     Mock mock = buildMockUIComponent();
214     UIComponent component = (UIComponent) mock.proxy();
215 
216     validator.validate(facesContext, component, new Date(millis + 1));
217 
218     mock.verify();
219   }
220 
221   /**
222    * Tests that dates before the minimum date cause a ValidationException.
223    */
224   public void testBeforeMinimumDate()
225   {
226     long millis = System.currentTimeMillis();
227     DateTimeRangeValidator validator = new DateTimeRangeValidator();
228     validator.setMinimum(new Date(millis));
229 
230     Mock mock = buildMockUIComponent();
231     UIComponent component = (UIComponent) mock.proxy();
232     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
233     setMockLabelForComponent(wrapper);
234     
235     setMockCreateConverter(application);
236 
237     try
238     {
239       setFacesContext(facesContext);
240       validator.setMessageDetailMinimum("min set");
241       validator.validate(facesContext, component, new Date(millis - 1));
242       fail("ValidatorException not thrown");
243     }
244     catch (ValidatorException e)
245     {
246       // pass
247       String msg = e.getFacesMessage().getDetail();
248       assertEquals(msg, "min set");
249     }
250     finally
251     {
252       setFacesContext(null);
253     }
254 
255     mock.verify();
256   }
257 
258   /**
259    * Tests that dates within the date range are valid.
260    *
261    * @throws ValidatorException  when test fails
262    */
263   public void testWithinDateRange() throws ValidatorException
264   {
265     long millis = System.currentTimeMillis();
266     DateTimeRangeValidator validator = new DateTimeRangeValidator();
267     validator.setMinimum(new Date(millis));
268     validator.setMaximum(new Date(millis + 2));
269 
270     Mock mock = buildMockUIComponent();
271     UIComponent component = (UIComponent) mock.proxy();
272 
273     try
274     {
275       setFacesContext(facesContext);
276       validator.validate(facesContext, component, new Date(millis + 1));
277     }
278     finally
279     {
280       setFacesContext(null);
281     }
282 
283     mock.verify();
284   }
285 
286   /**
287    * Tests that dates before the date range cause a ValidationException.
288    */
289   public void testBeforeDateRange()
290   {
291     long millis = System.currentTimeMillis();
292     DateTimeRangeValidator validator = new DateTimeRangeValidator();
293     validator.setMinimum(new Date(millis));
294     validator.setMaximum(new Date(millis + 10));
295 
296     Mock mock = buildMockUIComponent();
297     UIComponent component = (UIComponent) mock.proxy();
298     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
299     setMockLabelForComponent(wrapper);
300 
301     setMockCreateConverter(application);
302 
303     try
304     {
305       setFacesContext(facesContext);
306       validator.setMessageDetailMinimum("min");
307       validator.validate(facesContext, component, new Date(millis - 1));
308       fail("ValidatorException not thrown");
309     }
310     catch (ValidatorException e)
311     {
312       // pass
313 
314     }
315     finally
316     {
317       setFacesContext(null);
318     }
319 
320     mock.verify();
321   }
322 
323   /**
324    * Tests that dates after the date range cause a ValidationException.
325    */
326   public void testAfterDateRange()
327   {
328     long millis = System.currentTimeMillis();
329     DateTimeRangeValidator validator = new DateTimeRangeValidator();
330     validator.setMinimum(new Date(millis));
331     validator.setMaximum(new Date(millis + 10));
332 
333     Mock mock = buildMockUIComponent();
334     UIComponent component = (UIComponent) mock.proxy();
335     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
336     setMockLabelForComponent(wrapper);
337 
338     setMockCreateConverter(application);
339 
340     try
341     {
342       setFacesContext(facesContext);
343       validator.setMessageDetailNotInRange("not in range is set");
344       validator.validate(facesContext, component, new Date(millis + 20));
345       fail("ValidatorException not thrown");
346     }
347     catch (ValidatorException e)
348     {
349       //first pass
350       String msg = e.getFacesMessage().getDetail();
351       assertEquals(msg, "not in range is set");
352     }
353     finally
354     {
355       setFacesContext(null);
356     }
357 
358     mock.verify();
359   }
360 
361 
362   /**
363    * Tests that dates after the date range cause a ValidationException.
364    */
365   public void testStateHolderSaveRestore()
366   {
367     long millis = System.currentTimeMillis();
368     DateTimeRangeValidator originalValidator = new DateTimeRangeValidator();
369     originalValidator.setMinimum(new Date(millis));
370     originalValidator.setMaximum(new Date(millis + 10));
371 
372     originalValidator.setMessageDetailMinimum("min");
373     originalValidator.setMessageDetailMaximum("max");
374     originalValidator.setMessageDetailNotInRange("not in range");
375 
376     Mock mock = buildMockUIComponent();
377     UIComponent component = (UIComponent) mock.proxy();
378     MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
379 
380     DateTimeRangeValidator restoredValidator = new DateTimeRangeValidator();
381 
382     doTestStateHolderSaveRestore(originalValidator, restoredValidator,
383                                  facesContext, wrapper);
384   }
385 
386 }