From efa6e5428e6354c83825cb038dd1cf9ecfd8364f Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Mon, 15 Mar 2010 21:08:16 +0000 Subject: [PATCH] OPENJPA-1575: Tests to detect parameterized fields in a query expression to exclude queries from cache that use parameters for externalized fields. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@923442 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/jdbc/sqlcache/Book.java | 14 +++ .../sqlcache/TestExternalizedParameter.java | 111 ++++++++++++++++++ .../jdbc/sqlcache/TestPreparedQueryCache.java | 32 ++++- 3 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestExternalizedParameter.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Book.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Book.java index ac030776e..046558b06 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Book.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/Book.java @@ -26,6 +26,8 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.ManyToMany; +import org.apache.openjpa.persistence.ExternalValues; + @Entity @DiscriminatorValue("BOOK") public class Book extends Merchandise { @@ -34,8 +36,12 @@ public class Book extends Merchandise { @ManyToMany(fetch=FetchType.EAGER) private Set authors; + @ExternalValues({"SMALL=S", "MEDIUM=M", "LARGE=L"}) + private String token; + public Book() { this("?"); + token = "MEDIUM"; } public Book(String title) { @@ -62,4 +68,12 @@ public class Book extends Merchandise { a.addBook(this); } } + + public String getToken() { + return token; + } + + public void setToken(String t) { + token = t; + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestExternalizedParameter.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestExternalizedParameter.java new file mode 100644 index 000000000..478c5b9a1 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestExternalizedParameter.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.openjpa.persistence.jdbc.sqlcache; + +import java.util.List; +import java.util.Properties; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import junit.framework.TestCase; + +import org.apache.openjpa.kernel.StoreQuery; +import org.apache.openjpa.kernel.exps.QueryExpressions; +import org.apache.openjpa.lib.rop.ResultList; +import org.apache.openjpa.meta.FieldMetaData; +import org.apache.openjpa.persistence.OpenJPAPersistence; + +/** + * Tests that we can detect if a query is using query parameters for fields whose values are externalized. + * + * @author Pinaki Poddar + * + */ +public class TestExternalizedParameter extends TestCase { + private static String RESOURCE = "META-INF/persistence.xml"; + private static String UNIT_NAME = "PreparedQuery"; + private static EntityManagerFactory emf; + + public void setUp() throws Exception { + if (emf == null) { + Properties config = new Properties(); + config.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true,SchemaAction='drop,add')"); + config.put("openjpa.jdbc.QuerySQLCache", "true"); + config.put("openjpa.RuntimeUnenhancedClasses", "unsupported"); + config.put("openjpa.DynamicEnhancementAgent", "false"); + config.put("openjpa.Log", "SQL=WARN"); + emf = OpenJPAPersistence.createEntityManagerFactory(UNIT_NAME, RESOURCE, config); + } + } + + public void testNoFalseAlarmOnExternalizedParameterDetection() { + String jpql = "select b from Book b where b.title=:title"; + EntityManager em = emf.createEntityManager(); + QueryExpressions[] exps = getExpressions(em.createQuery(jpql) + .setParameter("title","XYZ") + .getResultList()); + assertNotNull(exps); + + assertFalse(isUsingExternalizedParameter(exps[0])); + } + + public void testCanDetectExternalizedSingleParameterValue() { + String jpql = "select b from Book b where b.token=:token"; + EntityManager em = emf.createEntityManager(); + QueryExpressions[] exps = getExpressions(em.createQuery(jpql) + .setParameter("token","MEDIUM") + .getResultList()); + assertNotNull(exps); + + assertTrue(isUsingExternalizedParameter(exps[0])); + } + + public void testCanDetectExternalizedMixedParameterValue() { + String jpql = "select b from Book b where b.token=:token and b.title = :title"; + EntityManager em = emf.createEntityManager(); + QueryExpressions[] exps = getExpressions(em.createQuery(jpql) + .setParameter("token","MEDIUM") + .setParameter("token", "LARGE") + .getResultList()); + assertNotNull(exps); + + assertTrue(isUsingExternalizedParameter(exps[0])); + } + + public QueryExpressions[] getExpressions(List result) { + Object userObject = ((ResultList)result).getUserObject(); + if (userObject == null || !userObject.getClass().isArray() || ((Object[])userObject).length != 2) + return null; + Object executor = ((Object[])userObject)[1]; + if (executor instanceof StoreQuery.Executor == false) + return null; + return ((StoreQuery.Executor)executor).getQueryExpressions(); + } + + boolean isUsingExternalizedParameter(QueryExpressions exp) { + List fmds = exp.getParameterizedFields(); + for (FieldMetaData fmd : fmds) { + if (fmd.isExternalized()) + return true; + } + return false; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java index 26c2f42d2..cddca2f91 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java @@ -171,8 +171,8 @@ public class TestPreparedQueryCache extends TestCase { CD c1 = new CD("CD1"); CD c2 = new CD("CD2"); - b1.setId(id++); - b2.setId(id++); + b1.setId(id++); b1.setTitle("title-1"); b1.setToken("LARGE"); + b2.setId(id++); b2.setTitle("title-2"); b2.setToken("MEDIUM"); c1.setId(id++); c2.setId(id++); b1.addAuthor(a1); @@ -807,6 +807,34 @@ public class TestPreparedQueryCache extends TestCase { } } + public void testParameterOnExternalizedFieldIsExcluded() { + String jpql = "select b from Book b where b.title=:title and b.token=:token"; + Query q1 = em.createQuery(jpql) + .setParameter("title", "title-1") + .setParameter("token", "LARGE"); + // default fetches authors eagerly and thus creates multiple SQL and hence not caches anyway + OpenJPAPersistence.cast(q1).getFetchPlan().removeFetchGroup("default"); + assertFalse(q1.getResultList().isEmpty()); + assertNotCached(jpql); + Query q2 = em.createQuery(jpql) + .setParameter("title", "title-2") + .setParameter("token", "MEDIUM"); + assertFalse(q2.getResultList().isEmpty()); + } + + public void testNoParameterOnExternalizedFieldIsIncluded() { + String jpql = "select b from Book b where b.title=:title"; + Query q1 = em.createQuery(jpql) + .setParameter("title", "title-1"); + // default fetches authors eagerly and thus creates multiple SQL and hence not caches anyway + OpenJPAPersistence.cast(q1).getFetchPlan().removeFetchGroup("default"); + assertFalse(q1.getResultList().isEmpty()); + assertCached(jpql); + Query q2 = em.createQuery(jpql) + .setParameter("title", "title-2"); + assertFalse(q2.getResultList().isEmpty()); + } + PreparedQueryCache getPreparedQueryCache() { return emf.getConfiguration().getQuerySQLCacheInstance();