001: package test.org.jreform;
002:
003: import static org.jreform.criteria.Criteria.emailAddress;
004: import static org.jreform.types.StringType.stringType;
005:
006: import org.jreform.DuplicateNameException;
007: import org.jreform.Group;
008: import org.jreform.HtmlForm;
009: import org.jreform.Input;
010:
011: public class InputGroupTest extends BaseTestCase {
012: private static final String CONTACT = "contact";
013: private static final String METRIC = "metric";
014: private static final String IMPERIAL = "imperial";
015: private static final String MISSING_HEIGHT_WEIGHT = "missingHeightWeight";
016:
017: private static final String SURNAME = "surname";
018: private static final String PHONE = "phone";
019: private static final String EMAIL = "email";
020:
021: private static final String HEIGHT_M = "heightMetres";
022: private static final String WEIGHT_KG = "weightKg";
023:
024: private static final String HEIGHT_FT = "heightFt";
025: private static final String HEIGHT_IN = "heightInches";
026: private static final String WEIGHT_LB = "weightLbs";
027:
028: private TestForm form;
029:
030: protected void init() {
031: form = new TestForm();
032: }
033:
034: protected HtmlForm getForm() {
035: return form;
036: }
037:
038: /** test add duplicate input throws exception */
039: public void testAddDuplicateInputThrowsException() {
040: try {
041: // add to the form
042: form.input(stringType(), SURNAME);
043: fail("cannot add inputs with identical names");
044: } catch (DuplicateNameException ex) { /* empty */
045: }
046:
047: try {
048: // add to another group
049: form.getGroup(METRIC).input(stringType(), SURNAME);
050: fail("cannot add inputs with identical names");
051: } catch (DuplicateNameException ex) { /* empty */
052: }
053:
054: }
055:
056: /** test input instance is the same from form and group */
057: public void testInputInstanceIsTheSameFromFormAndGroup() {
058: setContactGroupWithValidData();
059: setMetric("1.9", "95");
060:
061: assertTrue(validateForm());
062: assertSame(form.getInput(EMAIL), form.getGroup(CONTACT)
063: .getInput(EMAIL));
064: }
065:
066: /** validation fails if contact group is empty */
067: public void testValidationFailsIfContactGroupIsEmpty() {
068: setMetric("1.9", "95");
069:
070: assertFalse(validateForm());
071:
072: assertTrue(form.getGroup(CONTACT).isEmpty());
073: assertFalse(form.getGroup(CONTACT).isValid());
074:
075: assertFalse(form.getGroup(METRIC).isEmpty());
076: assertTrue(form.getGroup(METRIC).isValid());
077:
078: assertTrue(form.getGroup(IMPERIAL).isEmpty());
079: assertTrue(form.getGroup(IMPERIAL).isValid());
080:
081: assertFalse(form.getErrors().contains(MISSING_HEIGHT_WEIGHT));
082: }
083:
084: /** test additonal validation fails if both optional groups are empty */
085: public void testAdditionalValidationFailsIfBothOptionalGroupsAreEmpty() {
086: setContactGroupWithValidData();
087:
088: assertFalse(validateForm());
089: assertTrue(form.getGroup(METRIC).isEmpty());
090: assertTrue(form.getGroup(IMPERIAL).isEmpty());
091: assertTrue(form.getErrors().contains(MISSING_HEIGHT_WEIGHT));
092: }
093:
094: /** validation passes if metric group has valid data */
095: public void testValidationPassesIfMetricGroupHasValidData() {
096: final double heightM = 1.75;
097: final int weightKg = 65;
098:
099: setContactGroupWithValidData();
100:
101: setMetric(String.valueOf(heightM), String.valueOf(weightKg));
102:
103: assertTrue(validateForm());
104: assertTrue(form.getGroup(METRIC).isValid());
105: assertTrue(form.getGroup(IMPERIAL).isValid());
106:
107: assertTrue(form.heightM().getValue() == heightM);
108: assertTrue(form.weightKg().getValue() == weightKg);
109: }
110:
111: /** validation passes if imperial group has valid data */
112: public void testValidationPassesIfImperialGroupHasValidData() {
113: final int heightFt = 5;
114: final int heightIn = 7;
115: final int weightLb = 135;
116:
117: setContactGroupWithValidData();
118:
119: setImperial(String.valueOf(heightFt), String.valueOf(heightIn),
120: String.valueOf(weightLb));
121:
122: assertTrue(validateForm());
123: assertTrue(form.getGroup(METRIC).isValid());
124: assertTrue(form.getGroup(IMPERIAL).isValid());
125:
126: assertTrue(form.heightFt().getValue() == heightFt);
127: assertTrue(form.heightIn().getValue() == heightIn);
128: assertTrue(form.weightLb().getValue() == weightLb);
129: }
130:
131: /** validation fails if metric group has invalid data */
132: public void testValidationFailsIfMetricGroupHasInvalidData() {
133: final double heightM = 1.75;
134:
135: setContactGroupWithValidData();
136:
137: setMetric(String.valueOf(heightM), "this should be a number");
138:
139: assertFalse(validateForm());
140:
141: assertTrue(form.getGroup(CONTACT).isValid());
142: assertFalse(form.getGroup(CONTACT).isEmpty());
143:
144: assertFalse(form.getGroup(METRIC).isValid());
145: assertFalse(form.getGroup(METRIC).isEmpty());
146:
147: assertTrue(form.getGroup(IMPERIAL).isValid());
148: assertTrue(form.getGroup(IMPERIAL).isEmpty());
149:
150: assertTrue(form.heightM().getValue() == heightM);
151: }
152:
153: /** validation fails if contact group has invalid data */
154: public void testValidationFailsIfContactGroupHasInvalidData() {
155: setContact("Adam", "Smith",
156: "this should be a valid email address");
157:
158: setMetric("1.55", "65");
159:
160: assertFalse(validateForm());
161:
162: assertFalse(form.getGroup(CONTACT).isValid());
163: assertFalse(form.getGroup(CONTACT).isEmpty());
164: assertFalse(form.getInput(EMAIL).isValid());
165:
166: assertTrue(form.getGroup(METRIC).isValid());
167: assertFalse(form.getGroup(METRIC).isEmpty());
168:
169: assertTrue(form.getGroup(IMPERIAL).isValid());
170: assertTrue(form.getGroup(IMPERIAL).isEmpty());
171: }
172:
173: private void setContactGroupWithValidData() {
174: setContact("Adam", "Smith", "adam@smith.com");
175: }
176:
177: private void setContact(String surname, String phone, String email) {
178: setParameter(SURNAME, surname);
179: setParameter(PHONE, phone);
180: setParameter(EMAIL, email);
181: }
182:
183: private void setMetric(String m, String kg) {
184: setParameter(HEIGHT_M, m);
185: setParameter(WEIGHT_KG, kg);
186: }
187:
188: private void setImperial(String ft, String in, String lb) {
189: setParameter(HEIGHT_FT, ft);
190: setParameter(HEIGHT_IN, in);
191: setParameter(WEIGHT_LB, lb);
192: }
193:
194: private static class TestForm extends HtmlForm {
195: @SuppressWarnings("unchecked")
196: public TestForm() {
197: Group contact = requiredGroup(CONTACT);
198: contact.input(stringType(), SURNAME);
199: contact.input(stringType(), PHONE);
200: contact.input(stringType(), EMAIL, emailAddress())
201: .optional();
202:
203: Group metric = optionalGroup(METRIC);
204: metric.input(doubleType(), HEIGHT_M);
205: metric.input(intType(), WEIGHT_KG);
206:
207: Group imperial = optionalGroup(IMPERIAL);
208: imperial.input(intType(), HEIGHT_FT);
209: imperial.input(intType(), HEIGHT_IN);
210: imperial.input(intType(), WEIGHT_LB);
211:
212: try {
213: Group duplicate = optionalGroup(IMPERIAL);
214: fail("Error - duplicate group name must throw an exception");
215: } catch (DuplicateNameException ex) {
216: }
217: }
218:
219: protected void additionalValidate() {
220: Group metric = getGroup(METRIC);
221: Group imperial = getGroup(IMPERIAL);
222:
223: if (metric.isEmpty() && imperial.isEmpty()) {
224: addError(MISSING_HEIGHT_WEIGHT);
225: }
226: }
227:
228: public Input<Double> heightM() {
229: return getInput(HEIGHT_M);
230: }
231:
232: public Input<Integer> weightKg() {
233: return getInput(WEIGHT_KG);
234: }
235:
236: public Input<Integer> heightFt() {
237: return getInput(HEIGHT_FT);
238: }
239:
240: public Input<Integer> heightIn() {
241: return getInput(HEIGHT_IN);
242: }
243:
244: public Input<Integer> weightLb() {
245: return getInput(WEIGHT_LB);
246: }
247:
248: }
249:
250: }
|