KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > TestStringValidator


1 // Copyright 2004, 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.valid;
16
17 import org.apache.tapestry.form.IFormComponent;
18 import org.apache.tapestry.valid.StringValidator;
19 import org.apache.tapestry.valid.ValidationConstraint;
20 import org.apache.tapestry.valid.ValidatorException;
21
22 /**
23  * Tests the {@link StringValidator}class.
24  *
25  * @author Howard Lewis Ship
26  * @since 1.0.8
27  */

28
29 public class TestStringValidator extends BaseValidatorTestCase
30 {
31     private StringValidator v = new StringValidator();
32
33     public void testToString()
34     {
35         IFormComponent field = newField();
36
37         replayControls();
38
39         String JavaDoc in = "Foo";
40         String JavaDoc out = v.toString(field, in);
41
42         assertEquals("Result.", in, out);
43
44         verifyControls();
45     }
46
47     public void testToStringNull()
48     {
49         IFormComponent field = newField();
50
51         replayControls();
52
53         String JavaDoc out = v.toString(field, null);
54
55         assertNull("Null expected.", out);
56
57         verifyControls();
58     }
59
60     public void testToObjectRequiredFail()
61     {
62         IFormComponent field = newField("requiredField");
63
64         replayControls();
65
66         v.setRequired(true);
67
68         try
69         {
70             v.toObject(field, "");
71
72             unreachable();
73         }
74         catch (ValidatorException ex)
75         {
76             assertEquals("You must enter a value for requiredField.", ex.getMessage());
77             assertEquals(ValidationConstraint.REQUIRED, ex.getConstraint());
78         }
79
80         verifyControls();
81     }
82
83     public void testOverrideRequiredMessage()
84     {
85         IFormComponent field = newField("overrideMessage");
86
87         replayControls();
88
89         v.setRequired(true);
90         v.setRequiredMessage("Gimme a value for {0} you bastard.");
91
92         try
93         {
94             v.toObject(field, "");
95         }
96         catch (ValidatorException ex)
97         {
98             assertEquals("Gimme a value for overrideMessage you bastard.", ex.getMessage());
99         }
100
101         verifyControls();
102     }
103
104     public void testToObjectRequiredPass() throws ValidatorException
105     {
106         IFormComponent field = newField();
107
108         replayControls();
109
110         v.setRequired(true);
111
112         Object JavaDoc result = v.toObject(field, "stuff");
113
114         assertEquals("Result.", "stuff", result);
115
116         verifyControls();
117     }
118
119     public void testToObjectMinimumFail()
120     {
121         IFormComponent field = newField("minimumLength");
122
123         replayControls();
124
125         v.setMinimumLength(10);
126
127         try
128         {
129             v.toObject(field, "abc");
130
131             unreachable();
132         }
133         catch (ValidatorException ex)
134         {
135             assertEquals("You must enter at least 10 characters for minimumLength.", ex
136                     .getMessage());
137             assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
138         }
139
140         verifyControls();
141     }
142
143     public void testOverrideMinimumMessage()
144     {
145         IFormComponent field = newField("overrideMessage");
146
147         replayControls();
148
149         v.setMinimumLength(10);
150         v
151                 .setMinimumLengthMessage("You really think less than 10 characters for {0} is gonna cut it?");
152
153         try
154         {
155             v.toObject(field, "");
156         }
157         catch (ValidatorException ex)
158         {
159             assertEquals(
160                     "You really think less than 10 characters for overrideMessage is gonna cut it?",
161                     ex.getMessage());
162         }
163     }
164
165     public void testToObjectMinimumPass() throws ValidatorException
166     {
167         IFormComponent field = newField();
168
169         replayControls();
170
171         v.setMinimumLength(10);
172
173         String JavaDoc in = "ambidexterous";
174
175         Object JavaDoc out = v.toObject(field, in);
176
177         assertEquals("Result", in, out);
178
179         verifyControls();
180     }
181
182     /**
183      * An empty string is not subject to the minimum length constraint.
184      */

185
186     public void testToObjectMinimumNull() throws ValidatorException
187     {
188         IFormComponent field = newField();
189
190         replayControls();
191
192         v.setMinimumLength(10);
193
194         String JavaDoc in = "";
195
196         Object JavaDoc out = v.toObject(field, in);
197
198         assertNull("Result", out);
199
200         verifyControls();
201     }
202 }
Popular Tags