wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r820866 - in /incubator/wink/trunk/wink-common/src: main/java/org/apache/wink/common/internal/registry/ValueConvertor.java test/java/org/apache/wink/common/internal/registry/ValueConvertorTest.java
Date Fri, 02 Oct 2009 02:09:23 GMT
Author: bluk
Date: Fri Oct  2 02:09:23 2009
New Revision: 820866

URL: http://svn.apache.org/viewvc?rev=820866&view=rev
Log:
Enforce value conversion

Thanks Mike Rheinheimer for the patch.

See [WINK-198]

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ValueConvertor.java
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ValueConvertorTest.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ValueConvertor.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ValueConvertor.java?rev=820866&r1=820865&r2=820866&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ValueConvertor.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ValueConvertor.java
Fri Oct  2 02:09:23 2009
@@ -290,7 +290,17 @@
                 return null;
             }
             try {
-                return method.invoke(null, value);
+                Object objToReturn = method.invoke(null, value);
+                // can't use instanceof here?
+                if (!objToReturn.getClass().equals((method.getDeclaringClass()))) {
+                    // enforce E009 from http://jcp.org/aboutJava/communityprocess/maintenance/jsr311/311ChangeLog.html
+                    // note that we don't care what return object type the method declares,
only what it actually returns
+                    throw createConversionException(value, method.getDeclaringClass(),
+                            new Exception("Value returned from method " + method.toString()
+ " must be "
+                                    + "of type " + method.getDeclaringClass() + ".  "
+                                    + "Returned object was type " + objToReturn.getClass()));
+                }
+                return objToReturn;
             } catch (IllegalArgumentException e) {
                 throw createConversionException(value, method.getDeclaringClass(), e);
             } catch (IllegalAccessException e) {

Modified: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ValueConvertorTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ValueConvertorTest.java?rev=820866&r1=820865&r2=820866&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ValueConvertorTest.java
(original)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ValueConvertorTest.java
Fri Oct  2 02:09:23 2009
@@ -72,6 +72,50 @@
     }
 
     /**
+     * custom type with valueOf that returns incorrect object type and has no
+     * constructor
+     */
+    static class CustomTypeValueOfWrong {
+
+        private String _value = "";
+
+        private CustomTypeValueOfWrong(String value) {
+            _value = value;
+        }
+
+        static Object valueOf(String value) {
+            // intentionally returning incorrect object type
+            return new String(value + "_valueOf");
+        }
+
+        public String toString() {
+            return _value;
+        }
+    }
+
+    /**
+     * custom type with fromString that returns incorrect object type and has no
+     * constructor
+     */
+    static class CustomTypeFromStringWrong {
+
+        private String _value = "";
+
+        private CustomTypeFromStringWrong(String value) {
+            _value = value;
+        }
+
+        static Object fromString(String value) {
+            // intentionally returning incorrect object type
+            return new String(value + "_valueOf");
+        }
+
+        public String toString() {
+            return _value;
+        }
+    }
+
+    /**
      * custom type with fromString method only
      */
     static class CustomTypeFromString {
@@ -146,4 +190,25 @@
         assertEquals(MyEnumWithFromString.SUNDAY_fromString, fromStringConvertor.convert("SUNDAY"));
     }
 
+    // make sure wrong Object type returned from valueOf and fromString is
+    // handled and reported
+    public void testConvertorWrongObjectType() throws Exception {
+        try {
+            ValueConvertor valueOfConvertor =
+                ValueConvertor.createValueConvertor(CustomTypeValueOfWrong.class,
+                                                    CustomTypeValueOfWrong.class);
+            valueOfConvertor.convert("VALUE");
+            fail("Should have got an exception.");
+        } catch (Exception e) {
+        }
+
+        try {
+            ValueConvertor valueOfConvertor =
+                ValueConvertor.createValueConvertor(CustomTypeFromStringWrong.class,
+                                                    CustomTypeFromStringWrong.class);
+            valueOfConvertor.convert("VALUE");
+            fail("Should have got an exception.");
+        } catch (Exception e) {
+        }
+    }
 }



Mime
View raw message