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 java.math.BigInteger;
018: import java.text.SimpleDateFormat;
019: import java.util.Iterator;
020: import junit.framework.TestCase;
021: import org.araneaframework.InputData;
022: import org.araneaframework.core.StandardScope;
023: import org.araneaframework.http.core.StandardServletInputData;
024: import org.araneaframework.tests.mock.MockEnvironment;
025: import org.araneaframework.tests.mock.MockUiLibUtil;
026: import org.araneaframework.tests.util.RequestUtil;
027: import org.araneaframework.uilib.form.FormElement;
028: import org.araneaframework.uilib.form.FormWidget;
029: import org.araneaframework.uilib.form.constraint.ConstraintGroupHelper;
030: import org.araneaframework.uilib.form.constraint.NotEmptyConstraint;
031: import org.araneaframework.uilib.form.constraint.NumberInRangeConstraint;
032: import org.araneaframework.uilib.form.constraint.OptionalConstraint;
033: import org.araneaframework.uilib.form.constraint.RangeConstraint;
034: import org.araneaframework.uilib.form.control.ButtonControl;
035: import org.araneaframework.uilib.form.control.CheckboxControl;
036: import org.araneaframework.uilib.form.control.DateTimeControl;
037: import org.araneaframework.uilib.form.control.TextControl;
038: import org.araneaframework.uilib.form.data.BooleanData;
039: import org.araneaframework.uilib.form.data.DateData;
040: import org.araneaframework.uilib.form.data.LongData;
041: import org.springframework.mock.web.MockHttpServletRequest;
042:
043: /**
044: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
045: *
046: */
047: public class FormConstraintTest extends TestCase {
048: boolean eventsWork = false;
049:
050: public FormConstraintTest(String name) {
051: super (name);
052: }
053:
054: private FormWidget makeUsualForm() throws Exception {
055:
056: //Creating form :-)
057: FormWidget testForm = new FormWidget();
058: testForm._getComponent().init(
059: new StandardScope("testForm", null),
060: new MockEnvironment());
061:
062: //Adding elements to form
063: testForm.addElement("myCheckBox", "my checkbox",
064: new CheckboxControl(), new BooleanData(), true);
065: testForm.addElement("myLongText", "my long text",
066: new TextControl(), new LongData(), false);
067: testForm.addElement("myDateTime", "my date and time",
068: new DateTimeControl(), new DateData(), false);
069: testForm.addElement("myButton", "my button",
070: new ButtonControl(), null, false);
071:
072: markElementsRendered(testForm);
073:
074: return testForm;
075: }
076:
077: private void markElementsRendered(FormWidget testForm) {
078: for (Iterator i = testForm.getElements().values().iterator(); i
079: .hasNext();) {
080: Object element = i.next();
081: if (element instanceof FormWidget) {
082: markElementsRendered((FormWidget) element);
083: } else {
084: ((FormElement) element).rendered();
085: }
086: }
087: }
088:
089: protected void processRequest(InputData input, FormWidget testForm) {
090: markElementsRendered(testForm);
091: testForm._getWidget().update(input);
092: }
093:
094: /**
095: * Testing reading from request with a grouped constraint set.
096: */
097: public void testFormActiveGroupedConstraintInvalidates()
098: throws Exception {
099:
100: FormWidget testForm = makeUsualForm();
101:
102: MockHttpServletRequest request = new MockHttpServletRequest();
103:
104: request.addParameter("testForm.myCheckBox", "true");
105: request.addParameter("testForm.myLongText", "108");
106: request.addParameter("testForm.myDateTime", (String) null);
107:
108: // create helper
109: ConstraintGroupHelper groupHelper = new ConstraintGroupHelper();
110: testForm.getElement("myDateTime").setConstraint(
111: groupHelper.createGroupedConstraint(
112: new NotEmptyConstraint(), "active"));
113:
114: processRequest(new StandardServletInputData(request), testForm);
115:
116: groupHelper.setActiveGroup("active");
117: assertTrue(
118: "Test form must not be valid after reading from request",
119: !testForm.convertAndValidate());
120: }
121:
122: /**
123: * Testing reading from request with a primitive constraint set.
124: */
125: public void testFormOptionalConstraint() throws Exception {
126: FormWidget testForm = makeUsualForm();
127:
128: MockHttpServletRequest request = RequestUtil
129: .markSubmitted(new MockHttpServletRequest());
130:
131: //invalid 1, integer should be greater than 20000
132: request.addParameter("testForm.myCheckBox", "true");
133: request.addParameter("testForm.myLongText", "12345");
134: request.addParameter("testForm.myDateTime", (String) null);
135:
136: //Testing primitive constraint
137: testForm.getElement("myLongText").setConstraint(
138: new OptionalConstraint(new NumberInRangeConstraint(
139: BigInteger.valueOf(20000), null)));
140:
141: processRequest(new StandardServletInputData(request), testForm);
142:
143: assertTrue(
144: "Test form must not be valid after reading from request",
145: !testForm.convertAndValidate());
146:
147: //valid 1, long field is not read from request and should not be validated too
148: request = RequestUtil
149: .markSubmitted(new MockHttpServletRequest());
150:
151: request.addParameter("testForm.myCheckBox", "true");
152: request.addParameter("testForm.myDateTime", (String) null);
153:
154: //Testing primitive constraint
155: testForm.getElement("myLongText").setConstraint(
156: new OptionalConstraint(new NumberInRangeConstraint(
157: BigInteger.valueOf(20000), null)));
158:
159: processRequest(new StandardServletInputData(request), testForm);
160:
161: assertTrue(
162: "Test form must be valid after reading from request",
163: testForm.convertAndValidate());
164:
165: //valid
166:
167: request = RequestUtil
168: .markSubmitted(new MockHttpServletRequest());
169: request.addParameter("testForm.myCheckBox", "true");
170: request.addParameter("testForm.myLongText", "40000");
171: request.addParameter("testForm.myDateTime", (String) null);
172:
173: processRequest(new StandardServletInputData(request), testForm);
174:
175: assertTrue(
176: "Test form must be valid after reading from request",
177: testForm.convertAndValidate());
178:
179: //off
180:
181: request = RequestUtil
182: .markSubmitted(new MockHttpServletRequest());
183:
184: request.addParameter("testForm.myCheckBox", "true");
185: request.addParameter("testForm.myLongText", (String) null);
186: request.addParameter("testForm.myDateTime", (String) null);
187:
188: processRequest(new StandardServletInputData(request), testForm);
189:
190: assertTrue(
191: "Test form must be valid after reading from request",
192: testForm.convertAndValidate());
193:
194: }
195:
196: public void testFormRangeConstraint() throws Exception {
197:
198: FormWidget testForm = new FormWidget();
199: testForm._getComponent().init(
200: new StandardScope("testForm", null),
201: new MockEnvironment());
202:
203: //Adding elements to form
204: FormElement lo = testForm.createElement("my date and time",
205: new DateTimeControl(), new DateData(), false);
206: FormElement hi = testForm.createElement("my date and time",
207: new DateTimeControl(), new DateData(), false);
208: FormWidget date = testForm.addSubForm("date");
209: date.addElement("myDateLo", lo);
210: date.addElement("myDateHi", hi);
211:
212: MockHttpServletRequest request = new MockHttpServletRequest();
213:
214: SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
215:
216: //valid
217: request.addParameter("testForm.date.myDateLo.date", sdf
218: .format(new java.sql.Date(System.currentTimeMillis()
219: - 1000 * 60 * 60 * 24)));
220: request.addParameter("testForm.date.myDateHi.date", sdf
221: .format(new java.util.Date()));
222:
223: testForm
224: .getElement("date")
225: .setConstraint(
226: new RangeConstraint(
227: (FormElement) testForm
228: .getGenericElementByFullName("date.myDateLo"),
229: (FormElement) testForm
230: .getGenericElementByFullName("date.myDateHi"),
231: true));
232: processRequest(new StandardServletInputData(request), testForm);
233:
234: assertTrue(
235: "Test form must be valid after reading from request",
236: testForm.convertAndValidate());
237: }
238:
239: /**
240: * Test reading from request with a grouped constraint set, date is invalid.
241: */
242: public void testFormInactiveGroupedConstraintInValidates()
243: throws Exception {
244: FormWidget testForm = makeUsualForm();
245:
246: MockHttpServletRequest request = new MockHttpServletRequest();
247:
248: request.addParameter("testForm.myCheckBox", "true");
249: request.addParameter("testForm.myLongText", "108");
250: request.addParameter("testForm.myDateTime.date", "11.10.20151");
251: request.addParameter("testForm.myDateTime.time", "01:01");
252:
253: // create helper
254: ConstraintGroupHelper groupHelper = new ConstraintGroupHelper();
255: testForm.getElement("myDateTime").setConstraint(
256: groupHelper.createGroupedConstraint(
257: new NotEmptyConstraint(), "active"));
258:
259: MockUiLibUtil.emulateHandleRequest(testForm, "testForm",
260: request);
261:
262: assertFalse(
263: "Test form must be invalid after reading from request",
264: testForm.convertAndValidate());
265: }
266:
267: /**
268: * Test reading from request with a grouped constraint set, date is invalid.
269: */
270: public void anotherTestFormInactiveGroupedConstraintInValidates()
271: throws Exception {
272: FormWidget testForm = makeUsualForm();
273:
274: MockHttpServletRequest request = new MockHttpServletRequest();
275:
276: request.addParameter("testForm.myCheckBox", "true");
277: request.addParameter("testForm.myLongText", "108");
278: request.addParameter("testForm.myDateTime.date",
279: "11.10.20151278901");
280: request.addParameter("testForm.myDateTime.time", "01:01");
281:
282: // create helper
283: ConstraintGroupHelper groupHelper = new ConstraintGroupHelper();
284: testForm.getElement("myDateTime").setConstraint(
285: groupHelper.createGroupedConstraint(
286: new NotEmptyConstraint(), "active"));
287:
288: MockUiLibUtil.emulateHandleRequest(testForm, "testForm",
289: request);
290: assertFalse(
291: "Test form must be invalid after reading from request",
292: testForm.convertAndValidate());
293: }
294: }
|