From 5feff3b2c6336be3b3e8a4cb1d1d5eb8b17240c4 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 1 Sep 2017 12:16:15 +0200 Subject: [PATCH] HHH-11942 - ANTLR parser should fail when providing an extra parenthesis --- .../hql/internal/ast/QueryTranslatorImpl.java | 11 ++++++++--- .../org/hibernate/test/hql/ASTParserLoadingTest.java | 9 +++++++-- .../hibernate/test/sql/function/JpaFunctionTest.java | 3 +-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/QueryTranslatorImpl.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/QueryTranslatorImpl.java index a29e083ad4..985752fbca 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/QueryTranslatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/QueryTranslatorImpl.java @@ -274,22 +274,27 @@ public class QueryTranslatorImpl implements FilterTranslator { return w; } - private HqlParser parse(boolean filter) throws TokenStreamException, RecognitionException { + private HqlParser parse(boolean filter) throws TokenStreamException { // Parse the query string into an HQL AST. final HqlParser parser = HqlParser.getInstance( hql ); parser.setFilter( filter ); LOG.debugf( "parse() - HQL: %s", hql ); - parser.statement(); + try { + parser.statement(); + } + catch (RecognitionException e) { + throw new HibernateException( "Unexpected error parsing HQL", e ); + } final AST hqlAst = parser.getAST(); + parser.getParseErrorHandler().throwQueryException(); final NodeTraverser walker = new NodeTraverser( new JavaConstantConverter( factory ) ); walker.traverseDepthFirst( hqlAst ); showHqlAst( hqlAst ); - parser.getParseErrorHandler().throwQueryException(); return parser; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java index 6f2abe02bd..fb6db1c070 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java @@ -47,6 +47,7 @@ import org.hibernate.dialect.SybaseAnywhereDialect; import org.hibernate.dialect.SybaseDialect; import org.hibernate.dialect.TeradataDialect; import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory; +import org.hibernate.hql.internal.ast.QuerySyntaxException; import org.hibernate.internal.util.StringHelper; import org.hibernate.loader.MultipleBagFetchException; import org.hibernate.persister.entity.DiscriminatorType; @@ -77,6 +78,7 @@ import org.junit.Test; import org.jboss.logging.Logger; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; @@ -85,6 +87,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -3797,11 +3800,13 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase { "order by a.description ", Product.class) .setParameter( "description", "desc" ) .getResultList(); + fail("Should have thrown exception"); } ); } catch (IllegalArgumentException e) { - QueryException rootCause = (QueryException) e.getCause(); - assertTrue( rootCause.getMessage().startsWith( "node to traverse cannot be null!" ) ); + final Throwable cause = e.getCause(); + assertThat( cause, instanceOf( QuerySyntaxException.class ) ); + assertTrue( cause.getMessage().contains( "expecting EOF, found ')'" ) ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/sql/function/JpaFunctionTest.java b/hibernate-core/src/test/java/org/hibernate/test/sql/function/JpaFunctionTest.java index 4de2b5d747..0c4e962564 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/sql/function/JpaFunctionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/sql/function/JpaFunctionTest.java @@ -11,7 +11,6 @@ import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; -import org.hibernate.QueryException; import org.hibernate.dialect.H2Dialect; import org.hibernate.hql.internal.ast.QuerySyntaxException; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; @@ -81,7 +80,7 @@ public class JpaFunctionTest extends BaseEntityManagerFunctionalTestCase { } ); } catch ( Exception e ) { - assertEquals( QueryException.class, e.getCause().getClass() ); + assertEquals( QuerySyntaxException.class, e.getCause().getClass() ); } }