From e0963d56b98984222e2cd5120d8925e9303ec0cc Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Sun, 19 Jun 2016 12:00:15 +0100 Subject: [PATCH] HHH-10729 HHH-9021 HHH-10230 - Add test for issue --- ...CriteriaLiteralInSelectExpressionTest.java | 214 ++++++++++++++++++ ...eryWithLiteralsInSelectExpressionTest.java | 129 +++++++++++ 2 files changed, 343 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/nulliteral/CriteriaLiteralInSelectExpressionTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/query/QueryWithLiteralsInSelectExpressionTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/nulliteral/CriteriaLiteralInSelectExpressionTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/nulliteral/CriteriaLiteralInSelectExpressionTest.java new file mode 100644 index 0000000000..0332702cae --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/nulliteral/CriteriaLiteralInSelectExpressionTest.java @@ -0,0 +1,214 @@ +/* + * 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.jpa.test.criteria.nulliteral; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityManager; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Tuple; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import java.io.Serializable; +import java.util.List; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * @author Andrea Boriero + */ +public class CriteriaLiteralInSelectExpressionTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {MyEntity.class}; + } + + @Before + public void init() { + final EntityManager entityManager = getOrCreateEntityManager(); + try { + entityManager.getTransaction().begin(); + entityManager.persist( new MyEntity( "Fab", "A" ) ); + entityManager.getTransaction().commit(); + } + catch (Exception e) { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + throw e; + } + finally { + entityManager.close(); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-10729") + public void testBooleanLiteral() throws Exception { + final EntityManager entityManager = getOrCreateEntityManager(); + try { + entityManager.getTransaction().begin(); + + final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + final CriteriaQuery query = criteriaBuilder.createQuery( MyEntityDTO.class ); + final Root entity = query.from( MyEntity.class ); + + query.multiselect( criteriaBuilder.literal( false ), entity.get( "name" ) ); + + final List dtos = entityManager.createQuery( query ).getResultList(); + + assertThat( dtos.size(), is( 1 ) ); + + entityManager.getTransaction().commit(); + } + catch (Exception e) { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + throw e; + } + finally { + entityManager.close(); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-10861") + public void testNullLiteral() throws Exception { + final EntityManager entityManager = getOrCreateEntityManager(); + try { + entityManager.getTransaction().begin(); + + final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + final CriteriaQuery query = criteriaBuilder.createQuery( MyEntityDTO.class ); + final Root entity = query.from( MyEntity.class ); + + query.multiselect( criteriaBuilder.literal( false ), criteriaBuilder.nullLiteral( String.class ) ); + + final List dtos = entityManager.createQuery( query ).getResultList(); + + assertThat( dtos.size(), is( 1 ) ); + + entityManager.getTransaction().commit(); + } + catch (Exception e) { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + throw e; + } + finally { + entityManager.close(); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-10729") + public void testStringLiteral() throws Exception { + final EntityManager entityManager = getOrCreateEntityManager(); + try { + entityManager.getTransaction().begin(); + + final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + final CriteriaQuery query = criteriaBuilder.createQuery( MyEntityDTO.class ); + final Root entity = query.from( MyEntity.class ); + + query.multiselect( criteriaBuilder.literal( "Leo" ), entity.get( "surname" ) ); + + final List dtos = entityManager.createQuery( query ).getResultList(); + + assertThat( dtos.size(), is( 1 ) ); + + entityManager.getTransaction().commit(); + } + catch (Exception e) { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + throw e; + } + finally { + entityManager.close(); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-9021") + public void testStringLiteral2() { + final EntityManager entityManager = getOrCreateEntityManager(); + try { + final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + final CriteriaQuery criteriaQuery = builder.createQuery( Tuple.class ); + criteriaQuery.from( MyEntity.class ); + criteriaQuery.multiselect( builder.equal( builder.literal( 1 ), builder.literal( 2 ) ) ); + + final TypedQuery typedQuery = entityManager.createQuery( criteriaQuery ); + + final List results = typedQuery.getResultList(); + + assertThat( results.size(), is( 1 ) ); + } + finally { + entityManager.close(); + } + } + + @Entity(name = "MyEntity") + @Table(name = "MY_ENTITY") + public static class MyEntity implements Serializable { + @Id + @Column(name = "id") + @GeneratedValue + private Integer id; + private String name; + private String surname; + + public MyEntity() { + } + + public MyEntity(String name, String surname) { + this.name = name; + this.surname = surname; + } + + public MyEntity(String name, boolean surname) { + this.name = name; + } + } + + public static class MyEntityDTO { + private String name; + private String surname; + private boolean active; + + public MyEntityDTO() { + } + + public MyEntityDTO(String name, String surname) { + this.name = name; + this.surname = surname; + } + + public MyEntityDTO(boolean active, String name) { + this.name = name; + this.active = active; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/QueryWithLiteralsInSelectExpressionTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/QueryWithLiteralsInSelectExpressionTest.java new file mode 100644 index 0000000000..3434d718f7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/QueryWithLiteralsInSelectExpressionTest.java @@ -0,0 +1,129 @@ +/* + * 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.jpa.test.query; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityManager; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.List; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; + +/** + * @author Andrea Boriero + */ +public class QueryWithLiteralsInSelectExpressionTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {MyEntity.class}; + } + + @Before + public void init() { + final EntityManager entityManager = getOrCreateEntityManager(); + try { + entityManager.getTransaction().begin(); + entityManager.persist( new MyEntity( "Fab", "A" ) ); + entityManager.getTransaction().commit(); + } + catch (Exception e) { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + throw e; + } + finally { + entityManager.close(); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-10230") + public void testSelectLiterals() { + final EntityManager entityManager = getOrCreateEntityManager(); + try { + final List elements = entityManager.createQuery( + "SELECT true, false, e.name FROM MyEntity e", + Object[].class + ).getResultList(); + Assert.assertEquals( 1, elements.size() ); + } + finally { + entityManager.close(); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-10230") + public void testSelectNullLiterals() { + final EntityManager entityManager = getOrCreateEntityManager(); + try { + final List elements = entityManager.createQuery( + "SELECT cast(null as boolean), false, e.name FROM MyEntity e", + Object[].class + ).getResultList(); + Assert.assertEquals( 1, elements.size() ); + } + finally { + entityManager.close(); + } + } + + @Entity(name = "MyEntity") + @Table(name = "MY_ENTITY") + public static class MyEntity implements Serializable { + @Id + @Column(name = "id") + @GeneratedValue + private Integer id; + private String name; + private String surname; + + public MyEntity() { + } + + public MyEntity(String name, String surname) { + this.name = name; + this.surname = surname; + } + + public MyEntity(String name, boolean surname) { + this.name = name; + } + } + + public static class MyEntityDTO { + private String name; + private String surname; + private boolean active; + + public MyEntityDTO() { + } + + public MyEntityDTO(String name, String surname) { + this.name = name; + this.surname = surname; + } + + public MyEntityDTO(String name, boolean active) { + this.name = name; + this.active = active; + } + } + +}