Package org.strecks.bind.handler

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

/*
* 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 java.util.HashMap;
import java.util.Map;

import org.strecks.bind.internal.BindAnnotationReader;
import org.strecks.bind.internal.BindConvertInfo;
import org.strecks.converter.BindableBean;
import org.strecks.converter.NestedBindableBean;
import org.strecks.converter.SafeBeanUtilsConverter;
import org.strecks.form.impl.Gender;
import org.strecks.form.impl.Person;
import org.strecks.form.impl.SelectForm;
import org.testng.annotations.Test;

/**
* @author Phil Zoio
*/
public class TestBindablesReader
{

  @Test
  public void testSimpleBinding() throws Exception
  {

    NestedBindableBean nested = new NestedBindableBean();
    BindableBean bindable = new BindableBean();

    bindable.setNestedBindableBean(nested);
    bindable.setIntegerValue("5");

    BindAnnotationReader bindablesReader = new BindAnnotationReader();
    BindConvertInfo bindConvertInfo = bindablesReader.readBindables(bindable);
    Map<String, BindHandler> map = bindConvertInfo.getBindMap();
     
    assert map.size() == 2;

    BindHandler b = map.get("integerValue");

    assert b instanceof BindHandler;

    BindSimpleHandler b1 = (BindSimpleHandler) b;

    assert b1.getBeanLocatingExpression().equals("nestedBindableBean");
    assert b1.getBeanPropertyName().equals("integerValue");
    assert b1.getConverterClass().equals(SafeBeanUtilsConverter.class);

    // we don't know this until we've actually tried to read from or write
    // to the target
    assert b1.getConverter() != null;
    assert b1.getBeanPropertyClass() == null;

  }

  @Test
  public void testSelectInwardBinding() throws Exception
  {

    SelectForm selectForm = new SelectForm();
    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);

    Person person = new Person();
    person.setName("me");

    BindAnnotationReader bindablesReader = new BindAnnotationReader();
    BindConvertInfo bindConvertInfo = bindablesReader.readBindables(selectForm);
    Map<String, BindHandler> map = bindConvertInfo.getBindMap();
     
    assert map.size() == 1;

    BindHandler b = map.get("selectedGender");

    assert b instanceof BindSelectHandler;
    BindSelectHandler b1 = (BindSelectHandler) b;

    assert b1.getBeanLocatingExpression().equals("person");
    assert b1.getTargetBeanExpression().equals("person.gender");
    assert b1.getBeanPropertyName().equals("gender");
    assert b1.getConverterClass().equals(SafeBeanUtilsConverter.class);
    assert b1.getBeanPropertyClass() == Long.class;
    assert b1.getBeanLookupExpression().equals("availableGenders");
    assert b1.getBeanPropertyIdName().equals("id");
    assert b1.getConverter() != null;

  }

}
TOP

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

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.