Do not execute testcases which require subselects if the dictionary doesn't provide that support

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.x@805141 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2009-08-17 21:03:33 +00:00
parent 129dc13af8
commit 11b1833028
4 changed files with 110 additions and 69 deletions

View File

@ -146,32 +146,36 @@ public class TestDiscriminatorTypes extends SingleEMFTestCase {
em.close();
}
@SuppressWarnings("unchecked")
public void testExistsQuery() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
if (getDBDictionary(emf).supportsSubselect) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
StringRootEntity e = new StringRootEntity();
e.setName("foo");
em.persist(e);
StringRootEntity e = new StringRootEntity();
e.setName("foo");
em.persist(e);
e = new StringRootEntity();
e.setName("foo");
em.persist(e);
e = new StringRootEntity();
e.setName("foo");
em.persist(e);
e = new StringRootEntity();
e.setName("bar");
em.persist(e);
e = new StringRootEntity();
e.setName("bar");
em.persist(e);
em.getTransaction().commit();
em.close();
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
Query q = em.createQuery("select o from StringAbstractEntity o " +
"where exists (select o2 from StringLeafEntity o2)");
List<StringAbstractEntity> list = q.getResultList();
assertEquals(0, list.size());
for (StringAbstractEntity entity : list)
assertTrue(entity instanceof StringLeafEntity);
em.close();
em = emf.createEntityManager();
Query q =
em.createQuery("select o from StringAbstractEntity o "
+ "where exists (select o2 from StringLeafEntity o2)");
List<StringAbstractEntity> list = q.getResultList();
assertEquals(0, list.size());
for (StringAbstractEntity entity : list)
assertTrue(entity instanceof StringLeafEntity);
em.close();
}
}
}

View File

@ -18,12 +18,23 @@
*/
package org.apache.openjpa.persistence.models.company;
import java.beans.*;
import java.io.*;
import java.util.*;
import javax.persistence.*;
import junit.framework.*;
import org.apache.openjpa.persistence.test.*;
import java.beans.ExceptionListener;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.XMLDecoder;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import junit.framework.AssertionFailedError;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.test.SingleEMTestCase;
/**
* Generic test case that will be extended by a concrete company
@ -36,11 +47,13 @@ public abstract class CompanyModelTest
private static Map<Class,Class> factoryClasses;
private Map<Class,Class> impls;
private static Boolean canExecute = null;
public void setUp() {
// make a map of the implementations based on the class names in
// the current package of the test subclass
impls = new HashMap<Class,Class>();
impls = new HashMap<Class, Class>();
impls.put(IAddress.class, localClass("Address"));
impls.put(ICompany.class, localClass("Company"));
impls.put(ICustomer.class, localClass("Customer"));
@ -53,7 +66,15 @@ public abstract class CompanyModelTest
impls.put(IProduct.class, localClass("Product"));
setUp(impls.values().toArray(new Class[impls.size()]));
checkModel();
if (canExecute == null) {
// This testcase requires subselects. Skip it if subselects will not
// work.
canExecute = getDBDictionary(emf).supportsSubselect;
}
if (canExecute) {
checkModel();
}
}
private Class localClass(String name) {
@ -72,26 +93,29 @@ public abstract class CompanyModelTest
* should always return all known instances in the database.
*/
public void testBasicQueries() throws Exception {
for (Class c : impls.values()) {
for (PropertyDescriptor pd :
Introspector.getBeanInfo(c).getPropertyDescriptors()) {
if (canExecute) {
for (Class c : impls.values()) {
for (PropertyDescriptor pd : Introspector.getBeanInfo(c)
.getPropertyDescriptors()) {
if (pd.getWriteMethod() == null) // ignore read-only
continue;
if (pd.getWriteMethod() == null) // ignore read-only
continue;
Set<String> queries = new TreeSet<String>();
getBasicQueries(queries, pd, "x.");
Set<String> queries = new TreeSet<String>();
getBasicQueries(queries, pd, "x.");
StringBuilder str = new StringBuilder();
StringBuilder str = new StringBuilder();
// execute the individual queries
for (String query : queries) {
find(c, "where " + query);
str.append(str.length() > 0 ? " or " : "").append(query);
// execute the individual queries
for (String query : queries) {
find(c, "where " + query);
str.append(str.length() > 0 ? " or " : "")
.append(query);
}
// now execute all the queries combined
find(c, "where " + str);
}
// now execute all the queries combined
find(c, "where " + str);
}
}
}

View File

@ -87,21 +87,23 @@ public class TestSubquery
public void testSubquery() {
EntityManager em = emf.createEntityManager();
for (int i = 0; i < querys.length; i++) {
String q = querys[i];
List rs = em.createQuery(q).getResultList();
assertEquals(0, rs.size());
}
if(getDBDictionary(emf).supportsSubselect) {
EntityManager em = emf.createEntityManager();
for (int i = 0; i < querys.length; i++) {
String q = querys[i];
List rs = em.createQuery(q).getResultList();
assertEquals(0, rs.size());
}
em.getTransaction().begin();
for (int i = 0; i < updates.length; i++) {
int updateCount = em.createQuery(updates[i]).executeUpdate();
assertEquals(0, updateCount);
}
em.getTransaction().begin();
for (int i = 0; i < updates.length; i++) {
int updateCount = em.createQuery(updates[i]).executeUpdate();
assertEquals(0, updateCount);
}
em.getTransaction().rollback();
em.close();
em.getTransaction().rollback();
em.close();
}
}
/**
@ -109,19 +111,21 @@ public class TestSubquery
* without losing the correct alias information. This sort of query
* originally caused problems for DBDictionaries which used DATABASE syntax.
*/
public void testSubSelectMaxDateRange() {
String query =
"SELECT e,d from Employee e, Dependent d "
public void testSubSelectMaxDateRange() {
if(getDBDictionary(emf).supportsSubselect) {
String query =
"SELECT e,d from Employee e, Dependent d "
+ "WHERE e.empId = :empid "
+ "AND d.id.empid = (SELECT MAX (e2.empId) FROM Employee e2) "
+ "AND d.id.effDate > :minDate "
+ "AND d.id.effDate < :maxDate ";
EntityManager em = emf.createEntityManager();
Query q = em.createQuery(query);
q.setParameter("empid", (long) 101);
q.setParameter("minDate", new Date(100));
q.setParameter("maxDate", new Date(100000));
q.getResultList();
em.close();
EntityManager em = emf.createEntityManager();
Query q = em.createQuery(query);
q.setParameter("empid", (long) 101);
q.setParameter("minDate", new Date(100));
q.setParameter("maxDate", new Date(100000));
q.getResultList();
em.close();
}
}
}

View File

@ -20,21 +20,25 @@ package org.apache.openjpa.persistence.test;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.framework.TestResult;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.sql.DBDictionary;
import org.apache.openjpa.kernel.AbstractBrokerFactory;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.JPAFacadeHelper;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
/**
* Base test class providing persistence utilities.
@ -195,4 +199,9 @@ public abstract class PersistenceTestCase
ClassMetaData meta = JPAFacadeHelper.getMetaData(emf, c);
return (meta == null) ? null : meta.getTypeAlias();
}
protected DBDictionary getDBDictionary(OpenJPAEntityManagerFactorySPI emf) {
return ((JDBCConfiguration) emf.getConfiguration())
.getDBDictionaryInstance();
}
}