KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.RegexpMatcher;
23 import org.apache.tapestry.valid.ValidationConstraint;
24 import org.apache.tapestry.valid.ValidationStrings;
25 import org.apache.tapestry.valid.ValidatorException;
26 import org.easymock.MockControl;
27
28 /**
29  * Tests for {@link org.apache.tapestry.form.validator.Pattern}
30  *
31  * @author Howard Lewis Ship
32  * @since 4.0
33  */

34 public class TestPattern extends BaseValidatorTestCase
35 {
36
37     public void testOK() throws Exception JavaDoc
38     {
39         IFormComponent field = newField();
40         ValidationMessages messages = newMessages();
41
42         replayControls();
43
44         new Pattern("pattern=\\d+").validate(field, messages, "1232");
45
46         verifyControls();
47     }
48
49     public void testFail()
50     {
51         IFormComponent field = newField("My Pattern");
52         ValidationMessages messages = newMessages(
53                 null,
54                 ValidationStrings.REGEX_MISMATCH,
55                 new Object JavaDoc[]
56                 { "My Pattern" },
57                 "default message");
58
59         replayControls();
60
61         try
62         {
63             new Pattern("pattern=\\d+").validate(field, messages, "fred");
64             unreachable();
65         }
66         catch (ValidatorException ex)
67         {
68             assertEquals("default message", ex.getMessage());
69             assertEquals(ValidationConstraint.PATTERN_MISMATCH, ex.getConstraint());
70         }
71
72         verifyControls();
73     }
74
75     public void testFailCustomMessage()
76     {
77         IFormComponent field = newField("My Pattern");
78         ValidationMessages messages = newMessages(
79                 "custom",
80                 ValidationStrings.REGEX_MISMATCH,
81                 new Object JavaDoc[]
82                 { "My Pattern" },
83                 "custom message");
84
85         replayControls();
86
87         try
88         {
89             new Pattern("pattern=\\d+,message=custom").validate(field, messages, "fred");
90             unreachable();
91         }
92         catch (ValidatorException ex)
93         {
94             assertEquals("custom message", ex.getMessage());
95             assertEquals(ValidationConstraint.PATTERN_MISMATCH, ex.getConstraint());
96         }
97
98         verifyControls();
99     }
100
101     public void testRenderContribution()
102     {
103         String JavaDoc pattern = new RegexpMatcher().getEscapedPatternString("\\d+");
104
105         IMarkupWriter writer = newWriter();
106         IRequestCycle cycle = newCycle();
107
108         MockControl contextc = newControl(FormComponentContributorContext.class);
109         FormComponentContributorContext context = (FormComponentContributorContext) contextc
110                 .getMock();
111
112         context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
113
114         IFormComponent field = newField("Fred");
115
116         trainFormatMessage(contextc, context, null, ValidationStrings.REGEX_MISMATCH, new Object JavaDoc[]
117         { "Fred" }, "default message");
118
119         context.getFieldDOM();
120         contextc.setReturnValue("document.fred.barney");
121
122         context
123                 .addSubmitListener("function(event) { validate_regexp(event, document.fred.barney, '"
124                         + pattern + "', 'default message'); }");
125
126         replayControls();
127
128         new Pattern("pattern=\\d+").renderContribution(writer, cycle, context, field);
129
130         verifyControls();
131     }
132
133     public void testRenderContributionCustomMessage()
134     {
135         String JavaDoc pattern = new RegexpMatcher().getEscapedPatternString("\\d+");
136
137         IMarkupWriter writer = newWriter();
138         IRequestCycle cycle = newCycle();
139
140         MockControl contextc = newControl(FormComponentContributorContext.class);
141         FormComponentContributorContext context = (FormComponentContributorContext) contextc
142                 .getMock();
143
144         context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
145
146         IFormComponent field = newField("Fred");
147
148         trainFormatMessage(
149                 contextc,
150                 context,
151                 "custom",
152                 ValidationStrings.REGEX_MISMATCH,
153                 new Object JavaDoc[]
154                 { "Fred" },
155                 "custom message");
156
157         context.getFieldDOM();
158         contextc.setReturnValue("document.fred.barney");
159
160         context
161                 .addSubmitListener("function(event) { validate_regexp(event, document.fred.barney, '"
162                         + pattern + "', 'custom message'); }");
163
164         replayControls();
165
166         new Pattern("pattern=\\d+,message=custom").renderContribution(writer, cycle, context, field);
167
168         verifyControls();
169
170     }
171
172 }
173
Popular Tags