tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jkemn...@apache.org
Subject tapestry-5 git commit: TAP-2496: use TypeCoercer to convert enums to client-side values
Date Wed, 02 Sep 2015 08:14:09 GMT
Repository: tapestry-5
Updated Branches:
  refs/heads/master 8d155f91f -> b69ac22f3


TAP-2496: use TypeCoercer to convert enums to client-side values


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/b69ac22f
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/b69ac22f
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/b69ac22f

Branch: refs/heads/master
Commit: b69ac22f31c324129d72b15bbd542b5a1f62c2bc
Parents: 8d155f9
Author: Jochen Kemnade <jochen.kemnade@eddyson.de>
Authored: Wed Sep 2 10:09:17 2015 +0200
Committer: Jochen Kemnade <jochen.kemnade@eddyson.de>
Committed: Wed Sep 2 10:09:17 2015 +0200

----------------------------------------------------------------------
 .../apache/tapestry5/util/EnumValueEncoder.java |  2 +-
 .../tapestry5/util/EnumValueEncoderTest.java    | 44 ++++++++++++++++++--
 2 files changed, 42 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/b69ac22f/tapestry-core/src/main/java/org/apache/tapestry5/util/EnumValueEncoder.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/util/EnumValueEncoder.java b/tapestry-core/src/main/java/org/apache/tapestry5/util/EnumValueEncoder.java
index cc211c7..e6e06bb 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/util/EnumValueEncoder.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/util/EnumValueEncoder.java
@@ -40,7 +40,7 @@ public class EnumValueEncoder<E extends Enum<E>> implements
ValueEncoder<E>
         if (value == null)
             return null;
 
-        return value.name();
+        return typeCoercer.coerce(value, String.class);
     }
 
     public E toValue(String clientValue)

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/b69ac22f/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java
b/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java
index e713316..1100fc3 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java
@@ -17,10 +17,15 @@ package org.apache.tapestry5.util;
 import java.util.List;
 
 import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import org.apache.tapestry5.ioc.internal.services.TypeCoercerImpl;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry5.ioc.services.Coercion;
+import org.apache.tapestry5.ioc.services.CoercionTuple;
 import org.apache.tapestry5.ioc.services.TypeCoercer;
 import org.apache.tapestry5.ioc.util.UnknownValueException;
 import org.testng.annotations.Test;
 
+
 public class EnumValueEncoderTest extends InternalBaseTestCase
 {
     private enum Stooge
@@ -32,9 +37,10 @@ public class EnumValueEncoderTest extends InternalBaseTestCase
     // TAP5-1331
     public void valid_values_can_be_retrieved_from_exception()
     {
-        TypeCoercer typeCoercer = getService(TypeCoercer.class); 
+        TypeCoercer typeCoercer = getService(TypeCoercer.class);
         EnumValueEncoder<Stooge> encoder = new EnumValueEncoder<Stooge>(typeCoercer,
Stooge.class);
-        try{
+        try
+        {
           encoder.toValue("Foo");
           fail();
         } catch (RuntimeException e){
@@ -45,5 +51,37 @@ public class EnumValueEncoderTest extends InternalBaseTestCase
           assertTrue(availableValues.contains("LARRY"));
           assertTrue(availableValues.contains("CURLY_JOE"));
         }
-    }   
+    }
+
+    @Test
+    // TAP5-2496
+    public void roundtrip_with_custom_coercer()
+    {
+
+        CoercionTuple<Stooge, String> stoogeToString = CoercionTuple.create(Stooge.class,
String.class, new Coercion<Stooge, String>(){
+            @Override
+            public String coerce(Stooge input) {
+                return String.valueOf(input.ordinal());
+            }
+        });
+
+        CoercionTuple<String, Stooge> stringToStooge = CoercionTuple.create(String.class,
Stooge.class, new Coercion<String, Stooge>(){
+
+            @Override
+            public Stooge coerce(String input) {
+                return Stooge.values()[Integer.parseInt(input)];
+            }
+
+        });
+
+        TypeCoercer typeCoercer =  new TypeCoercerImpl(CollectionFactory.newList(stoogeToString,
stringToStooge));
+
+
+        EnumValueEncoder<Stooge> encoder = new EnumValueEncoder<Stooge>(typeCoercer,
Stooge.class);
+        Stooge serverValue = Stooge.LARRY;
+        String clientValue = encoder.toClient(serverValue);
+        Stooge convertedBack = encoder.toValue(clientValue);
+        assertEquals(convertedBack, serverValue);
+
+    }
 }


Mime
View raw message