Skip to content

Commit 006271e

Browse files
author
Howard Lewis Ship
committed
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
git-svn-id: https://svn.apache.org/repos/asf/tapestry/tapestry5/trunk@895090 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7055307 commit 006271e

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
*.iws
88
target
99
test-output
10+
temp-testng*.xml

tapestry-hibernate/src/main/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoder.java

+31-12
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public final class HibernateEntityValueEncoder<E> implements ValueEncoder<E>
4141

4242
private final Logger logger;
4343

44-
public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persistentClass, Session session,
45-
PropertyAccess propertyAccess, TypeCoercer typeCoercer, Logger logger)
44+
public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persistentClass,
45+
Session session, PropertyAccess propertyAccess, TypeCoercer typeCoercer, Logger logger)
4646
{
4747
this.entityClass = entityClass;
4848
this.session = session;
@@ -53,39 +53,58 @@ public HibernateEntityValueEncoder(Class<E> entityClass, PersistentClass persist
5353

5454
idPropertyName = property.getName();
5555

56-
propertyAdapter = propertyAccess.getAdapter(this.entityClass).getPropertyAdapter(idPropertyName);
56+
propertyAdapter = propertyAccess.getAdapter(this.entityClass).getPropertyAdapter(
57+
idPropertyName);
5758
}
5859

59-
6060
public String toClient(E value)
6161
{
62-
if (value == null) return null;
62+
if (value == null)
63+
return null;
6364

6465
Object id = propertyAdapter.get(value);
6566

6667
if (id == null)
67-
throw new IllegalStateException(String.format(
68-
"Entity %s has an %s property of null; this probably means that it has not been persisted yet.",
69-
value, idPropertyName));
68+
throw new IllegalStateException(
69+
String
70+
.format(
71+
"Entity %s has an %s property of null; this probably means that it has not been persisted yet.",
72+
value, idPropertyName));
7073

7174
return typeCoercer.coerce(id, String.class);
7275
}
7376

7477
@SuppressWarnings("unchecked")
7578
public E toValue(String clientValue)
7679
{
77-
if (InternalUtils.isBlank(clientValue)) return null;
80+
if (InternalUtils.isBlank(clientValue))
81+
return null;
82+
83+
Object id = null;
84+
85+
try
86+
{
7887

79-
Object id = typeCoercer.coerce(clientValue, propertyAdapter.getType());
88+
id = typeCoercer.coerce(clientValue, propertyAdapter.getType());
89+
}
90+
catch (Exception ex)
91+
{
92+
throw new RuntimeException(String.format(
93+
"Exception converting '%s' to instance of %s (id type for entity %s): %s",
94+
clientValue, propertyAdapter.getType().getName(), entityClass.getName(),
95+
InternalUtils.toMessage(ex)), ex);
96+
}
8097

8198
Serializable ser = Defense.cast(id, Serializable.class, "id");
8299

83100
E result = (E) session.get(entityClass, ser);
84101

85102
if (result == null)
86103
{
87-
// We don't identify the entity type in the message because the logger is based on the entity type.
88-
logger.error(String.format("Unable to convert client value '%s' into an entity instance.", clientValue));
104+
// We don't identify the entity type in the message because the logger is based on the
105+
// entity type.
106+
logger.error(String.format(
107+
"Unable to convert client value '%s' into an entity instance.", clientValue));
89108
}
90109

91110
return result;

tapestry-hibernate/src/test/java/org/apache/tapestry5/internal/hibernate/HibernateEntityValueEncoderTest.java

+35-4
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ public void to_client_id_null()
6868
HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
6969
SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
7070

71-
7271
try
7372
{
7473
encoder.toClient(entity);
7574
unreachable();
7675
}
7776
catch (IllegalStateException ex)
7877
{
79-
assertMessageContains(ex, "Entity org.apache.tapestry5.internal.hibernate.SampleEntity",
80-
"has an id property of null");
78+
assertMessageContains(ex,
79+
"Entity org.apache.tapestry5.internal.hibernate.SampleEntity",
80+
"has an id property of null");
8181
}
8282

8383
verify();
@@ -104,11 +104,42 @@ public void to_value_not_found()
104104
HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
105105
SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
106106

107-
108107
assertNull(encoder.toValue("12345"));
109108

110109
verify();
110+
}
111+
112+
@Test
113+
public void to_value_bad_type_coercion()
114+
{
115+
Session session = mockSession();
116+
Logger logger = mockLogger();
117+
118+
replay();
111119

120+
RootClass persistentClass = new RootClass();
121+
Property idProperty = new Property();
122+
idProperty.setName("id");
123+
persistentClass.setIdentifierProperty(idProperty);
124+
125+
HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(
126+
SampleEntity.class, persistentClass, session, access, typeCoercer, logger);
127+
128+
try
129+
{
130+
encoder.toValue("xyz");
131+
unreachable();
132+
}
133+
catch (RuntimeException ex)
134+
{
135+
assertMessageContains(
136+
ex,
137+
"Exception converting 'xyz' to instance of java.lang.Long (id type for entity org.apache.tapestry5.internal.hibernate.SampleEntity)");
138+
}
139+
140+
assertNull(encoder.toValue(""));
141+
142+
verify();
112143
}
113144

114145
protected final Session mockSession()

0 commit comments

Comments
 (0)