From 462585c532a975f9e62c471ab897adc955dc6cc4 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 27 Nov 2015 11:49:45 +0000 Subject: [PATCH] HHH-9343 - Add test fro issue --- .../simplecase/BasicSimpleCaseTest.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/simplecase/BasicSimpleCaseTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/simplecase/BasicSimpleCaseTest.java index 3454e7e427..fd1a1a143d 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/simplecase/BasicSimpleCaseTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/simplecase/BasicSimpleCaseTest.java @@ -10,8 +10,10 @@ import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.Id; import javax.persistence.Table; +import javax.persistence.Tuple; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Expression; import javax.persistence.criteria.Path; import javax.persistence.criteria.Root; @@ -19,6 +21,8 @@ import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.junit.Test; +import org.hibernate.testing.TestForIssue; + import static javax.persistence.criteria.CriteriaBuilder.SimpleCase; /** @@ -34,6 +38,88 @@ public class BasicSimpleCaseTest extends BaseEntityManagerFunctionalTestCase { return new Class[] {Customer.class}; } + @Test + @TestForIssue(jiraKey = "HHH-9343") + public void testCaseStringResult() { + EntityManager em = getOrCreateEntityManager(); + CriteriaBuilder builder = em.getCriteriaBuilder(); + + CriteriaQuery query = builder.createTupleQuery(); + Root root = query.from( Customer.class ); + + Path emailPath = root.get( "email" ); + CriteriaBuilder.Case selectCase = builder.selectCase(); + selectCase.when( builder.greaterThan( builder.length( emailPath ), 13 ), "Long" ); + selectCase.when( builder.greaterThan( builder.length( emailPath ), 12 ), "Normal" ); + Expression emailType = selectCase.otherwise( "Unknown" ); + + query.multiselect( emailPath, emailType ); + + em.createQuery( query ).getResultList(); + } + + @Test + @TestForIssue(jiraKey = "HHH-9343") + public void testCaseIntegerResult() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + CriteriaBuilder builder = em.getCriteriaBuilder(); + + CriteriaQuery query = builder.createTupleQuery(); + Root root = query.from( Customer.class ); + + Path emailPath = root.get( "email" ); + CriteriaBuilder.Case selectCase = builder.selectCase(); + selectCase.when( builder.greaterThan( builder.length( emailPath ), 13 ), 2 ); + selectCase.when( builder.greaterThan( builder.length( emailPath ), 12 ), 1 ); + Expression emailType = selectCase.otherwise( 0 ); + + query.multiselect( emailPath, emailType ); + + em.createQuery( query ).getResultList(); + } + + @Test + @TestForIssue(jiraKey = "HHH-9343") + public void testCaseLiteralResult() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery( Boolean.class ); + Root expense_ = cq.from( Customer.class ); + em.createQuery( + cq.distinct( true ).where( + cb.equal( expense_.get( "email" ), "@hibernate.com" ) + ).multiselect( + cb.selectCase() + .when( cb.gt( cb.count( expense_ ), cb.literal( 0L ) ), cb.literal( true ) ) + .otherwise( cb.literal( false ) ) + ) + ).getSingleResult(); + } + + @Test + @TestForIssue(jiraKey = "HHH-9343") + public void testCaseLiteralResult2() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery( Boolean.class ); + Root expense_ = cq.from( Customer.class ); + em.createQuery( + cq.distinct( true ).where( + cb.equal( expense_.get( "email" ), "@hibernate.com" ) + ).multiselect( + cb.selectCase() + .when( cb.gt( cb.count( expense_ ), cb.literal( 0L ) ), true ) + .otherwise( false ) + ) + ).getSingleResult(); + } + @Test public void testCaseInOrderBy() { EntityManager em = getOrCreateEntityManager();