01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.tests.constraints;
16:
17: import junit.framework.TestCase;
18: import org.araneaframework.mock.MockLifeCycle;
19: import org.araneaframework.tests.constraints.helper.ConstraintTestHelper;
20: import org.araneaframework.tests.mock.MockEnvironment;
21: import org.araneaframework.uilib.form.FormElement;
22: import org.araneaframework.uilib.form.FormWidget;
23: import org.araneaframework.uilib.form.constraint.StringLengthInRangeConstraint;
24: import org.araneaframework.uilib.form.control.TextControl;
25: import org.araneaframework.uilib.form.data.StringData;
26:
27: /**
28: * @author Taimo Peelo (taimo@araneaframework.org)
29: */
30: public class StringLengthInRangeConstraintTest extends TestCase {
31: private FormWidget form;
32: private FormElement stringElement;
33: private ConstraintTestHelper helper;
34:
35: public void setUp() throws Exception {
36: form = new FormWidget();
37: stringElement = form.createElement("#text", new TextControl(),
38: new StringData(), false);
39: form.addElement("string", stringElement);
40: MockLifeCycle.begin(form, new MockEnvironment());
41:
42: helper = new ConstraintTestHelper(form, stringElement);
43: }
44:
45: // test that null value validates if minLength == 0 and invalidates otherwise
46: public void testEmptyValidation() throws Exception {
47: helper.testConstraintValidness(
48: new StringLengthInRangeConstraint(0, 20), null, true);
49: helper.testConstraintValidness(
50: new StringLengthInRangeConstraint(1, 20), null, false);
51: }
52:
53: public void testValidation() throws Exception {
54: helper.testConstraintValidness(
55: new StringLengthInRangeConstraint(1, 1), "1", true);
56: helper.testConstraintValidness(
57: new StringLengthInRangeConstraint(2, 2), "1", false);
58: helper.testConstraintValidness(
59: new StringLengthInRangeConstraint(2, 2), "123", false);
60: helper.testConstraintValidness(
61: new StringLengthInRangeConstraint(0, 5), "1", true);
62: helper.testConstraintValidness(
63: new StringLengthInRangeConstraint(0, 5), "1234", true);
64: helper.testConstraintValidness(
65: new StringLengthInRangeConstraint(0, 5), "123456",
66: false);
67: }
68: }
|