Package org.strecks.bind.handler

Source Code of org.strecks.bind.handler.TestBindSimpleHandler

/*
* 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 org.apache.commons.beanutils.PropertyUtils;
import org.strecks.bind.handler.impl.BindableBean;
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.ApplicationRuntimeException;
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 TestBindSimpleHandler
{

  private BindSimpleHandler handler;

  @Configuration(beforeTestMethod = true)
  public void setUp() throws Exception
  {

    handler = new BindSimpleHandler();
    handler.setBeanLocatingExpression("targetBean");
    handler.setBeanPropertyName("integerProperty");
    handler.setConverter(new StandardBeanUtilsConverter());
    handler.setConversionHandler(new DefaultConversionHandler());

    // needed for binding outwards
    PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(new BindableBean(), "intProperty");
    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
    converter.setTargetClass(Integer.class);
    replay(converter);

    BindableBean form = new BindableBean();

    TargetBean targetBean = new TargetBean();
    form.setTargetBean(targetBean);

    handler.bindInwards(form, form, 4);
    Assert.assertEquals(targetBean.getIntegerProperty().intValue(), 4);

    verify(converter);
    reset(converter);

    converter.setTargetClass(Integer.class);
    replay(converter);
    handler.bindInwards(form, form, ConversionState.NULL);
    Assert.assertEquals(targetBean.getIntegerProperty(), null);
    verify(converter);
    reset(converter);

    converter.setTargetClass(Integer.class);
    expect(converter.toTargetType(null)).andReturn(null);
    replay(converter);
    handler.bindInwards(form, form, ConversionState.FAILURE);
    Assert.assertEquals(targetBean.getIntegerProperty(), null);
    verify(converter);
  }

  @Test
  public void testTargetBindOutwards()
  {

    BindableBean form = new BindableBean();

    TargetBean targetBean = new TargetBean();
    form.setTargetBean(targetBean);

    targetBean.setIntegerProperty(4);
    handler.bindOutwards(form, form);
    Assert.assertEquals(form.getIntProperty(), "4");

  }

  @Test
  public void testNullTargetBindOutwards()
  {
    BindableBean form = new BindableBean();
    handler.bindOutwards(form, form);
    Assert.assertEquals(form.getIntProperty(), null);
  }

  @Test
  public void testTargetBindInwards()
  {

    BindableBean form = new BindableBean();

    TargetBean targetBean = new TargetBean();
    form.setTargetBean(targetBean);

    form.setIntProperty("4");
    handler.bindInwards(form, form, null);
    Assert.assertEquals(targetBean.getIntegerProperty().intValue(), 4);

  }

  @Test
  public void testNullTargetBindInwards()
  {
    BindableBean form = new BindableBean();
    form.setIntProperty("4");
    handler.bindInwards(form, form, null);
  }

  @Test(expectedExceptions=ApplicationRuntimeException.class)
 
  public void testInwardDuffProperty()
  {
    handler.setBeanPropertyName("duffProperty");

    BindableBean form = new BindableBean();

    TargetBean targetBean = new TargetBean();
    form.setTargetBean(targetBean);

    form.setIntProperty("4");
    handler.bindInwards(form, form, null);
  }

  @Test(expectedExceptions=ApplicationRuntimeException.class)
  public void testOutwardDuffProperty()
  {
    handler.setBeanPropertyName("duffProperty");

    BindableBean form = new BindableBean();

    TargetBean targetBean = new TargetBean();
    form.setTargetBean(targetBean);

    targetBean.setIntegerProperty(4);
    handler.bindOutwards(form, form);
  }

}
TOP

Related Classes of org.strecks.bind.handler.TestBindSimpleHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.