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.BigDecimal;
018: import java.math.BigInteger;
019: import java.util.List;
020: import junit.framework.TestCase;
021: import org.araneaframework.core.StandardScope;
022: import org.araneaframework.tests.mock.MockEnvironment;
023: import org.araneaframework.tests.mock.MockFormElementContext;
024: import org.araneaframework.tests.mock.MockUiLibUtil;
025: import org.araneaframework.uilib.form.control.FloatControl;
026: import org.araneaframework.uilib.form.control.MultiSelectControl;
027: import org.araneaframework.uilib.form.control.NumberControl;
028: import org.araneaframework.uilib.form.control.StringArrayRequestControl;
029: import org.araneaframework.uilib.form.control.TextControl;
030: import org.springframework.mock.web.MockHttpServletRequest;
031:
032: /**
033: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
034: *
035: */
036: public class FormControlTest extends TestCase {
037:
038: /**
039: * Tests that {@link MultiSelectControl} returns an empty <code>List</code> for an empty request.
040: * @throws Exception
041: */
042: public void testMultiSelectOnEmptyRequest() throws Exception {
043:
044: MockHttpServletRequest emptyRequest = new MockHttpServletRequest();
045: MultiSelectControl ms = new MultiSelectControl();
046: ms.setFormElementCtx(new MockFormElementContext());
047: ms._getComponent().init(
048: new StandardScope("myMultiSelect", null),
049: new MockEnvironment());
050: MockUiLibUtil.emulateHandleRequest(ms, "myMultiSelect",
051: emptyRequest);
052: ms.convertAndValidate();
053:
054: assertNotNull(
055: "MultiSelect must not return null if it's not present in request",
056: ms.getRawValue());
057: assertTrue(
058: "MultiSelect must return List if it's not present in request",
059: ms.getRawValue() instanceof List);
060: assertTrue(
061: "MultiSelect must return empty List if it's not present in request",
062: ((List) ms.getRawValue()).size() == 0);
063:
064: ms._getComponent().destroy();
065: }
066:
067: /**
068: * Tests that {@link TextControl} return <code>null</code> on empty request.
069: * @throws Exception
070: */
071: public void testTextboxOnEmptyRequest() throws Exception {
072: MockHttpServletRequest emptyRequest = new MockHttpServletRequest();
073: emptyRequest.addParameter("myTextBox", "");
074:
075: TextControl textControl = new TextControl();
076: textControl.setFormElementCtx(new MockFormElementContext());
077: textControl._getComponent().init(new StandardScope(null, null),
078: new MockEnvironment());
079: MockUiLibUtil.emulateHandleRequest(textControl, "myTextBox",
080: emptyRequest);
081: textControl.convertAndValidate();
082:
083: assertNull("TextBox must return null on empty request.",
084: textControl.getRawValue());
085:
086: textControl._getComponent().destroy();
087: }
088:
089: /**
090: * Tests that {@link TextControl} return <code>null</code> on empty request.
091: * @throws Exception
092: */
093: public void testControlRequestDataSaving() throws Exception {
094: MockHttpServletRequest valueRequest = new MockHttpServletRequest();
095:
096: String DEV_NULL = "/dev/null";
097:
098: valueRequest.addParameter("myTextBox", DEV_NULL);
099:
100: TextControl textControl = new TextControl();
101: textControl.setFormElementCtx(new MockFormElementContext());
102: textControl._getComponent().init(
103: new StandardScope("myTextBox", null),
104: new MockEnvironment());
105: MockUiLibUtil.emulateHandleRequest(textControl, "myTextBox",
106: valueRequest);
107: StringArrayRequestControl.ViewModel vm = (StringArrayRequestControl.ViewModel) textControl
108: ._getViewable().getViewModel();
109:
110: assertEquals("TextBox must contain the value from request!",
111: DEV_NULL, vm.getSimpleValue());
112:
113: textControl._getComponent().destroy();
114: }
115:
116: //***********************************************************************
117: // Client-side validation tests
118: //***********************************************************************
119: /**
120: * Tests that {@link NumberControl} lets only valid integers through.
121: */
122: public void testNumberControlSimpleValidationNew() throws Exception {
123: MockHttpServletRequest correctValueRequest = new MockHttpServletRequest();
124: correctValueRequest.addParameter("myNumberInput", "108");
125:
126: NumberControl numberControl = new NumberControl();
127:
128: MockFormElementContext mockFormElementContext = new MockFormElementContext(
129: "TheLabel", false, false);
130:
131: numberControl.setFormElementCtx(mockFormElementContext);
132: numberControl._getComponent().init(
133: new StandardScope("myNumberInput", null),
134: new MockEnvironment());
135:
136: MockUiLibUtil.emulateHandleRequest(numberControl,
137: "myNumberInput", correctValueRequest);
138: numberControl.convertAndValidate();
139:
140: assertTrue("Number control must be valid.",
141: mockFormElementContext.isValid());
142: assertTrue("Number control value must be a 'BigInteger'.",
143: numberControl.getRawValue() instanceof BigInteger);
144: assertTrue(
145: "Number control value must be '108'.",
146: ((BigInteger) numberControl.getRawValue()).longValue() == 108L);
147:
148: MockHttpServletRequest incorrectValueRequest = new MockHttpServletRequest();
149: incorrectValueRequest.addParameter("myNumberInput", "abcd");
150:
151: MockUiLibUtil.emulateHandleRequest(numberControl,
152: "myNumberInput", incorrectValueRequest);
153: numberControl.convertAndValidate();
154:
155: assertTrue("Number control mustn't be valid.",
156: !mockFormElementContext.isValid());
157:
158: numberControl._getComponent().destroy();
159: }
160:
161: /**
162: * Tests that {@link NumberControl} uses the min/max value parameters
163: * for validation.
164: */
165: public void testNumberControlMinMaxValidation() throws Exception {
166: MockHttpServletRequest correctValueRequest = new MockHttpServletRequest();
167: correctValueRequest.addParameter("myNumberInput", "50");
168:
169: MockFormElementContext mockFormElementContext = new MockFormElementContext(
170: "TheLabel", false, false);
171: NumberControl numberControl = new NumberControl();
172: numberControl.setFormElementCtx(mockFormElementContext);
173: numberControl._getComponent().init(
174: new StandardScope("myNumberInput", null),
175: new MockEnvironment());
176:
177: numberControl.setMinValue(new BigInteger("25"));
178: numberControl.setMaxValue(new BigInteger("75"));
179:
180: MockUiLibUtil.emulateHandleRequest(numberControl,
181: "myNumberInput", correctValueRequest);
182: numberControl.convertAndValidate();
183:
184: assertTrue("Number control must be valid.",
185: mockFormElementContext.isValid());
186: assertTrue(
187: "Number control value must be '50'.",
188: ((BigInteger) numberControl.getRawValue()).longValue() == 50L);
189:
190: MockHttpServletRequest tooLittleValueRequest = new MockHttpServletRequest();
191: tooLittleValueRequest.addParameter("myNumberInput", "20");
192:
193: MockUiLibUtil.emulateHandleRequest(numberControl,
194: "myNumberInput", tooLittleValueRequest);
195: numberControl.convertAndValidate();
196:
197: assertTrue("Number control mustn't be valid.",
198: !mockFormElementContext.isValid());
199:
200: MockHttpServletRequest tooBigValueRequest = new MockHttpServletRequest();
201: tooBigValueRequest.addParameter("myNumberInput", "80");
202:
203: MockUiLibUtil.emulateHandleRequest(numberControl,
204: "myNumberInput", tooBigValueRequest);
205: numberControl.convertAndValidate();
206:
207: assertTrue("Number control mustn't be valid.",
208: !mockFormElementContext.isValid());
209:
210: numberControl._getComponent().destroy();
211: }
212:
213: /**
214: * Tests that {@link FloatControl} lets only valid decimals through.
215: */
216: public void testFloatControlSimpleValidation() throws Exception {
217: MockHttpServletRequest correctValueRequest = new MockHttpServletRequest();
218: correctValueRequest.addParameter("myFloatInput", "28.012");
219:
220: FloatControl nc = new FloatControl();
221: MockFormElementContext mockFormElementContext = new MockFormElementContext(
222: "TheLabel", false, false);
223: nc.setFormElementCtx(mockFormElementContext);
224: nc._getComponent().init(
225: new StandardScope("myFloatInput", null),
226: new MockEnvironment());
227: MockUiLibUtil.emulateHandleRequest(nc, "myFloatInput",
228: correctValueRequest);
229: nc.convertAndValidate();
230:
231: assertTrue("Float control must be valid.",
232: mockFormElementContext.isValid());
233: assertTrue("Float control value must be a 'BigDecimal'.", nc
234: .getRawValue() instanceof BigDecimal);
235: assertTrue("Float control value must be '28.012'.",
236: ((BigDecimal) nc.getRawValue()).doubleValue() == 28.012);
237:
238: MockHttpServletRequest incorrectValueRequest = new MockHttpServletRequest();
239: incorrectValueRequest.addParameter("myFloatInput", "abcd");
240:
241: MockUiLibUtil.emulateHandleRequest(nc, "myFloatInput",
242: incorrectValueRequest);
243: nc.convertAndValidate();
244:
245: assertTrue("Float control mustn't be valid.",
246: !mockFormElementContext.isValid());
247:
248: nc._getComponent().destroy();
249: }
250:
251: /**
252: * Tests that {@link FloatControl} uses the min/max value parameters
253: * for validation.
254: */
255: public void testFloatControlMinMaxValidation() throws Exception {
256: MockHttpServletRequest correctValueRequest = new MockHttpServletRequest();
257: correctValueRequest.addParameter("myFloatInput", "50.0018");
258:
259: FloatControl numberControl = new FloatControl();
260: MockFormElementContext mockFormElementContext = new MockFormElementContext(
261: "TheLabel", false, false);
262: numberControl.setFormElementCtx(mockFormElementContext);
263: numberControl._getComponent().init(
264: new StandardScope("myFloatInput", null),
265: new MockEnvironment());
266:
267: numberControl.setMinValue(new BigDecimal("25.001"));
268: numberControl.setMaxValue(new BigDecimal("75.002"));
269:
270: MockUiLibUtil.emulateHandleRequest(numberControl,
271: "myFloatInput", correctValueRequest);
272: numberControl.convertAndValidate();
273:
274: assertTrue("Float control must be valid.",
275: mockFormElementContext.isValid());
276: assertTrue("Float control value must be '50.0018'.",
277: ((BigDecimal) numberControl.getRawValue())
278: .doubleValue() == 50.0018);
279:
280: MockHttpServletRequest tooLittleValueRequest = new MockHttpServletRequest();
281: tooLittleValueRequest.addParameter("myFloatInput", "20.1");
282:
283: MockUiLibUtil.emulateHandleRequest(numberControl,
284: "myFloatInput", tooLittleValueRequest);
285: numberControl.convertAndValidate();
286:
287: assertTrue("Float control mustn't be valid.",
288: !mockFormElementContext.isValid());
289:
290: MockHttpServletRequest tooBigValueRequest = new MockHttpServletRequest();
291: tooBigValueRequest.addParameter("myFloatInput", "80.2");
292:
293: MockUiLibUtil.emulateHandleRequest(numberControl,
294: "myFloatInput", tooBigValueRequest);
295: numberControl.convertAndValidate();
296:
297: assertTrue("Float control mustn't be valid.",
298: !mockFormElementContext.isValid());
299: }
300:
301: /**
302: * Tests that {@link TextControl} uses the min/max length parameters
303: * for validation.
304: */
305: public void testTextboxControlMinMaxValidation() throws Exception {
306: //Basic
307: MockHttpServletRequest correctValueRequest = new MockHttpServletRequest();
308: correctValueRequest.addParameter("myTextBox", "i love me");
309:
310: TextControl textControl = new TextControl();
311: MockFormElementContext mockFormElementContext = new MockFormElementContext(
312: "TheLabel", false, false);
313: textControl.setFormElementCtx(mockFormElementContext);
314: textControl._getComponent().init(
315: new StandardScope("myTextBox", null),
316: new MockEnvironment());
317:
318: textControl.setMinLength(new Long(5));
319: textControl.setMaxLength(new Long(20));
320:
321: MockUiLibUtil.emulateHandleRequest(textControl, "myTextBox",
322: correctValueRequest);
323: textControl.convertAndValidate();
324:
325: assertTrue("Textbox control must be valid.",
326: mockFormElementContext.isValid());
327: assertTrue("Textbox control value must be 'i love me'.",
328: ((String) textControl.getRawValue())
329: .equals("i love me"));
330:
331: //Too short
332:
333: MockHttpServletRequest tooShortValueRequest = new MockHttpServletRequest();
334: tooShortValueRequest.addParameter("myTextBox", "boo");
335:
336: MockUiLibUtil.emulateHandleRequest(textControl, "myTextBox",
337: tooShortValueRequest);
338: textControl.convertAndValidate();
339:
340: assertTrue("Textbox control mustn't be valid.",
341: !mockFormElementContext.isValid());
342:
343: //Too long
344:
345: MockHttpServletRequest tooLongValueRequest = new MockHttpServletRequest();
346: tooLongValueRequest.addParameter("myTextBox",
347: "i love myself and others very very much");
348:
349: MockUiLibUtil.emulateHandleRequest(textControl, "myTextBox",
350: tooLongValueRequest);
351: textControl.convertAndValidate();
352:
353: assertTrue("Textbox control mustn't be valid.",
354: !mockFormElementContext.isValid());
355: mockFormElementContext.clearErrors();
356:
357: //min=max correct
358:
359: textControl.setMinLength(new Long(10));
360: textControl.setMaxLength(new Long(10));
361:
362: correctValueRequest = new MockHttpServletRequest();
363: correctValueRequest.addParameter("myTextBox", "1234567890");
364:
365: MockUiLibUtil.emulateHandleRequest(textControl, "myTextBox",
366: correctValueRequest);
367: textControl.convertAndValidate();
368:
369: assertTrue("Textbox control must be valid.",
370: mockFormElementContext.isValid());
371: assertTrue("Textbox control value must be '1234567890'.",
372: ((String) textControl.getRawValue())
373: .equals("1234567890"));
374:
375: //min=max too short
376:
377: tooShortValueRequest.addParameter("myTextBox", "123456789");
378:
379: MockUiLibUtil.emulateHandleRequest(textControl, "myTextBox",
380: tooShortValueRequest);
381: textControl.convertAndValidate();
382:
383: assertTrue("Textbox control mustn't be valid.",
384: !mockFormElementContext.isValid());
385:
386: //min=max too long
387:
388: tooShortValueRequest.addParameter("myTextBox", "12345678901");
389:
390: MockUiLibUtil.emulateHandleRequest(textControl, "myTextBox",
391: tooShortValueRequest);
392: textControl.convertAndValidate();
393:
394: assertTrue("Textbox control mustn't be valid.",
395: !mockFormElementContext.isValid());
396:
397: textControl._getComponent().destroy();
398: }
399: }
|