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
This commit is contained in:
Pinaki Poddar 2010-03-15 21:08:16 +00:00
parent d1b0d0fd6d
commit efa6e5428e
3 changed files with 155 additions and 2 deletions

View File

@ -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<Author> 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;
}
}

View File

@ -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<FieldMetaData> fmds = exp.getParameterizedFields();
for (FieldMetaData fmd : fmds) {
if (fmd.isExternalized())
return true;
}
return false;
}
}

View File

@ -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();