You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2010/01/02 00:15:09 UTC

svn commit: r895090 - in /tapestry/tapestry5/trunk: ./ tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/ tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/

Author: hlship
Date: Fri Jan  1 23:15:08 2010
New Revision: 895090

URL: http://svn.apache.org/viewvc?rev=895090&view=rev
Log:
TAP5-944: When a ValueEncoder is unable to convert an id to a entity, it should wrap the underlying type coercion exception to describe the input and expected output type

Modified:
    tapestry/tapestry5/trunk/.gitignore
    tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java
    tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java

Modified: tapestry/tapestry5/trunk/.gitignore
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/.gitignore?rev=895090&r1=895089&r2=895090&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/.gitignore (original)
+++ tapestry/tapestry5/trunk/.gitignore Fri Jan  1 23:15:08 2010
@@ -7,3 +7,4 @@
 *.iws
 target
 test-output
+temp-testng*.xml
\ No newline at end of file

Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java?rev=895090&r1=895089&r2=895090&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java Fri Jan  1 23:15:08 2010
@@ -41,8 +41,8 @@
 
     private final Logger logger;
 
-    public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persistentClass, Session session,
-                                       PropertyAccess propertyAccess, TypeCoercer typeCoercer, Logger logger)
+    public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persistentClass,
+            Session session, PropertyAccess propertyAccess, TypeCoercer typeCoercer, Logger logger)
     {
         this.entityClass = entityClass;
         this.session = session;
@@ -53,20 +53,23 @@
 
         idPropertyName = property.getName();
 
-        propertyAdapter = propertyAccess.getAdapter(this.entityClass).getPropertyAdapter(idPropertyName);
+        propertyAdapter = propertyAccess.getAdapter(this.entityClass).getPropertyAdapter(
+                idPropertyName);
     }
 
-
     public String toClient(E value)
     {
-        if (value == null) return null;
+        if (value == null)
+            return null;
 
         Object id = propertyAdapter.get(value);
 
         if (id == null)
-            throw new IllegalStateException(String.format(
-                    "Entity %s has an %s property of null; this probably means that it has not been persisted yet.",
-                    value, idPropertyName));
+            throw new IllegalStateException(
+                    String
+                            .format(
+                                    "Entity %s has an %s property of null; this probably means that it has not been persisted yet.",
+                                    value, idPropertyName));
 
         return typeCoercer.coerce(id, String.class);
     }
@@ -74,9 +77,23 @@
     @SuppressWarnings("unchecked")
     public E toValue(String clientValue)
     {
-        if (InternalUtils.isBlank(clientValue)) return null;
+        if (InternalUtils.isBlank(clientValue))
+            return null;
+
+        Object id = null;
+
+        try
+        {
 
-        Object id = typeCoercer.coerce(clientValue, propertyAdapter.getType());
+            id = typeCoercer.coerce(clientValue, propertyAdapter.getType());
+        }
+        catch (Exception ex)
+        {
+            throw new RuntimeException(String.format(
+                    "Exception converting '%s' to instance of %s (id type for entity %s): %s",
+                    clientValue, propertyAdapter.getType().getName(), entityClass.getName(),
+                    InternalUtils.toMessage(ex)), ex);
+        }
 
         Serializable ser = Defense.cast(id, Serializable.class, "id");
 
@@ -84,8 +101,10 @@
 
         if (result == null)
         {
-            // We don't identify the entity type in the message because the logger is based on the entity type.
-            logger.error(String.format("Unable to convert client value '%s' into an entity instance.", clientValue));
+            // We don't identify the entity type in the message because the logger is based on the
+            // entity type.
+            logger.error(String.format(
+                    "Unable to convert client value '%s' into an entity instance.", clientValue));
         }
 
         return result;

Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java?rev=895090&r1=895089&r2=895090&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java Fri Jan  1 23:15:08 2010
@@ -68,7 +68,6 @@
         HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
                 SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
 
-
         try
         {
             encoder.toClient(entity);
@@ -76,8 +75,9 @@
         }
         catch (IllegalStateException ex)
         {
-            assertMessageContains(ex, "Entity org.apache.tapestry5.internal.hibernate.SampleEntity",
-                                  "has an id property of null");
+            assertMessageContains(ex,
+                    "Entity org.apache.tapestry5.internal.hibernate.SampleEntity",
+                    "has an id property of null");
         }
 
         verify();
@@ -104,11 +104,42 @@
         HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
                 SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
 
-
         assertNull(encoder.toValue("12345"));
 
         verify();
+    }
+
+    @Test
+    public void to_value_bad_type_coercion()
+    {
+        Session session = mockSession();
+        Logger logger = mockLogger();
+
+        replay();
 
+        RootClass persistentClass = new RootClass();
+        Property idProperty = new Property();
+        idProperty.setName("id");
+        persistentClass.setIdentifierProperty(idProperty);
+
+        HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
+                SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
+
+        try
+        {
+            encoder.toValue("xyz");
+            unreachable();
+        }
+        catch (RuntimeException ex)
+        {
+            assertMessageContains(
+                    ex,
+                    "Exception converting 'xyz' to instance of java.lang.Long (id type for entity org.apache.tapestry5.internal.hibernate.SampleEntity)");
+        }
+
+        assertNull(encoder.toValue(""));
+
+        verify();
     }
 
     protected final Session mockSession()



Re: svn commit: r895090 - in /tapestry/tapestry5/trunk: ./ tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/ tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/

Posted by Howard Lewis Ship <hl...@gmail.com>.
Oops, forgot to update some copyrights!

