/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.form.controller;
import java.util.HashMap;
import java.util.Map;
import org.strecks.form.impl.Gender;
import org.strecks.form.impl.Person;
import org.strecks.form.impl.SelectForm;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestSelectFormBinding
{
private SelectForm selectForm;
private DelegatingForm delegator;
@BeforeMethod
public void setUp()
{
selectForm = new SelectForm();
delegator = FormTestUtils.getDelegatingForm(selectForm);
}
@Test
public void testSelectInwardBinding()
{
Map<Long, Gender> genders = new HashMap<Long, Gender>();
genders.put(1L, new Gender(1L, "male"));
genders.put(2L, new Gender(2L, "female"));
selectForm.setAvailableGenders(genders);
selectForm.setSelectedGender("1");
Person person = new Person();
person.setName("me");
selectForm.setPerson(person);
delegator.bindInwards(null);
// now see whether binding has worked
assert person.getGender().getId() == 1;
}
@Test
public void testSelectOutwardBinding() throws Exception
{
Person person = new Person();
person.setName("me");
person.setGender(new Gender(2L, "male"));
selectForm.setPerson(person);
delegator.bindOutwards(null);
assert selectForm.getSelectedGender().equals("2");
}
@Test
public void testSelectNullInwardBinding()
{
Map<Long, Gender> genders = new HashMap<Long, Gender>();
genders.put(1L, new Gender(1L, "male"));
genders.put(2L, new Gender(2L, "female"));
selectForm.setAvailableGenders(genders);
selectForm.setSelectedGender("1");
Person person = new Person();
person.setName("me");
delegator.bindInwards(null);
// now see whether binding has worked
assert person.getGender() == null;
}
@Test
public void testNullSelectOutwardBinding() throws Exception
{
Person person = new Person();
person.setName("me");
person.setGender(new Gender(2L, "male"));
// don't set anything to bind to
// selectForm.setPerson(person);
selectForm.setSelectedGender("1");
// should not fall over
delegator.bindOutwards(null);
assert selectForm.getSelectedGender().equals("1");
}
}