continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ri...@apache.org
Subject svn commit: r597334 - in /maven/continuum/branches/continuum-jpa/continuum-model-jpa: ./ src/main/java/org/apache/maven/continuum/model/project/ src/main/java/org/apache/maven/continuum/model/scm/ src/main/java/org/apache/maven/continuum/store/api/ src...
Date Thu, 22 Nov 2007 08:48:15 GMT
Author: rinku
Date: Thu Nov 22 00:48:11 2007
New Revision: 597334

URL: http://svn.apache.org/viewvc?rev=597334&view=rev
Log:
o  Moved around some resources
o  Started adding some unit test for store and related updates to impl.

Added:
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java
  (with props)
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
  (with props)
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java
  (with props)
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
  (with props)
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java
  (with props)
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/resources/
Removed:
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/resources/META-INF/
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/resources/jdbc.properties
Modified:
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/pom.xml
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/project/BuildResult.java
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/scm/ScmResult.java
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/api/Store.java
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectGroupStore.java
    maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectStore.java

Modified: maven/continuum/branches/continuum-jpa/continuum-model-jpa/pom.xml
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/pom.xml?rev=597334&r1=597333&r2=597334&view=diff
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/pom.xml (original)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/pom.xml Thu Nov 22 00:48:11
2007
@@ -20,5 +20,41 @@
       <artifactId>openjpa</artifactId>
       <version>1.0.0</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.derby</groupId>
+      <artifactId>derby</artifactId>
+      <version>10.3.1.4</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
+  <!-- build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>process-sources</phase>
+            <configuration>
+              <tasks>                  
+                <property name="compile_classpath"
+                  refid="maven.compile.classpath" />
+                <property name="runtime_classpath"
+                  refid="maven.runtime.classpath" />
+                <property name="test_classpath"
+                  refid="maven.test.classpath" />
+                <property name="plugin_classpath"
+                  refid="maven.plugin.classpath" />
+                <ant antfile="${basedir}/build.xml">
+                  <target name="refresh" />
+                </ant>
+              </tasks>
+            </configuration>
+            <goals>
+              <goal>run</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build -->
 </project>

Modified: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/project/BuildResult.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/project/BuildResult.java?rev=597334&r1=597333&r2=597334&view=diff
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/project/BuildResult.java
(original)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/project/BuildResult.java
Thu Nov 22 00:48:11 2007
@@ -8,7 +8,10 @@
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
 import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
 import javax.persistence.ManyToOne;
 import javax.persistence.OneToMany;
 import javax.persistence.OneToOne;
@@ -121,7 +124,9 @@
      * Field modifiedDependencies
      */
     @OneToMany( cascade = CascadeType.ALL )
-    @JoinColumn( name = "ID_PROJECT_DEPENDENCY" )
+    @JoinTable( name = "PROJECT_DEPENDENCY", 
+                joinColumns = @JoinColumn( name = "BUILD_ID" ), 
+                inverseJoinColumns = @JoinColumn( name = "ID_PROJECT_DEPENDENCY" ) )
     private List<ProjectDependency> modifiedDependencies;
 
     /**

Modified: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/scm/ScmResult.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/scm/ScmResult.java?rev=597334&r1=597333&r2=597334&view=diff
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/scm/ScmResult.java
(original)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/model/scm/ScmResult.java
Thu Nov 22 00:48:11 2007
@@ -12,7 +12,7 @@
 
 /**
  * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
- * @version $Id$
+ * @version $Id$ 
  */
 @Entity
 @Table( name = "SCM_RESULT" )
@@ -49,9 +49,10 @@
 
     /**
      * Field exception
+     * XXX: Renamed from 'EXCEPTION' to 'EXCEPTION_MSG'
      */
     @Basic
