KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class TestMaxLength extends BaseValidatorTestCase
28 {
29
30     public void testOK() throws Exception JavaDoc
31     {
32         IFormComponent field = newField();
33         ValidationMessages messages = newMessages();
34
35         String JavaDoc object = "short and sweet";
36
37         replayControls();
38
39         new MaxLength("maxLength=50").validate(field, messages, object);
40
41         verifyControls();
42     }
43
44     public void testFail()
45     {
46         IFormComponent field = newField("My Field");
47         ValidationMessages messages = newMessages(
48                 null,
49                 ValidationStrings.VALUE_TOO_LONG,
50                 new Object JavaDoc[]
51                 { new Integer JavaDoc(10), "My Field" },
52                 "Exception!");
53
54         replayControls();
55
56         try
57         {
58             new MaxLength("maxLength=10")
59                     .validate(field, messages, "brevity is the essence of wit");
60         }
61         catch (ValidatorException ex)
62         {
63             assertEquals("Exception!", ex.getMessage());
64             assertEquals(ValidationConstraint.MAXIMUM_WIDTH, ex.getConstraint());
65         }
66     }
67
68     public void testFailCustomMessage()
69     {
70         IFormComponent field = newField("My Field");
71         ValidationMessages messages = newMessages(
72                 "Too Long",
73                 ValidationStrings.VALUE_TOO_LONG,
74                 new Object JavaDoc[]
75                 { new Integer JavaDoc(10), "My Field" },
76                 "Exception!");
77
78         replayControls();
79
80         try
81         {
82             new MaxLength("maxLength=10,message=Too Long").validate(
83                     field,
84                     messages,
85                     "this should be more than ten characters");
86         }
87         catch (ValidatorException ex)
88         {
89             assertEquals("Exception!", ex.getMessage());
90             assertEquals(ValidationConstraint.MAXIMUM_WIDTH, ex.getConstraint());
91         }
92     }
93
94     public void testRenderContribution()
95     {
96         IMarkupWriter writer = newWriter();
97         IRequestCycle cycle = newCycle();
98         IFormComponent field = newField("My Field");
99         MockControl contextc = newControl(FormComponentContributorContext.class);
100         FormComponentContributorContext context = (FormComponentContributorContext) contextc
101                 .getMock();
102
103         context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
104
105         context.getFieldDOM();
106         contextc.setReturnValue("document.myform.myfield");
107
108         trainFormatMessage(contextc, context, null, ValidationStrings.VALUE_TOO_LONG, new Object JavaDoc[]
109         { new Integer JavaDoc(20), "My Field" }, "default message");
110
111         context
112                 .addSubmitListener("function(event) { validate_max_length(event, document.myform.myfield, 20, 'default message'); }");
113
114         replayControls();
115
116         new MaxLength("maxLength=20").renderContribution(writer, cycle, context, field);
117
118         verifyControls();
119     }
120 }
121
Popular Tags