KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 public class TestMin extends BaseValidatorTestCase
34 {
35     public void testOK() throws Exception JavaDoc
36     {
37         IFormComponent field = newField();
38         ValidationMessages messages = newMessages();
39
40         Integer JavaDoc object = new Integer JavaDoc(10);
41
42         replayControls();
43
44         new Min("min=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_SMALL,
55                 new Object JavaDoc[]
56                 { "My Field", new Double JavaDoc(10) },
57                 "Exception!");
58
59         replayControls();
60
61         try
62         {
63             new Min("min=10").validate(field, messages, new Integer JavaDoc(3));
64         }
65         catch (ValidatorException ex)
66         {
67             assertEquals("Exception!", ex.getMessage());
68             assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
69         }
70     }
71
72     public void testFailCustomMessage()
73     {
74         IFormComponent field = newField("My Field");
75         ValidationMessages messages = newMessages(
76                 "custom",
77                 ValidationStrings.VALUE_TOO_SMALL,
78                 new Object JavaDoc[]
79                 { "My Field", new Double JavaDoc(10) },
80                 "custom message");
81
82         replayControls();
83
84         try
85         {
86             new Min("min=10,message=custom").validate(field, messages, new Integer JavaDoc(3));
87         }
88         catch (ValidatorException ex)
89         {
90             assertEquals("custom message", ex.getMessage());
91             assertEquals(ValidationConstraint.TOO_SMALL, 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/NumberValidator.js");
105
106         trainFormatMessage(contextc, context, null, ValidationStrings.VALUE_TOO_SMALL, new Object JavaDoc[]
107         { "My Field", new Double JavaDoc(20) }, "default message");
108
109         context.getFieldDOM();
110         contextc.setReturnValue("document.myform.myfield");
111
112         context
113                 .addSubmitListener("function(event) { validate_min_number(event, document.myform.myfield, 20.0, 'default message'); }");
114
115         replayControls();
116
117         new Min("min=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/NumberValidator.js");
132
133         trainFormatMessage(
134                 contextc,
135                 context,
136                 "custom",
137                 ValidationStrings.VALUE_TOO_SMALL,
138                 new Object JavaDoc[]
139                 { "My Field", new Double JavaDoc(20) },
140                 "custom message");
141
142         context.getFieldDOM();
143         contextc.setReturnValue("document.myform.myfield");
144
145         context
146                 .addSubmitListener("function(event) { validate_min_number(event, document.myform.myfield, 20.0, 'custom message'); }");
147
148         replayControls();
149
150         new Min("min=20,message=custom").renderContribution(writer, cycle, context, field);
151
152         verifyControls();
153     }
154 }
155
Popular Tags