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.constraints;
016:
017: import junit.framework.TestCase;
018: import org.araneaframework.InputData;
019: import org.araneaframework.http.core.StandardServletInputData;
020: import org.araneaframework.mock.MockLifeCycle;
021: import org.araneaframework.tests.mock.MockEnvironment;
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.constraint.NotEmptyConstraint;
026: import org.araneaframework.uilib.form.control.TextControl;
027: import org.araneaframework.uilib.form.data.StringData;
028: import org.springframework.mock.web.MockHttpServletRequest;
029:
030: /**
031: * @author Taimo Peelo (taimo@araneaframework.org)
032: */
033: public class NotEmptyConstraintTest extends TestCase {
034: protected FormWidget form;
035: protected FormElement textInput;
036:
037: public void setUp() throws Exception {
038: form = new FormWidget();
039: textInput = form.createElement("#text", new TextControl(),
040: new StringData(), false);
041: form.addElement("text", textInput);
042: textInput.rendered();
043: MockLifeCycle.begin(form, new MockEnvironment());
044: }
045:
046: protected InputData createRequestWithText(String text) {
047: MockHttpServletRequest request = RequestUtil
048: .markSubmitted(new MockHttpServletRequest());
049: request.addParameter("text", text);
050: return new StandardServletInputData(request);
051: }
052:
053: protected void processRequestWithText(String text) {
054: InputData input = createRequestWithText(text);
055: form._getWidget().update(input);
056: }
057:
058: public void testValidFormElement() throws Exception {
059: textInput.setConstraint(new NotEmptyConstraint());
060: processRequestWithText("some text");
061: assertTrue(
062: "Textinput value should have been read from request.",
063: form.convertAndValidate());
064: }
065:
066: public void testValidForm() throws Exception {
067: form.setConstraint(new NotEmptyConstraint(textInput));
068: processRequestWithText("some text");
069: assertTrue(
070: "Textinput value should have been read from request.",
071: form.convertAndValidate());
072: }
073:
074: public void testInvalidFormElement() throws Exception {
075: textInput.setConstraint(new NotEmptyConstraint());
076: processRequestWithText((String) null);
077: assertFalse(
078: "Textinput value should have not been read from request.",
079: form.convertAndValidate());
080: }
081:
082: /**
083: * TODO: this test fails now, because we are emulating previous broken behaviour of forms,
084: * unfortunately some cases of validation (isRead() of controls) fails otherwise.
085: * Test that constraint correctly detects that formelement was submitted as empty, even after
086: * FormElement.setValue is called. */
087: public void testFormElementMissingInRequestExplicitSetValue()
088: throws Exception {
089: textInput.setConstraint(new NotEmptyConstraint());
090: processRequestWithText((String) null);
091: textInput.setValue("some text");
092: assertFalse(
093: "Textinput value should have not been read from request (it was set only explicitly).",
094: form.convertAndValidate());
095: }
096:
097: public void testInvalidForm() throws Exception {
098: form.setConstraint(new NotEmptyConstraint(textInput));
099: processRequestWithText((String) null);
100: assertFalse(
101: "Textinput value should have not been read from request.",
102: form.convertAndValidate());
103: }
104: }
|