/*
 * Copyright 2011, Mysema Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mysema.query;

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.domain.QCat;
import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.testutil.JPATestRunner;

@Ignore
@RunWith(JPATestRunner.class)
public class JPAQueryMutabilityTest implements JPATest {

    private EntityManager entityManager;

    protected JPAQuery query() {
        return new JPAQuery(entityManager);
    }

    @Override
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Test
    public void test() {
        QCat cat = QCat.cat;
        JPAQuery query = query().from(cat);

        query.count();
        assertProjectionEmpty(query);
        query.distinct().count();
        assertProjectionEmpty(query);

        query.iterate(cat);
        assertProjectionEmpty(query);
        query.iterate(cat,cat);
        assertProjectionEmpty(query);
        query.distinct().iterate(cat);
        assertProjectionEmpty(query);
        query.distinct().iterate(cat,cat);
        assertProjectionEmpty(query);

        query.list(cat);
        assertProjectionEmpty(query);
        query.list(cat,cat);
        assertProjectionEmpty(query);
        query.distinct().list(cat);
        assertProjectionEmpty(query);
        query.distinct().list(cat,cat);
        assertProjectionEmpty(query);

        query.listResults(cat);
        assertProjectionEmpty(query);
        query.distinct().listResults(cat);
        assertProjectionEmpty(query);

        query.map(cat.name, cat);
        assertProjectionEmpty(query);
    }

    @Test
    public void Clone() {
        QCat cat = QCat.cat;
        JPAQuery query = query().from(cat).where(cat.name.isNotNull());
        JPAQuery query2 = query.clone(entityManager);
        assertEquals(query.getMetadata().getJoins(), query2.getMetadata().getJoins());
        assertEquals(query.getMetadata().getWhere(), query2.getMetadata().getWhere());
        query2.list(cat);
    }

    private void assertProjectionEmpty(JPAQuery query) {
        assertTrue(query.getMetadata().getProjection().isEmpty());
    }

}

@@ -606,16 +606,84 @@
 query;%0A%0A
+import static com.mysema.query.support.Expressions.numberOperation;%0A
 import s
@@ -808,33 +808,8 @@
 r;%0A%0A
-import org.junit.Ignore;%0A
 impo
@@ -861,16 +861,101 @@
 nWith;%0A%0A
+import com.mysema.query.jpa.HQLTemplates;%0Aimport com.mysema.query.jpa.JPQLTemplates;%0A
 import c
@@ -1030,16 +1030,60 @@
 AQuery;%0A
+import com.mysema.query.types.OperatorImpl;%0A
 import c
@@ -1121,16 +1121,8 @@
 r;%0A%0A
-@Ignore%0A
 @Run
@@ -1257,76 +1257,424 @@
   pr
-otected JPAQuery query() %7B%0A        return new JPAQuery(entityManager
+ivate final OperatorImpl%3CInteger%3E customOperator = new OperatorImpl%3CInteger%3E(%22CUSTOM%22, %22SIGN%22);%0A%0A    private final JPQLTemplates customTemplates = new HQLTemplates() %7B%7B%0A            add(customOperator, %22sign(%7B0%7D)%22);%0A    %7D%7D;%0A%0A    protected JPAQuery query() %7B%0A        return new JPAQuery(entityManager);%0A    %7D%0A%0A    protected JPAQuery query(JPQLTemplates templates) %7B%0A        return new JPAQuery(entityManager, templates
 );%0A 
@@ -3254,24 +3254,1037 @@
 at);%0A    %7D%0A%0A
+    @Test%0A    public void Clone_Custom_Templates() %7B%0A        QCat cat = QCat.cat;%0A        JPAQuery query = query().from(cat);%0A        //attach using the custom templates%0A        query.clone(entityManager, customTemplates)%0A                .uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty));%0A    %7D%0A%0A    @Test%0A    public void Clone_Keep_Templates() %7B%0A        QCat cat = QCat.cat;%0A        JPAQuery query = query(customTemplates).from(cat);%0A        //keep the original templates%0A        query.clone()%0A                .uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty));%0A    %7D%0A%0A    @Test(expected = IllegalArgumentException.class)%0A    public void Clone_Lose_Templates() %7B%0A        QCat cat = QCat.cat;%0A        JPAQuery query = query(customTemplates).from(cat);%0A        //clone using the entitymanager's default templates%0A        query.clone(entityManager)%0A                .uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty));%0A    %7D%0A%0A
     private 

Implement the JPAQueryMutabilityTests