KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > TestRequirableFieldSupportImpl


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;
16
17 import org.apache.tapestry.IForm;
18 import org.apache.tapestry.IMarkupWriter;
19 import org.apache.tapestry.IRequestCycle;
20 import org.apache.tapestry.junit.TapestryTestCase;
21 import org.apache.tapestry.valid.IValidationDelegate;
22 import org.apache.tapestry.valid.ValidatorException;
23 import org.easymock.MockControl;
24
25 /**
26  * Test case for {@link RequirableFieldSupportImpl}.
27  *
28  * @author Paul Ferraro
29  * @since 4.0
30  */

31 public class TestRequirableFieldSupportImpl extends TapestryTestCase
32 {
33     private RequirableFieldSupport _support = new RequirableFieldSupportImpl();
34     
35     private MockControl _componentControl = MockControl.createControl(RequirableField.class);
36     private RequirableField _component = (RequirableField) _componentControl.getMock();
37     
38     private MockControl _writerControl = MockControl.createControl(IMarkupWriter.class);
39     private IMarkupWriter _writer = (IMarkupWriter) _writerControl.getMock();
40
41     private MockControl _cycleControl = MockControl.createControl(IRequestCycle.class);
42     private IRequestCycle _cycle = (IRequestCycle) _cycleControl.getMock();
43
44     private MockControl _formControl = MockControl.createControl(IForm.class);
45     private IForm _form = (IForm) _formControl.getMock();
46     
47     private MockControl _delegateControl = MockControl.createNiceControl(IValidationDelegate.class);
48     private IValidationDelegate _delegate = (IValidationDelegate) _delegateControl.getMock();
49     
50     /**
51      * @see org.apache.hivemind.test.HiveMindTestCase#tearDown()
52      */

53     protected void tearDown() throws Exception JavaDoc
54     {
55         _componentControl.reset();
56         _writerControl.reset();
57         _cycleControl.reset();
58         _formControl.reset();
59         _delegateControl.reset();
60         
61         super.tearDown();
62     }
63
64     private void replay()
65     {
66         _componentControl.replay();
67         _writerControl.replay();
68         _cycleControl.replay();
69         _formControl.replay();
70         _delegateControl.replay();
71     }
72
73     private void verify()
74     {
75         _componentControl.verify();
76         _writerControl.verify();
77         _cycleControl.verify();
78         _formControl.verify();
79         _delegateControl.verify();
80     }
81     
82     public void testNotRequiredRender()
83     {
84         _component.getForm();
85         _componentControl.setReturnValue(_form);
86         
87         _component.isRequired();
88         _componentControl.setReturnValue(false);
89         
90         replay();
91         
92         _support.render(_component, _writer, _cycle);
93         
94         verify();
95     }
96     
97     public void testRequiredClientValidationDisabledRender()
98     {
99         _component.getForm();
100         _componentControl.setReturnValue(_form);
101         
102         _component.isRequired();
103         _componentControl.setReturnValue(true);
104
105         _form.isClientValidationEnabled();
106         _formControl.setReturnValue(false);
107         
108         replay();
109         
110         _support.render(_component, _writer, _cycle);
111         
112         verify();
113     }
114
115     public void testRequiredClientValidationEnabledRender()
116     {
117         _component.getForm();
118         _componentControl.setReturnValue(_form);
119         
120         _component.isRequired();
121         _componentControl.setReturnValue(true);
122
123         _form.isClientValidationEnabled();
124         _formControl.setReturnValue(true);
125         
126         _form.getName();
127         _formControl.setReturnValue("formName");
128         
129         _component.getName();
130         _componentControl.setReturnValue("fieldName");
131         
132         _component.getRequiredMessage();
133         _componentControl.setReturnValue("You must enter a value for {0}.");
134         
135         _component.getDisplayName();
136         _componentControl.setReturnValue("Field Name");
137
138         _form.addEventHandler(FormEventType.SUBMIT, "require(event, document.formName.fieldName,'You must enter a value for Field Name.')");
139         _formControl.setVoidCallable();
140         
141         replay();
142         
143         _support.render(_component, _writer, _cycle);
144         
145         verify();
146     }
147     
148     public void testNotRequiredRewind()
149     {
150         _component.getSubmittedValue(_cycle);
151         _componentControl.setReturnValue("value");
152         
153         _component.isRequired();
154         _componentControl.setReturnValue(false);
155
156         try
157         {
158             _component.bind(_writer, _cycle);
159             _componentControl.setVoidCallable();
160         
161             replay();
162             
163             _support.rewind(_component, _writer, _cycle);
164         }
165         catch (ValidatorException e)
166         {
167             unreachable();
168         }
169         finally
170         {
171             verify();
172         }
173     }
174     
175     public void testRequiredBlankRewind()
176     {
177         _component.getSubmittedValue(_cycle);
178         _componentControl.setReturnValue("");
179         
180         _component.isRequired();
181         _componentControl.setReturnValue(true);
182
183         String JavaDoc pattern = "You must enter a value for {0}.";
184         String JavaDoc displayName = "Field Name";
185         
186         _component.getRequiredMessage();
187         _componentControl.setReturnValue(pattern);
188         
189         _component.getDisplayName();
190         _componentControl.setReturnValue(displayName);
191
192         _component.getForm();
193         _componentControl.setReturnValue(_form);
194         
195         _form.getDelegate();
196         _formControl.setReturnValue(_delegate);
197         
198         replay();
199         
200         _support.rewind(_component, _writer, _cycle);
201         
202         verify();
203     }
204     
205     public void testRequiredNotBlankRewind()
206     {
207         _component.getSubmittedValue(_cycle);
208         _componentControl.setReturnValue("value");
209         
210         _component.isRequired();
211         _componentControl.setReturnValue(true);
212         
213         try
214         {
215             _component.bind(_writer, _cycle);
216             _componentControl.setVoidCallable();
217         
218             replay();
219             
220             _support.rewind(_component, _writer, _cycle);
221         }
222         catch (ValidatorException e)
223         {
224             unreachable();
225         }
226         finally
227         {
228             verify();
229         }
230     }
231 }
232
Popular Tags