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.converter;
016:
017: import java.beans.PropertyDescriptor;
018:
019: import org.apache.commons.beanutils.BeanUtilsBean;
020: import org.apache.commons.beanutils.NestedNullException;
021: import org.apache.commons.beanutils.PropertyUtils;
022: import org.testng.annotations.Test;
023:
024: /**
025: * @author Phil Zoio
026: */
027: public class TestBind {
028:
029: @Test
030: public void testSimpleBinding() throws Exception {
031:
032: NestedBindableBean nested = new NestedBindableBean();
033: BindableBean bindable = new BindableBean();
034:
035: bindable.setNestedBindableBean(nested);
036: bindable.setIntegerValue("5");
037:
038: Object integerValue = BeanUtilsBean.getInstance()
039: .getConvertUtils().convert(bindable.getIntegerValue(),
040: Integer.class);
041: PropertyUtils.setProperty(bindable,
042: "nestedBindableBean.integerValue", integerValue);
043:
044: assert nested.getIntegerValue() == 5;
045:
046: }
047:
048: @Test(expectedExceptions=IllegalArgumentException.class)
049: public void testNullBinding() throws Exception {
050:
051: BindableBean bindable = new BindableBean();
052:
053: PropertyUtils.setProperty(bindable,
054: "nestedBindableBean.integerValue", 5);
055:
056: }
057:
058: @Test
059: public void testGetProperty() throws Exception {
060:
061: NestedBindableBean nested = new NestedBindableBean();
062: BindableBean bindable = new BindableBean();
063:
064: bindable.setNestedBindableBean(nested);
065: bindable.setIntegerValue("5");
066:
067: Object property = PropertyUtils.getProperty(bindable,
068: "nestedBindableBean.integerValue");
069: System.out.println(property);
070:
071: PropertyDescriptor propertyDescriptor = PropertyUtils
072: .getPropertyDescriptor(bindable,
073: "nestedBindableBean.integerValue");
074: System.out.println(propertyDescriptor);
075:
076: // now try get property descriptor null
077: // bindable.setNestedBindableBean(null);
078:
079: propertyDescriptor = PropertyUtils.getPropertyDescriptor(
080: bindable, "nestedBindableBean.integerValue");
081: System.out.println(propertyDescriptor);
082:
083: }
084:
085: @Test(expectedExceptions=NestedNullException.class)
086: public void testGetNullProperty() throws Exception {
087: BindableBean bindable = new BindableBean();
088: PropertyUtils.getProperty(bindable,
089: "nestedBindableBean.integerValue");
090: }
091:
092: @Test
093: public void testPropertyClass() throws Exception {
094:
095: NestedBindableBean nested = new NestedBindableBean();
096: BindableBean bindable = new BindableBean();
097: bindable.setNestedBindableBean(nested);
098: Object targetBean = PropertyUtils.getProperty(bindable,
099: "nestedBindableBean");
100: PropertyDescriptor propertyDescriptor = PropertyUtils
101: .getPropertyDescriptor(targetBean, "integerValue");
102:
103: assert propertyDescriptor.getPropertyType().equals(
104: Integer.class);
105:
106: }
107:
108: @Test(expectedExceptions=IllegalArgumentException.class)
109: public void tryStringBinding() throws Exception {
110:
111: NestedBindableBean nested = new NestedBindableBean();
112: BindableBean bindable = new BindableBean();
113:
114: bindable.setNestedBindableBean(nested);
115: bindable.setIntegerValue("5");
116:
117: // now try binding a string
118: bindable.setNestedBindableBean(nested);
119: PropertyUtils.setProperty(bindable,
120: "nestedBindableBean.integerValue", "6");
121:
122: assert nested.getIntegerValue() == 6;
123:
124: }
125:
126: @Test
127: public void tryBooleanBinding() throws Exception {
128:
129: BindableBean bindable = new BindableBean();
130: bindable.setBooleanValue(Boolean.FALSE);
131:
132: // now try binding a string
133: PropertyUtils.setProperty(bindable, "booleanValue", null);
134:
135: assert bindable.getBooleanValue() == null;
136:
137: }
138:
139: }
|