KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > BeanToPropertyValueTransformerTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.commons.beanutils;
18
19 import junit.framework.TestCase;
20
21
22 /**
23  * Test cases for <code>BeanToPropertyValueTransformer</code>.
24  *
25  * @author Norm Deane
26  */

27 public class BeanToPropertyValueTransformerTest extends TestCase {
28    
29     private static final Integer JavaDoc expectedIntegerValue = new Integer JavaDoc(123);
30     private static final Long JavaDoc expectedLongValue = new Long JavaDoc(123);
31     private static final Float JavaDoc expectedFloatValue = new Float JavaDoc(123.123f);
32     private static final Double JavaDoc expectedDoubleValue = new Double JavaDoc(567879.12344d);
33     private static final Boolean JavaDoc expectedBooleanValue = Boolean.TRUE;
34     private static final Byte JavaDoc expectedByteValue = new Byte JavaDoc("12");
35
36     /**
37      * Constructor for BeanToPropertyValueTransformerTest.
38      *
39      * @param name Name of this test case.
40      */

41     public BeanToPropertyValueTransformerTest(String JavaDoc name) {
42         super(name);
43     }
44
45     /**
46      * Test transform with simple String property.
47      */

48     public void testTransformWithSimpleStringProperty() {
49         BeanToPropertyValueTransformer transformer =
50             new BeanToPropertyValueTransformer("stringProperty");
51         TestBean testBean = new TestBean("foo");
52         assertEquals("foo", transformer.transform(testBean));
53     }
54
55     /**
56      * Test transform with simple String property and null value.
57      *
58      */

59     public void testTransformWithSimpleStringPropertyAndNullValue() {
60         BeanToPropertyValueTransformer transformer =
61             new BeanToPropertyValueTransformer("stringProperty");
62         TestBean testBean = new TestBean((String JavaDoc) null);
63         assertNull(transformer.transform(testBean));
64     }
65
66     /**
67      * Test transform with simple int property.
68      */

69     public void testTransformWithSimpleIntProperty() {
70         BeanToPropertyValueTransformer transformer =
71             new BeanToPropertyValueTransformer("intProperty");
72         TestBean testBean = new TestBean(expectedIntegerValue.intValue());
73         assertEquals(expectedIntegerValue, transformer.transform(testBean));
74     }
75
76     /**
77      * Test transform with simple long property.
78      */

79     public void testTransformWithSimpleLongProperty() {
80         BeanToPropertyValueTransformer transformer =
81             new BeanToPropertyValueTransformer("longProperty");
82         TestBean testBean = new TestBean();
83         testBean.setLongProperty(expectedLongValue.longValue());
84         assertEquals(expectedLongValue, transformer.transform(testBean));
85     }
86
87     /**
88      * Test transform with simple float property.
89      */

90     public void testTransformWithSimpleFloatProperty() {
91         BeanToPropertyValueTransformer transformer =
92             new BeanToPropertyValueTransformer("floatProperty");
93         TestBean testBean = new TestBean(expectedFloatValue.floatValue());
94         assertEquals(expectedFloatValue, transformer.transform(testBean));
95     }
96
97     /**
98      * Test transform with simple double property.
99      */

100     public void testTransformWithSimpleDoubleProperty() {
101         BeanToPropertyValueTransformer transformer =
102             new BeanToPropertyValueTransformer("doubleProperty");
103         TestBean testBean = new TestBean(expectedDoubleValue.doubleValue());
104         assertEquals(expectedDoubleValue, transformer.transform(testBean));
105     }
106
107     /**
108      * Test transform with simple byte property.
109      */

110     public void testTransformWithSimpleByteProperty() {
111         BeanToPropertyValueTransformer transformer =
112             new BeanToPropertyValueTransformer("byteProperty");
113         TestBean testBean = new TestBean();
114         testBean.setByteProperty(expectedByteValue.byteValue());
115         assertEquals(expectedByteValue, transformer.transform(testBean));
116     }
117
118     /**
119      * Test transform with simple boolean property.
120      */

121     public void testTransformWithSimpleBooleanProperty() {
122         BeanToPropertyValueTransformer transformer =
123             new BeanToPropertyValueTransformer("booleanProperty");
124         TestBean testBean = new TestBean(expectedBooleanValue.booleanValue());
125         assertEquals(expectedBooleanValue, transformer.transform(testBean));
126     }
127
128     /**
129      * Test transform with write only property.
130      */

131     public void testTransformWithWriteOnlyProperty() {
132         try {
133             new BeanToPropertyValueTransformer("writeOnlyProperty").transform(new TestBean());
134         } catch (IllegalArgumentException JavaDoc e) {
135             /* This is what should happen */
136         }
137     }
138
139     /**
140      * Test transform with read only property.
141      */

142     public void testTransformWithReadOnlyProperty() {
143         BeanToPropertyValueTransformer transformer =
144             new BeanToPropertyValueTransformer("readOnlyProperty");
145         TestBean testBean = new TestBean();
146         assertEquals(testBean.getReadOnlyProperty(), transformer.transform(testBean));
147     }
148
149     /**
150      * Test transform with invalid property.
151      */

152     public void testTransformWithInvalidProperty() {
153         try {
154             new BeanToPropertyValueTransformer("bogusProperty").transform(new TestBean());
155         } catch (IllegalArgumentException JavaDoc e) {
156             /* This is what should happen */
157         }
158     }
159
160     /**
161      * Test transform with nested property.
162      */

163     public void testTransformWithNestedProperty() {
164         BeanToPropertyValueTransformer transformer =
165             new BeanToPropertyValueTransformer("anotherNested.stringProperty");
166         TestBean testBean = new TestBean();
167         TestBean nestedBean = new TestBean("foo");
168         testBean.setAnotherNested(nestedBean);
169         assertEquals("foo", transformer.transform(testBean));
170     }
171
172     /**
173      * Test transform with mapped property.
174      */

175     public void testTransformWithMappedProperty() {
176         BeanToPropertyValueTransformer transformer =
177             new BeanToPropertyValueTransformer("mappedProperty(test-key)");
178         TestBean testBean = new TestBean();
179
180         // try a valid key
181
testBean.setMappedProperty("test-key", "test-value");
182         assertEquals("test-value", transformer.transform(testBean));
183
184         // now try an invalid key
185
transformer = new BeanToPropertyValueTransformer("mappedProperty(bogus-key)");
186         assertEquals(null, transformer.transform(testBean));
187     }
188
189     /**
190      * Test transform with indexed property.
191      */

192     public void testTransformWithIndexedProperty() {
193         BeanToPropertyValueTransformer transformer =
194             new BeanToPropertyValueTransformer("intIndexed[0]");
195         TestBean testBean = new TestBean();
196         testBean.setIntIndexed(0, expectedIntegerValue.intValue());
197         assertEquals(expectedIntegerValue, transformer.transform(testBean));
198
199         // test index out of range
200
transformer = new BeanToPropertyValueTransformer("intIndexed[9999]");
201
202         try {
203             transformer.transform(testBean);
204             fail("Should have thrown an ArrayIndexOutOfBoundsException");
205         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
206             /* this is what should happen */
207         }
208     }
209
210     /**
211      * Test transform with nested indexed property.
212      */

213     public void testTransformWithNestedIndexedProperty() {
214         BeanToPropertyValueTransformer transformer =
215             new BeanToPropertyValueTransformer("anotherNested.intIndexed[0]");
216         TestBean testBean = new TestBean();
217         TestBean nestedBean = new TestBean();
218         nestedBean.setIntIndexed(0, expectedIntegerValue.intValue());
219         testBean.setAnotherNested(nestedBean);
220         assertEquals(expectedIntegerValue, transformer.transform(testBean));
221     }
222
223     /**
224      * Test transform with null in property path.
225      */

226     public void testTransformWithNullInPath() {
227         BeanToPropertyValueTransformer transformer =
228             new BeanToPropertyValueTransformer("anotherNested.stringProperty");
229
230         try {
231             transformer.transform(new TestBean());
232             fail("Should have throw IllegalArgumentException");
233         } catch (IllegalArgumentException JavaDoc e) {
234             /* ignore this is what should happen */
235         }
236     }
237
238     /**
239      * Test transform with null in property path and ignore = true.
240      */

241     public void testTransformWithNullInPathAndIgnoreTrue() {
242         BeanToPropertyValueTransformer transformer =
243             new BeanToPropertyValueTransformer("anotherNested.stringProperty",true);
244         assertEquals(null, transformer.transform(new TestBean()));
245     }
246 }
247
Popular Tags