001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.tests;
016:
017: import junit.framework.TestCase;
018: import org.araneaframework.Widget;
019: import org.araneaframework.core.StandardScope;
020: import org.araneaframework.tests.mock.MockEnvironment;
021: import org.araneaframework.tests.mock.MockUiLibUtil;
022: import org.araneaframework.tests.util.RequestUtil;
023: import org.araneaframework.uilib.form.FormElement;
024: import org.araneaframework.uilib.form.FormWidget;
025: import org.araneaframework.uilib.form.control.ButtonControl;
026: import org.araneaframework.uilib.form.control.CheckboxControl;
027: import org.araneaframework.uilib.form.control.DateTimeControl;
028: import org.araneaframework.uilib.form.control.SelectControl;
029: import org.araneaframework.uilib.form.control.StringArrayRequestControl;
030: import org.araneaframework.uilib.form.control.TextControl;
031: import org.araneaframework.uilib.form.control.TextareaControl;
032: import org.araneaframework.uilib.form.data.BooleanData;
033: import org.araneaframework.uilib.form.data.DateData;
034: import org.araneaframework.uilib.form.data.LongData;
035: import org.araneaframework.uilib.form.data.StringData;
036: import org.araneaframework.uilib.support.DisplayItem;
037: import org.springframework.mock.web.MockHttpServletRequest;
038:
039: /**
040: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
041: *
042: */
043: public class WidgetTest extends TestCase {
044: boolean eventsWork = false;
045:
046: private FormWidget makeUsualForm() throws Exception {
047:
048: //Creating form :-)
049: FormWidget testForm = new FormWidget();
050:
051: //Adding elements to form
052: testForm.addElement("myCheckBox", "my checkbox",
053: new CheckboxControl(), new BooleanData(), true);
054: testForm.addElement("myLongText", "my long text",
055: new TextControl(), new LongData(), true);
056: testForm.addElement("myDateTime", "my date and time",
057: new DateTimeControl(), new DateData(), false);
058: testForm.addElement("myButton", "my button",
059: new ButtonControl(), null, false);
060:
061: //Adding a composite element
062: FormWidget hierarchyTest = testForm.addSubForm("hierarchyTest");
063: hierarchyTest.addElement("myTextarea", "my text area",
064: new TextareaControl(), new StringData(), true);
065:
066: //Filling in select control (which is under a composite element)
067: FormElement mySelectElement = hierarchyTest.addElement(
068: "mySelect", "my drop down", new SelectControl(),
069: new LongData(), true);
070: SelectControl mySelect = (SelectControl) mySelectElement
071: .getControl();
072: mySelect.addItem(new DisplayItem("1", "one"));
073: mySelect.addItem(new DisplayItem("2", "two"));
074: mySelect.addItem(new DisplayItem("3", "three"));
075: mySelect.addItem(new DisplayItem("4", "four"));
076:
077: testForm._getComponent().init(
078: new StandardScope("testForm", null),
079: new MockEnvironment());
080:
081: return testForm;
082: }
083:
084: /**
085: * Testing reading from valid request.
086: */
087: public void testFormRequestHandling() throws Exception {
088: FormWidget testForm = makeUsualForm();
089: Widget currentWidget = testForm;
090: MockHttpServletRequest validRequest = RequestUtil
091: .markSubmitted(new MockHttpServletRequest());
092:
093: validRequest.addParameter("testForm.myCheckBox", "true");
094: ((FormElement) testForm.getElement("myCheckBox")).rendered();
095:
096: validRequest.addParameter("testForm.myLongText", "108");
097: ((FormElement) testForm.getElement("myLongText")).rendered();
098:
099: ((FormElement) testForm.getElement("myDateTime")).rendered();
100: validRequest.addParameter("testForm.myDateTime.date",
101: "11.10.2015");
102: validRequest.addParameter("testForm.myDateTime.time", "01:01");
103:
104: (((FormElement) ((FormWidget) testForm
105: .getElement("hierarchyTest")).getElement("myTextarea")))
106: .rendered();
107: validRequest.addParameter("testForm.hierarchyTest.myTextarea",
108: "blah");
109: (((FormElement) ((FormWidget) testForm
110: .getElement("hierarchyTest")).getElement("mySelect")))
111: .rendered();
112: validRequest.addParameter("testForm.hierarchyTest.mySelect",
113: "2");
114:
115: MockUiLibUtil.emulateHandleRequest(currentWidget, "testForm",
116: validRequest);
117:
118: assertTrue(((StringArrayRequestControl.ViewModel) testForm
119: .getControlByFullName("myCheckBox")._getViewable()
120: .getViewModel()).getSimpleValue().equals("true"));
121: assertTrue(((StringArrayRequestControl.ViewModel) testForm
122: .getControlByFullName("myLongText")._getViewable()
123: .getViewModel()).getSimpleValue().equals("108"));
124: assertTrue(((StringArrayRequestControl.ViewModel) testForm
125: .getControlByFullName("hierarchyTest.myTextarea")
126: ._getViewable().getViewModel()).getSimpleValue()
127: .equals("blah"));
128: assertTrue(((StringArrayRequestControl.ViewModel) testForm
129: .getControlByFullName("hierarchyTest.mySelect")
130: ._getViewable().getViewModel()).getSimpleValue()
131: .equals("2"));
132: }
133: }
|