From c5733e7837a71b3588b8a9c2aed6aa57a65279f0 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Thu, 20 Oct 2011 11:30:01 -0700 Subject: [PATCH] HHH-6748 : Test failures due to inconsistent numeric return type from native query --- .../ColumnTransformerTest.java | 18 ++++++++++--- .../test/component/basic/ComponentTest.java | 10 +++++-- .../test/hql/ASTParserLoadingTest.java | 6 +++-- .../cases/TestCustomColumnReadAndWrite.java | 6 ++++- .../joinedsubclass/JoinedSubclassTest.java | 26 +++++++++++++------ .../test/subselect/SubselectTest.java | 8 ++++-- .../unionsubclass2/UnionSubclassTest.java | 25 ++++++++++++------ 7 files changed, 72 insertions(+), 27 deletions(-) diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/various/readwriteexpression/ColumnTransformerTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/various/readwriteexpression/ColumnTransformerTest.java index 371501c942..694f3da3be 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/various/readwriteexpression/ColumnTransformerTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/various/readwriteexpression/ColumnTransformerTest.java @@ -49,13 +49,21 @@ public class ColumnTransformerTest extends BaseCoreFunctionalTestCase { s.flush(); // Test value conversion during insert - Double heightViaSql = (Double)s.createSQLQuery("select size_in_cm from t_staff where t_staff.id=1").uniqueResult(); + // Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType; + // Cast returned value to Number then call Number.doubleValue() so it works on all dialects. + double heightViaSql = + ( (Number)s.createSQLQuery("select size_in_cm from t_staff where t_staff.id=1").uniqueResult() ) + .doubleValue(); assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); - heightViaSql = (Double)s.createSQLQuery("select radiusS from t_staff where t_staff.id=1").uniqueResult(); + heightViaSql = + ( (Number)s.createSQLQuery("select radiusS from t_staff where t_staff.id=1").uniqueResult() ) + .doubleValue(); assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); - heightViaSql = (Double)s.createSQLQuery("select diamet from t_staff where t_staff.id=1").uniqueResult(); + heightViaSql = + ( (Number)s.createSQLQuery("select diamet from t_staff where t_staff.id=1").uniqueResult() ) + .doubleValue(); assertEquals(HEIGHT_CENTIMETERS*2, heightViaSql, 0.01d); // Test projection @@ -78,7 +86,9 @@ public class ColumnTransformerTest extends BaseCoreFunctionalTestCase { // Test update staff.setSizeInInches(1); s.flush(); - heightViaSql = (Double)s.createSQLQuery("select size_in_cm from t_staff where t_staff.id=1").uniqueResult(); + heightViaSql = + ( (Number)s.createSQLQuery("select size_in_cm from t_staff where t_staff.id=1").uniqueResult() ) + .doubleValue(); assertEquals(2.54d, heightViaSql, 0.01d); s.delete(staff); t.commit(); diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/component/basic/ComponentTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/component/basic/ComponentTest.java index 1fda11e833..59af7dbd9c 100755 --- a/hibernate-core/src/matrix/java/org/hibernate/test/component/basic/ComponentTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/component/basic/ComponentTest.java @@ -249,7 +249,11 @@ public class ComponentTest extends BaseCoreFunctionalTestCase { s.flush(); // Test value conversion during insert - Double heightViaSql = (Double)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult(); + // Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType; + // Cast returned value to Number then call Number.doubleValue() so it works on all dialects. + Double heightViaSql = + ( (Number)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult()) + .doubleValue(); assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); // Test projection @@ -272,7 +276,9 @@ public class ComponentTest extends BaseCoreFunctionalTestCase { // Test update u.getPerson().setHeightInches(1); s.flush(); - heightViaSql = (Double)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult(); + heightViaSql = + ( (Number)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult() ) + .doubleValue(); assertEquals(2.54d, heightViaSql, 0.01d); s.delete(u); t.commit(); diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/hql/ASTParserLoadingTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/hql/ASTParserLoadingTest.java index 32f9928c45..d6ff402cba 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/hql/ASTParserLoadingTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/hql/ASTParserLoadingTest.java @@ -1446,7 +1446,9 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase { s.persist( image ); s.flush(); - Double sizeViaSql = (Double)s.createSQLQuery("select size_mb from image").uniqueResult(); + // Value returned by Oracle is a Types.NUMERIC, which is mapped to a BigDecimalType; + // Cast returned value to Number then call Number.doubleValue() so it works on all dialects. + Double sizeViaSql = ( (Number)s.createSQLQuery("select size_mb from image").uniqueResult() ).doubleValue(); assertEquals(SIZE_IN_MB, sizeViaSql, 0.01d); t.commit(); s.close(); @@ -1459,7 +1461,7 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase { s.update( image ); s.flush(); - sizeViaSql = (Double)s.createSQLQuery("select size_mb from image").uniqueResult(); + sizeViaSql = ( (Number)s.createSQLQuery("select size_mb from image").uniqueResult() ).doubleValue(); assertEquals(NEW_SIZE_IN_MB, sizeViaSql, 0.01d); s.delete(image); diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/instrument/cases/TestCustomColumnReadAndWrite.java b/hibernate-core/src/matrix/java/org/hibernate/test/instrument/cases/TestCustomColumnReadAndWrite.java index 8319f2eb7b..3d0a08a3d8 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/instrument/cases/TestCustomColumnReadAndWrite.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/instrument/cases/TestCustomColumnReadAndWrite.java @@ -42,7 +42,11 @@ public class TestCustomColumnReadAndWrite extends AbstractExecutable { t = s.beginTransaction(); // Check value conversion on insert - Double sizeViaSql = (Double)s.createSQLQuery("select size_mb from documents").uniqueResult(); + // Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType; + // Cast returned value to Number then call Number.doubleValue() so it works on all dialects. + Double sizeViaSql = + ( (Number)s.createSQLQuery("select size_mb from documents").uniqueResult() ) + .doubleValue(); assertEquals( SIZE_IN_MB, sizeViaSql, 0.01d ); // Test explicit fetch of all properties diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java index d6780eb501..20cd146b63 100755 --- a/hibernate-core/src/matrix/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java @@ -224,11 +224,17 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { s.flush(); // Test value conversion during insert - Double heightViaSql = (Double)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult(); + // Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType; + // Cast returned value to Number then call Number.doubleValue() so it works on all dialects. + Double heightViaSql = + ( (Number)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult() ) + .doubleValue(); assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); - Double expiryViaSql = (Double)s.createSQLQuery("select pwd_expiry_weeks from JEmployee where person_id=?") - .setLong(0, e.getId()) - .uniqueResult(); + Double expiryViaSql = + ( (Number)s.createSQLQuery("select pwd_expiry_weeks from JEmployee where person_id=?") + .setLong(0, e.getId()) + .uniqueResult() + ).doubleValue(); assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d); // Test projection @@ -263,11 +269,15 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { p.setHeightInches(1); e.setPasswordExpiryDays(7); s.flush(); - heightViaSql = (Double)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult(); + heightViaSql = + ( (Number)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult() ) + .doubleValue(); assertEquals(2.54d, heightViaSql, 0.01d); - expiryViaSql = (Double)s.createSQLQuery("select pwd_expiry_weeks from JEmployee where person_id=?") - .setLong(0, e.getId()) - .uniqueResult(); + expiryViaSql = + ( (Number)s.createSQLQuery("select pwd_expiry_weeks from JEmployee where person_id=?") + .setLong(0, e.getId()) + .uniqueResult() + ).doubleValue(); assertEquals(1d, expiryViaSql, 0.01d); s.delete(p); s.delete(e); diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/subselect/SubselectTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/subselect/SubselectTest.java index 1c9d562dac..e883e33fc9 100755 --- a/hibernate-core/src/matrix/java/org/hibernate/test/subselect/SubselectTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/subselect/SubselectTest.java @@ -109,9 +109,13 @@ public class SubselectTest extends BaseCoreFunctionalTestCase { s.flush(); // Test value conversion during insert - Double humanHeightViaSql = (Double)s.createSQLQuery("select height_centimeters from humans").uniqueResult(); + // Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType; + // Cast returned value to Number then call Number.doubleValue() so it works on all dialects. + Double humanHeightViaSql = + ( (Number)s.createSQLQuery("select height_centimeters from humans").uniqueResult() ).doubleValue(); assertEquals(HUMAN_CENTIMETERS, humanHeightViaSql, 0.01d); - Double alienHeightViaSql = (Double)s.createSQLQuery("select height_centimeters from aliens").uniqueResult(); + Double alienHeightViaSql = + ( (Number)s.createSQLQuery("select height_centimeters from aliens").uniqueResult() ).doubleValue(); assertEquals(ALIEN_CENTIMETERS, alienHeightViaSql, 0.01d); s.clear(); diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/unionsubclass2/UnionSubclassTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/unionsubclass2/UnionSubclassTest.java index 903a805735..ac2a50299b 100755 --- a/hibernate-core/src/matrix/java/org/hibernate/test/unionsubclass2/UnionSubclassTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/unionsubclass2/UnionSubclassTest.java @@ -191,11 +191,16 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase { s.flush(); // Test value conversion during insert - Double heightViaSql = (Double)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult(); + // Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType; + // Cast returned value to Number then call Number.doubleValue() so it works on all dialects. + Double heightViaSql = + ( (Number)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult() ).doubleValue(); assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); - Double expiryViaSql = (Double)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?") - .setLong(0, e.getId()) - .uniqueResult(); + Double expiryViaSql = + ( (Number)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?") + .setLong(0, e.getId()) + .uniqueResult() + ).doubleValue(); assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d); // Test projection @@ -230,11 +235,15 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase { p.setHeightInches(1); e.setPasswordExpiryDays(7); s.flush(); - heightViaSql = (Double)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult(); + heightViaSql = + ( (Number)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult() ) + .doubleValue(); assertEquals(2.54d, heightViaSql, 0.01d); - expiryViaSql = (Double)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?") - .setLong(0, e.getId()) - .uniqueResult(); + expiryViaSql = + ( (Number)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?") + .setLong(0, e.getId()) + .uniqueResult() + ).doubleValue(); assertEquals(1d, expiryViaSql, 0.01d); s.delete(p); s.delete(e);