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.commons.validator;
20  
21  import java.util.ArrayList;
22  import java.util.Calendar;
23  import java.util.Date;
24  import java.util.GregorianCalendar;
25  import java.util.List;
26  
27  import javax.faces.context.FacesContext;
28  import javax.faces.validator.ValidatorException;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  
33  import org.apache.myfaces.commons.validator.model.DateListProvider;
34  
35  public class DateRestrictionValidatorTestCase extends AbstractValidatorTestCase
36  {
37  
38      public DateRestrictionValidatorTestCase(String arg0)
39      {
40          super(arg0);
41      }
42  
43      DateRestrictionValidator dateRestrictionValidator;
44  
45      private static final String[] invalidMonths = { "jan", "feb", "mar" };
46      private static final String[] invalidDaysOfWeek = { "sat", "sun" };
47      
48      
49      
50      public class CustomDateListProvider implements DateListProvider
51      {
52          public List<Date> getDateList(FacesContext context, Calendar base,
53                  Date rangeStart, Date rangeEnd)
54          {
55              List<Date> list = new ArrayList<Date>();            
56              Calendar cal = new GregorianCalendar();
57              cal.set(2008, 0, 2, 0, 1, 0);            
58              list.add(cal.getTime());            
59              return list;
60          }
61      }
62  
63      protected void setUp() throws Exception
64      {
65          super.setUp();
66          dateRestrictionValidator = new DateRestrictionValidator();
67          dateRestrictionValidator.setInvalidMonths(invalidMonths);
68          dateRestrictionValidator.setInvalidDaysOfWeek(invalidDaysOfWeek);
69          dateRestrictionValidator.setInvalidDays(new CustomDateListProvider());
70      }
71  
72      protected void tearDown() throws Exception
73      {
74          super.tearDown();
75      }
76  
77      public static Test suite()
78      {
79          return new TestSuite(DateRestrictionValidatorTestCase.class);
80      }
81  
82      /**
83       * Test when context is set to null
84       */
85      public void testNullContext()
86      {
87          doTestNullContext(component, dateRestrictionValidator);
88      }
89  
90      public void testGoodDateRestriction()
91      {
92          Calendar cal = new GregorianCalendar();
93          cal.set(2008, 6, 16, 0, 1, 0);//MONDAY  
94          dateRestrictionValidator.validate(facesContext, component, cal
95                  .getTime());
96      }
97  
98      public void testBadDayOfWeekRestriction()
99      {
100         try
101         {
102             Calendar cal = new GregorianCalendar();
103             cal.set(2008, 5, 15, 0, 1, 0);//SUNDAY JUNE 15 2008
104             //System.out.println(cal.get(Calendar.DAY_OF_WEEK)+" "+cal.getTime().toString());
105             dateRestrictionValidator.validate(facesContext, component, cal
106                     .getTime());
107             fail("Expected ValidatorException");
108         }
109         catch (ValidatorException ve)
110         {
111             // if exception then fine.
112         }
113     }
114     
115     public void testBadMonthRestriction()
116     {
117         try
118         {
119             Calendar cal = new GregorianCalendar();
120             cal.set(2008, 0, 15, 0, 1, 0);//JANUARY 15 2008
121             //System.out.println(cal.get(Calendar.DAY_OF_WEEK)+" "+cal.getTime().toString());
122             dateRestrictionValidator.validate(facesContext, component, cal
123                     .getTime());
124             fail("Expected ValidatorException");
125         }
126         catch (ValidatorException ve)
127         {
128             // if exception then fine.
129         }
130     }
131 
132     public void testBadDayRestriction()
133     {
134         try
135         {
136             Calendar cal = new GregorianCalendar();
137             cal.set(2008, 0, 2, 0, 1, 0);
138             //System.out.println(cal.get(Calendar.DAY_OF_WEEK)+" "+cal.getTime().toString());
139             dateRestrictionValidator.validate(facesContext, component, cal
140                     .getTime());
141             fail("Expected ValidatorException");
142         }
143         catch (ValidatorException ve)
144         {
145             // if exception then fine.
146         }
147     }
148     
149 }