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.http.core.StandardServletInputData;
019: import org.araneaframework.tests.mock.MockEnvironment;
020: import org.araneaframework.uilib.form.FormElement;
021: import org.araneaframework.uilib.form.FormWidget;
022: import org.araneaframework.uilib.form.control.ButtonControl;
023: import org.araneaframework.uilib.form.control.StringArrayRequestControl;
024: import org.araneaframework.uilib.form.control.TextControl;
025: import org.araneaframework.uilib.form.control.TextareaControl;
026: import org.araneaframework.uilib.form.converter.StringToLongConverter;
027: import org.araneaframework.uilib.form.data.LongData;
028: import org.araneaframework.uilib.form.data.StringData;
029: import org.springframework.mock.web.MockHttpServletRequest;
030:
031: /**
032: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
033: */
034: public class FormElementTest extends TestCase {
035: public void testDataCycling() throws Exception {
036: MockHttpServletRequest emptyRequest = new MockHttpServletRequest();
037: emptyRequest.addParameter("myTextBox", "");
038:
039: FormElement sfe = new FormElement();
040: sfe.setLabel("textbox");
041:
042: sfe._getComponent().init(null, new MockEnvironment());
043:
044: TextControl tb = new TextControl();
045: sfe.setMandatory(true);
046:
047: sfe.setControl(tb);
048: sfe.setConverter(new StringToLongConverter());
049: sfe.setData(new LongData());
050:
051: sfe._getWidget().update(
052: new StandardServletInputData(emptyRequest));
053: sfe.convertAndValidate();
054:
055: sfe.getData().setValue(new Long(110));
056:
057: assertEquals("The textbox must have the data item value!",
058: ((StringArrayRequestControl.ViewModel) sfe.getControl()
059: ._getViewable().getViewModel())
060: .getSimpleValue(), "110");
061:
062: sfe._getComponent().destroy();
063: }
064:
065: /** Tests FormWidget getElement methods(); */
066: public void testGetElement() throws Exception {
067: FormWidget form = new FormWidget();
068: FormElement button = form.createElement("my button",
069: new ButtonControl(), null, false);
070: form.addElement("myButton", button);
071:
072: FormWidget subForm = form.addSubForm("subForm");
073: FormElement textArea = subForm.createElement("my text area",
074: new TextareaControl(), new StringData(), true);
075: subForm.addElement("myTextarea", textArea);
076:
077: form._getComponent().init(null, new MockEnvironment());
078:
079: // first level formElement
080: assertEquals(button, form.getElement("myButton"));
081: // the subForm
082: assertEquals(subForm, form.getElement("subForm"));
083: // second level formElement attached to subform
084: assertEquals(textArea, form.getElement("subForm.myTextarea"));
085:
086: // trying to get nonexistant element should just return null
087: assertNull(form.getElement("nonexistant"));
088: assertNull(form.getElement("really.nonexistant"));
089: }
090:
091: public void testUnInitializedFormElementConversion()
092: throws Exception {
093: FormWidget form = new FormWidget();
094: FormElement element = form.createElement("labelId",
095: new TextControl(), new StringData(), true);
096:
097: // should succeed
098: element.convert();
099: }
100: }
|