001: package test.org.jreform;
002:
003: import static org.jreform.CheckableState.CHECKED;
004: import static org.jreform.CheckableState.UNCHECKED;
005: import static org.jreform.criteria.Criteria.accept;
006: import static org.jreform.criteria.Criteria.acceptString;
007:
008: import org.jreform.CheckableState;
009: import org.jreform.HtmlForm;
010: import org.jreform.Radio;
011:
012: public class RadioTest extends BaseTestCase {
013: private static final String SUBSCRIBE = "subscribe";
014: private static final String GENDER = "gender";
015:
016: private static final String SUBSCRIBE_ERROR_MSG = "Must be yes or no";
017: private static final String GENDER_ERROR_MSG = "M or F expected";
018:
019: private TestForm form;
020:
021: protected void init() {
022: form = new TestForm();
023: }
024:
025: protected HtmlForm getForm() {
026: return form;
027: }
028:
029: /** validation fails if required radio button is not checked */
030: public void testValidationFailsIfRequiredRadioNotChecked() {
031: assertFalse(validateForm());
032:
033: assertTrue(form.subscribe().isRequired());
034: assertFalse(form.subscribe().isValid());
035:
036: assertNull(form.subscribe().getValue());
037: assertTrue(form.subscribe().getValueAttribute().equals(""));
038:
039: assertEquals(form.subscribe().getState().get("yes"), UNCHECKED);
040: assertEquals(form.subscribe().getState().get("no"), UNCHECKED);
041:
042: assertEquals(form.subscribe().getOnError(), SUBSCRIBE_ERROR_MSG);
043: }
044:
045: /** validation fails if criteria fail for required radio button */
046: public void testValidationFailsIfRequiredRadioCriteriaFail() {
047: final String value = "need_yes_or_no_to_validate_successfully";
048: setParameter(SUBSCRIBE, value);
049:
050: assertFalse(validateForm());
051: assertFalse(form.subscribe().isValid());
052:
053: assertEquals(form.subscribe().getValue(), value);
054: assertEquals(form.subscribe().getOnError(), SUBSCRIBE_ERROR_MSG);
055: }
056:
057: /** validation passes if required radio button has a valid value */
058: public void testValidationPassesIfRequiredRadioButtonHasValidValue() {
059: final String yes = "YES"; // ignoreCase() enabled
060:
061: setParameter(SUBSCRIBE, yes);
062:
063: assertTrue(validateForm());
064: assertTrue(form.subscribe().isValid());
065: assertTrue(form.subscribe().getOnError().equals(""));
066:
067: assertEquals(form.subscribe().getValue(), yes);
068: assertEquals(form.subscribe().getState().get(yes), CHECKED);
069: }
070:
071: /** validation fails if optional radio button has invalid value */
072: public void testValidationFailsIfOptionalRadioCriteriaFail() {
073: char invalidValue = 'm'; // criterion is case sensitive
074:
075: setParameter(SUBSCRIBE, "no"); // set required radio
076: setParameter(GENDER, String.valueOf(invalidValue));
077:
078: assertFalse(validateForm());
079:
080: assertTrue(form.subscribe().isValid());
081:
082: assertFalse(form.gender().isValid());
083: assertFalse(form.gender().isRequired());
084:
085: assertEquals(form.gender().getValue(), invalidValue);
086: assertEquals(form.gender().getOnError(), GENDER_ERROR_MSG);
087: }
088:
089: /** validation passes when optional and required radios have valid values */
090: public void testValidationPassesIfOptionalRadioHasValidValue() {
091: final String no = "no";
092: final char validValue = 'F';
093:
094: setParameter(SUBSCRIBE, no); // set required radio
095: setParameter(GENDER, String.valueOf(validValue));
096:
097: assertTrue(validateForm());
098:
099: assertTrue(form.subscribe().isValid());
100: assertTrue(form.gender().isValid());
101: assertTrue(form.gender().getOnError().equals(""));
102:
103: assertEquals(form.gender().getValue(), validValue);
104:
105: assertEquals(form.subscribe().getState().get(no), CHECKED);
106: assertEquals(form.gender().getState().get(
107: String.valueOf(validValue)), CHECKED);
108: }
109:
110: public void testSetValueAttributeSetsState() {
111: form.gender().setValueAttribute("X");
112: assertEquals(CheckableState.CHECKED, form.gender().getState()
113: .get("X"));
114: }
115:
116: private static class TestForm extends HtmlForm {
117: @SuppressWarnings("unchecked")
118: public TestForm() {
119: // required
120: radio(stringType(), SUBSCRIBE,
121: acceptString("yes", "no").ignoreCase()).onError(
122: SUBSCRIBE_ERROR_MSG);
123:
124: // optional
125: radio(charType(), GENDER, accept('M', 'F')).optional()
126: .onError(GENDER_ERROR_MSG);
127: }
128:
129: public Radio<String> subscribe() {
130: return getRadio(SUBSCRIBE);
131: }
132:
133: public Radio<String> gender() {
134: return getRadio(GENDER);
135: }
136: }
137:
138: }
|