01: package test.org.jreform;
02:
03: import static org.jreform.SelectableState.SELECTED;
04: import static org.jreform.SelectableState.UNSELECTED;
05:
06: import org.jreform.HtmlForm;
07: import org.jreform.MultiSelect;
08: import org.jreform.Select;
09: import org.jreform.SelectableState;
10:
11: public class SelectTest extends BaseTestCase {
12: private static final String COUNTRY = "country";
13: private static final String HOTELS = "hotels";
14:
15: private TestForm form;
16:
17: protected void init() {
18: form = new TestForm();
19: }
20:
21: protected HtmlForm getForm() {
22: return form;
23: }
24:
25: /** select multiple selections */
26: public void testSelectingMultipleSelections() {
27: final String england = "England";
28: final String claridges = "Claridge's";
29: final String fawlty = "Fawlty Towers";
30:
31: setParameter(COUNTRY, england);
32: setParameters(HOTELS, new String[] { claridges, fawlty });
33:
34: assertTrue(validateForm());
35:
36: assertTrue(form.country().isRequired());
37: assertTrue(form.country().isValid());
38:
39: assertFalse(form.hotels().isRequired());
40: assertTrue(form.hotels().isValid());
41:
42: assertEquals(form.country().getState().get(england), SELECTED);
43: assertEquals(form.hotels().getState().get(claridges), SELECTED);
44: assertEquals(form.hotels().getState().get(fawlty), SELECTED);
45:
46: assertEquals(form.country().getState().get("unknown"),
47: UNSELECTED);
48: assertEquals(form.hotels().getState().get("unknown"),
49: UNSELECTED);
50:
51: assertEquals(form.country().getValue(), england);
52: assertEquals(form.hotels().getValues().get(0), claridges);
53: assertEquals(form.hotels().getValues().get(1), fawlty);
54:
55: assertTrue(form.hotels().getValues().size() == 2);
56: }
57:
58: public void testSetValueAttributeSetsState() {
59: form.country().setValueAttribute("country");
60: assertEquals(SelectableState.SELECTED, form.country()
61: .getState().get("country"));
62:
63: form.hotels().setValueAttributes(new String[] { "one", "two" });
64: assertEquals(SelectableState.SELECTED, form.hotels().getState()
65: .get("one"));
66: assertEquals(SelectableState.SELECTED, form.hotels().getState()
67: .get("two"));
68: }
69:
70: private static class TestForm extends HtmlForm {
71: public TestForm() {
72: // required
73: select(stringType(), COUNTRY);
74:
75: // optional multi select
76: multiSelect(stringType(), HOTELS).optional();
77: }
78:
79: public Select<String> country() {
80: return getSelect(COUNTRY);
81: }
82:
83: public MultiSelect<String> hotels() {
84: return getMultiSelect(HOTELS);
85: }
86:
87: }
88:
89: }
|