001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.form.controller;
016:
017: import java.util.HashMap;
018: import java.util.Map;
019:
020: import org.strecks.form.impl.Gender;
021: import org.strecks.form.impl.Person;
022: import org.strecks.form.impl.SelectForm;
023: import org.testng.annotations.BeforeMethod;
024: import org.testng.annotations.Test;
025:
026: /**
027: * @author Phil Zoio
028: */
029: public class TestSelectFormBinding {
030:
031: private SelectForm selectForm;
032: private DelegatingForm delegator;
033:
034: @BeforeMethod
035: public void setUp() {
036:
037: selectForm = new SelectForm();
038: delegator = FormTestUtils.getDelegatingForm(selectForm);
039: }
040:
041: @Test
042: public void testSelectInwardBinding() {
043:
044: Map<Long, Gender> genders = new HashMap<Long, Gender>();
045: genders.put(1L, new Gender(1L, "male"));
046: genders.put(2L, new Gender(2L, "female"));
047: selectForm.setAvailableGenders(genders);
048:
049: selectForm.setSelectedGender("1");
050:
051: Person person = new Person();
052: person.setName("me");
053: selectForm.setPerson(person);
054:
055: delegator.bindInwards(null);
056:
057: // now see whether binding has worked
058: assert person.getGender().getId() == 1;
059:
060: }
061:
062: @Test
063: public void testSelectOutwardBinding() throws Exception {
064:
065: Person person = new Person();
066: person.setName("me");
067: person.setGender(new Gender(2L, "male"));
068: selectForm.setPerson(person);
069:
070: delegator.bindOutwards(null);
071:
072: assert selectForm.getSelectedGender().equals("2");
073:
074: }
075:
076: @Test
077: public void testSelectNullInwardBinding() {
078:
079: Map<Long, Gender> genders = new HashMap<Long, Gender>();
080: genders.put(1L, new Gender(1L, "male"));
081: genders.put(2L, new Gender(2L, "female"));
082: selectForm.setAvailableGenders(genders);
083:
084: selectForm.setSelectedGender("1");
085:
086: Person person = new Person();
087: person.setName("me");
088:
089: delegator.bindInwards(null);
090:
091: // now see whether binding has worked
092: assert person.getGender() == null;
093:
094: }
095:
096: @Test
097: public void testNullSelectOutwardBinding() throws Exception {
098:
099: Person person = new Person();
100: person.setName("me");
101: person.setGender(new Gender(2L, "male"));
102:
103: // don't set anything to bind to
104: // selectForm.setPerson(person);
105: selectForm.setSelectedGender("1");
106:
107: // should not fall over
108: delegator.bindOutwards(null);
109:
110: assert selectForm.getSelectedGender().equals("1");
111:
112: }
113:
114: }
|