On Fri, Jan 1, 2010 at 3:15 PM,  <hl...@apache.org> wrote:
> Author: hlship
> Date: Fri Jan  1 23:15:08 2010
> New Revision: 895090
>
> URL: http://svn.apache.org/viewvc?rev=895090&view=rev
> Log:
> TAP5-944: When a ValueEncoder is unable to convert an id to a entity, it should wrap the underlying type coercion exception to describe the input and expected output type
>
> Modified:
>    tapestry/tapestry5/trunk/.gitignore
>    tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java
>    tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java
>
> Modified: tapestry/tapestry5/trunk/.gitignore
> URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/.gitignore?rev=895090&r1=895089&r2=895090&view=diff
> ==============================================================================
> --- tapestry/tapestry5/trunk/.gitignore (original)
> +++ tapestry/tapestry5/trunk/.gitignore Fri Jan  1 23:15:08 2010
> @@ -7,3 +7,4 @@
>  *.iws
>  target
>  test-output
> +temp-testng*.xml
> \ No newline at end of file
>
> Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java
> URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java?rev=895090&r1=895089&r2=895090&view=diff
> ==============================================================================
> --- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java (original)
> +++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java Fri Jan  1 23:15:08 2010
> @@ -41,8 +41,8 @@
>
>     private final Logger logger;
>
> -    public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persistentClass, Session session,
> -                                       PropertyAccess propertyAccess, TypeCoercer typeCoercer, Logger logger)
> +    public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persistentClass,
> +            Session session, PropertyAccess propertyAccess, TypeCoercer typeCoercer, Logger logger)
>     {
>         this.entityClass = entityClass;
>         this.session = session;
> @@ -53,20 +53,23 @@
>
>         idPropertyName = property.getName();
>
> -        propertyAdapter = propertyAccess.getAdapter(this.entityClass).getPropertyAdapter(idPropertyName);
> +        propertyAdapter = propertyAccess.getAdapter(this.entityClass).getPropertyAdapter(
> +                idPropertyName);
>     }
>
> -
>     public String toClient(E value)
>     {
> -        if (value == null) return null;
> +        if (value == null)
> +            return null;
>
>         Object id = propertyAdapter.get(value);
>
>         if (id == null)
> -            throw new IllegalStateException(String.format(
> -                    "Entity %s has an %s property of null; this probably means that it has not been persisted yet.",
> -                    value, idPropertyName));
> +            throw new IllegalStateException(
> +                    String
> +                            .format(
> +                                    "Entity %s has an %s property of null; this probably means that it has not been persisted yet.",
> +                                    value, idPropertyName));
>
>         return typeCoercer.coerce(id, String.class);
>     }
> @@ -74,9 +77,23 @@
>     @SuppressWarnings("unchecked")
>     public E toValue(String clientValue)
>     {
> -        if (InternalUtils.isBlank(clientValue)) return null;
> +        if (InternalUtils.isBlank(clientValue))
> +            return null;
> +
> +        Object id = null;
> +
> +        try
> +        {
>
> -        Object id = typeCoercer.coerce(clientValue, propertyAdapter.getType());
> +            id = typeCoercer.coerce(clientValue, propertyAdapter.getType());
> +        }
> +        catch (Exception ex)
> +        {
> +            throw new RuntimeException(String.format(
> +                    "Exception converting '%s' to instance of %s (id type for entity %s): %s",
> +                    clientValue, propertyAdapter.getType().getName(), entityClass.getName(),
> +                    InternalUtils.toMessage(ex)), ex);
> +        }
>
>         Serializable ser = Defense.cast(id, Serializable.class, "id");
>
> @@ -84,8 +101,10 @@
>
>         if (result == null)
>         {
> -            // We don't identify the entity type in the message because the logger is based on the entity type.
> -            logger.error(String.format("Unable to convert client value '%s' into an entity instance.", clientValue));
> +            // We don't identify the entity type in the message because the logger is based on the
> +            // entity type.
> +            logger.error(String.format(
> +                    "Unable to convert client value '%s' into an entity instance.", clientValue));
>         }
>
>         return result;
>
> Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java
> URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java?rev=895090&r1=895089&r2=895090&view=diff
> ==============================================================================
> --- tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java (original)
> +++ tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java Fri Jan  1 23:15:08 2010
> @@ -68,7 +68,6 @@
>         HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
>                 SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
>
> -
>         try
>         {
>             encoder.toClient(entity);
> @@ -76,8 +75,9 @@
>         }
>         catch (IllegalStateException ex)
>         {
> -            assertMessageContains(ex, "Entity org.apache.tapestry5.internal.hibernate.SampleEntity",
> -                                  "has an id property of null");
> +            assertMessageContains(ex,
> +                    "Entity org.apache.tapestry5.internal.hibernate.SampleEntity",
> +                    "has an id property of null");
>         }
>
>         verify();
> @@ -104,11 +104,42 @@
>         HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
>                 SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
>
> -
>         assertNull(encoder.toValue("12345"));
>
>         verify();
> +    }
> +
> +    @Test
> +    public void to_value_bad_type_coercion()
> +    {
> +        Session session = mockSession();
> +        Logger logger = mockLogger();
> +
> +        replay();
>
> +        RootClass persistentClass = new RootClass();
> +        Property idProperty = new Property();
> +        idProperty.setName("id");
> +        persistentClass.setIdentifierProperty(idProperty);
> +
> +        HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
> +                SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
> +
> +        try
> +        {
> +            encoder.toValue("xyz");
> +            unreachable();
> +        }
> +        catch (RuntimeException ex)
> +        {
> +            assertMessageContains(
> +                    ex,
> +                    "Exception converting 'xyz' to instance of java.lang.Long (id type for entity org.apache.tapestry5.internal.hibernate.SampleEntity)");
> +        }
> +
> +        assertNull(encoder.toValue(""));
> +
> +        verify();
>     }
>
>     protected final Session mockSession()
>
>
>



-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org