001: /*
002: * $Id: CheckGroupTest.java 475147 2006-11-15 07:57:36Z ivaynberg $ $Revision:
003: * 1.2 $ $Date: 2006-11-15 08:57:36 +0100 (Wed, 15 Nov 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018:
019: package wicket.markup.html.form;
020:
021: import java.io.Serializable;
022: import java.util.ArrayList;
023: import java.util.HashSet;
024: import java.util.List;
025: import java.util.Set;
026:
027: import wicket.RequestCycle;
028: import wicket.WicketRuntimeException;
029: import wicket.WicketTestCase;
030: import wicket.markup.html.WebMarkupContainer;
031: import wicket.model.CompoundPropertyModel;
032: import wicket.model.Model;
033: import wicket.protocol.http.MockPage;
034:
035: /**
036: * @author jcompagner
037: */
038: /**
039: * Test for RadioGroup and Radio components
040: *
041: * @author igor
042: *
043: */
044: public class CheckGroupTest extends WicketTestCase {
045:
046: /**
047: * @param name
048: */
049: public CheckGroupTest(String name) {
050: super (name);
051: }
052:
053: /**
054: * mock model object with an embedded property used to test compound
055: * property model
056: *
057: * @author igor
058: *
059: */
060: public static class MockModelObject implements Serializable {
061: private static final long serialVersionUID = 1L;
062:
063: private Set prop1 = new HashSet();
064: private String prop2;
065:
066: /**
067: * @return prop1
068: */
069: public Set getProp1() {
070: return prop1;
071: }
072:
073: /**
074: * @param prop1
075: */
076: public void setProp1(Set prop1) {
077: this .prop1 = prop1;
078: }
079:
080: /**
081: * @return prop2
082: */
083: public String getProp2() {
084: return prop2;
085: }
086:
087: /**
088: * @param prop2
089: */
090: public void setProp2(String prop2) {
091: this .prop2 = prop2;
092: }
093:
094: }
095:
096: /**
097: * test component form processing
098: */
099: public void testFormProcessing() {
100: // setup some values we will use for testing as well as a test model
101: final String check1 = "check1-selection";
102: final String check2 = "check2-selection";
103:
104: MockModelObject modelObject = new MockModelObject();
105: modelObject.setProp2(check2);
106:
107: // test model constructors
108: List list = new ArrayList();
109: Model model = new Model((Serializable) list);
110:
111: final CheckGroup group2 = new CheckGroup("group2", model);
112: assertTrue(group2.getModelObject() == list);
113:
114: final CheckGroup group3 = new CheckGroup("group3", list);
115: assertTrue(group3.getModelObject() == list);
116:
117: // set up necessary objects to emulate a form submission
118:
119: RequestCycle cycle = application.createRequestCycle();
120:
121: MockPage page = new MockPage();
122:
123: // create component hierarchy
124:
125: final Form form = new Form("form", new CompoundPropertyModel(
126: modelObject));
127:
128: final CheckGroup group = new CheckGroup("prop1");
129:
130: final WebMarkupContainer container = new WebMarkupContainer(
131: "container");
132:
133: final Check choice1 = new Check("check1", new Model(check1));
134: final Check choice2 = new Check("prop2");
135:
136: page.add(form);
137: form.add(group);
138: group.add(container);
139: container.add(choice1);
140: group.add(choice2);
141:
142: // test mock form submissions
143:
144: modelObject.getProp1().add(check1);
145:
146: form.onFormSubmitted();
147: assertTrue(
148: "running with nothing selected - model must be empty",
149: modelObject.getProp1().size() == 0);
150:
151: application.getServletRequest().setParameter(
152: group.getInputName(),
153: String.valueOf(choice1.getValue()));
154: form.onFormSubmitted();
155: assertTrue(
156: "running with choice1 selected - model must only contain value of check1",
157: modelObject.getProp1().size() == 1
158: && modelObject.getProp1().contains(check1));
159:
160: application.getServletRequest().setParameter(
161: group.getInputName(),
162: String.valueOf(choice2.getValue()));
163: form.onFormSubmitted();
164: assertTrue(
165: "running with choice2 selected - model must only contain value of check2",
166: modelObject.getProp1().size() == 1
167: && modelObject.getProp1().contains(check2));
168:
169: // throw in some nulls into the request param to make sure they are
170: // ignored
171: application.getServletRequest().getParameterMap().put(
172: group.getInputName(),
173: new String[] { null,
174: String.valueOf(choice1.getValue()), null,
175: String.valueOf(choice2.getValue()) });
176: form.onFormSubmitted();
177: assertTrue(
178: "running with choice1 and choice2 selected - model must only contain values of check1 and check2",
179: modelObject.getProp1().size() == 2
180: && modelObject.getProp1().contains(check2)
181: && modelObject.getProp1().contains(check1));
182:
183: application
184: .getServletRequest()
185: .getParameterMap()
186: .put(
187: group.getInputName(),
188: new String[] { "some weird choice uuid to test error" });
189: try {
190: form.onFormSubmitted();
191: fail("running with an invalid choice value in the request param, should fail");
192: } catch (WicketRuntimeException e) {
193:
194: }
195:
196: }
197:
198: /**
199: * test component rendering
200: *
201: * @throws Exception
202: */
203: public void testRendering() throws Exception {
204: executeTest(CheckGroupTestPage1.class,
205: "CheckGroupTestPage1_expected.html");
206: executeTest(CheckGroupTestPage2.class,
207: "CheckGroupTestPage2_expected.html");
208: executeTest(CheckGroupTestPage3.class,
209: "CheckGroupTestPage3_expected.html");
210: executeTest(CheckGroupTestPage4.class,
211: "CheckGroupTestPage4_expected.html");
212: try {
213: executeTest(CheckGroupTestPage5.class, "");
214: fail("this will always fail");
215: } catch (WicketRuntimeException e) {
216: if (e
217: .getMessage()
218: .indexOf(
219: "Check component [4:form:check2] cannot find its parent CheckGroup") < 0) {
220: fail("failed with wrong exception");
221: }
222:
223: }
224: }
225:
226: /**
227: * @throws Exception
228: */
229: public void testDisabledCheckGroup() throws Exception {
230: executeTest(CheckGroupDisabledTestPage.class,
231: "CheckGroupDisabledTestPage_expected.html");
232: }
233:
234: }
|