1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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);
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);
104
105 dateRestrictionValidator.validate(facesContext, component, cal
106 .getTime());
107 fail("Expected ValidatorException");
108 }
109 catch (ValidatorException ve)
110 {
111
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);
121
122 dateRestrictionValidator.validate(facesContext, component, cal
123 .getTime());
124 fail("Expected ValidatorException");
125 }
126 catch (ValidatorException ve)
127 {
128
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
139 dateRestrictionValidator.validate(facesContext, component, cal
140 .getTime());
141 fail("Expected ValidatorException");
142 }
143 catch (ValidatorException ve)
144 {
145
146 }
147 }
148
149 }