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  package org.apache.commons.beanutils;
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  
22  /***
23   * Test Case for the {@link BeanUtilsBean2}.
24   *
25   * @version $Revision: 552381 $
26   */
27  public class BeanUtils2TestCase extends BeanUtilsTestCase {
28  
29      // ---------------------------------------------------------- Constructors
30  
31      /***
32       * Construct a new instance of this test case.
33       *
34       * @param name Name of the test case
35       */
36      public BeanUtils2TestCase(String name) {
37          super(name);
38      }
39  
40  
41      // -------------------------------------------------- Overall Test Methods
42  
43  
44      /***
45       * Set up instance variables required by this test case.
46       */
47      public void setUp() {
48          ConvertUtils.deregister();
49          BeanUtilsBean.setInstance(new BeanUtilsBean2());
50          setUpShared();
51      }
52  
53  
54      /***
55       * Return the tests included in this test suite.
56       */
57      public static Test suite() {
58          return (new TestSuite(BeanUtils2TestCase.class));
59      }
60  
61      /***
62       * Tear down instance variables required by this test case.
63       */
64      public void tearDown() {
65          bean = null;
66      }
67  
68      /***
69       * Test <code>copyProperty()</code> converting to a String.
70       */
71      public void testCopyPropertyConvertToString() {
72          try {
73              BeanUtils.copyProperty(bean, "stringProperty", testUtilDate);
74          } catch (Throwable t) {
75              fail("Threw " + t);
76          }
77          assertEquals("java.util.Date --> String", testStringDate, bean.getStringProperty());
78      }
79  
80      /***
81       * Test <code>copyProperty()</code> converting to a String.
82       */
83      public void testCopyPropertyConvertToStringArray() {
84          try {
85              bean.setStringArray(null);
86              BeanUtils.copyProperty(bean, "stringArray", new java.util.Date[] {testUtilDate});
87          } catch (Throwable t) {
88              fail("Threw " + t);
89          }
90          assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length);
91          assertEquals("java.util.Date[] --> String[] value ", testStringDate, bean.getStringArray()[0]);
92      }
93  
94      /***
95       * Test <code>copyProperty()</code> converting to a String on indexed property
96       */
97      public void testCopyPropertyConvertToStringIndexed() {
98          try {
99              bean.setStringArray(new String[1]);
100             BeanUtils.copyProperty(bean, "stringArray[0]", testUtilDate);
101         } catch (Throwable t) {
102             fail("Threw " + t);
103         }
104         assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length);
105         assertEquals("java.util.Date[] --> String[] value ", testStringDate, bean.getStringArray()[0]);
106     }
107 
108     /***
109      * Test <code>getArrayProperty()</code> converting to a String.
110      */
111     public void testGetArrayPropertyDate() {
112         String[] value = null;
113         try {
114             bean.setDateArrayProperty(new java.util.Date[] {testUtilDate});
115             value = BeanUtils.getArrayProperty(bean, "dateArrayProperty");
116         } catch (Throwable t) {
117             fail("Threw " + t);
118         }
119         assertEquals("java.util.Date[] --> String[] length", 1, value.length);
120         assertEquals("java.util.Date[] --> String[] value ", testStringDate, value[0]);
121     }
122 
123     /***
124      * Test <code>getArrayProperty()</code> converting to a String.
125      */
126     public void testGetIndexedPropertyDate() {
127         String value = null;
128         try {
129             bean.setDateArrayProperty(new java.util.Date[] {testUtilDate});
130             value = BeanUtils.getIndexedProperty(bean, "dateArrayProperty[0]");
131         } catch (Throwable t) {
132             fail("Threw " + t);
133         }
134         assertEquals("java.util.Date[0] --> String", testStringDate, value);
135     }
136 
137     /***
138      * Test <code>getSimpleProperty()</code> converting to a String.
139      */
140     public void testGetSimplePropertyDate() {
141         String value = null;
142         try {
143             bean.setDateProperty(testUtilDate);
144             value = BeanUtils.getSimpleProperty(bean, "dateProperty");
145         } catch (Throwable t) {
146             fail("Threw " + t);
147         }
148         assertEquals("java.util.Date --> String", testStringDate, value);
149     }
150 
151     /***
152      * Test <code>setProperty()</code> converting to a String.
153      */
154     public void testSetPropertyConvertToString() {
155         try {
156             BeanUtils.setProperty(bean, "stringProperty", testUtilDate);
157         } catch (Throwable t) {
158             fail("Threw " + t);
159         }
160         assertEquals("java.util.Date --> String", testStringDate, bean.getStringProperty());
161     }
162 
163     /***
164      * Test <code>setProperty()</code> converting to a String array.
165      */
166     public void testSetPropertyConvertToStringArray() {
167         try {
168             bean.setStringArray(null);
169             BeanUtils.setProperty(bean, "stringArray", new java.util.Date[] {testUtilDate});
170         } catch (Throwable t) {
171             fail("Threw " + t);
172         }
173         assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length);
174         assertEquals("java.util.Date[] --> String[] value ", testStringDate, bean.getStringArray()[0]);
175     }
176 
177     /***
178      * Test <code>setProperty()</code> converting to a String on indexed property
179      */
180     public void testSetPropertyConvertToStringIndexed() {
181         try {
182             bean.setStringArray(new String[1]);
183             BeanUtils.setProperty(bean, "stringArray[0]", testUtilDate);
184         } catch (Throwable t) {
185             fail("Threw " + t);
186         }
187         assertEquals("java.util.Date --> String[]", testStringDate, bean.getStringArray()[0]);
188     }
189 
190 }