KickJava   Java API By Example, From Geeks To Geeks.

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


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

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