Author: niallp Date: Sun Jul 1 19:00:17 2007 New Revision: 552381 URL: http://svn.apache.org/viewvc?view=rev&rev=552381 Log: BEANUTILS-285 and BEANUTILS-258 - provide new BeanUtilsBean / ConvertUtilsBean implementations that take full advantage of the new Converter implementations - default behaviour is compatible with BeanUtils 1.7.0. These new implementations are configured by calling BeanUtilsBean.setInstance(new BeanUtilsBean2);
Added: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean2.java (with props) jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean2.java (with props) jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtils2TestCase.java (with props) Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java?view=diff&rev=552381&r1=552380&r2=552381 ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java Sun Jul 1 19:00:17 2007 @@ -118,6 +118,19 @@ } /** + * <p>Constructs an instance using given conversion instances + * and new [EMAIL PROTECTED] PropertyUtilsBean} instance.</p> + * + * @param convertUtilsBean use this <code>ConvertUtilsBean</code> + * to perform conversions from one object to another + * + * @since 1.8.0 + */ + public BeanUtilsBean(ConvertUtilsBean convertUtilsBean) { + this(convertUtilsBean, new PropertyUtilsBean()); + } + + /** * <p>Constructs an instance using given property and conversion instances.</p> * * @param convertUtilsBean use this <code>ConvertUtilsBean</code> @@ -398,11 +411,7 @@ // Convert the specified value to the required type and store it if (index >= 0) { // Destination must be indexed - Converter converter = getConvertUtils().lookup(type.getComponentType()); - if (converter != null) { - log.trace(" USING CONVERTER " + converter); - value = converter.convert(type.getComponentType(), value); - } + value = convert(value, type.getComponentType()); try { getPropertyUtils().setIndexedProperty(target, propName, index, value); @@ -422,11 +431,7 @@ (e, "Cannot set " + propName); } } else { // Destination must be simple - Converter converter = getConvertUtils().lookup(type); - if (converter != null) { - log.trace(" USING CONVERTER " + converter); - value = converter.convert(type, value); - } + value = convert(value, type); try { getPropertyUtils().setSimpleProperty(target, propName, value); } catch (NoSuchMethodException e) { @@ -975,7 +980,7 @@ } else if (value instanceof String[]) { newValue = getConvertUtils().convert((String[]) value, type); } else { - newValue = getConvertUtils().convert(value, type); + newValue = convert(value, type); } } else if (type.isArray()) { // Indexed value into array if (value instanceof String || value == null) { @@ -985,7 +990,7 @@ newValue = getConvertUtils().convert(((String[]) value)[0], type.getComponentType()); } else { - newValue = getConvertUtils().convert(value, type.getComponentType()); + newValue = convert(value, type.getComponentType()); } } else { // Value into scalar if ((value instanceof String) || (value == null)) { @@ -994,7 +999,7 @@ newValue = getConvertUtils().convert(((String[]) value)[0], type); } else { - newValue = getConvertUtils().convert(value, type); + newValue = convert(value, type); } } @@ -1050,6 +1055,26 @@ } } return false; + } + + /** + * <p>Convert the value to an object of the specified class (if + * possible).</p> + * + * @param value Value to be converted (may be null) + * @param type Class of the value to be converted to + * @return The converted value + * + * @exception ConversionException if thrown by an underlying Converter + */ + protected Object convert(Object value, Class type) { + Converter converter = getConvertUtils().lookup(type); + if (converter != null) { + log.trace(" USING CONVERTER " + converter); + return converter.convert(type, value); + } else { + return value; + } } /** Added: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean2.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean2.java?view=auto&rev=552381 ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean2.java (added) +++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean2.java Sun Jul 1 19:00:17 2007 @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.beanutils; + +/** + * <p>[EMAIL PROTECTED] BeanUtilsBean} implementation that creates a + * [EMAIL PROTECTED] ConvertUtilsBean2} and delegates conversion to + * [EMAIL PROTECTED] ConvertUtilsBean#convert(Object, Class)}. + * </p> + * + * <p> + * To configure this implementation for the current context ClassLoader invoke + * <code>BeanUtilsBean.setInstance(new BeanUtilsBean2());</code> + * </p> + * + * <p> + * BeanUtils 1.7.0 delegated all conversion to String to the converter + * registered for the <code>String.class</code>. One of the improvements in + * BeanUtils 1.8.0 was to upgrade the [EMAIL PROTECTED] Converter} implementations so + * that they could handle conversion to String for their type (e.g. + * IntegerConverter now handles conversion from an Integer to a String as + * well as String to Integer). + * </p> + * + * <p> + * In order to take advantage of these improvements BeanUtils needs to change + * how it gets the appropriate [EMAIL PROTECTED] Converter}. This functionality has been + * implemented in the new [EMAIL PROTECTED] ConvertUtilsBean#lookup(Class, Class)} and + * [EMAIL PROTECTED] ConvertUtilsBean#convert(Object, Class)} methods. However changing + * [EMAIL PROTECTED] BeanUtilsBean} to use these methods could create compatibility + * issues for existing users. In order to avoid that, this new + * [EMAIL PROTECTED] BeanUtilsBean} implementation has been created (and the associated + * [EMAIL PROTECTED] ConvertUtilsBean2}). + * </p> + * + * @see ConvertUtilsBean2 + * @version $Revision$ $Date$ + * @since 1.8.0 + */ +public class BeanUtilsBean2 extends BeanUtilsBean { + + /** + * <p>Constructs an instance using new property + * and conversion instances.</p> + */ + public BeanUtilsBean2() { + super(new ConvertUtilsBean2()); + } + + /** + * <p>Convert the value to an object of the specified class (if + * possible).</p> + * + * @param value Value to be converted (may be null) + * @param type Class of the value to be converted to + * @return The converted value + */ + protected Object convert(Object value, Class type) { + return getConvertUtils().convert(value, type); + } +} Propchange: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean2.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean2.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean2.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean2.java?view=auto&rev=552381 ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean2.java (added) +++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean2.java Sun Jul 1 19:00:17 2007 @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.beanutils; + +/** + * [EMAIL PROTECTED] ConvertUtilsBean} implementation that delegates <code>convert()</code> + * methods to the new [EMAIL PROTECTED] ConvertUtilsBean#convert(Object, Class)} method. + * + * <p> + * To configure this implementation for the current context ClassLoader invoke + * <code>BeanUtilsBean.setInstance(new BeanUtilsBean2());</code> + * </p> + * + * @see BeanUtilsBean2 + * @version $Revision$ $Date$ + * @since 1.8.0 + */ +public class ConvertUtilsBean2 extends ConvertUtilsBean { + + /** + * Delegates to the new [EMAIL PROTECTED] ConvertUtilsBean#convert(Object, Class)} + * method. + * + * @param value Value to be converted (may be null) + * @return The converted String value + * + * @see ConvertUtilsBean#convert(String[], Class) + */ + public String convert(Object value) { + return (String)convert(value, String.class); + } + + /** + * Delegates to the new [EMAIL PROTECTED] ConvertUtilsBean#convert(Object, Class)} + * method. + * + * @param value Value to be converted (may be null) + * @param clazz Java class to be converted to + * @return The converted value + * + * @see ConvertUtilsBean#convert(String[], Class) + */ + public Object convert(String value, Class clazz) { + return convert((Object)value, clazz); + } + + /** + * Delegates to the new [EMAIL PROTECTED] ConvertUtilsBean#convert(Object, Class)} + * method. + * + * @param value Array of values to be converted + * @param clazz Java array or element class to be converted to + * @return The converted value + * + * @see ConvertUtilsBean#convert(String[], Class) + */ + public Object convert(String[] value, Class clazz) { + return convert((Object)value, clazz); + } + +} Propchange: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean2.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/ConvertUtilsBean2.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtils2TestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtils2TestCase.java?view=auto&rev=552381 ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtils2TestCase.java (added) +++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtils2TestCase.java Sun Jul 1 19:00:17 2007 @@ -0,0 +1,190 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.beanutils; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test Case for the [EMAIL PROTECTED] BeanUtilsBean2}. + * + * @version $Revision$ + */ +public class BeanUtils2TestCase extends BeanUtilsTestCase { + + // ---------------------------------------------------------- Constructors + + /** + * Construct a new instance of this test case. + * + * @param name Name of the test case + */ + public BeanUtils2TestCase(String name) { + super(name); + } + + + // -------------------------------------------------- Overall Test Methods + + + /** + * Set up instance variables required by this test case. + */ + public void setUp() { + ConvertUtils.deregister(); + BeanUtilsBean.setInstance(new BeanUtilsBean2()); + setUpShared(); + } + + + /** + * Return the tests included in this test suite. + */ + public static Test suite() { + return (new TestSuite(BeanUtils2TestCase.class)); + } + + /** + * Tear down instance variables required by this test case. + */ + public void tearDown() { + bean = null; + } + + /** + * Test <code>copyProperty()</code> converting to a String. + */ + public void testCopyPropertyConvertToString() { + try { + BeanUtils.copyProperty(bean, "stringProperty", testUtilDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String", testStringDate, bean.getStringProperty()); + } + + /** + * Test <code>copyProperty()</code> converting to a String. + */ + public void testCopyPropertyConvertToStringArray() { + try { + bean.setStringArray(null); + BeanUtils.copyProperty(bean, "stringArray", new java.util.Date[] {testUtilDate}); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length); + assertEquals("java.util.Date[] --> String[] value ", testStringDate, bean.getStringArray()[0]); + } + + /** + * Test <code>copyProperty()</code> converting to a String on indexed property + */ + public void testCopyPropertyConvertToStringIndexed() { + try { + bean.setStringArray(new String[1]); + BeanUtils.copyProperty(bean, "stringArray[0]", testUtilDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length); + assertEquals("java.util.Date[] --> String[] value ", testStringDate, bean.getStringArray()[0]); + } + + /** + * Test <code>getArrayProperty()</code> converting to a String. + */ + public void testGetArrayPropertyDate() { + String[] value = null; + try { + bean.setDateArrayProperty(new java.util.Date[] {testUtilDate}); + value = BeanUtils.getArrayProperty(bean, "dateArrayProperty"); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[] --> String[] length", 1, value.length); + assertEquals("java.util.Date[] --> String[] value ", testStringDate, value[0]); + } + + /** + * Test <code>getArrayProperty()</code> converting to a String. + */ + public void testGetIndexedPropertyDate() { + String value = null; + try { + bean.setDateArrayProperty(new java.util.Date[] {testUtilDate}); + value = BeanUtils.getIndexedProperty(bean, "dateArrayProperty[0]"); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[0] --> String", testStringDate, value); + } + + /** + * Test <code>getSimpleProperty()</code> converting to a String. + */ + public void testGetSimplePropertyDate() { + String value = null; + try { + bean.setDateProperty(testUtilDate); + value = BeanUtils.getSimpleProperty(bean, "dateProperty"); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String", testStringDate, value); + } + + /** + * Test <code>setProperty()</code> converting to a String. + */ + public void testSetPropertyConvertToString() { + try { + BeanUtils.setProperty(bean, "stringProperty", testUtilDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String", testStringDate, bean.getStringProperty()); + } + + /** + * Test <code>setProperty()</code> converting to a String array. + */ + public void testSetPropertyConvertToStringArray() { + try { + bean.setStringArray(null); + BeanUtils.setProperty(bean, "stringArray", new java.util.Date[] {testUtilDate}); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length); + assertEquals("java.util.Date[] --> String[] value ", testStringDate, bean.getStringArray()[0]); + } + + /** + * Test <code>setProperty()</code> converting to a String on indexed property + */ + public void testSetPropertyConvertToStringIndexed() { + try { + bean.setStringArray(new String[1]); + BeanUtils.setProperty(bean, "stringArray[0]", testUtilDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String[]", testStringDate, bean.getStringArray()[0]); + } + +} Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtils2TestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtils2TestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java?view=diff&rev=552381&r1=552380&r2=552381 ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java Sun Jul 1 19:00:17 2007 @@ -19,11 +19,16 @@ import java.lang.reflect.InvocationTargetException; +import java.util.Calendar; import java.util.HashMap; import java.util.Iterator; +import java.util.Locale; import java.util.Map; import java.util.StringTokenizer; +import org.apache.commons.beanutils.converters.ArrayConverter; +import org.apache.commons.beanutils.converters.DateConverter; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -94,6 +99,14 @@ "stringProperty" }; + /** Test Calendar value */ + protected java.util.Calendar testCalendar; + + /** Test java.util.Date value */ + protected java.util.Date testUtilDate; + + /** Test String Date value */ + protected String testStringDate; // ---------------------------------------------------------- Constructors @@ -114,7 +127,31 @@ * Set up instance variables required by this test case. */ public void setUp() { + ConvertUtils.deregister(); + BeanUtilsBean.setInstance(new BeanUtilsBean()); + setUpShared(); + } + + /** + * Shared Set up. + */ + protected void setUpShared() { bean = new TestBean(); + + DateConverter dateConverter = new DateConverter(null); + dateConverter.setLocale(Locale.US); + dateConverter.setPattern("dd.MM.yyyy"); + ConvertUtils.register(dateConverter, java.util.Date.class); + + ArrayConverter dateArrayConverter = + new ArrayConverter(java.util.Date[].class, dateConverter, 0); + ConvertUtils.register(dateArrayConverter, java.util.Date[].class); + + testCalendar = Calendar.getInstance(); + testCalendar.set(1992, 11, 28, 0, 0, 0); + testCalendar.set(Calendar.MILLISECOND, 0); + testUtilDate = testCalendar.getTime(); + testStringDate = "28.12.1992"; } @@ -434,6 +471,20 @@ } + /** + * Test <code>getArrayProperty()</code> converting to a String. + */ + public void testGetArrayPropertyDate() { + String[] value = null; + try { + bean.setDateArrayProperty(new java.util.Date[] {testUtilDate}); + value = BeanUtils.getArrayProperty(bean, "dateArrayProperty"); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[] --> String[] length", 1, value.length); + assertEquals("java.util.Date[] --> String[] value ", testUtilDate.toString(), value[0]); + } /** * tests getting an indexed property @@ -456,6 +507,19 @@ } } + /** + * Test <code>getArrayProperty()</code> converting to a String. + */ + public void testGetIndexedPropertyDate() { + String value = null; + try { + bean.setDateArrayProperty(new java.util.Date[] {testUtilDate}); + value = BeanUtils.getIndexedProperty(bean, "dateArrayProperty[0]"); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[0] --> String", testUtilDate.toString(), value); + } /** * tests getting an indexed property @@ -540,6 +604,19 @@ } } + /** + * Test <code>getSimpleProperty()</code> converting to a String. + */ + public void testGetSimplePropertyDate() { + String value = null; + try { + bean.setDateProperty(testUtilDate); + value = BeanUtils.getSimpleProperty(bean, "dateProperty"); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String", testUtilDate.toString(), value); + } /** * Test populate() method on individual array elements. @@ -854,6 +931,68 @@ } + /** + * Test <code>setProperty()</code> conversion. + */ + public void testSetPropertyConvert() { + try { + BeanUtils.setProperty(bean, "dateProperty", testCalendar); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("Calendar --> java.util.Date", testUtilDate, bean.getDateProperty()); + } + + /** + * Test <code>setProperty()</code> converting from a String. + */ + public void testSetPropertyConvertFromString() { + try { + BeanUtils.setProperty(bean, "dateProperty", testStringDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("String --> java.util.Date", testUtilDate, bean.getDateProperty()); + } + + /** + * Test <code>setProperty()</code> converting to a String. + */ + public void testSetPropertyConvertToString() { + try { + BeanUtils.setProperty(bean, "stringProperty", testUtilDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String", testUtilDate.toString(), bean.getStringProperty()); + } + + /** + * Test <code>setProperty()</code> converting to a String array. + */ + public void testSetPropertyConvertToStringArray() { + try { + bean.setStringArray(null); + BeanUtils.setProperty(bean, "stringArray", new java.util.Date[] {testUtilDate}); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length); + assertEquals("java.util.Date[] --> String[] value ", testUtilDate.toString(), bean.getStringArray()[0]); + } + + /** + * Test <code>setProperty()</code> converting to a String on indexed property + */ + public void testSetPropertyConvertToStringIndexed() { + try { + bean.setStringArray(new String[1]); + BeanUtils.setProperty(bean, "stringArray[0]", testUtilDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String[]", testUtilDate.toString(), bean.getStringArray()[0]); + } /** * Test narrowing and widening conversions on double. @@ -1019,6 +1158,68 @@ } + /** + * Test <code>copyProperty()</code> conversion. + */ + public void testCopyPropertyConvert() { + try { + BeanUtils.copyProperty(bean, "dateProperty", testCalendar); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("Calendar --> java.util.Date", testUtilDate, bean.getDateProperty()); + } + + /** + * Test <code>copyProperty()</code> converting from a String. + */ + public void testCopyPropertyConvertFromString() { + try { + BeanUtils.copyProperty(bean, "dateProperty", testStringDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("String --> java.util.Date", testUtilDate, bean.getDateProperty()); + } + + /** + * Test <code>copyProperty()</code> converting to a String. + */ + public void testCopyPropertyConvertToString() { + try { + BeanUtils.copyProperty(bean, "stringProperty", testUtilDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String", testUtilDate.toString(), bean.getStringProperty()); + } + + /** + * Test <code>copyProperty()</code> converting to a String. + */ + public void testCopyPropertyConvertToStringArray() { + try { + bean.setStringArray(null); + BeanUtils.copyProperty(bean, "stringArray", new java.util.Date[] {testUtilDate}); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length); + assertEquals("java.util.Date[] --> String[] value ", testUtilDate.toString(), bean.getStringArray()[0]); + } + + /** + * Test <code>copyProperty()</code> converting to a String on indexed property + */ + public void testCopyPropertyConvertToStringIndexed() { + try { + bean.setStringArray(new String[1]); + BeanUtils.copyProperty(bean, "stringArray[0]", testUtilDate); + } catch (Throwable t) { + fail("Threw " + t); + } + assertEquals("java.util.Date --> String[]", testUtilDate.toString(), bean.getStringArray()[0]); + } /** * Test narrowing and widening conversions on double. Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java?view=diff&rev=552381&r1=552380&r2=552381 ============================================================================== --- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java (original) +++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java Sun Jul 1 19:00:17 2007 @@ -141,6 +141,32 @@ /** + * A java.util.Date property. + */ + private java.util.Date dateProperty; + + public java.util.Date getDateProperty() { + return dateProperty; + } + + public void setDateProperty(java.util.Date dateProperty) { + this.dateProperty = dateProperty; + } + + /** + * A java.util.Date property. + */ + private java.util.Date[] dateArrayProperty; + + public java.util.Date[] getDateArrayProperty() { + return dateArrayProperty; + } + + public void setDateArrayProperty(java.util.Date[] dateArrayProperty) { + this.dateArrayProperty = dateArrayProperty; + } + + /** * A double property. */ private double doubleProperty = 321.0; --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]