You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by aw...@apache.org on 2007/03/30 20:45:17 UTC
svn commit: r524228 [2/2] - in /incubator/openjpa/trunk:
openjpa-kernel/src/main/java/org/apache/openjpa/meta/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/callbacks/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/pe...
Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java?view=auto&rev=524228
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java Fri Mar 30 11:45:15 2007
@@ -0,0 +1,72 @@
+package org.apache.openjpa.persistence.test;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+
+import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
+import org.apache.openjpa.lib.jdbc.JDBCEvent;
+import org.apache.openjpa.lib.jdbc.JDBCListener;
+
+/**
+ * Base class for tests that need to check generated SQL.
+ *
+ * @author Patrick Linskey
+ */
+public abstract class SQLListenerTestCase
+ extends SingleEMFTestCase {
+
+ protected List<String> sql = new ArrayList<String>();
+
+ @Override
+ public void setUp(Object... props) {
+ Object[] copy = new Object[props.length + 2];
+ System.arraycopy(props, 0, copy, 0, props.length);
+ copy[copy.length - 2] = "openjpa.jdbc.JDBCListeners";
+ copy[copy.length - 1] = new JDBCListener[] { new Listener() };
+ super.setUp(copy);
+ }
+
+ /**
+ * Confirm that the specified SQL has been executed.
+ *
+ * @param sqlExp the SQL expression. E.g., "SELECT FOO .*"
+ */
+ public void assertSQL(String sqlExp) {
+ for (String statement : sql) {
+ if (statement.matches(sqlExp))
+ return;
+ }
+
+ fail("Expected regular expression <" + sqlExp + "> to have"
+ + " existed in SQL statements: " + sql);
+ }
+
+ /**
+ * Confirm that the specified SQL has not been executed.
+ *
+ * @param sqlExp the SQL expression. E.g., "SELECT BADCOLUMN .*"
+ */
+ public void assertNotSQL(String sqlExp) {
+ boolean failed = false;
+
+ for (String statement : sql) {
+ if (statement.matches(sqlExp))
+ failed = true;
+ }
+
+ if (failed)
+ fail("Regular expression <" + sqlExp + ">"
+ + " should not have been executed in SQL statements: " + sql);
+ }
+
+ public class Listener
+ extends AbstractJDBCListener {
+
+ @Override
+ public void beforeExecuteStatement(JDBCEvent event) {
+ if (event.getSQL() != null && sql != null)
+ sql.add(event.getSQL());
+ }
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java?view=auto&rev=524228
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java Fri Mar 30 11:45:15 2007
@@ -0,0 +1,38 @@
+package org.apache.openjpa.persistence.test;
+
+import java.util.Map;
+import java.util.HashMap;
+import javax.persistence.Persistence;
+
+import junit.framework.TestCase;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+
+public abstract class SingleEMFTestCase
+ extends PersistenceTestCase {
+
+ protected OpenJPAEntityManagerFactory emf;
+
+ /**
+ * Initialize entity manager factory.
+ *
+ * @param props list of persistent types used in testing and/or
+ * configuration values in the form key,value,key,value...
+ */
+ protected void setUp(Object... props) {
+ emf = createEMF(props);
+ }
+
+ /**
+ * Closes the entity manager factory.
+ */
+ public void tearDown() {
+ if (emf == null)
+ return;
+
+ try {
+ clear(emf);
+ } finally {
+ closeEMF(emf);
+ }
+ }
+}
Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Copied: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java (from r523359, incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTest.java)
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java?view=diff&rev=524228&p1=incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTest.java&r1=523359&p2=incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java&r2=524228
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTest.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java Fri Mar 30 11:45:15 2007
@@ -15,9 +15,12 @@
*/
package org.apache.openjpa.persistence.test;
-import java.util.*;
+import java.util.Collections;
+import java.util.List;
+import javax.persistence.EntityTransaction;
-import javax.persistence.*;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAQuery;
/**
* A base test case that can be used to easily test scenarios where there
@@ -25,42 +28,30 @@
*
* @author Marc Prud'hommeaux
*/
-public abstract class SingleEMTest extends SingleEMFTest {
+public abstract class SingleEMTestCase
+ extends SingleEMFTestCase {
- protected EntityManager em;
+ protected OpenJPAEntityManager em;
- public SingleEMTest(Class... classes) {
- super(classes);
+ @Override
+ public void setUp(Object... props) {
+ super.setUp(props);
+ em = emf.createEntityManager();
}
- /**
- * Rolls back the current transaction and closes the EntityManager.
- */
@Override
- public void tearDown() throws Exception {
+ public void tearDown() {
rollback();
close();
super.tearDown();
}
- /**
- * Returns the current EntityManager, creating one from the
- * EntityManagerFactory if it doesn't already exist.
- */
- public EntityManager em() {
- if (em == null) {
- em = emf().createEntityManager();
- }
-
- return em;
- }
-
/**
* Start a new transaction if there isn't currently one active.
* @return true if a transaction was started, false if one already existed
*/
- public boolean begin() {
- EntityTransaction tx = em().getTransaction();
+ protected boolean begin() {
+ EntityTransaction tx = em.getTransaction();
if (tx.isActive())
return false;
@@ -72,8 +63,8 @@
* Commit the current transaction, if it is active.
* @return true if the transaction was committed
*/
- public boolean commit() {
- EntityTransaction tx = em().getTransaction();
+ protected boolean commit() {
+ EntityTransaction tx = em.getTransaction();
if (!tx.isActive())
return false;
@@ -85,8 +76,8 @@
* Rollback the current transaction, if it is active.
* @return true if the transaction was rolled back
*/
- public boolean rollback() {
- EntityTransaction tx = em().getTransaction();
+ protected boolean rollback() {
+ EntityTransaction tx = em.getTransaction();
if (!tx.isActive())
return false;
@@ -98,7 +89,7 @@
* Closes the current EntityManager if it is open.
* @return false if the EntityManager was already closed.
*/
- public boolean close() {
+ protected boolean close() {
if (em == null)
return false;
@@ -111,41 +102,18 @@
return !em.isOpen();
}
- @Override
- public boolean closeEMF() {
- close();
- return super.closeEMF();
- }
-
- /**
- * Returns the entity name of the specified class. If the class
- * declares an @Entity, then it will be used, otherwise the base
- * name of the class will be returned.
- *
- * Note that this will not correctly return the entity name of
- * a class declared in an orm.xml file.
- */
- public String entityName(Class c) {
- Entity e = (Entity) c.getAnnotation(Entity.class);
- if (e != null && e.name() != null && e.name().length() > 0)
- return e.name();
-
- String name = c.getSimpleName();
- name = name.substring(name.lastIndexOf(".") + 1);
- return name;
- }
-
/**
* Delete all of the instances.
*
* If no transaction is running, then one will be started and committed.
* Otherwise, the operation will take place in the current transaction.
*/
- public void remove(Object... obs) {
+ protected void remove(Object... obs) {
boolean tx = begin();
for (Object ob : obs)
- em().remove(ob);
- if (tx) commit();
+ em.remove(ob);
+ if (tx)
+ commit();
}
/**
@@ -154,18 +122,19 @@
* If no transaction is running, then one will be started and committed.
* Otherwise, the operation will take place in the current transaction.
*/
- public void persist(Object... obs) {
+ protected void persist(Object... obs) {
boolean tx = begin();
for (Object ob : obs)
- em().persist(ob);
- if (tx) commit();
+ em.persist(ob);
+ if (tx)
+ commit();
}
/**
* Creates a query in the current EntityManager with the specified string.
*/
- public Query query(String str) {
- return em().createQuery(str);
+ protected OpenJPAQuery query(String str) {
+ return em.createQuery(str);
}
/**
@@ -178,10 +147,10 @@
* @param params the parameters, if any
* @return the Query object
*/
- public Query query(Class c, String str, Object... params) {
- String query = "select x from " + entityName(c) + " x "
+ protected OpenJPAQuery query(Class c, String str, Object... params) {
+ String query = "select x from " + entityName(emf, c) + " x "
+ (str == null ? "" : str);
- Query q = em().createQuery(query);
+ OpenJPAQuery q = em.createQuery(query);
for (int i = 0; params != null && i < params.length; i++)
q.setParameter(i + 1, params[i]);
return q;
@@ -196,31 +165,14 @@
*
* @see #query(java.lang.Class,java.lang.String)
*/
- public <E> List<E> find(Class<E> c, String q, Object... params) {
+ protected <E> List<E> find(Class<E> c, String q, Object... params) {
return Collections.checkedList(query(c, q, params).getResultList(), c);
}
- public <E> List<E> find(Class<E> c) {
- return find(c, null);
- }
-
/**
- * Deletes all instances of the specific class from the database.
- *
- * If no transaction is running, then one will be started and committed.
- * Otherwise, the operation will take place in the current transaction.
- *
- * @return the total number of instanes deleted
+ * Returns a list of all instances of the specific class in the database.
*/
- public int delete(Class... classes) {
- boolean tx = begin();
- int total = 0;
- for (Class c : classes) {
- total += query("delete from " + entityName(c) + " x").
- executeUpdate();
- }
- if (tx) commit();
-
- return total;
+ protected <E> List<E> find(Class<E> c) {
+ return find(c, null);
}
-}
\ No newline at end of file
+}
Modified: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/TestSimpleXmlEntity.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/TestSimpleXmlEntity.java?view=diff&rev=524228&r1=524227&r2=524228
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/TestSimpleXmlEntity.java (original)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/TestSimpleXmlEntity.java Fri Mar 30 11:45:15 2007
@@ -1,30 +1,20 @@
package org.apache.openjpa.persistence.xml;
-import java.util.Map;
-import javax.persistence.EntityManager;
-
-import org.apache.openjpa.persistence.test.SingleEMTest;
import org.apache.openjpa.persistence.ArgumentException;
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
public class TestSimpleXmlEntity
- extends SingleEMTest {
-
- public TestSimpleXmlEntity() {
- super(SimpleXmlEntity.class);
- }
+ extends SingleEMTestCase {
- protected void setEMFProps(Map props) {
- super.setEMFProps(props);
+ public void setUp() {
+ setUp(SimpleXmlEntity.class);
}
public void testNamedQueryInXmlNamedEntity() {
- EntityManager em = emf.createEntityManager();
em.createNamedQuery("SimpleXml.findAll").getResultList();
- em.close();
}
public void testNamedQueryInXmlUsingShortClassName() {
- EntityManager em = emf.createEntityManager();
try {
em.createNamedQuery("SimpleXmlEntity.findAll").getResultList();
fail("should not be able to execute query using short class name " +
@@ -32,17 +22,13 @@
} catch (ArgumentException ae) {
// expected
}
- em.close();
}
public void testNamedEntityInDynamicQuery() {
- EntityManager em = emf.createEntityManager();
em.createQuery("select o from SimpleXml o").getResultList();
- em.close();
}
public void testShortClassNameInDynamicQuery() {
- EntityManager em = emf.createEntityManager();
try {
em.createQuery("select o from SimpleXmlEntity o").getResultList();
fail("should not be able to execute query using short class name " +
@@ -50,6 +36,5 @@
} catch (ArgumentException ae) {
// expected
}
- em.close();
}
}