svn commit: r1307551 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: ObjectType.java test/ObjectTypeTests.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1307551 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: ObjectType.java test/ObjectTypeTests.java

adrianc
Author: adrianc
Date: Fri Mar 30 17:48:32 2012
New Revision: 1307551

URL: http://svn.apache.org/viewvc?rev=1307551&view=rev
Log:
Small code cleanup in ObjectType.simpleTypeConvert, added support for null target type (returns original object). Updated unit tests.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java?rev=1307551&r1=1307550&r2=1307551&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java Fri Mar 30 17:48:32 2012
@@ -471,28 +471,12 @@ public class ObjectType {
     @SourceMonitored
     @SuppressWarnings("unchecked")
     public static Object simpleTypeConvert(Object obj, String type, String format, TimeZone timeZone, Locale locale, boolean noTypeFail) throws GeneralException {
-        if (obj == null) {
-            return null;
-        }
-
-        int genericsStart = type.indexOf("<");
-        if (genericsStart != -1) {
-            type = type.substring(0, genericsStart);
+        if (obj == null || type == null || "Object".equals(type) || "java.lang.Object".equals(type)) {
+            return obj;
         }
-
         if ("PlainString".equals(type)) {
             return obj.toString();
         }
-        Class<?> sourceClass = obj.getClass();
-        if (sourceClass.getName().equals(type)) {
-            return obj;
-        }
-        if ("Object".equals(type) || "java.lang.Object".equals(type)) {
-            return obj;
-        }
-        if (obj instanceof String && UtilValidate.isEmpty(obj)) {
-            return null;
-        }
         if (obj instanceof Node) {
             Node node = (Node) obj;
             String nodeValue =  node.getTextContent();
@@ -502,13 +486,24 @@ public class ObjectType {
                 return simpleTypeConvert(nodeValue, type, format, timeZone, locale, noTypeFail);
             }
         }
+        int genericsStart = type.indexOf("<");
+        if (genericsStart != -1) {
+            type = type.substring(0, genericsStart);
+        }
+        Class<?> sourceClass = obj.getClass();
         Class<?> targetClass = null;
         try {
             targetClass = loadClass(type);
         } catch (ClassNotFoundException e) {
             throw new GeneralException("Conversion from " + sourceClass.getName() + " to " + type + " not currently supported", e);
         }
-        Converter<Object,Object> converter = null;
+        if (sourceClass.equals(targetClass)) {
+            return obj;
+        }
+        if (obj instanceof String && ((String) obj).length() == 0) {
+            return null;
+        }
+        Converter<Object, Object> converter = null;
         try {
             converter = (Converter<Object, Object>) Converters.getConverter(sourceClass, targetClass);
         } catch (ClassNotFoundException e) {}

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java?rev=1307551&r1=1307550&r2=1307551&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java Fri Mar 30 17:48:32 2012
@@ -94,7 +94,8 @@ public class ObjectTypeTests extends Gen
 
     public static void simpleTypeConvertTest(String label, Object toConvert, String type, Object wanted) throws GeneralException {
         basicTest(label, toConvert);
-        assertEquals(label + ":null", (Object) null, simpleTypeConvert(null, type, null, null, null, true));
+        assertEquals(label + ":null target type", toConvert, simpleTypeConvert(toConvert, null, null, null, null, true));
+        assertEquals(label + ":null source object", (Object) null, simpleTypeConvert(null, type, null, null, null, true));
         assertEquals(label, wanted, simpleTypeConvert(toConvert, type, null, null, null, true));
         if (toConvert instanceof String) {
             String str = (String) toConvert;
@@ -208,7 +209,6 @@ public class ObjectTypeTests extends Gen
         simpleTypeConvertTest("String->String", "one", "String", "one");
         simpleTypeConvertTest("String->String", "one", "java.lang.String", "one");
         simpleTypeConvertTestSingleMulti("empty-String->anything", "", new String[] {"List", "Map"}, null);
-        simpleTypeConvertTestSingleMulti("empty-String->bad-class", "", new String[] {"no-class"}, null);
         simpleTypeConvertTestError("String->error", "one", new String[] {});
         simpleTypeConvertTestMultiMulti("String->Boolean(true)", new String[] {"true", " true ", " TrUe"}, new String[] {"Boolean", "java.lang.Boolean"}, Boolean.TRUE);
         simpleTypeConvertTestMultiMulti("String->Boolean(false)", new String[] {"false", " false ", " FaLsE"}, new String[] {"Boolean", "java.lang.Boolean"}, Boolean.FALSE);