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 java.math.BigInteger;
018: import junit.framework.TestCase;
019: import org.araneaframework.InputData;
020: import org.araneaframework.http.core.StandardServletInputData;
021: import org.araneaframework.mock.MockLifeCycle;
022: import org.araneaframework.tests.mock.MockEnvironment;
023: import org.araneaframework.tests.util.RequestUtil;
024: import org.araneaframework.uilib.form.FormElement;
025: import org.araneaframework.uilib.form.FormWidget;
026: import org.araneaframework.uilib.form.constraint.AndConstraint;
027: import org.araneaframework.uilib.form.constraint.NotEmptyConstraint;
028: import org.araneaframework.uilib.form.constraint.NumberInRangeConstraint;
029: import org.araneaframework.uilib.form.constraint.OrConstraint;
030: import org.araneaframework.uilib.form.control.FloatControl;
031: import org.araneaframework.uilib.form.control.TextControl;
032: import org.araneaframework.uilib.form.data.BigDecimalData;
033: import org.araneaframework.uilib.form.data.StringData;
034: import org.springframework.mock.web.MockHttpServletRequest;
035:
036: /**
037: * @author Taimo Peelo (taimo@araneaframework.org)
038: */
039: public class CompositeConstraintTest extends TestCase {
040: protected FormWidget form;
041: protected FormElement textInput1, textInput2, numberInput;
042:
043: public void setUp() throws Exception {
044: form = new FormWidget();
045: textInput1 = form.createElement("#text1", new TextControl(),
046: new StringData(), false);
047: textInput2 = form.createElement("#text2", new TextControl(),
048: new StringData(), false);
049: numberInput = form.createElement("#number", new FloatControl(),
050: new BigDecimalData(), false);
051: form.addElement("text1", textInput1);
052: form.addElement("text2", textInput2);
053: form.addElement("number", numberInput);
054: MockLifeCycle.begin(form, new MockEnvironment());
055: }
056:
057: protected void markElementsRendered() {
058: textInput1.rendered();
059: textInput2.rendered();
060: numberInput.rendered();
061: }
062:
063: protected InputData createRequestWithText(String text1, String text2) {
064: MockHttpServletRequest request = RequestUtil
065: .markSubmitted(new MockHttpServletRequest());
066: request.addParameter("text1", text1);
067: request.addParameter("text2", text2);
068: return new StandardServletInputData(request);
069: }
070:
071: protected InputData createRequestWithNumber(String number) {
072: MockHttpServletRequest request = RequestUtil
073: .markSubmitted(new MockHttpServletRequest());
074: request.addParameter("number", number);
075: return new StandardServletInputData(request);
076: }
077:
078: protected void processRequest(InputData input) {
079: markElementsRendered();
080: form._getWidget().update(input);
081: }
082:
083: protected void processRequireAll(String text1, String text2) {
084: AndConstraint requireAllConstraint = new AndConstraint();
085: requireAllConstraint
086: .setCustomErrorMessage("AND constraint violated.");
087: requireAllConstraint.addConstraint(new NotEmptyConstraint(
088: textInput1));
089: requireAllConstraint.addConstraint(new NotEmptyConstraint(
090: textInput2));
091: form.setConstraint(requireAllConstraint);
092:
093: processRequest(createRequestWithText(text1, text2));
094: }
095:
096: protected void processRequireAny(String text1, String text2) {
097: OrConstraint requireAnyConstraint = new OrConstraint();
098: requireAnyConstraint
099: .setCustomErrorMessage("OR constraint violated.");
100: requireAnyConstraint.addConstraint(new NotEmptyConstraint(
101: textInput1));
102: requireAnyConstraint.addConstraint(new NotEmptyConstraint(
103: textInput2));
104: form.setConstraint(requireAnyConstraint);
105:
106: processRequest(createRequestWithText(text1, text2));
107: }
108:
109: /** Tests that composite AND constraint set on FORM validates with input. */
110: public void testValidRequireAll() throws Exception {
111: processRequireAll("some text", "more text");
112: assertTrue("Should be valid as both textinputs are present.",
113: form.convertAndValidate());
114: }
115:
116: /** Tests that composite AND constraint set on FORM does not validate with input. */
117: public void testInvalidRequireAll() throws Exception {
118: processRequireAll("some text", null);
119: assertFalse("Should be invalid as one textinput is missing.",
120: form.convertAndValidate());
121: }
122:
123: /** Tests that composite OR constraint set on FORM validates with input. */
124: public void testValidRequireAny_AllPresent() throws Exception {
125: processRequireAny("some text", "more text");
126: assertTrue("Should be valid as both textinputs are present.",
127: form.convertAndValidate());
128: }
129:
130: /** Tests that composite OR constraint set on FORM validates with input. */
131: public void testValidRequireAny_OnePresent() throws Exception {
132: processRequireAny("some text", null);
133: assertTrue("Should be valid as one textinput is present.", form
134: .convertAndValidate());
135: }
136:
137: /** Tests that composite OR constraint set on FORM invalidates with input. */
138: public void testInvalidRequireAny_OnePresent() throws Exception {
139: processRequireAny(null, null);
140: assertFalse("Should be invalid as no textinputs are present.",
141: form.convertAndValidate());
142: }
143:
144: /** test that setting composite constraint directly on formelement works */
145: public void testCompositeSettingOnFormElementAndConstraintClearance()
146: throws Exception {
147: OrConstraint constraint = new OrConstraint();
148: constraint.addConstraint(new NumberInRangeConstraint(
149: new BigInteger("100"), new BigInteger("200")));
150: constraint.addConstraint(new NumberInRangeConstraint(
151: new BigInteger("800"), new BigInteger("900")));
152: numberInput.setConstraint(constraint);
153:
154: processRequest(createRequestWithNumber("400"));
155: assertFalse(
156: "Should be invalid as number does not fall into valid ranges.",
157: form.convertAndValidate());
158:
159: processRequest(createRequestWithNumber("150"));
160: numberInput.getValue();
161: assertTrue("Should be valid as number is in valid range.", form
162: .convertAndValidate());
163:
164: processRequest(createRequestWithNumber("872"));
165: assertTrue("Should be valid as number is in valid range.", form
166: .convertAndValidate());
167:
168: // test that clearing composite constraint works
169: constraint.clearConstraints();
170: processRequest(createRequestWithNumber("400"));
171: assertTrue(
172: "Should be valid as composite constraint should be empty.",
173: form.convertAndValidate());
174: }
175: }
|