From bc1210e6412189d810f8ef9e08506de12d3c0ef7 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Thu, 4 Jan 2024 11:55:06 +0100 Subject: [PATCH] HHH-17435 Add test for issue --- .../orm/test/query/hql/FunctionTests.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java index 5da622ce15..bf9264392b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java @@ -19,6 +19,8 @@ import org.hibernate.dialect.OracleDialect; import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.dialect.SybaseDialect; import org.hibernate.dialect.TiDBDialect; +import org.hibernate.query.sqm.produce.function.FunctionArgumentException; + import org.hibernate.testing.TestForIssue; import org.hibernate.testing.orm.domain.StandardDomainModel; import org.hibernate.testing.orm.domain.gambit.EntityOfBasics; @@ -829,6 +831,52 @@ public class FunctionTests { } } + @Test + @JiraKey( "HHH-17435" ) + public void testTrimFunctionParameters(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + assertThat( session.createQuery( "select trim(:param)", String.class ) + .setParameter( "param", " hello " ) + .getSingleResult(), is( "hello" ) ); + assertThat( session.createQuery( "select trim(' ' from :param)", String.class ) + .setParameter( "param", " hello " ) + .getSingleResult(), is( "hello" ) ); + assertThat( session.createQuery( "select trim('''' from :param)", String.class ) + .setParameter( "param", "''hello'''" ) + .getSingleResult(), is( "hello" ) ); + assertThat( session.createQuery( "select trim(:param from '-- hello it''s me ---')", String.class ) + .setParameter( "param", '-' ) + .getSingleResult(), is( " hello it's me " ) ); + assertThat( session.createQuery( "select trim(:param from '--- hello it''s me -- ')", String.class ) + .setParameter( "param", '-' ) + .getSingleResult(), is( " hello it's me -- " ) ); + assertThat( session.createQuery( "select trim(leading ?1 from ' hello it''s me ')", String.class ) + .setParameter( 1, ' ' ) + .getSingleResult(), is( "hello it's me " ) ); + assertThat( session.createQuery( "select trim(trailing ?1 from ' hello it''s me ')", String.class ) + .setParameter( 1, ' ' ) + .getSingleResult(), is( " hello it's me" ) ); + assertThat( session.createQuery( "select trim(?1 from ?2)", String.class ) + .setParameter( 1, ' ' ) + .setParameter( 2, " hello it's me " ) + .getSingleResult(), is( "hello it's me" ) ); + } + ); + + try { + scope.inTransaction( + session -> session.createQuery( "select trim(:param from 'hello')", String.class ) + .setParameter( "param", 1 ) + .getResultList() + ); + fail(); + } + catch (IllegalArgumentException e) { + assertThat( e.getCause(), is( instanceOf( FunctionArgumentException.class ) ) ); + } + } + @Test @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsPadWithChar.class) public void testPadFunction(SessionFactoryScope scope) {