You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2010/02/10 23:33:07 UTC

svn commit: r908691 - in /ofbiz/trunk/framework/base: build.xml src/org/ofbiz/base/conversion/Converters.java src/org/ofbiz/base/conversion/test/ src/org/ofbiz/base/conversion/test/MiscTests.java testdef/basetests.xml

Author: doogie
Date: Wed Feb 10 22:32:51 2010
New Revision: 908691

URL: http://svn.apache.org/viewvc?rev=908691&view=rev
Log:
Correct pass thru conversion, to return the correct source/target types.

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/
    ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java
Modified:
    ofbiz/trunk/framework/base/build.xml
    ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java
    ofbiz/trunk/framework/base/testdef/basetests.xml

Modified: ofbiz/trunk/framework/base/build.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/build.xml?rev=908691&r1=908690&r2=908691&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/build.xml (original)
+++ ofbiz/trunk/framework/base/build.xml Wed Feb 10 22:32:51 2010
@@ -38,6 +38,7 @@
     </path>
 
     <filelist id="test.classes" dir="${src.dir}">
+        <file name="org/ofbiz/base/conversion/test/MiscTests.java"/>
         <file name="org/ofbiz/base/test/BaseUnitTests.java"/>
         <file name="org/ofbiz/base/util/collections/test/GenericMapTest.java"/>
         <file name="org/ofbiz/base/concurrent/test/SyncTTLObjectTest.java"/>

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java?rev=908691&r1=908690&r2=908691&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java Wed Feb 10 22:32:51 2010
@@ -94,8 +94,9 @@
                     }
                     // Null converter must be checked last
                     if (nullConverter.canConvert(sourceClass, targetClass)) {
-                        converterMap.put(key, nullConverter);
-                        return UtilGenerics.cast(nullConverter);
+                        Converter passThruConverter = new PassThruConverter<S>(sourceClass);
+                        converterMap.put(key, passThruConverter);
+                        return UtilGenerics.cast(passThruConverter);
                     }
                     noConversions.add(key);
                     Debug.logWarning("*** No converter found, converting from " +
@@ -175,4 +176,33 @@
             return Object.class;
         }
     }
+
+    /** Pass thru converter used when the source and target java object
+     * types are the same. The <code>convert</code> method returns the
+     * source object.
+     *
+     */
+    protected static class PassThruConverter<T> implements Converter<T, T> {
+        private final Class<T> clz;
+
+        public PassThruConverter(Class<T> clz) {
+            this.clz = clz;
+        }
+
+        public boolean canConvert(Class<?> sourceClass, Class<?> targetClass) {
+            return sourceClass == clz && targetClass == clz;
+        }
+
+        public T convert(T obj) throws ConversionException {
+            return obj;
+        }
+
+        public Class<?> getSourceClass() {
+            return clz;
+        }
+
+        public Class<?> getTargetClass() {
+            return clz;
+        }
+    }
 }

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java?rev=908691&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java Wed Feb 10 22:32:51 2010
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * 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.ofbiz.base.conversion.test;
+
+import java.math.BigDecimal;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+
+import javolution.util.FastList;
+import javolution.util.FastMap;
+
+import org.ofbiz.base.concurrent.TTLObject;
+import org.ofbiz.base.conversion.Converter;
+import org.ofbiz.base.conversion.Converters;
+import org.ofbiz.base.test.GenericTestCaseBase;
+import org.ofbiz.base.util.UtilMisc;
+
+public class MiscTests extends GenericTestCaseBase {
+
+    public MiscTests(String name) {
+        super(name);
+    }
+
+    public void testPassthru() throws Exception {
+        String string = "ofbiz";
+        BigDecimal bigDecimal = new BigDecimal("1.234");
+        URL url = new URL("http://ofbiz.apache.org");
+        List<String> baseList = UtilMisc.toList("a", "1", "b", "2", "c", "3");
+        ArrayList<String> arrayList = new ArrayList<String>();
+        arrayList.addAll(baseList);
+        List<String> fastList = FastList.newInstance();
+        fastList.addAll(baseList);
+        Map<String, String> baseMap = UtilMisc.toMap("a", "1", "b", "2", "c", "3");
+        HashMap<String, String> hashMap = new HashMap<String, String>();
+        hashMap.putAll(baseMap);
+        Map<String, String> fastMap = FastMap.newInstance();
+        fastMap.putAll(baseMap);
+        Object[] testObjects = new Object[] {
+            string,
+            bigDecimal,
+            url,
+            arrayList,
+            fastList,
+            hashMap,
+            fastMap,
+        };
+        for (Object testObject: testObjects) {
+            Converter converter = Converters.getConverter(testObject.getClass(), testObject.getClass());
+            Object result = converter.convert(testObject);
+            assertEquals("pass thru convert", testObject, result);
+            assertTrue("pass thru exact equals", testObject == result);
+            assertTrue("pass thru can convert", converter.canConvert(testObject.getClass(), testObject.getClass()));
+            assertFalse("pass thru can't convert to object", converter.canConvert(testObject.getClass(), Object.class));
+            assertFalse("pass thru can't convert from object", converter.canConvert(Object.class, testObject.getClass()));
+            assertEquals("pass thru source class", testObject.getClass(), converter.getSourceClass());
+            assertEquals("pass thru target class", result.getClass(), converter.getTargetClass());
+        }
+    }
+}

Modified: ofbiz/trunk/framework/base/testdef/basetests.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/testdef/basetests.xml?rev=908691&r1=908690&r2=908691&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/testdef/basetests.xml (original)
+++ ofbiz/trunk/framework/base/testdef/basetests.xml Wed Feb 10 22:32:51 2010
@@ -21,6 +21,7 @@
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/test-suite.xsd">
     <test-case case-name="basetests">
+        <junit-test-suite class-name="org.ofbiz.base.conversion.test.MiscTests.java"/>
         <junit-test-suite class-name="org.ofbiz.base.test.BaseUnitTests"/>
     </test-case>
 </test-suite>