You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2009/09/18 01:04:54 UTC

svn commit: r816404 - 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

Author: bluk
Date: Thu Sep 17 23:04:53 2009
New Revision: 816404

URL: http://svn.apache.org/viewvc?rev=816404&view=rev
Log:
Give constructors precedence over valueOf

See [WINK-201]

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=816404&r1=816403&r2=816404&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 Thu Sep 17 23:04:53 2009
@@ -28,6 +28,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
@@ -125,6 +126,13 @@
             return null;
         }
 
+        try {
+            Constructor<?> constructor = classType.getConstructor(String.class);
+            return new ConstructorConvertor(constructor);
+        } catch (SecurityException e) {
+        } catch (NoSuchMethodException e) {
+        }
+
         // see JAX-RS 1.1 C006:
         // http://jcp.org/aboutJava/communityprocess/maintenance/jsr311/311ChangeLog.html
         // precendence for enums is fromString, then valueOf
@@ -141,13 +149,6 @@
             }
         }
 
-        try {
-            Constructor<?> constructor = classType.getConstructor(String.class);
-            return new ConstructorConvertor(constructor);
-        } catch (SecurityException e) {
-        } catch (NoSuchMethodException e) {
-        }
-
         throw new IllegalArgumentException("type '" + classType
             + "' is not a supported resource method parameter");
     }
@@ -172,6 +173,13 @@
         }
 
         try {
+            Constructor<?> constructor = classType.getConstructor(String.class);
+            return new ConstructorConvertor(constructor);
+        } catch (SecurityException e) {
+        } catch (NoSuchMethodException e) {
+        }
+
+        try {
             Method valueOf = classType.getDeclaredMethod("valueOf", String.class);
             return new ValueOfConvertor(valueOf);
         } catch (SecurityException e) {
@@ -187,13 +195,6 @@
             }
         }
 
-        try {
-            Constructor<?> constructor = classType.getConstructor(String.class);
-            return new ConstructorConvertor(constructor);
-        } catch (SecurityException e) {
-        } catch (NoSuchMethodException e) {
-        }
-
         throw new IllegalArgumentException("type '" + classType
             + "' is not a supported resource method parameter");
     }
@@ -243,7 +244,9 @@
             if (values == null || values.size() == 0) {
                 return convert((String)null);
             }
-            return convert(values.get(0));
+            List<String> valuesSorted = new ArrayList<String>(values);
+            Collections.sort(valuesSorted);
+            return convert(valuesSorted.get(0));
         }
     }
 

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=816404&r1=816403&r2=816404&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 Thu Sep 17 23:04:53 2009
@@ -19,27 +19,30 @@
  *******************************************************************************/
 package org.apache.wink.common.internal.registry;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import junit.framework.TestCase;
 
 public class ValueConvertorTest extends TestCase {
 
     /**
-     * custom type with both valueOf and fromString methods
+     * custom type with constructor, valueOf, and fromString methods
      */
-    static class CustomType {
+    static class CustomTypeConstructor {
 
         private String _value = "";
 
-        public CustomType(String value) {
+        public CustomTypeConstructor(String value) {
             _value = value;
         }
 
-        static CustomType valueOf(String value) {
-            return new CustomType(value + "_valueOf");
+        static CustomTypeConstructor valueOf(String value) {
+            return new CustomTypeConstructor(value + "_valueOf");
         }
 
-        static CustomType fromString(String value) {
-            return new CustomType(value + "_fromString");
+        static CustomTypeConstructor fromString(String value) {
+            return new CustomTypeConstructor(value + "_fromString");
         }
 
         public String toString() {
@@ -48,17 +51,39 @@
     }
 
     /**
-     * custom type with fromString method only
+     * custom type with both valueOf and fromString methods and no constructor
      */
-    static class CustomTypeNoValueOf {
+    static class CustomTypeValueOf {
+
         private String _value = "";
 
-        public CustomTypeNoValueOf(String value) {
+        private CustomTypeValueOf(String value) {
             _value = value;
         }
 
-        static CustomTypeNoValueOf fromString(String value) {
-            return new CustomTypeNoValueOf(value + "_fromString");
+        static CustomTypeValueOf valueOf(String value) {
+            return new CustomTypeValueOf(value + "_valueOf");
+        }
+
+        static CustomTypeValueOf fromString(String value) {
+            return new CustomTypeValueOf(value + "_fromString");
+        }
+
+        public String toString() {
+            return _value;
+        }
+    }
+
+    /**
+     * custom type with fromString method only
+     */
+    static class CustomTypeFromString {
+        private String _value = "";
+
+        static CustomTypeFromString fromString(String value) {
+            CustomTypeFromString ct = new CustomTypeFromString();
+            ct._value = value + "_fromString";
+            return ct;
         }
 
         public String toString() {
@@ -84,18 +109,28 @@
         }
     }
 
+    // make sure constructor is favored over "valueOf" and "fromString"
+    public void testConvertorPrecedenceConstructor() throws Exception {
+        ValueConvertor constructorConvertor =
+            ValueConvertor.createConcreteValueConvertor(CustomTypeConstructor.class,
+                                                        CustomTypeConstructor.class);
+        assertEquals("VALUE", constructorConvertor.convert("VALUE").toString());
+    }
+
     // make sure "valueOf" is favored over "fromString"
-    public void testConvertorPrecedence() throws Exception {
+    public void testConvertorPrecedenceNoConstructor() throws Exception {
         ValueConvertor valueOfConvertor =
-            ValueConvertor.createConcreteValueConvertor(CustomType.class, CustomType.class);
+            ValueConvertor.createConcreteValueConvertor(CustomTypeValueOf.class,
+                                                        CustomTypeValueOf.class);
         assertEquals("VALUE_valueOf", valueOfConvertor.convert("VALUE").toString());
     }
 
-    // make sure fallback to "fromString" if no "valueOf" method is found
-    public void testConvertorPrecedenceNoValueOf() throws Exception {
+    // make sure fallback to "fromString" if no "valueOf" method nor constructor
+    // is found
+    public void testConvertorPrecedenceNoConstructorNoValueOf() throws Exception {
         ValueConvertor fromStringConvertor =
-            ValueConvertor.createConcreteValueConvertor(CustomTypeNoValueOf.class,
-                                                        CustomTypeNoValueOf.class);
+            ValueConvertor.createConcreteValueConvertor(CustomTypeFromString.class,
+                                                        CustomTypeFromString.class);
         assertEquals("VALUE_fromString", fromStringConvertor.convert("VALUE").toString());
     }
 
@@ -114,4 +149,16 @@
         assertEquals(MyEnumWithFromString.SUNDAY_fromString, fromStringConvertor.convert("SUNDAY"));
     }
 
+    // make sure that a single value conversion will sort multiple values
+    // correctly
+    public void testMultipleValuesSortedReturned() throws Exception {
+        ValueConvertor constructorConvertor =
+            ValueConvertor.createValueConvertor(CustomTypeConstructor.class,
+                                                CustomTypeConstructor.class);
+        List<String> values = new ArrayList<String>();
+        values.add("z");
+        values.add("a");
+        values.add("aa");
+        assertEquals("a", constructorConvertor.convert(values).toString());
+    }
 }