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: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import org.apache.commons.beanutils.PropertyUtils;
030: import org.strecks.bind.handler.impl.BindableBean;
031: import org.strecks.bind.handler.impl.DomainClass;
032: import org.strecks.bind.handler.impl.TargetBean;
033: import org.strecks.converter.ConversionState;
034: import org.strecks.converter.Converter;
035: import org.strecks.converter.StandardBeanUtilsConverter;
036: import org.strecks.converter.handler.DefaultConversionHandler;
037: import org.strecks.exceptions.ConversionException;
038: import org.testng.Assert;
039: import org.testng.annotations.BeforeTest;
040: import org.testng.annotations.Configuration;
041: import org.testng.annotations.Test;
042:
043: /**
044: * @author Phil Zoio
045: */
046: public class TestBindSelectHandler {
047:
048: private BindSelectHandler handler;
049:
050: @Configuration(beforeTestMethod=true)
051: public void setUp() throws Exception {
052:
053: handler = new BindSelectHandler();
054: handler.setBeanLocatingExpression("targetBean");
055: handler.setTargetBeanExpression("targetBean.domainClass");
056: handler.setBeanPropertyIdName("id");
057: handler.setBeanLookupExpression("lookupMap");
058: handler.setBeanPropertyName("domainClass");
059: handler.setBeanPropertyClass(Integer.class);
060: handler.setConversionHandler(new DefaultConversionHandler());
061:
062: Converter converter = new StandardBeanUtilsConverter();
063: handler.setConverter(converter);
064: converter.setTargetClass(Integer.class);
065:
066: // needed for binding outwards
067: PropertyDescriptor propertyDescriptor = PropertyUtils
068: .getPropertyDescriptor(new BindableBean(), "selectedId");
069: handler.setPropertyDescriptor(propertyDescriptor);
070:
071: }
072:
073: @SuppressWarnings("unchecked")
074: @Test
075: public void testConvertedValueBindInwards()
076: throws ConversionException {
077:
078: Converter converter = createStrictMock(Converter.class);
079: handler.setConverter(converter);
080:
081: // make sure converter does not get called for conversion
082: replay(converter);
083:
084: BindableBean form = new BindableBean();
085:
086: TargetBean targetBean = new TargetBean();
087: form.setTargetBean(targetBean);
088:
089: handler.bindInwards(form, form, 4);
090:
091: verify(converter);
092: reset(converter);
093:
094: replay(converter);
095: handler.bindInwards(form, form, ConversionState.NULL);
096: Assert.assertEquals(targetBean.getIntegerProperty(), null);
097: verify(converter);
098: reset(converter);
099:
100: expect(converter.toTargetType(null)).andReturn(null);
101: replay(converter);
102: handler.bindInwards(form, form, ConversionState.FAILURE);
103: Assert.assertEquals(targetBean.getIntegerProperty(), null);
104: verify(converter);
105:
106: }
107:
108: @SuppressWarnings("unchecked")
109: @Test
110: public void testTargetBindOutwards() {
111:
112: handler.setBeanLookupExpression(null);
113: handler.setTargetBeanExpression(null);
114: handler.setBeanPropertyName("domainClass");
115: handler.setBeanPropertyClass(Integer.class);
116:
117: BindableBean form = new BindableBean();
118: Collection collection = new ArrayList();
119: collection.add(new DomainClass(1));
120: collection.add(new DomainClass(3));
121: collection.add(new DomainClass(4));
122: Map map = handler.getPropertyAsMap(collection);
123: form.setLookupMap(map);
124:
125: TargetBean targetBean = new TargetBean();
126: form.setTargetBean(targetBean);
127: targetBean.setDomainClass(new DomainClass(4));
128:
129: handler.bindOutwards(form, form);
130:
131: Assert.assertEquals(form.getSelectedId(), "4");
132:
133: }
134:
135: @SuppressWarnings("unchecked")
136: @Test
137: public void testEmptyMapBindOutwards() {
138:
139: BindableBean form = new BindableBean();
140: Map map = handler.getPropertyAsMap(new HashMap());
141: form.setLookupMap(map);
142:
143: TargetBean targetBean = new TargetBean();
144: form.setTargetBean(targetBean);
145: targetBean.setDomainClass(new DomainClass(4));
146:
147: handler.bindOutwards(form, form);
148:
149: Assert.assertEquals(form.getSelectedId(), "4");
150:
151: }
152:
153: @SuppressWarnings("unchecked")
154: @Test
155: public void testNullTargetBindOutwards() {
156:
157: BindableBean form = new BindableBean();
158: Collection collection = new ArrayList();
159: collection.add(new DomainClass(1));
160: collection.add(new DomainClass(3));
161: collection.add(new DomainClass(4));
162: Map map = handler.getPropertyAsMap(collection);
163: form.setLookupMap(map);
164:
165: TargetBean targetBean = new TargetBean();
166: // form.setTargetBean(targetBean);
167: targetBean.setDomainClass(new DomainClass(4));
168:
169: handler.bindOutwards(form, form);
170:
171: Assert.assertEquals(form.getSelectedId(), null);
172:
173: }
174:
175: @SuppressWarnings("unchecked")
176: @Test
177: public void testNullMapBindOutwards() {
178:
179: handler.setBeanLookupExpression(null);
180: handler.setTargetBeanExpression(null);
181: handler.setBeanPropertyName("domainClass");
182: handler.setBeanPropertyClass(Integer.class);
183:
184: BindableBean form = new BindableBean();
185: form.setLookupMap(null);
186:
187: TargetBean targetBean = new TargetBean();
188: form.setTargetBean(targetBean);
189: targetBean.setDomainClass(new DomainClass(4));
190:
191: handler.bindOutwards(form, form);
192:
193: Assert.assertEquals(form.getSelectedId(), "4");
194:
195: }
196:
197: @SuppressWarnings("unchecked")
198: @Test
199: public void testTargetBindInwards() {
200:
201: BindableBean form = new BindableBean();
202: Collection collection = new ArrayList();
203: collection.add(new DomainClass(1));
204: collection.add(new DomainClass(3));
205: collection.add(new DomainClass(4));
206: Map map = handler.getPropertyAsMap(collection);
207: form.setLookupMap(map);
208:
209: TargetBean targetBean = new TargetBean();
210: form.setTargetBean(targetBean);
211:
212: form.setSelectedId("1");
213: handler.bindInwards(form, form, null);
214:
215: assert targetBean.getDomainClass().getId() == 1;
216:
217: }
218:
219: @SuppressWarnings("unchecked")
220: @Test
221: public void testNullTargetBindInwards() {
222:
223: BindableBean form = new BindableBean();
224: Collection collection = new ArrayList();
225: collection.add(new DomainClass(1));
226: collection.add(new DomainClass(3));
227: collection.add(new DomainClass(4));
228: Map map = handler.getPropertyAsMap(collection);
229: form.setLookupMap(map);
230:
231: TargetBean targetBean = new TargetBean();
232: // form.setTargetBean(targetBean);
233:
234: form.setSelectedId("1");
235: handler.bindInwards(form, form, null);
236:
237: assert targetBean.getDomainClass() == null;
238:
239: }
240:
241: @SuppressWarnings("unchecked")
242: @Test
243: public void testEmptyMapBindInwards() {
244:
245: BindableBean form = new BindableBean();
246: form.setLookupMap(new HashMap());
247:
248: TargetBean targetBean = new TargetBean();
249: form.setTargetBean(targetBean);
250:
251: form.setSelectedId("1");
252: handler.bindInwards(form, form, null);
253:
254: assert targetBean.getDomainClass() == null;
255:
256: }
257:
258: @SuppressWarnings("unchecked")
259: @Test
260: public void testNullMapBindInwards() {
261:
262: BindableBean form = new BindableBean();
263: form.setLookupMap(null);
264:
265: TargetBean targetBean = new TargetBean();
266: form.setTargetBean(targetBean);
267:
268: form.setSelectedId("1");
269: handler.bindInwards(form, form, null);
270:
271: assert targetBean.getDomainClass() == null;
272:
273: }
274:
275: @SuppressWarnings("unchecked")
276: @Test
277: public void testInvalidIdBindInwards() {
278:
279: BindableBean form = new BindableBean();
280: Collection collection = new ArrayList();
281: collection.add(new DomainClass(1));
282: collection.add(new DomainClass(3));
283: collection.add(new DomainClass(4));
284: Map map = handler.getPropertyAsMap(collection);
285: form.setLookupMap(map);
286:
287: TargetBean targetBean = new TargetBean();
288: form.setTargetBean(targetBean);
289:
290: form.setSelectedId("10");
291: handler.bindInwards(form, form, null);
292:
293: assert targetBean.getDomainClass() == null;
294:
295: }
296:
297: }
|