001: package org.araneaframework.tests;
002:
003: import junit.framework.TestCase;
004: import org.araneaframework.http.core.StandardServletInputData;
005: import org.araneaframework.tests.mock.MockEnvironment;
006: import org.araneaframework.tests.util.RequestUtil;
007: import org.araneaframework.uilib.form.Data;
008: import org.araneaframework.uilib.form.FormElement;
009: import org.araneaframework.uilib.form.FormWidget;
010: import org.araneaframework.uilib.form.control.BaseControl;
011: import org.araneaframework.uilib.form.control.FloatControl;
012: import org.araneaframework.uilib.form.control.TextControl;
013: import org.araneaframework.uilib.form.data.BigDecimalData;
014: import org.araneaframework.uilib.form.data.StringData;
015: import org.springframework.mock.web.MockHttpServletRequest;
016:
017: /**
018: * Regression test for changes made in change #153 ("Asynchronous form modifications").
019: * @author Taimo Peelo (taimo@araneaframework.org)
020: */
021: public class AsyncFormModificationTest extends TestCase {
022: protected FormWidget makeForm() throws Exception {
023: FormWidget testForm = new FormWidget();
024: testForm.addElement("myLongText", "my long text",
025: new TextControl(), new StringData(), true);
026: testForm._getComponent().init(null, new MockEnvironment());
027: return testForm;
028: }
029:
030: /* Test setting the value of fully initialized FormElement. */
031: public void testSetValue_1() throws Exception {
032: String value = "newvalue";
033:
034: FormWidget form = makeForm();
035: FormElement el = form.getElementByFullName("myLongText");
036:
037: el.setValue(value);
038:
039: assertEquals("Element value incorrect: '" + value + "' != '"
040: + el.getValue() + "'", "newvalue", el.getValue());
041: }
042:
043: /* Test whether the createElement() with initialValue set produces
044: * expected results when FormElement is left uninited. */
045: public void testSetValue_2() throws Exception {
046: String value = "newvalue";
047:
048: FormWidget form = makeForm();
049: FormElement el = form.createElement("labelId",
050: new TextControl(), new StringData(), value, false);
051:
052: assertEquals("Element value incorrect", value, el.getValue());
053: }
054:
055: /* Test that replacing inited FormElement's Data changes FormElement's value to Data's value.
056: * After that, make sure that Control sees the same value (meaning that asynchronous form
057: * modifications work). */
058: public void testSetValue_3() throws Exception {
059: String value = "newvalue";
060: Data data = new StringData();
061: data.setValue(value);
062:
063: FormWidget form = makeForm();
064: FormElement element = form.getElementByFullName("myLongText");
065:
066: element.setData(data);
067: assertEquals("Element value incorrect", value, element
068: .getValue());
069:
070: TextControl.ViewModel viewModel = (TextControl.ViewModel) ((BaseControl) (element
071: .getControl())).getViewModel();
072:
073: assertEquals(
074: "Inited formelement's Control value differs from Data value",
075: value, viewModel.getSimpleValue());
076: }
077:
078: /* Test that replacing uninited FormElement's Data changes FormElement's value to Data's value.
079: * After that, demonstrate that Control inside uninited FormElement is still unaware of its value. */
080: public void testSetValue_4() throws Exception {
081: String value = "newvalue";
082: Data data = new StringData();
083: data.setValue(value);
084:
085: FormWidget form = makeForm();
086: FormElement element = form.createElement("labelId",
087: new TextControl(), new StringData(), "initial", false);
088:
089: element.setData(data);
090: assertEquals("Element value incorrect", value, element
091: .getValue());
092:
093: TextControl.ViewModel viewModel = (TextControl.ViewModel) ((BaseControl) (element
094: .getControl())).getViewModel();
095:
096: // As Control's converters are not yet in place, this is expected to be false.
097: if (value.equals(viewModel.getSimpleValue())) {
098: fail("Unexpected semantic change (might be real useful :D): See test comments for details.");
099: }
100: }
101:
102: /* Test that convert() retains the Control contents when request with invalid form field content comes in. */
103: public void testConvert_1() throws Exception {
104: String notNumber = "qwe";
105: String someText = "someText";
106:
107: FormWidget testForm = new FormWidget();
108: testForm.addElement("number", "#Number", new FloatControl(),
109: new BigDecimalData(), true);
110: testForm.addElement("text", "#Text", new TextControl(),
111: new StringData(), false);
112: testForm._getComponent().init(null, new MockEnvironment());
113:
114: // construct the request
115: MockHttpServletRequest validRequest = RequestUtil
116: .markSubmitted(new MockHttpServletRequest());
117:
118: validRequest.addParameter("number", notNumber);
119: ((FormElement) testForm.getElement("number")).rendered();
120:
121: validRequest.addParameter("text", someText);
122: ((FormElement) testForm.getElement("text")).rendered();
123:
124: // process the request
125: StandardServletInputData input = new StandardServletInputData(
126: validRequest);
127: testForm._getWidget().update(input);
128:
129: // convert
130: testForm.convert();
131:
132: assertEquals(someText, testForm.getControlByFullName("text")
133: .getRawValue());
134: String simpleValue = ((FloatControl.ViewModel) ((BaseControl) testForm
135: .getControlByFullName("number")).getViewModel())
136: .getSimpleValue();
137: assertEquals(notNumber, simpleValue);
138:
139: // this is not true (because of legacy code in StringArrayRequestControl.process())
140: assertEquals(someText, testForm.getControlByFullName("text")
141: .getRawValue());
142:
143: simpleValue = ((FloatControl.ViewModel) ((BaseControl) testForm
144: .getControlByFullName("number")).getViewModel())
145: .getSimpleValue();
146: assertEquals(notNumber, simpleValue);
147: }
148: }
|