001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.forms.formmodel;
019:
020: import org.apache.cocoon.core.container.ContainerTestCase;
021: import org.apache.cocoon.environment.mock.MockRequest;
022: import org.apache.cocoon.forms.FormContext;
023: import org.apache.cocoon.forms.event.ValueChangedEvent;
024: import org.apache.cocoon.forms.event.ValueChangedListener;
025: import org.w3c.dom.Document;
026:
027: /**
028: * Test case for CForm's Field widget
029: *
030: * @version $Id: FieldTestCase.java 449149 2006-09-23 03:58:05Z crossley $
031: */
032:
033: public class FieldTestCase extends ContainerTestCase {
034:
035: public static final String VALUE_PATH = "fi:fragment/fi:field/fi:value";
036: public static final String VALIDATION_PATH = "fi:fragment/fi:field/fi:validation-message";
037:
038: /**
039: * Nominal test where the request data is syntactically correct and validates
040: */
041: public void testValueDoesParseAndValidate() throws Exception {
042: Form form = WidgetTestHelper.loadForm(getManager(), this ,
043: "FieldTestCase.model.xml");
044: Field field = (Field) form.getChild("intfield");
045: Action button = (Action) form.getChild("action");
046: MockRequest request;
047:
048: request = new MockRequest();
049: request.addParameter("intfield", "11");
050: request.addParameter("action", "pressed");
051: form.process(new FormContext(request));
052:
053: // No parsing nor validation where performed
054: Document doc = WidgetTestHelper.getWidgetFragment(field, null);
055: WidgetTestHelper.assertXPathEquals("Displayed value", "11",
056: VALUE_PATH, doc);
057: WidgetTestHelper.assertXPathNotExists("Validation error",
058: VALIDATION_PATH, doc);
059:
060: // Now do some parsing.
061: assertEquals("Field value", new Integer(11), field.getValue());
062: // And still no validation error (do not call getValidationError() as it does validate)
063: doc = WidgetTestHelper.getWidgetFragment(field, null);
064: WidgetTestHelper.assertXPathNotExists("Validation error",
065: VALIDATION_PATH, doc);
066:
067: // Now validate
068: assertTrue("Field does validate", field.validate());
069: assertNull("getValidationError() null after validation", field
070: .getValidationError());
071: doc = WidgetTestHelper.getWidgetFragment(field, null);
072: WidgetTestHelper.assertXPathNotExists("Validation error",
073: VALIDATION_PATH, doc);
074: }
075:
076: /**
077: * Request data is not syntactically correct
078: */
079: public void testValueDoesNotParse() throws Exception {
080: Form form = WidgetTestHelper.loadForm(getManager(), this ,
081: "FieldTestCase.model.xml");
082: Field field = (Field) form.getChild("intfield");
083: Action button = (Action) form.getChild("action");
084: MockRequest request;
085:
086: request = new MockRequest();
087: request.addParameter("intfield", "foo");
088: request.addParameter("action", "pressed");
089: form.process(new FormContext(request));
090:
091: // No parsing nor validation where performed
092: Document doc = WidgetTestHelper.getWidgetFragment(field, null);
093: WidgetTestHelper.assertXPathEquals("Displayed velue", "foo",
094: VALUE_PATH, doc);
095: WidgetTestHelper.assertXPathNotExists(
096: "Validation error before parse", VALIDATION_PATH, doc);
097:
098: // Now do some parsing. Will return null as it's not parseable
099: assertNull("Field value", field.getValue());
100: // But still no validation error
101: doc = WidgetTestHelper.getWidgetFragment(field, null);
102: WidgetTestHelper.assertXPathEquals("Displayed value", "foo",
103: VALUE_PATH, doc);
104: WidgetTestHelper.assertXPathNotExists(
105: "Validation error after parse", VALIDATION_PATH, doc);
106:
107: // Now validate
108: assertFalse("Field validation", field.validate());
109: doc = WidgetTestHelper.getWidgetFragment(field, null);
110: WidgetTestHelper.assertXPathEquals("Displayed velue", "foo",
111: VALUE_PATH, doc);
112: WidgetTestHelper
113: .assertXPathExists("Validation not null after parse",
114: VALIDATION_PATH, doc);
115: assertNotNull("getValidationError() not null after validation",
116: field.getValidationError());
117: }
118:
119: /**
120: * Request data is syntactically correct but doesn't validate
121: */
122: public void testValueDoesNotValidate() throws Exception {
123: Form form = WidgetTestHelper.loadForm(getManager(), this ,
124: "FieldTestCase.model.xml");
125: Field field = (Field) form.getChild("intfield");
126: Action button = (Action) form.getChild("action");
127: MockRequest request;
128:
129: request = new MockRequest();
130: request.addParameter("intfield", "1");
131: request.addParameter("action", "pressed");
132: form.process(new FormContext(request));
133:
134: // No parsing nor validation where performed
135: Document doc = WidgetTestHelper.getWidgetFragment(field, null);
136: WidgetTestHelper.assertXPathEquals("Displayed value", "1",
137: VALUE_PATH, doc);
138: WidgetTestHelper.assertXPathNotExists(
139: "Validation error before parse", VALIDATION_PATH, doc);
140:
141: // Now do some parsing. Will return null although syntactically correct as it's invalid
142: assertNull("Field value", field.getValue());
143: // But still no validation error
144: doc = WidgetTestHelper.getWidgetFragment(field, null);
145: WidgetTestHelper.assertXPathNotExists(
146: "Validation error after parse", VALIDATION_PATH, doc);
147:
148: // Now validate
149: assertFalse("Field validation", field.validate());
150: doc = WidgetTestHelper.getWidgetFragment(field, null);
151: WidgetTestHelper.assertXPathExists(
152: "Validation error after validation", VALIDATION_PATH,
153: doc);
154: assertNotNull("getValidationError() not null after validation",
155: field.getValidationError());
156: }
157:
158: /**
159: * Test that a field's value is properly set by a call to setValue("") with an
160: * empty string when the field is in unparsed state (there used to be a bug in
161: * that case)
162: */
163: public void testSetEmptyValueWhenValueChangedOnRequest()
164: throws Exception {
165: Form form = WidgetTestHelper.loadForm(getManager(), this ,
166: "FieldTestCase.model.xml");
167: Field field = (Field) form.getChild("stringfield");
168: Action button = (Action) form.getChild("action");
169: MockRequest request;
170:
171: // Set a value in stringfield and submit with an action
172: // (no validation, thus no call to doParse())
173: request = new MockRequest();
174: request.addParameter("stringfield", "bar");
175: request.addParameter("action", "pressed");
176: form.process(new FormContext(request));
177:
178: // Verify submit widget, just to be sure that validation did not occur
179: assertEquals("Form submit widget", button, form
180: .getSubmitWidget());
181:
182: // Set the value to an empty string. In that case, a faulty test made
183: // it actually ignore it when state was VALUE_UNPARSED
184: field.setValue("");
185:
186: // Check value by various means
187: Document doc = WidgetTestHelper.getWidgetFragment(field, null);
188: WidgetTestHelper.assertXPathEquals("Displayed value", "",
189: VALUE_PATH, doc);
190: assertEquals("Datatype string conversion", "", field
191: .getDatatype().convertToString(field.value, null));
192: assertEquals("Field value", "", (String) field.getValue());
193: }
194:
195: /**
196: * Test that the previous field value is correctly passed to event listeners
197: * even if it was not already parsed.
198: */
199: public void testOldValuePresentInEventEvenIfNotParsed()
200: throws Exception {
201: Form form = WidgetTestHelper.loadForm(getManager(), this ,
202: "FieldTestCase.model.xml");
203: Field field = (Field) form.getChild("stringfield");
204: Action button = (Action) form.getChild("action");
205: MockRequest request;
206:
207: // Set a value on "stringfield", and submit using an action so that
208: // it stays in unparsed state
209: request = new MockRequest();
210: request.addParameter("stringfield", "foo");
211: request.addParameter("action", "pressed");
212: form.process(new FormContext(request));
213:
214: // Now add an event listener that will check old an new value
215: field.addValueChangedListener(new ValueChangedListener() {
216: public void valueChanged(ValueChangedEvent event) {
217: assertEquals("Old value", "foo", (String) event
218: .getOldValue());
219: assertEquals("New value", "bar", (String) event
220: .getNewValue());
221: }
222: });
223:
224: // Change value to "bar", still without explicit validation
225: // That will call the event listener
226: request = new MockRequest();
227: request.addParameter("stringfield", "bar");
228: request.addParameter("button", "pressed");
229: form.process(new FormContext(request));
230: }
231:
232: /**
233: * Request parameters are not read when a field is not in active state
234: */
235: public void testParameterNotReadWhenDisabled() throws Exception {
236: Form form = WidgetTestHelper.loadForm(getManager(), this ,
237: "FieldTestCase.model.xml");
238: Field field = (Field) form.getChild("stringfield");
239: MockRequest request;
240:
241: // Disable the form
242: form.setState(WidgetState.DISABLED);
243: field.setValue("foo");
244:
245: request = new MockRequest();
246: request.addParameter("stringfield", "bar");
247: form.process(new FormContext(request));
248:
249: // Check that "bar" was not read
250: assertEquals("foo", field.getValue());
251:
252: // Switch back to active and resumbit the same request
253: form.setState(WidgetState.ACTIVE);
254: form.process(new FormContext(request));
255:
256: // Should have changed now
257: assertEquals("bar", field.getValue());
258: }
259: }
|