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.bind.handler;
016:
017: import java.util.HashMap;
018: import java.util.Map;
019:
020: import org.strecks.bind.internal.BindAnnotationReader;
021: import org.strecks.bind.internal.BindConvertInfo;
022: import org.strecks.converter.BindableBean;
023: import org.strecks.converter.NestedBindableBean;
024: import org.strecks.converter.SafeBeanUtilsConverter;
025: import org.strecks.form.impl.Gender;
026: import org.strecks.form.impl.Person;
027: import org.strecks.form.impl.SelectForm;
028: import org.testng.annotations.Test;
029:
030: /**
031: * @author Phil Zoio
032: */
033: public class TestBindablesReader {
034:
035: @Test
036: public void testSimpleBinding() throws Exception {
037:
038: NestedBindableBean nested = new NestedBindableBean();
039: BindableBean bindable = new BindableBean();
040:
041: bindable.setNestedBindableBean(nested);
042: bindable.setIntegerValue("5");
043:
044: BindAnnotationReader bindablesReader = new BindAnnotationReader();
045: BindConvertInfo bindConvertInfo = bindablesReader
046: .readBindables(bindable);
047: Map<String, BindHandler> map = bindConvertInfo.getBindMap();
048:
049: assert map.size() == 2;
050:
051: BindHandler b = map.get("integerValue");
052:
053: assert b instanceof BindHandler;
054:
055: BindSimpleHandler b1 = (BindSimpleHandler) b;
056:
057: assert b1.getBeanLocatingExpression().equals(
058: "nestedBindableBean");
059: assert b1.getBeanPropertyName().equals("integerValue");
060: assert b1.getConverterClass().equals(
061: SafeBeanUtilsConverter.class);
062:
063: // we don't know this until we've actually tried to read from or write
064: // to the target
065: assert b1.getConverter() != null;
066: assert b1.getBeanPropertyClass() == null;
067:
068: }
069:
070: @Test
071: public void testSelectInwardBinding() throws Exception {
072:
073: SelectForm selectForm = new SelectForm();
074: Map<Long, Gender> genders = new HashMap<Long, Gender>();
075: genders.put(1L, new Gender(1L, "male"));
076: genders.put(2L, new Gender(2L, "female"));
077:
078: selectForm.setAvailableGenders(genders);
079:
080: Person person = new Person();
081: person.setName("me");
082:
083: BindAnnotationReader bindablesReader = new BindAnnotationReader();
084: BindConvertInfo bindConvertInfo = bindablesReader
085: .readBindables(selectForm);
086: Map<String, BindHandler> map = bindConvertInfo.getBindMap();
087:
088: assert map.size() == 1;
089:
090: BindHandler b = map.get("selectedGender");
091:
092: assert b instanceof BindSelectHandler;
093: BindSelectHandler b1 = (BindSelectHandler) b;
094:
095: assert b1.getBeanLocatingExpression().equals("person");
096: assert b1.getTargetBeanExpression().equals("person.gender");
097: assert b1.getBeanPropertyName().equals("gender");
098: assert b1.getConverterClass().equals(
099: SafeBeanUtilsConverter.class);
100: assert b1.getBeanPropertyClass() == Long.class;
101: assert b1.getBeanLookupExpression().equals("availableGenders");
102: assert b1.getBeanPropertyIdName().equals("id");
103: assert b1.getConverter() != null;
104:
105: }
106:
107: }
|