From 4ecba55bc848abee10de33b5319cf8d78508846b Mon Sep 17 00:00:00 2001 From: Ruben Dijkstra Date: Wed, 17 Dec 2014 23:04:10 +0100 Subject: [PATCH] Implement the JPAQueryMutabilityTests --- .../mysema/query/JPAQueryMutabilityTest.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/querydsl-jpa/src/test/java/com/mysema/query/JPAQueryMutabilityTest.java b/querydsl-jpa/src/test/java/com/mysema/query/JPAQueryMutabilityTest.java index 3fcde62f5..a042dd639 100644 --- a/querydsl-jpa/src/test/java/com/mysema/query/JPAQueryMutabilityTest.java +++ b/querydsl-jpa/src/test/java/com/mysema/query/JPAQueryMutabilityTest.java @@ -13,29 +13,41 @@ */ package com.mysema.query; +import static com.mysema.query.support.Expressions.numberOperation; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import javax.persistence.EntityManager; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; +import com.mysema.query.jpa.HQLTemplates; +import com.mysema.query.jpa.JPQLTemplates; import com.mysema.query.jpa.domain.QCat; import com.mysema.query.jpa.impl.JPAQuery; +import com.mysema.query.types.OperatorImpl; import com.mysema.testutil.JPATestRunner; -@Ignore @RunWith(JPATestRunner.class) public class JPAQueryMutabilityTest implements JPATest { private EntityManager entityManager; + private final OperatorImpl customOperator = new OperatorImpl("CUSTOM", "SIGN"); + + private final JPQLTemplates customTemplates = new HQLTemplates() {{ + add(customOperator, "sign({0})"); + }}; + protected JPAQuery query() { return new JPAQuery(entityManager); } + protected JPAQuery query(JPQLTemplates templates) { + return new JPAQuery(entityManager, templates); + } + @Override public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; @@ -88,6 +100,33 @@ public class JPAQueryMutabilityTest implements JPATest { query2.list(cat); } + @Test + public void Clone_Custom_Templates() { + QCat cat = QCat.cat; + JPAQuery query = query().from(cat); + //attach using the custom templates + query.clone(entityManager, customTemplates) + .uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty)); + } + + @Test + public void Clone_Keep_Templates() { + QCat cat = QCat.cat; + JPAQuery query = query(customTemplates).from(cat); + //keep the original templates + query.clone() + .uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty)); + } + + @Test(expected = IllegalArgumentException.class) + public void Clone_Lose_Templates() { + QCat cat = QCat.cat; + JPAQuery query = query(customTemplates).from(cat); + //clone using the entitymanager's default templates + query.clone(entityManager) + .uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty)); + } + private void assertProjectionEmpty(JPAQuery query) { assertTrue(query.getMetadata().getProjection().isEmpty()); }