KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > FieldTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.forms.formmodel;
18
19 import org.apache.cocoon.core.container.ContainerTestCase;
20 import org.apache.cocoon.environment.mock.MockRequest;
21 import org.apache.cocoon.forms.FormContext;
22 import org.apache.cocoon.forms.event.ValueChangedEvent;
23 import org.apache.cocoon.forms.event.ValueChangedListener;
24 import org.w3c.dom.Document JavaDoc;
25
26 /**
27  * Test case for CForm's Field widget
28  *
29  * @version $Id: FieldTestCase.java 106132 2004-11-21 22:29:26Z sylvain $
30  */

31
32 public class FieldTestCase extends ContainerTestCase {
33     
34     public static final String JavaDoc VALUE_PATH = "fi:fragment/fi:field/fi:value";
35     public static final String JavaDoc VALIDATION_PATH = "fi:fragment/fi:field/fi:validation-message";
36     
37     
38     /**
39      * Nominal test where the request data is syntactically correct and validates
40      */

41     public void testValueDoesParseAndValidate() throws Exception JavaDoc {
42         Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml");
43         Field field = (Field)form.getChild("intfield");
44         Action button = (Action)form.getChild("action");
45         MockRequest request;
46         
47         request = new MockRequest();
48         request.addParameter("intfield", "11");
49         request.addParameter("action", "pressed");
50         form.process(new FormContext(request));
51         
52         // No parsing nor validation where performed
53
Document JavaDoc doc = WidgetTestHelper.getWidgetFragment(field, null);
54         WidgetTestHelper.assertXPathEquals("Displayed value", "11", VALUE_PATH, doc);
55         WidgetTestHelper.assertXPathNotExists("Validation error", VALIDATION_PATH, doc);
56         
57         // Now do some parsing.
58
assertEquals("Field value", new Integer JavaDoc(11), field.getValue());
59         // And still no validation error (do not call getValidationError() as it does validate)
60
doc = WidgetTestHelper.getWidgetFragment(field, null);
61         WidgetTestHelper.assertXPathNotExists("Validation error", VALIDATION_PATH, doc);
62         
63         // Now validate
64
assertTrue("Field does validate", field.validate());
65         assertNull("getValidationError() null after validation", field.getValidationError());
66         doc = WidgetTestHelper.getWidgetFragment(field, null);
67         WidgetTestHelper.assertXPathNotExists("Validation error", VALIDATION_PATH, doc);
68     }
69     
70     /**
71      * Request data is not syntactically correct
72      */

73     public void testValueDoesNotParse() throws Exception JavaDoc {
74         Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml");
75         Field field = (Field)form.getChild("intfield");
76         Action button = (Action)form.getChild("action");
77         MockRequest request;
78         
79         request = new MockRequest();
80         request.addParameter("intfield", "foo");
81         request.addParameter("action", "pressed");
82         form.process(new FormContext(request));
83         
84         // No parsing nor validation where performed
85
Document JavaDoc doc = WidgetTestHelper.getWidgetFragment(field, null);
86         WidgetTestHelper.assertXPathEquals("Displayed velue", "foo", VALUE_PATH, doc);
87         WidgetTestHelper.assertXPathNotExists("Validation error before parse", VALIDATION_PATH, doc);
88         
89         // Now do some parsing. Will return null as it's not parseable
90
assertNull("Field value", field.getValue());
91         // But still no validation error
92
doc = WidgetTestHelper.getWidgetFragment(field, null);
93         WidgetTestHelper.assertXPathEquals("Displayed value", "foo", VALUE_PATH, doc);
94         WidgetTestHelper.assertXPathNotExists("Validation error after parse", VALIDATION_PATH, doc);
95         
96         // Now validate
97
assertFalse("Field validation", field.validate());
98         doc = WidgetTestHelper.getWidgetFragment(field, null);
99         WidgetTestHelper.assertXPathEquals("Displayed velue", "foo", VALUE_PATH, doc);
100         WidgetTestHelper.assertXPathExists("Validation not null after parse", VALIDATION_PATH, doc);
101         assertNotNull("getValidationError() not null after validation", field.getValidationError());
102     }
103     
104     /**
105      * Request data is syntactically correct but doesn't validate
106      */

107     public void testValueDoesNotValidate() throws Exception JavaDoc {
108         Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml");
109         Field field = (Field)form.getChild("intfield");
110         Action button = (Action)form.getChild("action");
111         MockRequest request;
112         
113         request = new MockRequest();
114         request.addParameter("intfield", "1");
115         request.addParameter("action", "pressed");
116         form.process(new FormContext(request));
117         
118         // No parsing nor validation where performed
119
Document JavaDoc doc = WidgetTestHelper.getWidgetFragment(field, null);
120         WidgetTestHelper.assertXPathEquals("Displayed value", "1", VALUE_PATH, doc);
121         WidgetTestHelper.assertXPathNotExists("Validation error before parse", VALIDATION_PATH, doc);
122         
123         // Now do some parsing. Will return null although syntactically correct as it's invalid
124
assertNull("Field value", field.getValue());
125         // But still no validation error
126
doc = WidgetTestHelper.getWidgetFragment(field, null);
127         WidgetTestHelper.assertXPathNotExists("Validation error after parse", VALIDATION_PATH, doc);
128         
129         // Now validate
130
assertFalse("Field validation", field.validate());
131         doc = WidgetTestHelper.getWidgetFragment(field, null);
132         WidgetTestHelper.assertXPathExists("Validation error after validation", VALIDATION_PATH, doc);
133         assertNotNull("getValidationError() not null after validation", field.getValidationError());
134     }
135     
136     /**
137      * Test that a field's value is properly set by a call to setValue("") with an
138      * empty string when the field is in unparsed state (there used to be a bug in
139      * that case)
140      */

141     public void testSetEmptyValueWhenValueChangedOnRequest() throws Exception JavaDoc {
142         Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml");
143         Field field = (Field)form.getChild("stringfield");
144         Action button = (Action)form.getChild("action");
145         MockRequest request;
146         
147         // Set a value in stringfield and submit with an action
148
// (no validation, thus no call to doParse())
149
request = new MockRequest();
150         request.addParameter("stringfield", "bar");
151         request.addParameter("action", "pressed");
152         form.process(new FormContext(request));
153         
154         // Verify submit widget, just to be sure that validation did not occur
155
assertEquals("Form submit widget", button, form.getSubmitWidget());
156         
157         // Set the value to an empty string. In that case, a faulty test made
158
// it actually ignore it when state was VALUE_UNPARSED
159
field.setValue("");
160         
161         // Check value by various means
162
Document JavaDoc doc = WidgetTestHelper.getWidgetFragment(field, null);
163         WidgetTestHelper.assertXPathEquals("Displayed value", "", VALUE_PATH, doc);
164         assertEquals("Datatype string conversion", "", field.getDatatype().convertToString(field.value, null));
165         assertEquals("Field value", "", (String JavaDoc)field.getValue());
166     }
167     
168     /**
169      * Test that the previous field value is correctly passed to event listeners
170      * even if it was not already parsed.
171      */

172     public void testOldValuePresentInEventEvenIfNotParsed() throws Exception JavaDoc {
173         Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml");
174         Field field = (Field)form.getChild("stringfield");
175         Action button = (Action)form.getChild("action");
176         MockRequest request;
177         
178         // Set a value on "stringfield", and submit using an action so that
179
// it stays in unparsed state
180
request = new MockRequest();
181         request.addParameter("stringfield", "foo");
182         request.addParameter("action", "pressed");
183         form.process(new FormContext(request));
184
185         // Now add an event listener that will check old an new value
186
field.addValueChangedListener(new ValueChangedListener (){
187             public void valueChanged(ValueChangedEvent event) {
188                 assertEquals("Old value", "foo", (String JavaDoc)event.getOldValue());
189                 assertEquals("New value", "bar", (String JavaDoc)event.getNewValue());
190             }
191         });
192         
193         // Change value to "bar", still without explicit validation
194
// That will call the event listener
195
request = new MockRequest();
196         request.addParameter("stringfield", "bar");
197         request.addParameter("button", "pressed");
198         form.process(new FormContext(request));
199     }
200     
201     /**
202      * Request parameters are not read when a field is not in active state
203      */

204     public void testParameterNotReadWhenDisabled() throws Exception JavaDoc {
205         Form form = WidgetTestHelper.loadForm(getManager(), this, "FieldTestCase.model.xml");
206         Field field = (Field)form.getChild("stringfield");
207         MockRequest request;
208
209         // Disable the form
210
form.setState(WidgetState.DISABLED);
211         field.setValue("foo");
212         
213         request = new MockRequest();
214         request.addParameter("stringfield", "bar");
215         form.process(new FormContext(request));
216         
217         // Check that "bar" was not read
218
assertEquals("foo", field.getValue());
219         
220         // Switch back to active and resumbit the same request
221
form.setState(WidgetState.ACTIVE);
222         form.process(new FormContext(request));
223         
224         // Should have changed now
225
assertEquals("bar", field.getValue());
226     }
227 }
228
Popular Tags