KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tapestry.IMarkupWriter;
18 import org.apache.tapestry.IRequestCycle;
19 import org.apache.tapestry.form.FormComponentContributorContext;
20 import org.apache.tapestry.form.IFormComponent;
21 import org.apache.tapestry.form.ValidationMessages;
22 import org.apache.tapestry.valid.ValidationConstraint;
23 import org.apache.tapestry.valid.ValidationStrings;
24 import org.apache.tapestry.valid.ValidatorException;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.form.validator.Required}.
29  *
30  * @author Howard Lewis Ship
31  * @since 4.0
32  */

33 public class TestRequired extends BaseValidatorTestCase
34 {
35     public void testValidateNotNull() throws Exception JavaDoc
36     {
37         IFormComponent field = newField();
38         ValidationMessages messages = newMessages();
39
40         replayControls();
41
42         new Required().validate(field, messages, "not null");
43
44         verifyControls();
45     }
46
47     public void testValidateNull() throws Exception JavaDoc
48     {
49         IFormComponent field = newField("Fred");
50         ValidationMessages messages = newMessages(
51                 null,
52                 ValidationStrings.REQUIRED_TEXT_FIELD,
53                 new Object JavaDoc[]
54                 { "Fred" },
55                 "Default Message for Fred.");
56
57         replayControls();
58
59         try
60         {
61             new Required().validate(field, messages, null);
62             unreachable();
63         }
64         catch (ValidatorException ex)
65         {
66             assertEquals("Default Message for Fred.", ex.getMessage());
67             assertSame(ValidationConstraint.REQUIRED, ex.getConstraint());
68         }
69         
70         verifyControls();
71     }
72
73     public void testValidateNullCustomMessage() throws Exception JavaDoc
74     {
75         IFormComponent field = newField("Fred");
76         ValidationMessages messages = newMessages(
77                 "custom",
78                 ValidationStrings.REQUIRED_TEXT_FIELD,
79                 new Object JavaDoc[]
80                 { "Fred" },
81                 "Custom Message for Fred.");
82
83         replayControls();
84
85         try
86         {
87             Required required = new Required("message=custom");
88
89             required.validate(field, messages, null);
90             unreachable();
91         }
92         catch (ValidatorException ex)
93         {
94             assertEquals("Custom Message for Fred.", ex.getMessage());
95             assertSame(ValidationConstraint.REQUIRED, ex.getConstraint());
96         }
97         
98         verifyControls();
99     }
100
101     public void testRenderContribution()
102     {
103         IMarkupWriter writer = newWriter();
104         IRequestCycle cycle = newCycle();
105
106         MockControl contextc = newControl(FormComponentContributorContext.class);
107         FormComponentContributorContext context = (FormComponentContributorContext) contextc
108                 .getMock();
109
110         IFormComponent field = newField("Fred");
111
112         context.getFieldDOM();
113         contextc.setReturnValue("document.fred.barney");
114
115         trainFormatMessage(
116                 contextc,
117                 context,
118                 null,
119                 ValidationStrings.REQUIRED_TEXT_FIELD,
120                 new Object JavaDoc[]
121                 { "Fred" },
122                 "Default Message for Fred.");
123
124         context
125                 .addSubmitListener("function(event) { require(event, document.fred.barney, 'Default Message for Fred.'); }");
126
127         replayControls();
128
129         new Required().renderContribution(writer, cycle, context, field);
130         
131         verifyControls();
132     }
133 }
134
Popular Tags