From 13db8b95e3a732307e1b07de453933561ac09c3d Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Tue, 28 Jun 2016 17:11:26 -0700 Subject: [PATCH] HHH-10729 HHH-9021 HHH-10230 : Add more checks to test cases --- ...CriteriaLiteralInSelectExpressionTest.java | 45 +++++++++++++++++++ ...eryWithLiteralsInSelectExpressionTest.java | 8 ++++ 2 files changed, 53 insertions(+) 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 index 0332702cae..36b8cbeaf5 100644 --- 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 @@ -28,6 +28,7 @@ import org.junit.Test; import org.hibernate.testing.TestForIssue; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; /** @@ -75,6 +76,9 @@ public class CriteriaLiteralInSelectExpressionTest extends BaseEntityManagerFunc final List dtos = entityManager.createQuery( query ).getResultList(); assertThat( dtos.size(), is( 1 ) ); + assertThat( dtos.get( 0 ).active, is( false ) ); + assertThat( dtos.get( 0 ).name, is( "Fab" ) ); + assertThat( dtos.get( 0 ).surname, nullValue() ); entityManager.getTransaction().commit(); } @@ -105,6 +109,42 @@ public class CriteriaLiteralInSelectExpressionTest extends BaseEntityManagerFunc final List dtos = entityManager.createQuery( query ).getResultList(); assertThat( dtos.size(), is( 1 ) ); + assertThat( dtos.get( 0 ).active, is( false ) ); + assertThat( dtos.get( 0 ).name, nullValue() ); + assertThat( dtos.get( 0 ).surname, nullValue() ); + + 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 testNullLiteralFirst() 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.nullLiteral( String.class ), entity.get( "surname" ) ); + + final List dtos = entityManager.createQuery( query ).getResultList(); + + assertThat( dtos.size(), is( 1 ) ); + assertThat( dtos.get( 0 ).name, nullValue() ); + assertThat( dtos.get( 0 ).surname, is( "A" ) ); + assertThat( dtos.get( 0 ).active, is( false ) ); entityManager.getTransaction().commit(); } @@ -135,6 +175,9 @@ public class CriteriaLiteralInSelectExpressionTest extends BaseEntityManagerFunc final List dtos = entityManager.createQuery( query ).getResultList(); assertThat( dtos.size(), is( 1 ) ); + assertThat( dtos.get( 0 ).name, is( "Leo" ) ); + assertThat( dtos.get( 0 ).surname, is( "A" ) ); + assertThat( dtos.get( 0 ).active, is( false ) ); entityManager.getTransaction().commit(); } @@ -164,6 +207,8 @@ public class CriteriaLiteralInSelectExpressionTest extends BaseEntityManagerFunc final List results = typedQuery.getResultList(); assertThat( results.size(), is( 1 ) ); + assertThat( results.get( 0 ).getElements().size(), is( 1 ) ); + assertThat( results.get( 0 ).get( 0 ), is( false ) ); } finally { entityManager.close(); 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 index 3434d718f7..ba1c4e70be 100644 --- 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 @@ -62,6 +62,10 @@ public class QueryWithLiteralsInSelectExpressionTest extends BaseEntityManagerFu Object[].class ).getResultList(); Assert.assertEquals( 1, elements.size() ); + Assert.assertEquals( 3, elements.get( 0 ).length ); + Assert.assertEquals( true, elements.get( 0 )[ 0 ] ); + Assert.assertEquals( false, elements.get( 0 )[ 1 ] ); + Assert.assertEquals( "Fab", elements.get( 0 )[ 2 ] ); } finally { entityManager.close(); @@ -78,6 +82,10 @@ public class QueryWithLiteralsInSelectExpressionTest extends BaseEntityManagerFu Object[].class ).getResultList(); Assert.assertEquals( 1, elements.size() ); + Assert.assertEquals( 3, elements.get( 0 ).length ); + Assert.assertEquals( null, elements.get( 0 )[ 0 ] ); + Assert.assertEquals( false, elements.get( 0 )[ 1 ] ); + Assert.assertEquals( "Fab", elements.get( 0 )[ 2 ] ); } finally { entityManager.close();