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 static org.easymock.EasyMock.expect;
018: import static org.easymock.classextension.EasyMock.createStrictMock;
019: import static org.easymock.classextension.EasyMock.replay;
020: import static org.easymock.classextension.EasyMock.reset;
021: import static org.easymock.classextension.EasyMock.verify;
022:
023: import java.beans.PropertyDescriptor;
024:
025: import org.apache.commons.beanutils.PropertyUtils;
026: import org.strecks.bind.handler.impl.BindableBean;
027: import org.strecks.bind.handler.impl.TargetBean;
028: import org.strecks.converter.ConversionState;
029: import org.strecks.converter.Converter;
030: import org.strecks.converter.StandardBeanUtilsConverter;
031: import org.strecks.converter.handler.DefaultConversionHandler;
032: import org.strecks.exceptions.ApplicationRuntimeException;
033: import org.strecks.exceptions.ConversionException;
034: import org.testng.Assert;
035: import org.testng.annotations.BeforeTest;
036: import org.testng.annotations.Configuration;
037: import org.testng.annotations.Test;
038:
039: /**
040: * @author Phil Zoio
041: */
042: public class TestBindSimpleHandler {
043:
044: private BindSimpleHandler handler;
045:
046: @Configuration(beforeTestMethod=true)
047: public void setUp() throws Exception {
048:
049: handler = new BindSimpleHandler();
050: handler.setBeanLocatingExpression("targetBean");
051: handler.setBeanPropertyName("integerProperty");
052: handler.setConverter(new StandardBeanUtilsConverter());
053: handler.setConversionHandler(new DefaultConversionHandler());
054:
055: // needed for binding outwards
056: PropertyDescriptor propertyDescriptor = PropertyUtils
057: .getPropertyDescriptor(new BindableBean(),
058: "intProperty");
059: handler.setPropertyDescriptor(propertyDescriptor);
060:
061: }
062:
063: @SuppressWarnings("unchecked")
064: @Test
065: public void testConvertedValueBindInwards()
066: throws ConversionException {
067:
068: Converter converter = createStrictMock(Converter.class);
069: handler.setConverter(converter);
070:
071: // make sure converter does not get called for conversion
072: converter.setTargetClass(Integer.class);
073: replay(converter);
074:
075: BindableBean form = new BindableBean();
076:
077: TargetBean targetBean = new TargetBean();
078: form.setTargetBean(targetBean);
079:
080: handler.bindInwards(form, form, 4);
081: Assert.assertEquals(targetBean.getIntegerProperty().intValue(),
082: 4);
083:
084: verify(converter);
085: reset(converter);
086:
087: converter.setTargetClass(Integer.class);
088: replay(converter);
089: handler.bindInwards(form, form, ConversionState.NULL);
090: Assert.assertEquals(targetBean.getIntegerProperty(), null);
091: verify(converter);
092: reset(converter);
093:
094: converter.setTargetClass(Integer.class);
095: expect(converter.toTargetType(null)).andReturn(null);
096: replay(converter);
097: handler.bindInwards(form, form, ConversionState.FAILURE);
098: Assert.assertEquals(targetBean.getIntegerProperty(), null);
099: verify(converter);
100: }
101:
102: @Test
103: public void testTargetBindOutwards() {
104:
105: BindableBean form = new BindableBean();
106:
107: TargetBean targetBean = new TargetBean();
108: form.setTargetBean(targetBean);
109:
110: targetBean.setIntegerProperty(4);
111: handler.bindOutwards(form, form);
112: Assert.assertEquals(form.getIntProperty(), "4");
113:
114: }
115:
116: @Test
117: public void testNullTargetBindOutwards() {
118: BindableBean form = new BindableBean();
119: handler.bindOutwards(form, form);
120: Assert.assertEquals(form.getIntProperty(), null);
121: }
122:
123: @Test
124: public void testTargetBindInwards() {
125:
126: BindableBean form = new BindableBean();
127:
128: TargetBean targetBean = new TargetBean();
129: form.setTargetBean(targetBean);
130:
131: form.setIntProperty("4");
132: handler.bindInwards(form, form, null);
133: Assert.assertEquals(targetBean.getIntegerProperty().intValue(),
134: 4);
135:
136: }
137:
138: @Test
139: public void testNullTargetBindInwards() {
140: BindableBean form = new BindableBean();
141: form.setIntProperty("4");
142: handler.bindInwards(form, form, null);
143: }
144:
145: @Test(expectedExceptions=ApplicationRuntimeException.class)
146: public void testInwardDuffProperty() {
147: handler.setBeanPropertyName("duffProperty");
148:
149: BindableBean form = new BindableBean();
150:
151: TargetBean targetBean = new TargetBean();
152: form.setTargetBean(targetBean);
153:
154: form.setIntProperty("4");
155: handler.bindInwards(form, form, null);
156: }
157:
158: @Test(expectedExceptions=ApplicationRuntimeException.class)
159: public void testOutwardDuffProperty() {
160: handler.setBeanPropertyName("duffProperty");
161:
162: BindableBean form = new BindableBean();
163:
164: TargetBean targetBean = new TargetBean();
165: form.setTargetBean(targetBean);
166:
167: targetBean.setIntegerProperty(4);
168: handler.bindOutwards(form, form);
169: }
170:
171: }
|