1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17   
18  package org.apache.commons.beanutils;
19  
20  import junit.framework.TestCase;
21  
22  
23  /***
24   * Test cases for <code>BeanToPropertyValueTransformer</code>.
25   *
26   * @author Norm Deane
27   */
28  public class BeanToPropertyValueTransformerTestCase extends TestCase {
29     
30      private static final Integer expectedIntegerValue = new Integer(123);
31      private static final Long expectedLongValue = new Long(123);
32      private static final Float expectedFloatValue = new Float(123.123f);
33      private static final Double expectedDoubleValue = new Double(567879.12344d);
34      private static final Boolean expectedBooleanValue = Boolean.TRUE;
35      private static final Byte expectedByteValue = new Byte("12");
36  
37      /***
38       * Constructor for BeanToPropertyValueTransformerTestCase.
39       *
40       * @param name Name of this test case.
41       */
42      public BeanToPropertyValueTransformerTestCase(String name) {
43          super(name);
44      }
45  
46      /***
47       * Test transform with simple String property.
48       */
49      public void testTransformWithSimpleStringProperty() {
50          BeanToPropertyValueTransformer transformer = 
51              new BeanToPropertyValueTransformer("stringProperty");
52          TestBean testBean = new TestBean("foo");
53          assertEquals("foo", transformer.transform(testBean));
54      }
55  
56      /***
57       * Test transform with simple String property and null value.
58       *
59       */
60      public void testTransformWithSimpleStringPropertyAndNullValue() {
61          BeanToPropertyValueTransformer transformer = 
62              new BeanToPropertyValueTransformer("stringProperty");
63          TestBean testBean = new TestBean((String) null);
64          assertNull(transformer.transform(testBean));
65      }
66  
67      /***
68       * Test transform with simple int property.
69       */
70      public void testTransformWithSimpleIntProperty() {
71          BeanToPropertyValueTransformer transformer = 
72              new BeanToPropertyValueTransformer("intProperty");
73          TestBean testBean = new TestBean(expectedIntegerValue.intValue());
74          assertEquals(expectedIntegerValue, transformer.transform(testBean));
75      }
76  
77      /***
78       * Test transform with simple long property.
79       */
80      public void testTransformWithSimpleLongProperty() {
81          BeanToPropertyValueTransformer transformer = 
82              new BeanToPropertyValueTransformer("longProperty");
83          TestBean testBean = new TestBean();
84          testBean.setLongProperty(expectedLongValue.longValue());
85          assertEquals(expectedLongValue, transformer.transform(testBean));
86      }
87  
88      /***
89       * Test transform with simple float property.
90       */
91      public void testTransformWithSimpleFloatProperty() {
92          BeanToPropertyValueTransformer transformer = 
93              new BeanToPropertyValueTransformer("floatProperty");
94          TestBean testBean = new TestBean(expectedFloatValue.floatValue());
95          assertEquals(expectedFloatValue, transformer.transform(testBean));
96      }
97  
98      /***
99       * Test transform with simple double property.
100      */
101     public void testTransformWithSimpleDoubleProperty() {
102         BeanToPropertyValueTransformer transformer = 
103             new BeanToPropertyValueTransformer("doubleProperty");
104         TestBean testBean = new TestBean(expectedDoubleValue.doubleValue());
105         assertEquals(expectedDoubleValue, transformer.transform(testBean));
106     }
107 
108     /***
109      * Test transform with simple byte property.
110      */
111     public void testTransformWithSimpleByteProperty() {
112         BeanToPropertyValueTransformer transformer = 
113             new BeanToPropertyValueTransformer("byteProperty");
114         TestBean testBean = new TestBean();
115         testBean.setByteProperty(expectedByteValue.byteValue());
116         assertEquals(expectedByteValue, transformer.transform(testBean));
117     }
118 
119     /***
120      * Test transform with simple boolean property.
121      */
122     public void testTransformWithSimpleBooleanProperty() {
123         BeanToPropertyValueTransformer transformer = 
124             new BeanToPropertyValueTransformer("booleanProperty");
125         TestBean testBean = new TestBean(expectedBooleanValue.booleanValue());
126         assertEquals(expectedBooleanValue, transformer.transform(testBean));
127     }
128 
129     /***
130      * Test transform with write only property.
131      */
132     public void testTransformWithWriteOnlyProperty() {
133         try {
134             new BeanToPropertyValueTransformer("writeOnlyProperty").transform(new TestBean());
135         } catch (IllegalArgumentException e) { 
136             /* This is what should happen */
137         }
138     }
139 
140     /***
141      * Test transform with read only property.
142      */
143     public void testTransformWithReadOnlyProperty() {
144         BeanToPropertyValueTransformer transformer = 
145             new BeanToPropertyValueTransformer("readOnlyProperty");
146         TestBean testBean = new TestBean();
147         assertEquals(testBean.getReadOnlyProperty(), transformer.transform(testBean));
148     }
149 
150     /***
151      * Test transform with invalid property.
152      */
153     public void testTransformWithInvalidProperty() {
154         try {
155             new BeanToPropertyValueTransformer("bogusProperty").transform(new TestBean());
156         } catch (IllegalArgumentException e) { 
157             /* This is what should happen */
158         }
159     }
160 
161     /***
162      * Test transform with nested property.
163      */
164     public void testTransformWithNestedProperty() {
165         BeanToPropertyValueTransformer transformer = 
166             new BeanToPropertyValueTransformer("anotherNested.stringProperty");
167         TestBean testBean = new TestBean();
168         TestBean nestedBean = new TestBean("foo");
169         testBean.setAnotherNested(nestedBean);
170         assertEquals("foo", transformer.transform(testBean));
171     }
172 
173     /***
174      * Test transform with mapped property.
175      */
176     public void testTransformWithMappedProperty() {
177         BeanToPropertyValueTransformer transformer = 
178             new BeanToPropertyValueTransformer("mappedProperty(test-key)");
179         TestBean testBean = new TestBean();
180 
181         // try a valid key
182         testBean.setMappedProperty("test-key", "test-value");
183         assertEquals("test-value", transformer.transform(testBean));
184 
185         // now try an invalid key
186         transformer = new BeanToPropertyValueTransformer("mappedProperty(bogus-key)");
187         assertEquals(null, transformer.transform(testBean));
188     }
189 
190     /***
191      * Test transform with indexed property.
192      */
193     public void testTransformWithIndexedProperty() {
194         BeanToPropertyValueTransformer transformer = 
195             new BeanToPropertyValueTransformer("intIndexed[0]");
196         TestBean testBean = new TestBean();
197         testBean.setIntIndexed(0, expectedIntegerValue.intValue());
198         assertEquals(expectedIntegerValue, transformer.transform(testBean));
199 
200         // test index out of range
201         transformer = new BeanToPropertyValueTransformer("intIndexed[9999]");
202 
203         try {
204             transformer.transform(testBean);
205             fail("Should have thrown an ArrayIndexOutOfBoundsException");
206         } catch (ArrayIndexOutOfBoundsException e) { 
207             /* this is what should happen */
208         }
209     }
210 
211     /***
212      * Test transform with nested indexed property.
213      */
214     public void testTransformWithNestedIndexedProperty() {
215         BeanToPropertyValueTransformer transformer = 
216             new BeanToPropertyValueTransformer("anotherNested.intIndexed[0]");
217         TestBean testBean = new TestBean();
218         TestBean nestedBean = new TestBean();
219         nestedBean.setIntIndexed(0, expectedIntegerValue.intValue());
220         testBean.setAnotherNested(nestedBean);
221         assertEquals(expectedIntegerValue, transformer.transform(testBean));
222     }
223 
224     /***
225      * Test transform with null in property path.
226      */
227     public void testTransformWithNullInPath() {
228         BeanToPropertyValueTransformer transformer = 
229             new BeanToPropertyValueTransformer("anotherNested.stringProperty");
230 
231         try {
232             transformer.transform(new TestBean());
233             fail("Should have throw IllegalArgumentException");
234         } catch (IllegalArgumentException e) { 
235             /* ignore this is what should happen */
236         }
237     }
238 
239     /***
240      * Test transform with null in property path and ignore = true.
241      */
242     public void testTransformWithNullInPathAndIgnoreTrue() {
243         BeanToPropertyValueTransformer transformer = 
244             new BeanToPropertyValueTransformer("anotherNested.stringProperty",true);
245         assertEquals(null, transformer.transform(new TestBean()));
246     }
247 }