You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2002/07/13 04:22:09 UTC
cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils BeanUtilsTestCase.java DynaBeanUtilsTestCase.java
craigmcc 2002/07/12 19:22:09
Modified: beanutils/src/java/org/apache/commons/beanutils
BeanUtils.java
beanutils/src/java/org/apache/commons/beanutils/converters
BigDecimalConverter.java BigIntegerConverter.java
BooleanArrayConverter.java BooleanConverter.java
ByteArrayConverter.java ByteConverter.java
CharacterArrayConverter.java
CharacterConverter.java DoubleArrayConverter.java
DoubleConverter.java FloatArrayConverter.java
FloatConverter.java IntegerArrayConverter.java
IntegerConverter.java LongArrayConverter.java
LongConverter.java ShortArrayConverter.java
ShortConverter.java SqlDateConverter.java
SqlTimeConverter.java SqlTimestampConverter.java
StringArrayConverter.java
beanutils/src/test/org/apache/commons/beanutils
BeanUtilsTestCase.java DynaBeanUtilsTestCase.java
Log:
Add BeanUtils.copyProperties() and associated BeanUtils.copyProperty() method,
plus associated unit tests. BeanUtils.copyProperties() is similar to
PropertyUtils.copyProperties(), but it attempts to perform type conversions
needed to adapt to differences between the two beans.
For example, it is common (especially in Struts applications) to use String
properties in a form bean that receives values from an HTTP form, even though
your corresponding model objects have int or boolean properties of the same
name. Now, you can call BeanUtils.copyProperties() and have these conversions
(or any other conversions for which you have registered a Converter with
ConvertUtils.register()) automatically.
Revision Changes Path
1.25 +219 -13 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java
Index: BeanUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- BeanUtils.java 7 Jul 2002 23:08:41 -0000 1.24
+++ BeanUtils.java 13 Jul 2002 02:22:08 -0000 1.25
@@ -169,11 +169,206 @@
/**
+ * <p>Copy property values from the origin bean to the destination bean
+ * for all cases where the property names are the same. For each
+ * property, a conversion is attempted as necessary. All combinations of
+ * standard JavaBeans and DynaBeans as origin and destination are
+ * supported. Properties that exist in the origin bean, but do not exist
+ * in the destination bean (or are read-only in the destination bean) are
+ * silently ignored.</p>
+ *
+ * <p>This method differs from <code>populate()</code>, which
+ * was primarily designed for populating JavaBeans from the map of request
+ * parameters retrieved on an HTTP request, is that no scalar->indexed
+ * or indexed->scalar manipulations are performed. If the origin property
+ * is indexed, the destination property must be also.</p>
+ *
+ * <p>If you know that no type conversions are required, the
+ * <code>copyProperties()</code> method in {@link PropertyUtils} will
+ * execute faster than this method.</p>
+ *
+ * <p><strong>FIXME</strong> - Indexed and mapped properties that do not
+ * have getter and setter methods for the underlying array or Map are not
+ * copied by this method.</p>
+ *
+ * @param dest Destination bean whose properties are modified
+ * @param orig Origin bean whose properties are retrieved
+ *
+ * @exception IllegalAccessException if the caller does not have
+ * access to the property accessor method
+ * @exception IllegalArgumentException if the <code>dest</code> or
+ * <code>orig</code> argument is null
+ * @exception InvocationTargetException if the property accessor method
+ * throws an exception
+ * @exception NullPointerException if <code>orig</code> or
+ * <code>dest</code> is <code>null</code>
+ */
+ public static void copyProperties(Object dest, Object orig)
+ throws IllegalAccessException, InvocationTargetException {
+
+ // Validate existence of the specified beans
+ if (dest == null) {
+ throw new IllegalArgumentException
+ ("No destination bean specified");
+ }
+ if (orig == null) {
+ throw new IllegalArgumentException("No origin bean specified");
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("BeanUtils.copyProperties(" + dest + ", " +
+ orig + ")");
+ }
+
+ // Copy the properties, converting as necessary
+ if (orig instanceof DynaBean) {
+ DynaProperty origDescriptors[] =
+ ((DynaBean) orig).getDynaClass().getDynaProperties();
+ for (int i = 0; i < origDescriptors.length; i++) {
+ String name = origDescriptors[i].getName();
+ Object value = ((DynaBean) orig).get(name);
+ copyProperty(dest, name, value);
+ }
+ } else /* if (!(orig instanceof DynaBean)) */ {
+ PropertyDescriptor origDescriptors[] =
+ PropertyUtils.getPropertyDescriptors(orig);
+ for (int i = 0; i < origDescriptors.length; i++) {
+ if (origDescriptors[i].getReadMethod() == null) {
+ if (log.isTraceEnabled()) {
+ log.trace("-->No getter on JavaBean for " +
+ origDescriptors[i].getName() + ", skipping");
+ }
+ continue;
+ }
+ String name = origDescriptors[i].getName();
+ if ("class".equals(name)) {
+ continue; // No point in trying to set an object's class
+ }
+ Object value = null;
+ try {
+ value = PropertyUtils.getSimpleProperty(orig, name);
+ } catch (NoSuchMethodException e) {
+ log.error("-->Should not have happened", e);
+ value = null; // Can not happen
+ }
+ copyProperty(dest, name, value);
+ }
+ }
+
+ }
+
+
+ /**
+ * <p>Copy the specified property value to the specified destination bean,
+ * performing any type conversion that is required. If the specified
+ * bean does not have a property of the specified name, return without
+ * doing anything. If you have custom destination property types, register
+ * {@link Converter}s for them by calling the <code>register()</code>
+ * method of {@link ConvertUtils}.</p>
+ *
+ * <p><strong>FIXME</strong> - Indexed and mapped properties that do not
+ * have getter and setter methods for the underlying array or Map are not
+ * copied by this method.</p>
+ *
+ * @param bean Bean on which setting is to be performed
+ * @param name Simple property name of the property to be set
+ * @param value Property value to be set
+ *
+ * @exception IllegalAccessException if the caller does not have
+ * access to the property accessor method
+ * @exception InvocationTargetException if the property accessor method
+ * throws an exception
+ */
+ public static void copyProperty(Object bean, String name, Object value)
+ throws IllegalAccessException, InvocationTargetException {
+
+ if (log.isTraceEnabled()) {
+ StringBuffer sb = new StringBuffer(" copyProperty(");
+ sb.append(bean);
+ sb.append(", ");
+ sb.append(name);
+ sb.append(", ");
+ if (value == null) {
+ sb.append("<NULL>");
+ } else if (value instanceof String) {
+ sb.append((String) value);
+ } else if (value instanceof String[]) {
+ String values[] = (String[]) value;
+ sb.append('[');
+ for (int i = 0; i < values.length; i++) {
+ if (i > 0) {
+ sb.append(',');
+ }
+ sb.append(values[i]);
+ }
+ sb.append(']');
+ } else {
+ sb.append(value.toString());
+ }
+ sb.append(')');
+ log.trace(sb.toString());
+ }
+
+ if (bean instanceof DynaBean) {
+ DynaProperty propDescriptor =
+ ((DynaBean) bean).getDynaClass().getDynaProperty(name);
+ if (propDescriptor != null) {
+ Converter converter =
+ ConvertUtils.lookup(propDescriptor.getType());
+ if (converter != null) {
+ value = converter.convert(propDescriptor.getType(), value);
+ }
+ try {
+ PropertyUtils.setSimpleProperty(bean, name, value);
+ } catch (NoSuchMethodException e) {
+ log.error("-->Should not have happened", e);
+ ; // Silently ignored
+ }
+ } else {
+ if (log.isTraceEnabled()) {
+ log.trace("-->No setter on 'to' DynaBean, skipping");
+ }
+ }
+ } else /* if (!(bean instanceof DynaBean)) */ {
+ PropertyDescriptor propDescriptor = null;
+ try {
+ propDescriptor =
+ PropertyUtils.getPropertyDescriptor(bean, name);
+ } catch (NoSuchMethodException e) {
+ propDescriptor = null;
+ }
+ if ((propDescriptor != null) &&
+ (propDescriptor.getWriteMethod() == null)) {
+ propDescriptor = null;
+ }
+ if (propDescriptor != null) {
+ Converter converter =
+ ConvertUtils.lookup(propDescriptor.getPropertyType());
+ if (converter != null) {
+ value = converter.convert
+ (propDescriptor.getPropertyType(), value);
+ }
+ try {
+ PropertyUtils.setSimpleProperty(bean, name, value);
+ } catch (NoSuchMethodException e) {
+ log.error("-->Should not have happened", e);
+ ; // Silently ignored
+ }
+ } else {
+ if (log.isTraceEnabled()) {
+ log.trace("-->No setter on JavaBean, skipping");
+ }
+ }
+ }
+
+ }
+
+
+ /**
* Return the entire set of properties for which the specified bean
* provides a read method. This map can be fed back to a call to
* <code>BeanUtils.populate()</code> to reconsitute the same set of
* properties, modulo differences for read-only and write-only
- * properties.
+ * properties, but only if there are no indexed properties.
*
* @param bean Bean whose properties are to be extracted
*
@@ -458,24 +653,30 @@
/**
- * Populate the JavaBeans properties of the specified bean, based on
+ * <p>Populate the JavaBeans properties of the specified bean, based on
* the specified name/value pairs. This method uses Java reflection APIs
* to identify corresponding "property setter" method names, and deals
* with setter arguments of type <code>String</code>, <code>boolean</code>,
* <code>int</code>, <code>long</code>, <code>float</code>, and
* <code>double</code>. In addition, array setters for these types (or the
- * corresponding primitive types) can also be identified.
- * <p>
- * The particular setter method to be called for each property is
+ * corresponding primitive types) can also be identified.</p>
+ *
+ * <p>The particular setter method to be called for each property is
* determined using the usual JavaBeans introspection mechanisms. Thus,
* you may identify custom setter methods using a BeanInfo class that is
* associated with the class of the bean itself. If no such BeanInfo
* class is available, the standard method name conversion ("set" plus
- * the capitalized name of the property in question) is used.
- * <p>
- * <strong>NOTE</strong>: It is contrary to the JavaBeans Specification
+ * the capitalized name of the property in question) is used.</p>
+ *
+ * <p><strong>NOTE</strong>: It is contrary to the JavaBeans Specification
* to have more than one setter method (with different argument
- * signatures) for the same property.
+ * signatures) for the same property.</p>
+ *
+ * <p><strong>WARNING</strong> - The logic of this method is customized
+ * for extracting String-based request parameters from an HTTP request.
+ * It is probably not what you want for general property copying with
+ * type conversion. For that purpose, check out the
+ * <code>copyProperties()</code> method instead.</p>
*
* @param bean JavaBean whose properties are being populated
* @param properties Map keyed by property name, with the
@@ -526,6 +727,11 @@
*
* <p>If <code>null</code> is passed into a property expecting a primitive value,
* then this will be converted as if it were a <code>null</code> string.</p>
+ *
+ * <p><strong>WARNING</strong> - The logic of this method is customized
+ * to meet the needs of <code>populate()</code>, and is probably not what
+ * you want for general property copying with type conversion. For that
+ * purpose, check out the <code>copyProperty()</code> method instead.</p>
*
* @param bean Bean on which setting is to be performed
* @param name Property name (can be nested/indexed/mapped/combo)
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BigDecimalConverter.java
Index: BigDecimalConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BigDecimalConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BigDecimalConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ BigDecimalConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -149,8 +149,12 @@
}
}
+ if (value instanceof BigDecimal) {
+ return (value);
+ }
+
try {
- return (new BigDecimal((String) value));
+ return (new BigDecimal(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BigIntegerConverter.java
Index: BigIntegerConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BigIntegerConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BigIntegerConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ BigIntegerConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -149,8 +149,12 @@
}
}
+ if (value instanceof BigInteger) {
+ return (value);
+ }
+
try {
- return (new BigInteger((String) value));
+ return (new BigInteger(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BooleanArrayConverter.java
Index: BooleanArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BooleanArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BooleanArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ BooleanArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static boolean model[] = new boolean[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.5 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BooleanConverter.java
Index: BooleanConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BooleanConverter.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- BooleanConverter.java 15 Jun 2002 21:14:34 -0000 1.4
+++ BooleanConverter.java 13 Jul 2002 02:22:08 -0000 1.5
@@ -148,8 +148,12 @@
}
}
+ if (value instanceof Boolean) {
+ return (value);
+ }
+
try {
- String stringValue = (String) value;
+ String stringValue = value.toString();
if (stringValue.equalsIgnoreCase("yes") ||
stringValue.equalsIgnoreCase("y") ||
stringValue.equalsIgnoreCase("true") ||
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ByteArrayConverter.java
Index: ByteArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ByteArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ByteArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ ByteArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static byte model[] = new byte[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ByteConverter.java
Index: ByteConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ByteConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ByteConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ ByteConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -148,8 +148,12 @@
}
}
+ if (value instanceof Byte) {
+ return (value);
+ }
+
try {
- return (new Byte((String) value));
+ return (new Byte(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/CharacterArrayConverter.java
Index: CharacterArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/CharacterArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharacterArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ CharacterArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static char model[] = new char[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
Index: CharacterConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- CharacterConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ CharacterConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -148,8 +148,12 @@
}
}
+ if (value instanceof Character) {
+ return (value);
+ }
+
try {
- return (new Character(((String) value).charAt(0)));
+ return (new Character(value.toString().charAt(0)));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/DoubleArrayConverter.java
Index: DoubleArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/DoubleArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DoubleArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ DoubleArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static double model[] = new double[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/DoubleConverter.java
Index: DoubleConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/DoubleConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DoubleConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ DoubleConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -148,8 +148,12 @@
}
}
+ if (value instanceof Double) {
+ return (value);
+ }
+
try {
- return (new Double((String) value));
+ return (new Double(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/FloatArrayConverter.java
Index: FloatArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/FloatArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FloatArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ FloatArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static float model[] = new float[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/FloatConverter.java
Index: FloatConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/FloatConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FloatConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ FloatConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -148,8 +148,12 @@
}
}
+ if (value instanceof Float) {
+ return (value);
+ }
+
try {
- return (new Float((String) value));
+ return (new Float(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/IntegerArrayConverter.java
Index: IntegerArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/IntegerArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- IntegerArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ IntegerArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static int model[] = new int[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/IntegerConverter.java
Index: IntegerConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/IntegerConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- IntegerConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ IntegerConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -148,8 +148,12 @@
}
}
+ if (value instanceof Integer) {
+ return (value);
+ }
+
try {
- return (new Integer((String) value));
+ return (new Integer(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/LongArrayConverter.java
Index: LongArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/LongArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LongArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ LongArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static long model[] = new long[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/LongConverter.java
Index: LongConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/LongConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LongConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ LongConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -148,8 +148,12 @@
}
}
+ if (value instanceof Long) {
+ return (value);
+ }
+
try {
- return (new Long((String) value));
+ return (new Long(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ShortArrayConverter.java
Index: ShortArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ShortArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ShortArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ ShortArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static short model[] = new short[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ShortConverter.java
Index: ShortConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ShortConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ShortConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ ShortConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -148,8 +148,12 @@
}
}
+ if (value instanceof Short) {
+ return (value);
+ }
+
try {
- return (new Short((String) value));
+ return (new Short(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/SqlDateConverter.java
Index: SqlDateConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/SqlDateConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SqlDateConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ SqlDateConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -149,8 +149,12 @@
}
}
+ if (value instanceof Date) {
+ return (value);
+ }
+
try {
- return (Date.valueOf((String) value));
+ return (Date.valueOf(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java
Index: SqlTimeConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SqlTimeConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ SqlTimeConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -149,8 +149,12 @@
}
}
+ if (value instanceof Time) {
+ return (value);
+ }
+
try {
- return (Time.valueOf((String) value));
+ return (Time.valueOf(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.3 +9 -5 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java
Index: SqlTimestampConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SqlTimestampConverter.java 11 Apr 2002 19:50:06 -0000 1.2
+++ SqlTimestampConverter.java 13 Jul 2002 02:22:08 -0000 1.3
@@ -149,8 +149,12 @@
}
}
+ if (value instanceof Timestamp) {
+ return (value);
+ }
+
try {
- return (Timestamp.valueOf((String) value));
+ return (Timestamp.valueOf(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
1.2 +18 -4 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/StringArrayConverter.java
Index: StringArrayConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/StringArrayConverter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StringArrayConverter.java 29 Jun 2002 22:29:22 -0000 1.1
+++ StringArrayConverter.java 13 Jul 2002 02:22:08 -0000 1.2
@@ -111,6 +111,15 @@
}
+ // ------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Model object for type comparisons.</p>
+ */
+ private static String model[] = new String[0];
+
+
// --------------------------------------------------------- Public Methods
@@ -133,6 +142,11 @@
} else {
throw new ConversionException("No value specified");
}
+ }
+
+ // Deal with the no-conversion-needed case
+ if (model.getClass() == value.getClass()) {
+ return (value);
}
// Parse the input value as a String into elements
1.12 +130 -4 jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java
Index: BeanUtilsTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- BeanUtilsTestCase.java 7 Jul 2002 23:08:42 -0000 1.11
+++ BeanUtilsTestCase.java 13 Jul 2002 02:22:09 -0000 1.12
@@ -176,6 +176,132 @@
/**
+ * Test the copyProperties() method from a DynaBean.
+ */
+ public void testCopyPropertiesDynaBean() {
+
+ // Set up an origin bean with customized properties
+ DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
+ DynaBean orig = null;
+ try {
+ orig = dynaClass.newInstance();
+ } catch (Exception e) {
+ fail("newInstance(): " + e);
+ }
+ orig.set("booleanProperty", Boolean.FALSE);
+ orig.set("doubleProperty", new Double(333.33));
+ orig.set("intArray", new int[] { 100, 200, 300 });
+ orig.set("intProperty", new Integer(333));
+ orig.set("longProperty", new Long(3333));
+ orig.set("shortProperty", new Short((short) 33));
+ orig.set("stringArray", new String[] { "New 0", "New 1" });
+ orig.set("stringProperty", "Custom string");
+
+ // Copy the origin bean to our destination test bean
+ try {
+ BeanUtils.copyProperties(bean, orig);
+ } catch (Exception e) {
+ fail("Threw exception: " + e);
+ }
+
+ // Validate the results for scalar properties
+ assertEquals("Copied boolean property",
+ false,
+ bean.getBooleanProperty());
+ assertEquals("Copied double property",
+ 333.33,
+ bean.getDoubleProperty(),
+ 0.005);
+ assertEquals("Copied int property",
+ 333,
+ bean.getIntProperty());
+ assertEquals("Copied long property",
+ (long) 3333,
+ bean.getLongProperty());
+ assertEquals("Copied short property",
+ (short) 33,
+ bean.getShortProperty());
+ assertEquals("Copied string property",
+ "Custom string",
+ bean.getStringProperty());
+
+ // Validate the results for array properties
+ int intArray[] = bean.getIntArray();
+ assertNotNull("intArray present", intArray);
+ assertEquals("intArray length", 3, intArray.length);
+ assertEquals("intArray[0]", 100, intArray[0]);
+ assertEquals("intArray[1]", 200, intArray[1]);
+ assertEquals("intArray[2]", 300, intArray[2]);
+ String stringArray[] = bean.getStringArray();
+ assertNotNull("stringArray present", stringArray);
+ assertEquals("stringArray length", 2, stringArray.length);
+ assertEquals("stringArray[0]", "New 0", stringArray[0]);
+ assertEquals("stringArray[1]", "New 1", stringArray[1]);
+
+ }
+
+
+ /**
+ * Test the copyProperties() method from a standard JavaBean.
+ */
+ public void testCopyPropertiesStandard() {
+
+ // Set up an origin bean with customized properties
+ TestBean orig = new TestBean();
+ orig.setBooleanProperty(false);
+ orig.setDoubleProperty(333.33);
+ orig.setIntArray(new int[] { 100, 200, 300 });
+ orig.setIntProperty(333);
+ orig.setLongProperty(3333);
+ orig.setShortProperty((short) 33);
+ orig.setStringArray(new String[] { "New 0", "New 1" });
+ orig.setStringProperty("Custom string");
+
+ // Copy the origin bean to our destination test bean
+ try {
+ BeanUtils.copyProperties(bean, orig);
+ } catch (Exception e) {
+ fail("Threw exception: " + e);
+ }
+
+ // Validate the results for scalar properties
+ assertEquals("Copied boolean property",
+ false,
+ bean.getBooleanProperty());
+ assertEquals("Copied double property",
+ 333.33,
+ bean.getDoubleProperty(),
+ 0.005);
+ assertEquals("Copied int property",
+ 333,
+ bean.getIntProperty());
+ assertEquals("Copied long property",
+ (long) 3333,
+ bean.getLongProperty());
+ assertEquals("Copied short property",
+ (short) 33,
+ bean.getShortProperty());
+ assertEquals("Copied string property",
+ "Custom string",
+ bean.getStringProperty());
+
+ // Validate the results for array properties
+ int intArray[] = bean.getIntArray();
+ assertNotNull("intArray present", intArray);
+ assertEquals("intArray length", 3, intArray.length);
+ assertEquals("intArray[0]", 100, intArray[0]);
+ assertEquals("intArray[1]", 200, intArray[1]);
+ assertEquals("intArray[2]", 300, intArray[2]);
+ String stringArray[] = bean.getStringArray();
+ assertNotNull("stringArray present", stringArray);
+ assertEquals("stringArray length", 2, stringArray.length);
+ assertEquals("stringArray[0]", "New 0", stringArray[0]);
+ assertEquals("stringArray[1]", "New 1", stringArray[1]);
+
+ }
+
+
+ /**
* Test the describe() method.
*/
public void testDescribe() {
1.10 +131 -5 jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java
Index: DynaBeanUtilsTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- DynaBeanUtilsTestCase.java 7 Jul 2002 23:08:42 -0000 1.9
+++ DynaBeanUtilsTestCase.java 13 Jul 2002 02:22:09 -0000 1.10
@@ -217,6 +217,132 @@
/**
+ * Test the copyProperties() method from a DynaBean.
+ */
+ public void testCopyPropertiesDynaBean() {
+
+ // Set up an origin bean with customized properties
+ DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass();
+ DynaBean orig = null;
+ try {
+ orig = dynaClass.newInstance();
+ } catch (Exception e) {
+ fail("newInstance(): " + e);
+ }
+ orig.set("booleanProperty", Boolean.FALSE);
+ orig.set("doubleProperty", new Double(333.33));
+ orig.set("intArray", new int[] { 100, 200, 300 });
+ orig.set("intProperty", new Integer(333));
+ orig.set("longProperty", new Long(3333));
+ orig.set("shortProperty", new Short((short) 33));
+ orig.set("stringArray", new String[] { "New 0", "New 1" });
+ orig.set("stringProperty", "Custom string");
+
+ // Copy the origin bean to our destination test bean
+ try {
+ BeanUtils.copyProperties(bean, orig);
+ } catch (Exception e) {
+ fail("Threw exception: " + e);
+ }
+
+ // Validate the results for scalar properties
+ assertEquals("Copied boolean property",
+ false,
+ ((Boolean) bean.get("booleanProperty")).booleanValue());
+ assertEquals("Copied double property",
+ 333.33,
+ ((Double) bean.get("doubleProperty")).doubleValue(),
+ 0.005);
+ assertEquals("Copied int property",
+ 333,
+ ((Integer) bean.get("intProperty")).intValue());
+ assertEquals("Copied long property",
+ (long) 3333,
+ ((Long) bean.get("longProperty")).longValue());
+ assertEquals("Copied short property",
+ (short) 33,
+ ((Short) bean.get("shortProperty")).shortValue());
+ assertEquals("Copied string property",
+ "Custom string",
+ (String) bean.get("stringProperty"));
+
+ // Validate the results for array properties
+ int intArray[] = (int[]) bean.get("intArray");
+ assertNotNull("intArray present", intArray);
+ assertEquals("intArray length", 3, intArray.length);
+ assertEquals("intArray[0]", 100, intArray[0]);
+ assertEquals("intArray[1]", 200, intArray[1]);
+ assertEquals("intArray[2]", 300, intArray[2]);
+ String stringArray[] = (String[]) bean.get("stringArray");
+ assertNotNull("stringArray present", stringArray);
+ assertEquals("stringArray length", 2, stringArray.length);
+ assertEquals("stringArray[0]", "New 0", stringArray[0]);
+ assertEquals("stringArray[1]", "New 1", stringArray[1]);
+
+ }
+
+
+ /**
+ * Test the copyProperties() method from a standard JavaBean.
+ */
+ public void testCopyPropertiesStandard() {
+
+ // Set up an origin bean with customized properties
+ TestBean orig = new TestBean();
+ orig.setBooleanProperty(false);
+ orig.setDoubleProperty(333.33);
+ orig.setIntArray(new int[] { 100, 200, 300 });
+ orig.setIntProperty(333);
+ orig.setLongProperty(3333);
+ orig.setShortProperty((short) 33);
+ orig.setStringArray(new String[] { "New 0", "New 1" });
+ orig.setStringProperty("Custom string");
+
+ // Copy the origin bean to our destination test bean
+ try {
+ BeanUtils.copyProperties(bean, orig);
+ } catch (Exception e) {
+ fail("Threw exception: " + e);
+ }
+
+ // Validate the results for scalar properties
+ assertEquals("Copied boolean property",
+ false,
+ ((Boolean) bean.get("booleanProperty")).booleanValue());
+ assertEquals("Copied double property",
+ 333.33,
+ ((Double) bean.get("doubleProperty")).doubleValue(),
+ 0.005);
+ assertEquals("Copied int property",
+ 333,
+ ((Integer) bean.get("intProperty")).intValue());
+ assertEquals("Copied long property",
+ (long) 3333,
+ ((Long) bean.get("longProperty")).longValue());
+ assertEquals("Copied short property",
+ (short) 33,
+ ((Short) bean.get("shortProperty")).shortValue());
+ assertEquals("Copied string property",
+ "Custom string",
+ (String) bean.get("stringProperty"));
+
+ // Validate the results for array properties
+ int intArray[] = (int[]) bean.get("intArray");
+ assertNotNull("intArray present", intArray);
+ assertEquals("intArray length", 3, intArray.length);
+ assertEquals("intArray[0]", 100, intArray[0]);
+ assertEquals("intArray[1]", 200, intArray[1]);
+ assertEquals("intArray[2]", 300, intArray[2]);
+ String stringArray[] = (String[]) bean.get("stringArray");
+ assertNotNull("stringArray present", stringArray);
+ assertEquals("stringArray length", 2, stringArray.length);
+ assertEquals("stringArray[0]", "New 0", stringArray[0]);
+ assertEquals("stringArray[1]", "New 1", stringArray[1]);
+
+ }
+
+
+ /**
* Test the describe() method.
*/
public void testDescribe() {
@@ -639,7 +765,7 @@
* Create and return a <code>DynaClass</code> instance for our test
* <code>DynaBean</code>.
*/
- protected DynaClass createDynaClass() {
+ protected static DynaClass createDynaClass() {
int intArray[] = new int[0];
String stringArray[] = new String[0];
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>