From b31f2c02d7561cbfd495599f96f62e73716f2acb Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 15 Apr 2021 10:53:54 -0500 Subject: [PATCH] re-enable tests re-organize some tests CastNullSelectExpressionTest (not-implemented-yet) --- .../sqm/sql/BaseSqmToSqlAstConverter.java | 5 + .../hql/CastNullSelectExpressionTest.java | 120 ++++++++++++++++++ .../hql/CastNullSelectExpressionTest.java | 118 ----------------- .../orm/junit/NotImplementedYetExtension.java | 3 +- 4 files changed, 127 insertions(+), 119 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/CastNullSelectExpressionTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/hql/CastNullSelectExpressionTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index f221e9ede5..8162cf9cdc 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -2287,7 +2287,12 @@ public abstract class BaseSqmToSqlAstConverter extends Base return new NullnessLiteral( mappingModelExpressable ); } final MappingModelExpressable keyExpressable = getKeyExpressable( mappingModelExpressable ); + if ( keyExpressable == null ) { + throw new IllegalArgumentException( "Could not determine type for null literal" ); + } + final List expressions = new ArrayList<>( keyExpressable.getJdbcTypeCount() ); + keyExpressable.forEachJdbcType( (index, jdbcMapping) -> expressions.add( new QueryLiteral<>( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/CastNullSelectExpressionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/CastNullSelectExpressionTest.java new file mode 100644 index 0000000000..ecb86c593f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/CastNullSelectExpressionTest.java @@ -0,0 +1,120 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.query.hql; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.NotImplementedYet; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * @author Gail Badner + */ +@DomainModel( annotatedClasses = CastNullSelectExpressionTest.Person.class ) +@SessionFactory +@NotImplementedYet( reason = "Combination of https://github.com/hibernate/hibernate-orm/discussions/3921 and https://github.com/hibernate/hibernate-orm/discussions/3889" ) +public class CastNullSelectExpressionTest { + + @Test + @TestForIssue(jiraKey = "HHH-10757") + public void testSelectCastNull(SessionFactoryScope scope) { + scope.inTransaction( + (session) -> { + Object[] result = (Object[]) session.createQuery( + "select firstName, cast( null as string ), lastName from CastNullSelectExpressionTest$Person where lastName='Munster'" + ).uniqueResult(); + + assertEquals( 3, result.length ); + assertEquals( "Herman", result[0] ); + assertNull( result[1] ); + assertEquals( "Munster", result[2] ); + } + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-10757") + public void testSelectNewCastNull(SessionFactoryScope scope) { + scope.inTransaction( + (session) -> { + Person result = (Person) session.createQuery( + "select new CastNullSelectExpressionTest$Person( id, firstName, cast( null as string ), lastName ) from CastNullSelectExpressionTest$Person where lastName='Munster'" + ).uniqueResult(); + assertEquals( "Herman", result.firstName ); + assertNull( result.middleName ); + assertEquals( "Munster", result.lastName ); + } + ); + } + + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( + (session) -> { + Person person = new Person(); + person.firstName = "Herman"; + person.middleName = "Joseph"; + person.lastName = "Munster"; + session.persist( person ); + } + ); + } + + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( + (session) -> session.createQuery( "delete Person" ).executeUpdate() + ); + } + + @Entity + @Table(name = "PERSON") + public static class Person { + @Id + @GeneratedValue + private long id; + + private String firstName; + + private String middleName; + + private String lastName; + + private Integer age; + + Person() { + } + + public Person(long id, String firstName, String middleName, String lastName) { + this.id = id; + this.firstName = firstName; + this.middleName = middleName; + this.lastName = lastName; + } + + public Person(long id, String firstName, Integer age, String lastName) { + this.id = id; + this.firstName = firstName; + this.middleName = null; + this.lastName = lastName; + this.age = age; + } + + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/CastNullSelectExpressionTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/CastNullSelectExpressionTest.java deleted file mode 100644 index 3b6057b1bb..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/CastNullSelectExpressionTest.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.hql; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.junit.Test; - -import org.hibernate.Session; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -/** - * @author Gail Badner - */ -@TestForIssue( jiraKey = "HHH-10757") -public class CastNullSelectExpressionTest extends BaseCoreFunctionalTestCase { - - @Test - @TestForIssue(jiraKey = "HHH-10757") - public void testSelectCastNull() { - Session s = openSession(); - s.getTransaction().begin(); - Person person = new Person(); - person.firstName = "Herman"; - person.middleName = "Joseph"; - person.lastName = "Munster"; - s.persist( person ); - s.flush(); - s.clear(); - - Object[] result = (Object[]) s.createQuery( - "select firstName, cast( null as string ), lastName from CastNullSelectExpressionTest$Person where lastName='Munster'" - ).uniqueResult(); - - assertEquals( 3, result.length ); - assertEquals( "Herman", result[0] ); - assertNull( result[1] ); - assertEquals( "Munster", result[2] ); - - s.getTransaction().rollback(); - s.close(); - } - - @Test - @TestForIssue(jiraKey = "HHH-10757") - public void testSelectNewCastNull() { - Session s = openSession(); - s.getTransaction().begin(); - Person person = new Person(); - person.firstName = "Herman"; - person.middleName = "Joseph"; - person.lastName = "Munster"; - s.persist( person ); - s.flush(); - s.clear(); - - Person result = (Person) s.createQuery( - "select new CastNullSelectExpressionTest$Person( id, firstName, cast( null as string ), lastName ) from CastNullSelectExpressionTest$Person where lastName='Munster'" - ).uniqueResult(); - assertEquals( "Herman", result.firstName ); - assertNull( result.middleName ); - assertEquals( "Munster", result.lastName ); - - s.getTransaction().rollback(); - s.close(); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Person.class }; - } - - @Entity - @Table(name = "PERSON") - private static class Person { - @Id - @GeneratedValue - private long id; - - private String firstName; - - private String middleName; - - private String lastName; - - private Integer age; - - Person() { - } - - public Person(long id, String firstName, String middleName, String lastName) { - this.id = id; - this.firstName = firstName; - this.middleName = middleName; - this.lastName = lastName; - } - - public Person(long id, String firstName, Integer age, String lastName) { - this.id = id; - this.firstName = firstName; - this.middleName = null; - this.lastName = lastName; - this.age = age; - } - - } -} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/NotImplementedYetExtension.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/NotImplementedYetExtension.java index 212d52dcbe..8e653aed8a 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/NotImplementedYetExtension.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/NotImplementedYetExtension.java @@ -54,7 +54,8 @@ public class NotImplementedYetExtension return ConditionEvaluationResult.enabled( "@NotImplementedYet validation" ); } else { - return ConditionEvaluationResult.disabled( "Disabled : @NotImplementedYet" ); + final Optional annotation = TestingUtil.findEffectiveAnnotation( context, NotImplementedYet.class ); + return ConditionEvaluationResult.disabled( "Disabled : @NotImplementedYet - " + annotation.get().reason() ); } } return ConditionEvaluationResult.enabled( "No @NotImplementedYet" );