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