/*
* 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.bind.handler;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createStrictMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.reset;
import static org.easymock.classextension.EasyMock.verify;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import org.strecks.bind.handler.impl.BindableBean;
import org.strecks.bind.handler.impl.DomainClass;
import org.strecks.bind.handler.impl.TargetBean;
import org.strecks.converter.ConversionState;
import org.strecks.converter.Converter;
import org.strecks.converter.StandardBeanUtilsConverter;
import org.strecks.converter.handler.DefaultConversionHandler;
import org.strecks.exceptions.ConversionException;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Configuration;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestBindSelectHandler
{
private BindSelectHandler handler;
@Configuration(beforeTestMethod = true)
public void setUp() throws Exception
{
handler = new BindSelectHandler();
handler.setBeanLocatingExpression("targetBean");
handler.setTargetBeanExpression("targetBean.domainClass");
handler.setBeanPropertyIdName("id");
handler.setBeanLookupExpression("lookupMap");
handler.setBeanPropertyName("domainClass");
handler.setBeanPropertyClass(Integer.class);
handler.setConversionHandler(new DefaultConversionHandler());
Converter converter = new StandardBeanUtilsConverter();
handler.setConverter(converter);
converter.setTargetClass(Integer.class);
// needed for binding outwards
PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(new BindableBean(), "selectedId");
handler.setPropertyDescriptor(propertyDescriptor);
}
@SuppressWarnings("unchecked")
@Test
public void testConvertedValueBindInwards() throws ConversionException
{
Converter converter = createStrictMock(Converter.class);
handler.setConverter(converter);
// make sure converter does not get called for conversion
replay(converter);
BindableBean form = new BindableBean();
TargetBean targetBean = new TargetBean();
form.setTargetBean(targetBean);
handler.bindInwards(form, form, 4);
verify(converter);
reset(converter);
replay(converter);
handler.bindInwards(form, form, ConversionState.NULL);
Assert.assertEquals(targetBean.getIntegerProperty(), null);
verify(converter);
reset(converter);
expect(converter.toTargetType(null)).andReturn(null);
replay(converter);
handler.bindInwards(form, form, ConversionState.FAILURE);
Assert.assertEquals(targetBean.getIntegerProperty(), null);
verify(converter);
}
@SuppressWarnings("unchecked")
@Test
public void testTargetBindOutwards()
{
handler.setBeanLookupExpression(null);
handler.setTargetBeanExpression(null);
handler.setBeanPropertyName("domainClass");
handler.setBeanPropertyClass(Integer.class);
BindableBean form = new BindableBean();
Collection collection = new ArrayList();
collection.add(new DomainClass(1));
collection.add(new DomainClass(3));
collection.add(new DomainClass(4));
Map map = handler.getPropertyAsMap(collection);
form.setLookupMap(map);
TargetBean targetBean = new TargetBean();
form.setTargetBean(targetBean);
targetBean.setDomainClass(new DomainClass(4));
handler.bindOutwards(form, form);
Assert.assertEquals(form.getSelectedId(), "4");
}
@SuppressWarnings("unchecked")
@Test
public void testEmptyMapBindOutwards()
{
BindableBean form = new BindableBean();
Map map = handler.getPropertyAsMap(new HashMap());
form.setLookupMap(map);
TargetBean targetBean = new TargetBean();
form.setTargetBean(targetBean);
targetBean.setDomainClass(new DomainClass(4));
handler.bindOutwards(form, form);
Assert.assertEquals(form.getSelectedId(), "4");
}
@SuppressWarnings("unchecked")
@Test
public void testNullTargetBindOutwards()
{
BindableBean form = new BindableBean();
Collection collection = new ArrayList();
collection.add(new DomainClass(1));
collection.add(new DomainClass(3));
collection.add(new DomainClass(4));
Map map = handler.getPropertyAsMap(collection);
form.setLookupMap(map);
TargetBean targetBean = new TargetBean();
// form.setTargetBean(targetBean);
targetBean.setDomainClass(new DomainClass(4));
handler.bindOutwards(form, form);
Assert.assertEquals(form.getSelectedId(), null);
}
@SuppressWarnings("unchecked")
@Test
public void testNullMapBindOutwards()
{
handler.setBeanLookupExpression(null);
handler.setTargetBeanExpression(null);
handler.setBeanPropertyName("domainClass");
handler.setBeanPropertyClass(Integer.class);
BindableBean form = new BindableBean();
form.setLookupMap(null);
TargetBean targetBean = new TargetBean();
form.setTargetBean(targetBean);
targetBean.setDomainClass(new DomainClass(4));
handler.bindOutwards(form, form);
Assert.assertEquals(form.getSelectedId(), "4");
}
@SuppressWarnings("unchecked")
@Test
public void testTargetBindInwards()
{
BindableBean form = new BindableBean();
Collection collection = new ArrayList();
collection.add(new DomainClass(1));
collection.add(new DomainClass(3));
collection.add(new DomainClass(4));
Map map = handler.getPropertyAsMap(collection);
form.setLookupMap(map);
TargetBean targetBean = new TargetBean();
form.setTargetBean(targetBean);
form.setSelectedId("1");
handler.bindInwards(form, form, null);
assert targetBean.getDomainClass().getId() == 1;
}
@SuppressWarnings("unchecked")
@Test
public void testNullTargetBindInwards()
{
BindableBean form = new BindableBean();
Collection collection = new ArrayList();
collection.add(new DomainClass(1));
collection.add(new DomainClass(3));
collection.add(new DomainClass(4));
Map map = handler.getPropertyAsMap(collection);
form.setLookupMap(map);
TargetBean targetBean = new TargetBean();
// form.setTargetBean(targetBean);
form.setSelectedId("1");
handler.bindInwards(form, form, null);
assert targetBean.getDomainClass() == null;
}
@SuppressWarnings("unchecked")
@Test
public void testEmptyMapBindInwards()
{
BindableBean form = new BindableBean();
form.setLookupMap(new HashMap());
TargetBean targetBean = new TargetBean();
form.setTargetBean(targetBean);
form.setSelectedId("1");
handler.bindInwards(form, form, null);
assert targetBean.getDomainClass() == null;
}
@SuppressWarnings("unchecked")
@Test
public void testNullMapBindInwards()
{
BindableBean form = new BindableBean();
form.setLookupMap(null);
TargetBean targetBean = new TargetBean();
form.setTargetBean(targetBean);
form.setSelectedId("1");
handler.bindInwards(form, form, null);
assert targetBean.getDomainClass() == null;
}
@SuppressWarnings("unchecked")
@Test
public void testInvalidIdBindInwards()
{
BindableBean form = new BindableBean();
Collection collection = new ArrayList();
collection.add(new DomainClass(1));
collection.add(new DomainClass(3));
collection.add(new DomainClass(4));
Map map = handler.getPropertyAsMap(collection);
form.setLookupMap(map);
TargetBean targetBean = new TargetBean();
form.setTargetBean(targetBean);
form.setSelectedId("10");
handler.bindInwards(form, form, null);
assert targetBean.getDomainClass() == null;
}
}