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.util;
016:
017: import org.strecks.bind.handler.impl.BindableBean;
018: import org.strecks.bind.handler.impl.DomainClass;
019: import org.strecks.bind.handler.impl.TargetBean;
020: import org.strecks.exceptions.ApplicationRuntimeException;
021: import org.strecks.util.impl.JavaBean;
022: import org.testng.Assert;
023: import org.testng.annotations.Test;
024:
025: /**
026: * @author Phil Zoio
027: */
028: public class TestPropertyValueGetter {
029:
030: @Test
031: public void testPropertyValueGetter1() throws Exception {
032: JavaBean bean = new JavaBean();
033: bean.setStringProperty("stringval");
034: bean.setBooleanProperty(true);
035:
036: Assert.assertEquals(PropertyValueGetter.getPropertyValue(bean,
037: "stringProperty"), "stringval");
038: Assert.assertEquals(PropertyValueGetter.getPropertyValue(bean,
039: "booleanProperty"), true);
040: }
041:
042: @Test
043: public void testPropertyValueGetterUnknown() throws Exception {
044: JavaBean bean = new JavaBean();
045:
046: try {
047: PropertyValueGetter.getPropertyValue(bean,
048: "unknownProperty");
049: } catch (ApplicationRuntimeException e) {
050: Assert
051: .assertEquals(
052: e.getMessage(),
053: "Required getter methods not found from class org.strecks.util.impl.JavaBean using property expression unknownProperty");
054: }
055: }
056:
057: @Test
058: public void testGetPropertyDescriptor() throws Exception {
059: JavaBean bean = new JavaBean();
060: assert null != PropertyValueGetter.getPropertyDescriptor(bean,
061: "stringProperty");
062: assert null != PropertyValueGetter.getPropertyDescriptor(bean,
063: "booleanProperty");
064:
065: }
066:
067: @Test
068: public void testPropertyDescriptorGetterUnknown() throws Exception {
069: JavaBean bean = new JavaBean();
070:
071: try {
072: PropertyValueGetter.getPropertyDescriptor(bean,
073: "unknownProperty");
074: } catch (ApplicationRuntimeException e) {
075: Assert
076: .assertEquals(
077: e.getMessage(),
078: "Unable to read property descriptor for bean org.strecks.util.impl.JavaBean, property unknownProperty");
079: }
080: }
081:
082: @Test
083: public void testGetSilentPropertyDescriptor() throws Exception {
084: JavaBean bean = new JavaBean();
085: assert null != PropertyValueGetter
086: .getPropertyDescriptorSilently(bean, "stringProperty");
087: assert null == PropertyValueGetter
088: .getPropertyDescriptorSilently(bean, "unknownProperty");
089:
090: }
091:
092: @Test
093: public void testGetSilentPropertyClass() throws Exception {
094: JavaBean bean = new JavaBean();
095: assert null != PropertyValueGetter.getPropertyTypeSilently(
096: bean, "stringProperty");
097: assert null == PropertyValueGetter.getPropertyTypeSilently(
098: bean, "unknownProperty");
099:
100: }
101:
102: @Test
103: public void testGetTargetBean() {
104:
105: TargetBean bean = new TargetBean();
106: bean.setStringProperty("2");
107: BindableBean form = new BindableBean();
108: form.setTargetBean(bean);
109: DomainClass domclass = new DomainClass(1);
110: bean.setDomainClass(domclass);
111:
112: Object targetBean = PropertyValueGetter.getTargetBean(form,
113: "targetBean");
114: assert targetBean instanceof TargetBean;
115:
116: targetBean = PropertyValueGetter.getTargetBean(form,
117: "targetBean.domainClass");
118: assert targetBean instanceof DomainClass;
119:
120: bean.setDomainClass(null);
121: assert null == PropertyValueGetter.getTargetBean(form,
122: "targetBean.domainClass");
123:
124: form.setTargetBean(null);
125: assert null == PropertyValueGetter.getTargetBean(form,
126: "targetBean.domainClass");
127: assert null == PropertyValueGetter.getTargetBean(form,
128: "targetBean");
129:
130: }
131:
132: }
|