001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.beanutils;
018:
019: import junit.framework.Test;
020: import junit.framework.TestSuite;
021:
022: /**
023: * Test Case for the {@link BeanUtilsBean2}.
024: *
025: * @version $Revision: 552381 $
026: */
027: public class BeanUtils2TestCase extends BeanUtilsTestCase {
028:
029: // ---------------------------------------------------------- Constructors
030:
031: /**
032: * Construct a new instance of this test case.
033: *
034: * @param name Name of the test case
035: */
036: public BeanUtils2TestCase(String name) {
037: super (name);
038: }
039:
040: // -------------------------------------------------- Overall Test Methods
041:
042: /**
043: * Set up instance variables required by this test case.
044: */
045: public void setUp() {
046: ConvertUtils.deregister();
047: BeanUtilsBean.setInstance(new BeanUtilsBean2());
048: setUpShared();
049: }
050:
051: /**
052: * Return the tests included in this test suite.
053: */
054: public static Test suite() {
055: return (new TestSuite(BeanUtils2TestCase.class));
056: }
057:
058: /**
059: * Tear down instance variables required by this test case.
060: */
061: public void tearDown() {
062: bean = null;
063: }
064:
065: /**
066: * Test <code>copyProperty()</code> converting to a String.
067: */
068: public void testCopyPropertyConvertToString() {
069: try {
070: BeanUtils
071: .copyProperty(bean, "stringProperty", testUtilDate);
072: } catch (Throwable t) {
073: fail("Threw " + t);
074: }
075: assertEquals("java.util.Date --> String", testStringDate, bean
076: .getStringProperty());
077: }
078:
079: /**
080: * Test <code>copyProperty()</code> converting to a String.
081: */
082: public void testCopyPropertyConvertToStringArray() {
083: try {
084: bean.setStringArray(null);
085: BeanUtils.copyProperty(bean, "stringArray",
086: new java.util.Date[] { testUtilDate });
087: } catch (Throwable t) {
088: fail("Threw " + t);
089: }
090: assertEquals("java.util.Date[] --> String[] length", 1, bean
091: .getStringArray().length);
092: assertEquals("java.util.Date[] --> String[] value ",
093: testStringDate, bean.getStringArray()[0]);
094: }
095:
096: /**
097: * Test <code>copyProperty()</code> converting to a String on indexed property
098: */
099: public void testCopyPropertyConvertToStringIndexed() {
100: try {
101: bean.setStringArray(new String[1]);
102: BeanUtils
103: .copyProperty(bean, "stringArray[0]", testUtilDate);
104: } catch (Throwable t) {
105: fail("Threw " + t);
106: }
107: assertEquals("java.util.Date[] --> String[] length", 1, bean
108: .getStringArray().length);
109: assertEquals("java.util.Date[] --> String[] value ",
110: testStringDate, bean.getStringArray()[0]);
111: }
112:
113: /**
114: * Test <code>getArrayProperty()</code> converting to a String.
115: */
116: public void testGetArrayPropertyDate() {
117: String[] value = null;
118: try {
119: bean
120: .setDateArrayProperty(new java.util.Date[] { testUtilDate });
121: value = BeanUtils.getArrayProperty(bean,
122: "dateArrayProperty");
123: } catch (Throwable t) {
124: fail("Threw " + t);
125: }
126: assertEquals("java.util.Date[] --> String[] length", 1,
127: value.length);
128: assertEquals("java.util.Date[] --> String[] value ",
129: testStringDate, value[0]);
130: }
131:
132: /**
133: * Test <code>getArrayProperty()</code> converting to a String.
134: */
135: public void testGetIndexedPropertyDate() {
136: String value = null;
137: try {
138: bean
139: .setDateArrayProperty(new java.util.Date[] { testUtilDate });
140: value = BeanUtils.getIndexedProperty(bean,
141: "dateArrayProperty[0]");
142: } catch (Throwable t) {
143: fail("Threw " + t);
144: }
145: assertEquals("java.util.Date[0] --> String", testStringDate,
146: value);
147: }
148:
149: /**
150: * Test <code>getSimpleProperty()</code> converting to a String.
151: */
152: public void testGetSimplePropertyDate() {
153: String value = null;
154: try {
155: bean.setDateProperty(testUtilDate);
156: value = BeanUtils.getSimpleProperty(bean, "dateProperty");
157: } catch (Throwable t) {
158: fail("Threw " + t);
159: }
160: assertEquals("java.util.Date --> String", testStringDate, value);
161: }
162:
163: /**
164: * Test <code>setProperty()</code> converting to a String.
165: */
166: public void testSetPropertyConvertToString() {
167: try {
168: BeanUtils.setProperty(bean, "stringProperty", testUtilDate);
169: } catch (Throwable t) {
170: fail("Threw " + t);
171: }
172: assertEquals("java.util.Date --> String", testStringDate, bean
173: .getStringProperty());
174: }
175:
176: /**
177: * Test <code>setProperty()</code> converting to a String array.
178: */
179: public void testSetPropertyConvertToStringArray() {
180: try {
181: bean.setStringArray(null);
182: BeanUtils.setProperty(bean, "stringArray",
183: new java.util.Date[] { testUtilDate });
184: } catch (Throwable t) {
185: fail("Threw " + t);
186: }
187: assertEquals("java.util.Date[] --> String[] length", 1, bean
188: .getStringArray().length);
189: assertEquals("java.util.Date[] --> String[] value ",
190: testStringDate, bean.getStringArray()[0]);
191: }
192:
193: /**
194: * Test <code>setProperty()</code> converting to a String on indexed property
195: */
196: public void testSetPropertyConvertToStringIndexed() {
197: try {
198: bean.setStringArray(new String[1]);
199: BeanUtils.setProperty(bean, "stringArray[0]", testUtilDate);
200: } catch (Throwable t) {
201: fail("Threw " + t);
202: }
203: assertEquals("java.util.Date --> String[]", testStringDate,
204: bean.getStringArray()[0]);
205: }
206:
207: }
|