001: package test.org.jreform;
002:
003: import static org.jreform.CheckableState.CHECKED;
004: import static org.jreform.CheckableState.UNCHECKED;
005:
006: import org.jreform.CheckableState;
007: import org.jreform.Checkbox;
008: import org.jreform.HtmlForm;
009: import org.jreform.MultiCheckbox;
010:
011: public class CheckboxTest extends BaseTestCase {
012: private static final String SUBSCRIBE = "subscribe";
013: private static final String CHOICES = "choices";
014: private static final String OPT_CHOICES = "optionalChoices";
015:
016: private TestForm form;
017:
018: protected void init() {
019: form = new TestForm();
020: }
021:
022: protected HtmlForm getForm() {
023: return form;
024: }
025:
026: /** single value checkbox can only be optional and is always valid */
027: public void testSingleValueCheckboxIsAlwaysOptionalAndValid() {
028: final String choiceOne = "choiceOne";
029: final String choiceTwo = "choiceTwo";
030:
031: // set required input
032: setParameters(CHOICES, new String[] { choiceOne, choiceTwo });
033:
034: assertTrue(validateForm());
035:
036: assertFalse("Can't be required", form.subscribe().isRequired());
037: assertTrue("Always valid", form.subscribe().isValid());
038:
039: assertNull(form.subscribe().getValue());
040: assertTrue(form.subscribe().getValueAttribute().equals(""));
041:
042: assertTrue(form.subscribe().getState() == UNCHECKED);
043:
044: assertTrue(form.choices().getState().get(choiceOne) == CHECKED);
045: assertTrue(form.choices().getState().get(choiceTwo) == CHECKED);
046:
047: assertTrue(form.choices().getValues().size() == 2);
048:
049: assertEquals(form.choices().getValues().get(0), choiceOne);
050: assertEquals(form.choices().getValues().get(1), choiceTwo);
051: }
052:
053: /** validation fails if required multi checkbox is not checked */
054: public void testMulticheckboxIsRequired() {
055: assertFalse(validateForm());
056:
057: assertTrue(form.choices().isRequired());
058: assertFalse(form.choices().isValid());
059:
060: assertTrue(form.choices().getValues().isEmpty());
061: assertTrue(form.choices().getValueAttributes().length == 0);
062: }
063:
064: public void testOptionalMultiCheckbox() {
065: Integer optChoiceOne = 10;
066: Integer optChoiceTwo = 99;
067:
068: setParameters(CHOICES, new String[] { "required choices" });
069: setParameters(OPT_CHOICES, new String[] {
070: optChoiceOne.toString(), optChoiceTwo.toString() });
071:
072: assertTrue(validateForm());
073:
074: assertTrue(form.optionalChoices().getState().get(
075: optChoiceOne.toString()) == CHECKED);
076: assertTrue(form.optionalChoices().getState().get(
077: optChoiceTwo.toString()) == CHECKED);
078: assertTrue(form.optionalChoices().getState().get("doesntExist") == UNCHECKED);
079: }
080:
081: public void testSetValueAttributeSetsState() {
082: form.subscribe().setValueAttribute("value");
083: assertEquals(CheckableState.CHECKED, form.subscribe()
084: .getState());
085:
086: form.choices()
087: .setValueAttributes(new String[] { "one", "two" });
088: assertEquals(CheckableState.CHECKED, form.choices().getState()
089: .get("one"));
090: assertEquals(CheckableState.CHECKED, form.choices().getState()
091: .get("two"));
092: }
093:
094: private static class TestForm extends HtmlForm {
095: public TestForm() {
096: // always optional
097: checkbox(stringType(), SUBSCRIBE);
098:
099: // required multi checkbox
100: multiCheckbox(stringType(), CHOICES);
101:
102: // optional multi checkbox
103: multiCheckbox(intType(), OPT_CHOICES).optional();
104: }
105:
106: public Checkbox<String> subscribe() {
107: return getCheckbox(SUBSCRIBE);
108: }
109:
110: public MultiCheckbox<String> choices() {
111: return getMultiCheckbox(CHOICES);
112: }
113:
114: public MultiCheckbox<Integer> optionalChoices() {
115: return getMultiCheckbox(OPT_CHOICES);
116: }
117:
118: }
119:
120: }
|