-    @Column( name = "EXCEPTION" )
+    @Column( name = "EXCEPTION_MSG" )
     private String exception;
 
     /**

Modified: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/api/Store.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/api/Store.java?rev=597334&r1=597333&r2=597334&view=diff
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/api/Store.java
(original)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/api/Store.java
Thu Nov 22 00:48:11 2007
@@ -7,7 +7,8 @@
 
 /**
  * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
- * 
+ * @version $Id$
+ * @since 1.2
  */
 public interface Store<T>
 {

Modified: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectGroupStore.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectGroupStore.java?rev=597334&r1=597333&r2=597334&view=diff
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectGroupStore.java
(original)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectGroupStore.java
Thu Nov 22 00:48:11 2007
@@ -13,7 +13,8 @@
 
 /**
  * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
- * 
+ * @version $Id$
+ * @since 1.2
  */
 public class JpaProjectGroupStore implements Store<ProjectGroup>
 {

Modified: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectStore.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectStore.java?rev=597334&r1=597333&r2=597334&view=diff
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectStore.java
(original)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/main/java/org/apache/maven/continuum/store/jpa/JpaProjectStore.java
Thu Nov 22 00:48:11 2007
@@ -13,7 +13,8 @@
 
 /**
  * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
- * 
+ * @version $Id$
+ * @since 1.2
  */
 public class JpaProjectStore implements Store<Project>
 {

Added: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java?rev=597334&view=auto
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java
(added)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java
Thu Nov 22 00:48:11 2007
@@ -0,0 +1,61 @@
+/**
+ * 
+ */
+package org.apache.maven.continuum.store.jpa;
+
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.openjpa.persistence.OpenJPAQuery;
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
+
+/**
+ * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
+ * @since 1.2
+ * @version $Id$
+ * @see {@linkplain http://mail-archives.apache.org/mod_mbox/openjpa-users/200706.mbox/%3CBF2B99E3-7EF3-4E99-91E1-8AEB940524C7@apache.org%3E}
+ */
+public class JpaProjectStoreTest extends SingleEMTestCase
+{
+    private static final String PERSITENT_UNIT_CONTINUUM_STORE = "continuum-store";
+
+    @Override
+    public void setUp()
+    {
+        Properties propMap = new Properties();
+        setUp( propMap );
+    }
+
+    /**
+     * Returns the name of the persistent-unit setup in <code>persistence.xml</code>.
+     */
+    @Override
+    protected String getPersistenceUnitName()
+    {
+        return PERSITENT_UNIT_CONTINUUM_STORE;
+    }
+
+    public void testContinuumJPAStoreActions()
+    {
+        OpenJPAQuery q = em.createQuery( "select p from Project p" );
+        String[] sql = q.getDataStoreActions( null );
+        assertEquals( 1, sql.length );
+        assertTrue( sql[0].startsWith( "SELECT" ) );
+        // TODO: Uncomment following!
+        //List results = q.getResultList();
+        //assertNotNull( results );
+        //assertEquals( 0, results.size() );
+    }
+
+    /**
+     * TODO: Investigate {@link org.apache.openjpa.persistence.PersistenceException} attempting
to clear tables from
+     * schema.
+     */
+    @Override
+    public void tearDown() throws Exception
+    {
+        // super.tearDown();
+        // do nothing
+    }
+
+}

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/maven/continuum/store/jpa/JpaProjectStoreTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java?rev=597334&view=auto
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
(added)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
Thu Nov 22 00:48:11 2007
@@ -0,0 +1,205 @@
+/*
+ * 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.test;
+
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+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 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;
+
+/**
+ * Base test class providing persistence utilities.
+ */
+public abstract class PersistenceTestCase
+    extends TestCase {
+
+    /**
+     * Marker object you an pass to {@link #setUp} to indicate that the
+     * database tables should be cleared.
+     */
+    protected static final Object CLEAR_TABLES = new Object();
+
+    /**
+     * Create an entity manager factory. Put {@link #CLEAR_TABLES} in
+     * this list to tell the test framework to delete all table contents
+     * before running the tests.
+     *
+     * @param props list of persistent types used in testing and/or
+     * configuration values in the form key,value,key,value...
+     */
+    protected OpenJPAEntityManagerFactorySPI createEMF(Object... props) {
+        return createNamedEMF(getPersistenceUnitName(), props);
+    }
+
+    /**
+     * The name of the persistence unit that this test class should use
+     * by default. This defaults to "test".
+     */
+    protected String getPersistenceUnitName() {
+        return "test";
+    }
+
+    /**
+     * Create an entity manager factory for persistence unit <code>pu</code>.
+     * Put {@link #CLEAR_TABLES} in
+     * this list to tell the test framework to delete all table contents
+     * before running the tests.
+     *
+     * @param props list of persistent types used in testing and/or
+     * configuration values in the form key,value,key,value...
+     */
+    protected OpenJPAEntityManagerFactorySPI createNamedEMF(String pu,
+        Object... props) {
+        Map map = new HashMap(System.getProperties());
+        List<Class> types = new ArrayList<Class>();
+        boolean prop = false;
+        for (int i = 0; i < props.length; i++) {
+            if (prop) {
+                map.put(props[i - 1], props[i]);
+                prop = false;
+            } else if (props[i] == CLEAR_TABLES) {
+                map.put("openjpa.jdbc.SynchronizeMappings",
+                    "buildSchema(ForeignKeys=true," 
+                    + "SchemaAction='add,deleteTableContents')");
+            } else if (props[i] instanceof Class)
+                types.add((Class) props[i]);
+            else if (props[i] != null)
+                prop = true;
+        }
+
+        if (!types.isEmpty()) {
+            StringBuffer buf = new StringBuffer();
+            for (Class c : types) {
+                if (buf.length() > 0)
+                    buf.append(";");
+                buf.append(c.getName());
+            }
+            map.put("openjpa.MetaDataFactory",
+                "jpa(Types=" + buf.toString() + ")");
+        }
+
+        return (OpenJPAEntityManagerFactorySPI) Persistence.
+            createEntityManagerFactory(pu, map);
+    }
+
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    /**
+     * Safely close the given factory.
+     */
+    protected boolean closeEMF(EntityManagerFactory emf) {
+        if (emf == null)
+            return false;
+        if (!emf.isOpen())
+            return false;
+
+        for (Iterator iter = ((AbstractBrokerFactory) JPAFacadeHelper
+            .toBrokerFactory(emf)).getOpenBrokers().iterator();
+            iter.hasNext(); ) {
+            Broker b = (Broker) iter.next();
+            if (b != null && !b.isClosed()) {
+                EntityManager em = JPAFacadeHelper.toEntityManager(b);
+                if (em.getTransaction().isActive())
+                    em.getTransaction().rollback();
+                em.close();
+            }
+        }
+
+        emf.close();
+        return !emf.isOpen();
+    }
+
+    /**
+     * Delete all instances of the given types using bulk delete queries.
+     */
+    protected void clear(EntityManagerFactory emf, Class... types) {
+        if (emf == null || types.length == 0)
+            return;
+
+        List<ClassMetaData> metas = new ArrayList<ClassMetaData>(types.length);
+        for (Class c : types) {
+            ClassMetaData meta = JPAFacadeHelper.getMetaData(emf, c);
+            if (meta != null)
+                metas.add(meta);
+        }
+        clear(emf, metas.toArray(new ClassMetaData[metas.size()]));
+    }
+
+    /**
+     * Delete all instances of the persistent types registered with the given
+     * factory using bulk delete queries.
+     */
+    protected void clear(EntityManagerFactory emf) {
+        if (emf == null)
+            return;
+        clear(emf, ((OpenJPAEntityManagerFactorySPI) emf).getConfiguration().
+            getMetaDataRepositoryInstance().getMetaDatas());
+    }
+
+    /**
+     * Delete all instances of the given types using bulk delete queries.
+     */
+    private void clear(EntityManagerFactory emf, ClassMetaData... types) {
+        if (emf == null || types.length == 0)
+            return;
+
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        for (ClassMetaData meta : types) {
+            if (!meta.isMapped() || meta.isEmbeddedOnly() 
+                || Modifier.isAbstract(meta.getDescribedType().getModifiers()))
+                continue;
+            em.createQuery("DELETE FROM " + meta.getTypeAlias() + " o").
+                executeUpdate();
+        }
+        em.getTransaction().commit();
+        em.close();
+    }
+
+    /**
+     * Return the entity name for the given type.   
+     */
+    protected String entityName(EntityManagerFactory emf, Class c) {
+        ClassMetaData meta = JPAFacadeHelper.getMetaData(emf, c);
+        return (meta == null) ? null : meta.getTypeAlias();
+    }
+
+    public static void assertNotEquals(Object o1, Object o2) {
+        if (o1 == o2)
+            fail("expected args to be different; were the same instance.");
+        else if (o1 == null || o2 == null)
+            return;
+        else if (o1.equals(o2))
+            fail("expected args to be different; compared equal.");
+    }
+}

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java?rev=597334&view=auto
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java
(added)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java
Thu Nov 22 00:48:11 2007
@@ -0,0 +1,125 @@
+/*
+ * 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.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>();
+    protected int sqlCount;
+    
+    @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);
+    }
+
+    /**
+     * Confirm that the executed SQL String contains the specified sqlExp.
+     *
+     * @param sqlExp the SQL expression. E.g., "SELECT BADCOLUMN .*"
+     */
+    public void assertContainsSQL(String sqlExp) {
+        for (String statement : sql) {
+            if (statement.contains(sqlExp))
+                return;
+        }
+
+        fail("Expected regular expression <" + sqlExp + "> to be"
+            + " contained in SQL statements: " + sql);
+    }
+    
+    /**
+     * Gets the number of SQL issued since last reset.
+     */
+    public int getSQLCount() {
+    	return sqlCount;
+    }
+    
+    /**
+     * Resets SQL count.
+     * @return number of SQL counted since last reset.
+     */
+    public int resetSQLCount() {
+    	int tmp = sqlCount;
+    	sqlCount = 0;
+    	return tmp;
+    }
+
+    public class Listener
+        extends AbstractJDBCListener {
+
+        @Override
+        public void beforeExecuteStatement(JDBCEvent event) {
+            if (event.getSQL() != null && sql != null) {
+                sql.add(event.getSQL());
+                sqlCount++;
+            }
+		}
+	}
+}

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SQLListenerTestCase.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java?rev=597334&view=auto
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
(added)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
Thu Nov 22 00:48:11 2007
@@ -0,0 +1,71 @@
+/*
+ * 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.test;
+
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+
+public abstract class SingleEMFTestCase
+    extends PersistenceTestCase {
+
+    protected OpenJPAEntityManagerFactorySPI emf;
+
+    /**
+     * Call {@link #setUp(Object...)} with no arguments so that the emf
+     * set-up happens even if <code>setUp()</code> is not called from the
+     * subclass.
+     */
+    public void setUp() throws Exception {
+        setUp(new Object[0]);
+    }
+
+    /**
+     * Initialize entity manager factory. Put {@link #CLEAR_TABLES} in
+     * this list to tell the test framework to delete all table contents
+     * before running the tests.
+     *
+     * @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() throws Exception {
+        super.tearDown();
+
+        if (emf == null)
+            return;
+
+        try {
+            clear(emf);
+        } finally {
+            closeEMF(emf);
+        }
+    }
+    
+    protected ClassMapping getMapping(String name) {
+        return (ClassMapping) emf.getConfiguration()
+                .getMetaDataRepositoryInstance().getMetaData(name,
+                        getClass().getClassLoader(), true);
+    }
+}

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java
URL: http://svn.apache.org/viewvc/maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java?rev=597334&view=auto
==============================================================================
--- maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java
(added)
+++ maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java
Thu Nov 22 00:48:11 2007
@@ -0,0 +1,194 @@
+/*
+ * 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.test;
+
+import java.util.Collections;
+import java.util.List;
+import javax.persistence.EntityTransaction;
+
+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
+ * is only a single EntityManager at any given time.
+ *
+ * @author Marc Prud'hommeaux
+ */
+public abstract class SingleEMTestCase 
+    extends SingleEMFTestCase {
+
+    protected OpenJPAEntityManager em;
+
+    @Override
+    public void setUp() {
+        setUp(new Object[0]);
+    }
+
+    @Override
+    public void setUp(Object... props) {
+        super.setUp(props);
+        em = emf.createEntityManager(); 
+    }
+
+    /** 
+     * Clear the current EntityManager and re-initialize it.
+     */
+    protected void reset() {
+        close();
+        em = emf.createEntityManager(); 
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        rollback();
+        close();
+        super.tearDown();
+    }
+
+    /** 
+     * Start a new transaction if there isn't currently one active. 
+     * @return  true if a transaction was started, false if one already existed
+     */
+    protected boolean begin() {
+        EntityTransaction tx = em.getTransaction();
+        if (tx.isActive())
+            return false;
+
+        tx.begin();
+        return true;
+    }
+
+    /** 
+     * Commit the current transaction, if it is active. 
+     * @return true if the transaction was committed
+     */
+    protected boolean commit() {
+        EntityTransaction tx = em.getTransaction();
+        if (!tx.isActive())
+            return false;
+
+        tx.commit();
+        return true;
+    }
+
+    /** 
+     * Rollback the current transaction, if it is active. 
+     * @return true if the transaction was rolled back
+     */
+    protected boolean rollback() {
+        EntityTransaction tx = em.getTransaction();
+        if (!tx.isActive())
+            return false;
+
+        tx.rollback();
+        return true;
+    }
+
+    /** 
+     * Closes the current EntityManager if it is open. 
+     * @return false if the EntityManager was already closed.
+     */
+    protected boolean close() {
+        if (em == null)
+            return false;
+
+        rollback();
+
+        if (!em.isOpen())
+            return false;
+
+        em.close();
+        return !em.isOpen();
+    }
+
+    /** 
+     * 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.
+     */
+    protected void remove(Object... obs) {
+        boolean tx = begin();
+        for (Object ob : obs)
+            em.remove(ob);
+        if (tx) 
+            commit();
+    }
+
+    /** 
+     * Persist 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.
+     */
+    protected void persist(Object... obs) {
+        boolean tx = begin();
+        for (Object ob : obs)
+            em.persist(ob);
+        if (tx) 
+            commit();
+    }
+
+    /** 
+     * Creates a query in the current EntityManager with the specified string. 
+     */
+    protected OpenJPAQuery query(String str) {
+        return em.createQuery(str);
+    }
+
+    /** 
+     * Create a query against the specified class, which will be aliased
+     * as "x". For example, query(Person.class, "where x.age = 21") will
+     * create the query "select x from Person x where x.age = 21".
+     *  
+     * @param  c  the class to query against
+     * @param  str  the query suffix
+     * @param  params  the parameters, if any
+     * @return the Query object
+     */
+    protected OpenJPAQuery query(Class c, String str, Object... params) {
+        String query = "select x from " + entityName(emf, c) + " x "
+            + (str == null ? "" : str);
+        OpenJPAQuery q = em.createQuery(query);
+        for (int i = 0; params != null && i < params.length; i++)
+            q.setParameter(i + 1, params[i]);
+        return q;
+    }
+
+    /** 
+     * Returns a list of all instances of the specific class in the database. 
+     *
+     * @param c the class to find
+     * @param q the query string suffix to use
+     * @param params the positional parameter list value
+     *
+     * @see #query(java.lang.Class,java.lang.String)
+     */
+    protected <E> List<E> find(Class<E> c, String q, Object... params)
{
+        return Collections.checkedList(query(c, q, params).getResultList(), c);
+    }
+
+    /** 
+     * Returns a list of all instances of the specific class in the database. 
+     */
+    protected <E> List<E> find(Class<E> c) {
+        return find(c, null);
+    }
+}

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/continuum/branches/continuum-jpa/continuum-model-jpa/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"



Mime
View raw message