001: package test.org.jreform;
002:
003: import static org.jreform.criteria.Criteria.minLength;
004: import static org.jreform.criteria.Criteria.range;
005:
006: import org.jreform.HtmlForm;
007: import org.jreform.MultiCheckbox;
008: import org.jreform.MultiSelect;
009:
010: //
011: // test required and optional fields:
012: // with criteria
013: // without criteria
014: //
015:
016: public class MultiInputTest extends BaseTestCase {
017: private static final int MIN = 10;
018: private static final int MAX = 20;
019:
020: private static final int MIN_LENGTH = 3;
021:
022: private static final String REQ_INT = "required_int_field";
023: private static final String OPT_INT = "optional_int_field";
024:
025: private static final String REQ_STRING = "required_string_field";
026: private static final String OPT_STRING = "optional_string_field";
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: /** Required input fails without a value */
039: public void testRequiredFieldFailsWithoutAValue() {
040: assertFalse(validateForm());
041:
042: assertTrue(form.requiredInt().isBlank());
043: assertTrue(form.requiredString().isBlank());
044:
045: assertTrue(form.requiredInt().getValueAttributes().length == 0);
046: assertTrue(form.requiredString().getValueAttributes().length == 0);
047:
048: assertFalse(form.requiredInt().isValid());
049: assertFalse(form.requiredString().isValid());
050:
051: assertTrue(form.requiredInt().getOnError().startsWith(
052: "Invalid or missing value"));
053: assertTrue(form.requiredString().getOnError().startsWith(
054: "Invalid or missing value"));
055: }
056:
057: /** Input fails if value can't be converted to input's type */
058: public void testFieldFailsIfGivenInvalidType() {
059: setRequiredRequestParameters(
060: new String[] { "Passing string instead of an int." },
061: new String[] { "some value" });
062:
063: setOptionalRequestParameters(
064: new String[] { "Passing string instead of an int." },
065: new String[] { "some value" });
066:
067: assertTrue(form.requiredInt().isBlank());
068: assertTrue(form.requiredString().isBlank());
069:
070: assertFalse(validateForm());
071:
072: assertFalse(form.requiredInt().isBlank());
073: assertFalse(form.requiredString().isBlank());
074:
075: assertTrue(form.requiredInt().getValues().isEmpty());
076: assertTrue(form.optionalInt().getValues().isEmpty());
077:
078: assertFalse(form.requiredInt().isValid());
079: assertFalse(form.optionalInt().isValid());
080:
081: assertNotNull(form.requiredInt().getOnError());
082: assertNotNull(form.optionalInt().getOnError());
083:
084: assertTrue(form.requiredInt().getOnError().startsWith(
085: "Invalid or missing value"));
086: assertTrue(form.optionalInt().getOnError().startsWith(
087: "Invalid or missing value"));
088: }
089:
090: /** Field fails if criteria are not satisfied */
091: public void testFieldFailsIfCriteriaAreNotSatisfied() {
092: String stringTooShort = "x";
093: Integer numTooBig = 100;
094:
095: setRequiredRequestParameters(new String[] { String
096: .valueOf(numTooBig) }, new String[] { "some input" });
097:
098: setOptionalRequestParameters(
099: new String[] { String.valueOf(15) },
100: new String[] { stringTooShort });
101:
102: assertFalse("Criteria not satisfied", validateForm());
103:
104: assertEquals(stringTooShort, form.optionalString()
105: .getValueAttributes()[0]);
106: assertEquals(numTooBig, form.requiredInt().getValues().get(0));
107:
108: assertFalse(form.requiredInt().isValid());
109: assertFalse(form.optionalString().isValid());
110: assertTrue(form.optionalInt().isValid());
111: assertTrue(form.requiredString().isValid());
112:
113: assertTrue(form.requiredInt().getOnError().equals(
114: "The value must be between " + MIN + " and " + MAX));
115: }
116:
117: /** Required input passes with a valid value */
118: public void testRequiredFieldPassesWithValidValue() {
119: int number = 15;
120:
121: setRequiredRequestParameters(new String[] { String
122: .valueOf(number) }, new String[] { "some input" });
123:
124: assertTrue(number >= MIN && number <= MAX);
125: assertTrue(validateForm());
126:
127: assertTrue(form.requiredInt().isValid());
128:
129: int parsedInt = form.requiredInt().getValues().get(0);
130: assertTrue(parsedInt == number);
131: assertTrue(parsedInt >= MIN);
132: assertTrue(parsedInt <= MAX);
133:
134: assertTrue(form.requiredString().isValid());
135: assertTrue(form.requiredString().getValues().get(0).length() >= MIN_LENGTH);
136: }
137:
138: /** Optional input passes without a value */
139: public void testOptionalFieldPassesWithoutAValue() {
140: setRequiredRequestParameters(new String[] { "15" },
141: new String[] { "some input" });
142:
143: assertTrue(validateForm());
144:
145: assertTrue(form.optionalInt().getValues().isEmpty());
146: assertTrue(form.optionalInt().getValueAttributes().length == 0);
147:
148: assertTrue(form.optionalString().getValues().isEmpty());
149: assertTrue(form.optionalString().getValueAttributes().length == 0);
150: }
151:
152: private void setRequiredRequestParameters(String[] intField,
153: String[] stringField) {
154: setParameters(REQ_INT, intField);
155: setParameters(REQ_STRING, stringField);
156: }
157:
158: private void setOptionalRequestParameters(String[] intField,
159: String[] stringField) {
160: setParameters(OPT_INT, intField);
161: setParameters(OPT_STRING, stringField);
162: }
163:
164: private static class TestForm extends HtmlForm {
165: @SuppressWarnings("unchecked")
166: public TestForm() {
167: // required and optional fields with criteria
168: multiSelect(intType(), REQ_INT, range(MIN, MAX));
169: multiCheckbox(stringType(), OPT_STRING,
170: minLength(MIN_LENGTH)).optional();
171:
172: // required and optional fields without criteria
173: multiCheckbox(stringType(), REQ_STRING);
174: multiSelect(intType(), OPT_INT).optional();
175: }
176:
177: public MultiSelect<Integer> requiredInt() {
178: return getMultiSelect(REQ_INT);
179: }
180:
181: public MultiSelect<Integer> optionalInt() {
182: return getMultiSelect(OPT_INT);
183: }
184:
185: public MultiCheckbox<String> requiredString() {
186: return getMultiCheckbox(REQ_STRING);
187: }
188:
189: public MultiCheckbox<String> optionalString() {
190: return getMultiCheckbox(OPT_STRING);
191: }
192: }
193:
194: }
|