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.apache.commons.lang.builder.EqualsBuilder;
019: import org.araneaframework.mock.MockLifeCycle;
020: import org.araneaframework.tests.mock.MockEnvironment;
021: import org.araneaframework.uilib.form.FormElement;
022: import org.araneaframework.uilib.form.FormWidget;
023: import org.araneaframework.uilib.form.constraint.BaseFieldConstraint;
024: import org.araneaframework.uilib.form.constraint.NotEmptyConstraint;
025: import org.araneaframework.uilib.form.control.FloatControl;
026: import org.araneaframework.uilib.form.data.BigDecimalData;
027:
028: /**
029: * Tests general {@link org.araneaframework.uilib.form.Constraint} behaviour.
030: * @author Taimo Peelo (taimo@araneaframework.org)
031: */
032: public class GeneralConstraintContractTest extends TestCase {
033: private FormWidget form;
034:
035: public void setUp() throws Exception {
036: form = new FormWidget();
037: MockLifeCycle.begin(form, new MockEnvironment());
038: }
039:
040: /** Test that field constraints cannot be set on various other widgets. */
041: public void testInvalidFieldConstraintSetting() throws Exception {
042: try {
043: form.setConstraint(new NotEmptyConstraint());
044: form.validate();
045:
046: fail("Exception should have occured, because field constraint NotEmptyConstraint is not applicable to FormWidget");
047: } catch (BaseFieldConstraint.FieldConstraintException e) {
048: // ok
049: }
050: }
051:
052: /** Test that setting field constraint to null works and does not throw exception */
053: public void testFieldNullConstraint() throws Exception {
054: FormElement el = form.createElement("#number",
055: new FloatControl(), new BigDecimalData(), false);
056: el.setConstraint(new NotEmptyConstraint());
057: el.setConstraint(null);
058: form.addElement("number", el);
059:
060: assertTrue(
061: "Form is supposed to valid because constraint is not set.",
062: form.validate());
063: }
064:
065: /** Test that setting {@link FormWidget} constraint to null works and does not throw exception */
066: public void testFormNullConstraint() throws Exception {
067: form.addElement("number", "#number", new FloatControl(),
068: new BigDecimalData(), false);
069: form.getElement("number").setConstraint(
070: new NotEmptyConstraint());
071: form.getElement("number").setConstraint(null);
072:
073: form.setConstraint(new NotEmptyConstraint((FormElement) form
074: .getElement("number")));
075: form.setConstraint(null);
076:
077: assertTrue(
078: "Form is supposed to valid because constraint is not set.",
079: form.validate());
080: }
081:
082: /** Test environment propagation. */
083: public void testEnvironmentPropagation() throws Exception {
084: // test propagation when both formwidget and formelement are inited when constraint is set
085: // and constraint is set on a formelement
086: form.addElement("number", "#number", new FloatControl(),
087: new BigDecimalData(), false);
088: NotEmptyConstraint constraint = new NotEmptyConstraint();
089: form.getElement("number").setConstraint(constraint);
090: assertTrue(EqualsBuilder.reflectionEquals(form.getElement(
091: "number").getConstraintEnvironment(), constraint
092: .getEnvironment()));
093:
094: // test propagation when both formwidget and formelement are inited when constraint is set
095: // and constraint is set on a formwidget
096: form.getElement("number").setConstraint(null);
097: constraint = new NotEmptyConstraint((FormElement) form
098: .getElement("number"));
099: form.setConstraint(constraint);
100: assertTrue(EqualsBuilder.reflectionEquals(form.getElement(
101: "number").getConstraintEnvironment(), constraint
102: .getEnvironment()));
103:
104: // test propagation when constraint is set on a formelement that is not yet
105: // inited and is added to formwidget after setting the constraint
106: form.removeElement("number");
107: FormElement element = form.createElement("#number",
108: new FloatControl(), new BigDecimalData(), false);
109: constraint = new NotEmptyConstraint();
110: element.setConstraint(constraint);
111: form.addElement("number", element);
112: assertTrue(EqualsBuilder.reflectionEquals(form.getElement(
113: "number").getConstraintEnvironment(), constraint
114: .getEnvironment()));
115:
116: // test propagation when constraint is set on a formwidget while element is not yet inited
117: // and is added to formwidget after setting the constraint
118: form.removeElement("number");
119: element = form.createElement("#number", new FloatControl(),
120: new BigDecimalData(), false);
121: constraint = new NotEmptyConstraint(element);
122: form.setConstraint(constraint);
123: form.addElement("number", element);
124: assertTrue(EqualsBuilder.reflectionEquals(form.getElement(
125: "number").getConstraintEnvironment(), constraint
126: .getEnvironment()));
127:
128: // test propagation when constraint is set on uninited formwidget while element is not yet inited
129: // and is added to formwidget after setting the constraint
130: form = new FormWidget();
131: element = form.createElement("#number", new FloatControl(),
132: new BigDecimalData(), false);
133: constraint = new NotEmptyConstraint(element);
134: form.setConstraint(constraint);
135: form.addElement("number", element);
136: MockLifeCycle.begin(form, new MockEnvironment());
137: assertTrue(EqualsBuilder.reflectionEquals(form.getElement(
138: "number").getConstraintEnvironment(), constraint
139: .getEnvironment()));
140:
141: // test propagation when constraint is set on uninited formelement belonging to uninited formwidget
142: form = new FormWidget();
143: element = form.createElement("#number", new FloatControl(),
144: new BigDecimalData(), false);
145: constraint = new NotEmptyConstraint();
146: element.setConstraint(constraint);
147: form.addElement("number", element);
148: MockLifeCycle.begin(form, new MockEnvironment());
149: assertTrue(EqualsBuilder.reflectionEquals(form.getElement(
150: "number").getConstraintEnvironment(), constraint
151: .getEnvironment()));
152: }
153: }
|