KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > validator > TestMaxDate


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form.validator;
16
17 import java.util.Date JavaDoc;
18
19 import org.apache.tapestry.IMarkupWriter;
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.form.FormComponentContributorContext;
22 import org.apache.tapestry.form.IFormComponent;
23 import org.apache.tapestry.form.ValidationMessages;
24 import org.apache.tapestry.valid.ValidationConstraint;
25 import org.apache.tapestry.valid.ValidationStrings;
26 import org.apache.tapestry.valid.ValidatorException;
27
28 /**
29  * Tests for {@link org.apache.tapestry.form.validator.MaxDate}
30  *
31  * @author Howard Lewis Ship
32  * @since 4.0
33  */

34 public class TestMaxDate extends BaseValidatorTestCase
35 {
36     private static final long ONE_DAY = 24 * 60 * 60 * 1000l;
37
38     public void testOK() throws Exception JavaDoc
39     {
40         long now = System.currentTimeMillis();
41
42         Date JavaDoc today = new Date JavaDoc(now);
43         Date JavaDoc yesterday = new Date JavaDoc(now - ONE_DAY);
44
45         IFormComponent field = newField();
46         ValidationMessages message = newMessages();
47
48         replayControls();
49
50         MaxDate v = new MaxDate();
51         v.setMaxDate(today);
52
53         v.validate(field, message, yesterday);
54
55         verifyControls();
56     }
57
58     public void testFail() throws Exception JavaDoc
59     {
60         long now = System.currentTimeMillis();
61
62         Date JavaDoc today = new Date JavaDoc(now);
63         Date JavaDoc tomorrow = new Date JavaDoc(now + ONE_DAY);
64
65         IFormComponent field = newField("Fred");
66         ValidationMessages message = newMessages(
67                 null,
68                 ValidationStrings.DATE_TOO_LATE,
69                 new Object JavaDoc[]
70                 { "Fred", today },
71                 "default message");
72
73         replayControls();
74
75         MaxDate v = new MaxDate();
76         v.setMaxDate(today);
77
78         try
79         {
80             v.validate(field, message, tomorrow);
81             unreachable();
82         }
83         catch (ValidatorException ex)
84         {
85             assertEquals("default message", ex.getMessage());
86             assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint());
87         }
88
89         verifyControls();
90     }
91
92     public void testFailCustomMessage() throws Exception JavaDoc
93     {
94         long now = System.currentTimeMillis();
95
96         Date JavaDoc today = new Date JavaDoc(now);
97         Date JavaDoc tomorrow = new Date JavaDoc(now + ONE_DAY);
98
99         IFormComponent field = newField("Fred");
100         ValidationMessages message = newMessages(
101                 "custom",
102                 ValidationStrings.DATE_TOO_LATE,
103                 new Object JavaDoc[]
104                 { "Fred", today },
105                 "custom message");
106
107         replayControls();
108
109         MaxDate v = new MaxDate("message=custom");
110         v.setMaxDate(today);
111
112         try
113         {
114             v.validate(field, message, tomorrow);
115             unreachable();
116         }
117         catch (ValidatorException ex)
118         {
119             assertEquals("custom message", ex.getMessage());
120             assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint());
121         }
122
123         verifyControls();
124     }
125
126     public void testRenderComponentNoOp()
127     {
128         IMarkupWriter writer = newWriter();
129         IRequestCycle cycle = newCycle();
130         FormComponentContributorContext context = newContext();
131         IFormComponent field = newField();
132
133         replayControls();
134
135         new MaxDate().renderContribution(writer, cycle, context, field);
136
137         verifyControls();
138     }
139 }
140
Popular Tags