KickJava   Java API By Example, From Geeks To Geeks.

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


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.MinLength}.
29  *
30  * @author Howard Lewis Ship
31  * @since 4.0
32  */

33 public class TestMinLength extends BaseValidatorTestCase
34 {
35     public void testOK() throws Exception JavaDoc
36     {
37         IFormComponent field = newField();
38         ValidationMessages messages = newMessages();
39
40         String JavaDoc object = "a nice long string";
41
42         replayControls();
43
44         new MinLength("minLength=5").validate(field, messages, object);
45
46         verifyControls();
47     }
48
49     public void testFail()
50     {
51         IFormComponent field = newField("My Field");
52         ValidationMessages messages = newMessages(
53                 null,
54                 ValidationStrings.VALUE_TOO_SHORT,
55                 new Object JavaDoc[]
56                 { new Integer JavaDoc(10), "My Field" },
57                 "Exception!");
58
59         replayControls();
60
61         try
62         {
63             new MinLength("minLength=10").validate(field, messages, "short");
64         }
65         catch (ValidatorException ex)
66         {
67             assertEquals("Exception!", ex.getMessage());
68             assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
69         }
70     }
71
72     public void testFailCustomMessage()
73     {
74         IFormComponent field = newField("My Field");
75         ValidationMessages messages = newMessages(
76                 "Too Short",
77                 ValidationStrings.VALUE_TOO_SHORT,
78                 new Object JavaDoc[]
79                 { new Integer JavaDoc(10), "My Field" },
80                 "Exception!");
81
82         replayControls();
83
84         try
85         {
86             new MinLength("minLength=10,message=Too Short").validate(field, messages, "short");
87         }
88         catch (ValidatorException ex)
89         {
90             assertEquals("Exception!", ex.getMessage());
91             assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
92         }
93     }
94
95     public void testRenderContribution()
96     {
97         IMarkupWriter writer = newWriter();
98         IRequestCycle cycle = newCycle();
99         IFormComponent field = newField("My Field");
100         MockControl contextc = newControl(FormComponentContributorContext.class);
101         FormComponentContributorContext context = (FormComponentContributorContext) contextc
102                 .getMock();
103
104         context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
105
106         context.getFieldDOM();
107         contextc.setReturnValue("document.myform.myfield");
108
109         trainFormatMessage(contextc, context, null, ValidationStrings.VALUE_TOO_SHORT, new Object JavaDoc[]
110         { new Integer JavaDoc(20), "My Field" }, "default message");
111
112         context
113                 .addSubmitListener("function(event) { validate_min_length(event, document.myform.myfield, 20, 'default message'); }");
114
115         replayControls();
116
117         new MinLength("minLength=20").renderContribution(writer, cycle, context, field);
118
119         verifyControls();
120     }
121
122     public void testRenderContributionCustomMessage()
123     {
124         IMarkupWriter writer = newWriter();
125         IRequestCycle cycle = newCycle();
126         IFormComponent field = newField("My Field");
127         MockControl contextc = newControl(FormComponentContributorContext.class);
128         FormComponentContributorContext context = (FormComponentContributorContext) contextc
129                 .getMock();
130
131         context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
132
133         context.getFieldDOM();
134         contextc.setReturnValue("document.myform.myfield");
135
136         trainFormatMessage(
137                 contextc,
138                 context,
139                 "custom",
140                 ValidationStrings.VALUE_TOO_SHORT,
141                 new Object JavaDoc[]
142                 { new Integer JavaDoc(25), "My Field" },
143                 "custom message");
144
145         context
146                 .addSubmitListener("function(event) { validate_min_length(event, document.myform.myfield, 25, 'custom message'); }");
147
148         replayControls();
149
150         new MinLength("minLength=25,message=custom").renderContribution(
151                 writer,
152                 cycle,
153                 context,
154                 field);
155
156         verifyControls();
157     }
158 }
159
Popular Tags