cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johnth...@apache.org
Subject svn commit: r1523710 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/
Date Mon, 16 Sep 2013 15:44:03 GMT
Author: johnthuss
Date: Mon Sep 16 15:44:02 2013
New Revision: 1523710

URL: http://svn.apache.org/r1523710
Log:
CAY-1847 Make ConverterFactory extensible

Added:
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ToAnyConverter.java
Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/Converter.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ConverterFactory.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/EnumConverter.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/PropertyUtilsTest.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/TestJavaBean.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=1523710&r1=1523709&r2=1523710&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Mon Sep 16 15:44:02 2013
@@ -24,6 +24,7 @@ CAY-1855 Iterated and paginated queries 
 CAY-1856 Expression.expWithParameters does not work when parameters are placed in the inline
collection 
 CAY-1860 In-memory matching of DataObjects against ObjectId or int
 CAY-1861 Remove runtime relationships
+CAY-1847 Make ConverterFactory extensible
 
 Bug Fixes:
 

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/Converter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/Converter.java?rev=1523710&r1=1523709&r2=1523710&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/Converter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/Converter.java
Mon Sep 16 15:44:02 2013
@@ -24,11 +24,16 @@ package org.apache.cayenne.reflect;
  * 
  * @since 1.2
  */
-// keeping non-public for now, but this can also be used in expressions and such...
-abstract class Converter {
+public abstract class Converter<T> {
 
-    /**
-     * Converts object to supported class without doing any type checking.
-     */
-    abstract Object convert(Object value, Class<?> type);
+	/**
+	 * Converts object to supported class without doing any type checking.
+	 * @param value 
+	 * 		the object to convert; the source
+	 * @param type
+	 * 		the Class to convert the value to; the destination type
+	 * @return
+	 * 		an object of type @code{type}. If the conversion fails an exception will be thrown.
If value is null then the result will be null.
+	 */
+    protected abstract T convert(Object value, Class<T> type);
 }

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ConverterFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ConverterFactory.java?rev=1523710&r1=1523709&r2=1523710&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ConverterFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ConverterFactory.java
Mon Sep 16 15:44:02 2013
@@ -21,50 +21,46 @@ package org.apache.cayenne.reflect;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.sql.Timestamp;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
+
 /**
  * A factory of property type converters.
  * 
  * @since 1.2
  */
-class ConverterFactory {
+public class ConverterFactory {
 
     static final ConverterFactory factory = new ConverterFactory();
 
-    private Map<String, Converter> converters;
+    private Map<Class<?>, Converter<?>> converters;
     private EnumConverter enumConveter = new EnumConverter();
-
-    static final Converter noopConverter = new Converter() {
-
-        @Override
-        Object convert(Object object, Class<?> type) {
-            return object;
-        }
-    };
-
+    private Converter<Object> toAnyConverter = new ToAnyConverter<Object>();
+    
     private ConverterFactory() {
 
-        Converter stringConverter = new Converter() {
+        Converter<String> toStringConverter = new Converter<String>() {
 
             @Override
-            Object convert(Object object, Class<?> type) {
+			protected String convert(Object object, Class<String> type) {
                 return object != null ? object.toString() : null;
             }
         };
 
-        Converter booleanConverter = new Converter() {
+        Converter<Boolean> toBooleanConverter = new Converter<Boolean>() {
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected Boolean convert(Object object, Class<Boolean> type) {
                 if (object == null) {
                     return type.isPrimitive() ? Boolean.FALSE : null;
                 }
 
                 if (object instanceof Boolean) {
-                    return object;
-                } else if (object instanceof Integer || object instanceof Long || object
instanceof Short) {
+                    return (Boolean)object;
+                } else if (object instanceof Integer || object instanceof Long || object
instanceof Short || object instanceof Byte) {
                 	if (((Number)object).longValue() == 0)
                 		return Boolean.FALSE;
                 	else if (((Number)object).longValue() == 1)
@@ -77,64 +73,80 @@ class ConverterFactory {
             }
         };
 
-        Converter intConverter = new Converter() {
+        Converter<Long> toLongConverter = new Converter<Long>() {
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected Long convert(Object object, Class<Long> type) {
+                if (object == null) {
+                    return type.isPrimitive() ? Long.valueOf(0) : null;
+                }
+
+                if (object instanceof Long) {
+                    return (Long)object;
+                }
+
+                return new Long(object.toString());
+            }
+        };
+        
+        Converter<Integer> toIntConverter = new Converter<Integer>() {
+
+            @Override
+            protected Integer convert(Object object, Class<Integer> type) {
                 if (object == null) {
                     return type.isPrimitive() ? Integer.valueOf(0) : null;
                 }
 
                 if (object instanceof Integer) {
-                    return object;
+                    return (Integer)object;
                 }
 
                 return new Integer(object.toString());
             }
         };
 
-        Converter byteConverter = new Converter() {
+        Converter<Byte> toByteConverter = new Converter<Byte>() {
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected Byte convert(Object object, Class<Byte> type) {
                 if (object == null) {
                     return type.isPrimitive() ? Byte.valueOf((byte) 0) : null;
                 }
 
                 if (object instanceof Byte) {
-                    return object;
+                    return (Byte)object;
                 }
 
                 return new Byte(object.toString());
             }
         };
 
-        Converter shortConverter = new Converter() {
+        Converter<Short> toShortConverter = new Converter<Short>() {
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected Short convert(Object object, Class<Short> type) {
                 if (object == null) {
                     return type.isPrimitive() ? Short.valueOf((short) 0) : null;
                 }
 
                 if (object instanceof Short) {
-                    return object;
+                    return (Short)object;
                 }
 
                 return new Short(object.toString());
             }
         };
 
-        Converter charConverter = new Converter() {
+        Converter<Character> toCharConverter = new Converter<Character>() {
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected Character convert(Object object, Class<Character> type) {
                 if (object == null) {
                     return type.isPrimitive() ? Character.valueOf((char) 0) : null;
                 }
 
                 if (object instanceof Character) {
-                    return object;
+                    return (Character)object;
                 }
 
                 String string = object.toString();
@@ -142,102 +154,145 @@ class ConverterFactory {
             }
         };
 
-        Converter doubleConverter = new Converter() {
+        Converter<Double> toDoubleConverter = new Converter<Double>() {
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected Double convert(Object object, Class<Double> type) {
                 if (object == null) {
                     return type.isPrimitive() ? new Double(0.0d) : null;
                 }
 
                 if (object instanceof Double) {
-                    return object;
+                    return (Double)object;
                 }
 
                 return new Double(object.toString());
             }
         };
 
-        Converter floatConverter = new Converter() {
+        Converter<Float> toFloatConverter = new Converter<Float>() {
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected Float convert(Object object, Class<Float> type) {
                 if (object == null) {
                     return type.isPrimitive() ? new Float(0.0f) : null;
                 }
 
                 if (object instanceof Float) {
-                    return object;
+                    return (Float)object;
                 }
 
                 return new Float(object.toString());
             }
         };
 
-        Converter bigDecimalConverter = new Converter() {
+        Converter<BigDecimal> toBigDecimalConverter = new Converter<BigDecimal>()
{
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected BigDecimal convert(Object object, Class<BigDecimal> type) {
                 if (object == null) {
                     return null;
                 }
 
                 if (object instanceof BigDecimal) {
-                    return object;
+                    return (BigDecimal)object;
                 }
 
                 return new BigDecimal(object.toString());
             }
         };
 
-        Converter bigIntegerConverter = new Converter() {
+        Converter<BigInteger> toBigIntegerConverter = new Converter<BigInteger>()
{
 
             @Override
-            Object convert(Object object, Class<?> type) {
+            protected BigInteger convert(Object object, Class<BigInteger> type) {
                 if (object == null) {
                     return null;
                 }
 
                 if (object instanceof BigInteger) {
-                    return object;
+                    return (BigInteger)object;
                 }
 
                 return new BigInteger(object.toString());
             }
         };
 
-        // TODO: byte[] converter...
-
-        converters = new HashMap<String, Converter>();
-
-        converters.put(Boolean.class.getName(), booleanConverter);
-        converters.put("boolean", booleanConverter);
-
-        converters.put(Short.class.getName(), shortConverter);
-        converters.put("short", shortConverter);
-
-        converters.put(Byte.class.getName(), byteConverter);
-        converters.put("byte", byteConverter);
-
-        converters.put(Integer.class.getName(), intConverter);
-        converters.put("int", intConverter);
-
-        converters.put(Double.class.getName(), doubleConverter);
-        converters.put("double", doubleConverter);
-
-        converters.put(Float.class.getName(), floatConverter);
-        converters.put("float", floatConverter);
-
-        converters.put(Character.class.getName(), charConverter);
-        converters.put("char", charConverter);
-
-        converters.put(BigDecimal.class.getName(), bigDecimalConverter);
-        converters.put(BigInteger.class.getName(), bigIntegerConverter);
-        converters.put(Number.class.getName(), bigDecimalConverter);
-        converters.put(String.class.getName(), stringConverter);
+		Converter<Date> toDateConverter = new Converter<Date>() {
+			@Override
+			protected Date convert(Object value, Class<Date> type) {
+				if (value == null) return null;
+				if (value instanceof Date) return (Date) value;
+				if (value instanceof Number) return new Date(((Number)value).longValue());
+				return new Date(value.toString());
+			}
+		};
+		
+		Converter<Timestamp> toTimestampConverter = new Converter<Timestamp>() {
+			@Override
+			protected Timestamp convert(Object value, Class<Timestamp> type) {
+				if (value == null) return null;
+				if (value instanceof Timestamp) return (Timestamp) value;
+				if (value instanceof Number) return new Timestamp(((Number)value).longValue());
+				return new Timestamp(Date.parse(value.toString()));
+			}
+		};
+		
+		// TODO: byte[] converter...
+
+        converters = new HashMap<Class<?>, Converter<?>>();
+
+        _addConverter(Boolean.class, toBooleanConverter);
+        _addConverter(boolean.class, toBooleanConverter);
+
+        _addConverter(Short.class, toShortConverter);
+        _addConverter(short.class, toShortConverter);
+
+        _addConverter(Byte.class, toByteConverter);
+        _addConverter(byte.class, toByteConverter);
+
+        _addConverter(Integer.class, toIntConverter);
+        _addConverter(int.class, toIntConverter);
+
+        _addConverter(Long.class, toLongConverter);
+        _addConverter(long.class, toLongConverter);
+        
+        _addConverter(Double.class, toDoubleConverter);
+        _addConverter(double.class, toDoubleConverter);
+
+        _addConverter(Float.class, toFloatConverter);
+        _addConverter(float.class, toFloatConverter);
+
+        _addConverter(Character.class, toCharConverter);
+        _addConverter(char.class, toCharConverter);
+
+        _addConverter(BigDecimal.class, toBigDecimalConverter);
+        _addConverter(BigInteger.class, toBigIntegerConverter);
+        _addConverter(Number.class, toBigDecimalConverter);
+        _addConverter(String.class, toStringConverter);
+		_addConverter(Date.class, toDateConverter);
+		_addConverter(Timestamp.class, toTimestampConverter);
     }
 
-    Converter getConverter(Class<?> type) {
+    /**
+     * Converters are used by {@link PropertyUtils#setProperty(Object, String, Object)} to
coerce
+     * generic Object values into the specific type expected by the named setter.
+     * 
+     * @param type
+     * 		the Class to convert a value to; the destination type
+     * @param converter
+     * 		a converter used to convert the value from Object to T
+     * @since 3.2
+     */
+    public static <T> void addConverter(Class<? super T> type, Converter<T>
converter) {
+    	factory._addConverter(type, converter);
+    }
+    
+    private <T> void _addConverter(Class<? super T> type, Converter<T>
converter) {
+    	converters.put(type, converter);
+    }
+    
+    <T> Converter<T> getConverter(Class<T> type) {
         if (type == null) {
             throw new IllegalArgumentException("Null type");
         }
@@ -247,7 +302,7 @@ class ConverterFactory {
             return enumConveter;
         }
 
-        Converter c = converters.get(type.getName());
-        return c != null ? c : noopConverter;
+        Converter<T> c = (Converter<T>) converters.get(type);
+        return c != null ? c : (Converter<T>)toAnyConverter;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/EnumConverter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/EnumConverter.java?rev=1523710&r1=1523709&r2=1523710&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/EnumConverter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/EnumConverter.java
Mon Sep 16 15:44:02 2013
@@ -26,11 +26,11 @@ import org.apache.cayenne.util.Util;
 /**
  * @since 1.2
  */
-class EnumConverter extends Converter {
+class EnumConverter<T extends Enum & ExtendedEnumeration> extends Converter<T>
{
 
     @Override
     @SuppressWarnings("unchecked")
-    Object convert(Object object, Class type) {
+    protected T convert(Object object, Class<T> type) {
 
         if (ExtendedEnumeration.class.isAssignableFrom(type)) {
             ExtendedEnumeration[] values;
@@ -45,7 +45,7 @@ class EnumConverter extends Converter {
 
             for (ExtendedEnumeration en : values) {
                 if (Util.nullSafeEquals(en.getDatabaseValue(), object)) {
-                    return en;
+                    return (T) en;
                 }
             }
 
@@ -56,6 +56,6 @@ class EnumConverter extends Converter {
             return null;
         }
 
-        return Enum.valueOf(type, object.toString());
+        return (T) Enum.valueOf(type, object.toString());
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java?rev=1523710&r1=1523709&r2=1523710&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java
Mon Sep 16 15:44:02 2013
@@ -230,10 +230,10 @@ public class PropertyUtils {
             }
 
             // do basic conversions
-
-            value = ConverterFactory.factory
-                    .getConverter(descriptor.getPropertyType())
-                    .convert(value, descriptor.getPropertyType());
+            Converter<?> converter = ConverterFactory.factory.getConverter(descriptor.getPropertyType());
+            value = (converter != null)
+            			? converter.convert(value, (Class)descriptor.getPropertyType()) 
+            			: value;
 
             // set
             writer.invoke(object, value);

Added: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ToAnyConverter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ToAnyConverter.java?rev=1523710&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ToAnyConverter.java
(added)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/reflect/ToAnyConverter.java
Mon Sep 16 15:44:02 2013
@@ -0,0 +1,30 @@
+package org.apache.cayenne.reflect;
+
+import java.lang.reflect.Constructor;
+
+import org.apache.cayenne.CayenneRuntimeException;
+
+/**
+ * Can convert to any class that has a constructor that takes a 
+ * single Object or a single String parameter.
+ */
+public class ToAnyConverter<T> extends Converter<T> {
+	@Override
+	protected T convert(Object value, Class<T> type) {
+		if (value == null) return null;
+		if (type.isAssignableFrom(value.getClass())) return (T) value; // no conversion needed
+		
+        try {
+            Constructor<?> constructor;
+            try {
+            	constructor = type.getConstructor(Object.class);
+            } catch (NoSuchMethodException e) {
+                constructor = type.getConstructor(String.class);
+            	value = value.toString();
+            }
+            return (T) constructor.newInstance(value);
+        } catch (Exception e) {
+            throw new CayenneRuntimeException(e);
+        }
+	}
+}
\ No newline at end of file

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/PropertyUtilsTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/PropertyUtilsTest.java?rev=1523710&r1=1523709&r2=1523710&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/PropertyUtilsTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/PropertyUtilsTest.java
Mon Sep 16 15:44:02 2013
@@ -20,15 +20,21 @@
 package org.apache.cayenne.reflect;
 
 import java.math.BigDecimal;
+import java.sql.Timestamp;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.cayenne.reflect.PropertyUtils;
+import junit.framework.TestCase;
+
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.access.types.MockEnum;
 import org.apache.cayenne.access.types.MockEnumHolder;
 
-import junit.framework.TestCase;
-
 public class PropertyUtilsTest extends TestCase {
 
     public void testCreateAccessor() {
@@ -144,11 +150,90 @@ public class PropertyUtilsTest extends T
         PropertyUtils.setProperty(o1, "integerField", "25");
         assertEquals(new Integer(25), o1.getIntegerField());
 
-        // string to primitive
+        // string to byte primitive
+        PropertyUtils.setProperty(o1, "byteField", "2");
+        assertEquals(2, o1.getByteField());
+        
+        // string to short primitive
+        PropertyUtils.setProperty(o1, "shortField", "3");
+        assertEquals(3, o1.getShortField());
+        
+        // string to int primitive
         PropertyUtils.setProperty(o1, "intField", "28");
         assertEquals(28, o1.getIntField());
-    }
+        
+        // string to long primitive
+        PropertyUtils.setProperty(o1, "longField", "29");
+        assertEquals(29, o1.getLongField());
+        
+        // string to float primitive
+        PropertyUtils.setProperty(o1, "floatField", "4.5");
+        assertEquals(4.5f, o1.getFloatField());
+        
+        // string to double primitive
+        PropertyUtils.setProperty(o1, "doubleField", "5.5");
+        assertEquals(5.5, o1.getDoubleField());
+        
+        // string to boolean
+        PropertyUtils.setProperty(o1, "booleanField", "true");
+        assertTrue(o1.isBooleanField());
+        PropertyUtils.setProperty(o1, "booleanField", "false");
+        assertFalse(o1.isBooleanField());
+        
+        // int to boolean
+        PropertyUtils.setProperty(o1, "booleanField", 1);
+        assertTrue(o1.isBooleanField());
+        PropertyUtils.setProperty(o1, "booleanField", 0);
+        assertFalse(o1.isBooleanField());
+        
+        // long to boolean
+        PropertyUtils.setProperty(o1, "booleanField", 1L);
+        assertTrue(o1.isBooleanField());
+        PropertyUtils.setProperty(o1, "booleanField", 0L);
+        assertFalse(o1.isBooleanField());
+        
+        // long to date
+        PropertyUtils.setProperty(o1, "dateField", 0L);
+        assertEquals(new Date(0L), o1.getDateField());
+        
+        // long to timestamp
+        PropertyUtils.setProperty(o1, "timestampField", 0L);
+        assertEquals(new Timestamp(0L), o1.getTimestampField());
+        
+        // arbitrary string/object to field
+        PropertyUtils.setProperty(o1, "stringBuilderField", "abc");
+        assertEquals(new StringBuilder("abc").toString(), o1.getStringBuilderField().toString());
+    }
+
+    public void testSetConvertedWithCustomConverter() {
+    	ConverterFactory.addConverter(Date.class, new Converter<Date>() {
+			@Override
+			protected Date convert(Object value, Class<Date> type) {
+				if (value == null) return null;
+				if (value instanceof Date) {
+					return (Date)value;
+				}
+				if (value instanceof String) {
+					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+					try {
+						return format.parse((String) value);
+					} catch (ParseException e) {
+						throw new CayenneRuntimeException("Unable to convert '" + value + "' to a Date", e);
+					}
+				}
+				throw new CayenneRuntimeException("Unable to convert '" + value + "' to a Date");
+			}
+    	});
+    	
+        TestJavaBean o1 = new TestJavaBean();
 
+        // String to date
+        PropertyUtils.setProperty(o1, "dateField", "2013-08-01");
+        
+        Calendar cal = new GregorianCalendar(2013, 7, 1, 0, 0, 0);
+        assertEquals(cal.getTime(), o1.getDateField());
+    }
+    
     public void testSetNull() {
         TestJavaBean o1 = new TestJavaBean();
 
@@ -156,9 +241,33 @@ public class PropertyUtilsTest extends T
         PropertyUtils.setProperty(o1, "stringField", null);
         assertNull(o1.getStringField());
 
+        o1.setBooleanField(true);
+        PropertyUtils.setProperty(o1, "booleanField", null);
+        assertEquals(false, o1.isBooleanField());
+        
+        o1.setByteField((byte) 2);
+        PropertyUtils.setProperty(o1, "byteField", null);
+        assertEquals((byte)0, o1.getByteField());
+        
+        o1.setShortField((short) 3);
+        PropertyUtils.setProperty(o1, "shortField", null);
+        assertEquals((short)0, o1.getShortField());
+        
         o1.setIntField(99);
         PropertyUtils.setProperty(o1, "intField", null);
         assertEquals(0, o1.getIntField());
+        
+        o1.setLongField(98);
+        PropertyUtils.setProperty(o1, "longField", null);
+        assertEquals(0L, o1.getLongField());
+        
+        o1.setFloatField(4.5f);
+        PropertyUtils.setProperty(o1, "floatField", null);
+        assertEquals(0.0f, o1.getFloatField());
+        
+        o1.setDoubleField(5.5f);
+        PropertyUtils.setProperty(o1, "doubleField", null);
+        assertEquals(0.0, o1.getDoubleField());
     }
 
     public void testSetConvertedEnum() {

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/TestJavaBean.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/TestJavaBean.java?rev=1523710&r1=1523709&r2=1523710&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/TestJavaBean.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/test/java/org/apache/cayenne/reflect/TestJavaBean.java
Mon Sep 16 15:44:02 2013
@@ -19,17 +19,28 @@
 
 package org.apache.cayenne.reflect;
 
+import java.sql.Timestamp;
+import java.util.Date;
+
 public class TestJavaBean extends Object {
 
     protected String stringField;
+    protected byte byteField;
+    protected short shortField;
     protected int intField;
+    protected long longField;
+    protected float floatField;
+    protected double doubleField;
     protected boolean booleanField;
     protected Integer integerField;
     protected Number numberField;
     protected byte[] byteArrayField;
     protected Object objectField;
     protected TestJavaBean related;
-
+    protected Date dateField;
+    protected Timestamp timestampField;
+    protected StringBuilder stringBuilderField;
+    
     public byte[] getByteArrayField() {
         return byteArrayField;
     }
@@ -97,4 +108,68 @@ public class TestJavaBean extends Object
     public void setRelated(TestJavaBean related) {
         this.related = related;
     }
+
+	public Date getDateField() {
+		return dateField;
+	}
+
+	public void setDateField(Date dateField) {
+		this.dateField = dateField;
+	}
+
+	public StringBuilder getStringBuilderField() {
+		return stringBuilderField;
+	}
+
+	public void setStringBuilderField(StringBuilder stringBuilderField) {
+		this.stringBuilderField = stringBuilderField;
+	}
+
+	public Timestamp getTimestampField() {
+		return timestampField;
+	}
+
+	public void setTimestampField(Timestamp timestampField) {
+		this.timestampField = timestampField;
+	}
+
+	public long getLongField() {
+		return longField;
+	}
+
+	public void setLongField(long longField) {
+		this.longField = longField;
+	}
+
+	public byte getByteField() {
+		return byteField;
+	}
+
+	public void setByteField(byte byteField) {
+		this.byteField = byteField;
+	}
+
+	public short getShortField() {
+		return shortField;
+	}
+
+	public void setShortField(short shortField) {
+		this.shortField = shortField;
+	}
+
+	public float getFloatField() {
+		return floatField;
+	}
+
+	public void setFloatField(float floatField) {
+		this.floatField = floatField;
+	}
+
+	public double getDoubleField() {
+		return doubleField;
+	}
+
+	public void setDoubleField(double doubleField) {
+		this.doubleField = doubleField;
+	}
 }



Mime
View raw message