geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject svn commit: r434468 - in /geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction: ./ jpa/ jta11/ mockjpa/
Date Thu, 24 Aug 2006 18:49:35 GMT
Author: djencks
Date: Thu Aug 24 11:49:35 2006
New Revision: 434468

URL: http://svn.apache.org/viewvc?rev=434468&view=rev
Log:
Add some tests of TransactionSynchronizationRegistry and a cmp EntityManager

Added:
    geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/
    geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/
    geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/CMPEntityManagerTest.java
  (with props)
    geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/
    geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/TransactionSynchronizationRegistryTest.java
  (with props)
    geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/
    geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManager.java
  (with props)
    geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManagerFactory.java
  (with props)

Added: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/CMPEntityManagerTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/CMPEntityManagerTest.java?rev=434468&view=auto
==============================================================================
--- geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/CMPEntityManagerTest.java
(added)
+++ geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/CMPEntityManagerTest.java
Thu Aug 24 11:49:35 2006
@@ -0,0 +1,168 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed 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.geronimo.transaction.jpa;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import javax.persistence.EntityManager;
+import javax.persistence.TransactionRequiredException;
+
+import junit.framework.TestCase;
+import org.apache.geronimo.transaction.jta11.GeronimoTransactionManagerJTA11;
+import org.apache.geronimo.transaction.mockjpa.MockEntityManagerFactory;
+import org.apache.geronimo.transaction.mockjpa.MockEntityManager;
+
+/**
+ * @version $Rev:$ $Date:$
+ */
+public class CMPEntityManagerTest extends TestCase {
+
+    private GeronimoTransactionManagerJTA11 tm;
+    private String persistenceUnit = "foo";
+    private MockEntityManagerFactory entityManagerFactory;
+
+    protected void setUp() throws Exception {
+        tm  = new GeronimoTransactionManagerJTA11();
+        entityManagerFactory = new MockEntityManagerFactory();
+    }
+
+    /**
+     * section 5.8.2
+     * use the same persistence context for all work in a tx
+     */
+    public void testSamePersistenceContext() throws Exception {
+        tm.begin();
+        CMPEntityManager entityManager1 = new CMPEntityManager(tm, persistenceUnit, entityManagerFactory,
null, true);
+        EntityManager pc1 = entityManager1.find(EntityManager.class, "this");
+        CMPEntityManager entityManager2 = new CMPEntityManager(tm, persistenceUnit, entityManagerFactory,
null, true);
+        EntityManager pc2 = entityManager2.find(EntityManager.class, "this");
+        assertSame("Should get same entity manager for all work in a tx", pc1, pc2);
+        tm.commit();
+    }
+
+    /**
+     * section 5.9.1
+     * close or cleared is called when tx commits
+     */
+    public void testCloseOnCommit() throws Exception {
+        tm.begin();
+        CMPEntityManager entityManager1 = new CMPEntityManager(tm, persistenceUnit, entityManagerFactory,
null, true);
+        MockEntityManager pc1 = (MockEntityManager) entityManager1.find(EntityManager.class,
"this");
+        assertTrue("entityManager should not be closed or cleared", !pc1.isClosed() &
!pc1.isCleared());
+        tm.commit();
+        assertTrue("entityManager should be closed or cleared", pc1.isClosed() || pc1.isCleared());
+        tm.begin();
+        CMPEntityManager entityManager2 = new CMPEntityManager(tm, persistenceUnit, entityManagerFactory,
null, true);
+        MockEntityManager pc2 = (MockEntityManager) entityManager2.find(EntityManager.class,
"this");
+        assertTrue("entityManager should not be closed or cleared", !pc2.isClosed() &
!pc2.isCleared());
+        tm.rollback();
+        assertTrue("entityManager should be closed or cleared", pc2.isClosed() || pc2.isCleared());
+    }
+
+    /**
+     * section 5.9.1
+     * transaction required for persist, remove, merge, refresh
+     */
+    public void testTransactionRequired() throws Exception {
+        CMPEntityManager entityManager1 = new CMPEntityManager(tm, persistenceUnit, entityManagerFactory,
null, true);
+        try {
+            entityManager1.persist("foo");
+            fail("expected TransactionRequiredException");
+        } catch (TransactionRequiredException e) {
+            //expected
+        } catch (Exception e) {
+            fail ("Wrong exception" + e);
+        }
+        try {
+            entityManager1.remove("foo");
+            fail("expected TransactionRequiredException");
+        } catch (TransactionRequiredException e) {
+            //expected
+        } catch (Exception e) {
+            fail ("Wrong exception" + e);
+        }
+        try {
+            entityManager1.merge("foo");
+            fail("expected TransactionRequiredException");
+        } catch (TransactionRequiredException e) {
+            //expected
+        } catch (Exception e) {
+            fail ("Wrong exception" + e);
+        }
+        try {
+            entityManager1.refresh("foo");
+            fail("expected TransactionRequiredException");
+        } catch (TransactionRequiredException e) {
+            //expected
+        } catch (Exception e) {
+            fail ("Wrong exception" + e);
+        }
+    }
+
+    /**
+     * section 5.9.1
+     * application must not call close on its entityManager
+     */
+    public void testAppCallsCloseForbidden() throws Exception {
+        CMPEntityManager entityManager1 = new CMPEntityManager(tm, persistenceUnit, entityManagerFactory,
null, true);
+        try {
+            entityManager1.close();
+            fail("Application should not be able to call close on its EntityManager");
+        } catch (IllegalStateException e) {
+            //expected
+        }
+        tm.begin();
+        try {
+            entityManager1.close();
+            fail("Application should not be able to call close on its EntityManager");
+        } catch (IllegalStateException e) {
+            //expected
+        }
+        tm.commit();
+    }
+
+
+    /**
+     * section 5.9.1
+     * @throws Exception
+     */
+    public void testNoPropertiesUsed() throws Exception {
+
+        CMPEntityManager entityManager = new CMPEntityManager(tm, persistenceUnit, entityManagerFactory,
null, true);
+        tm.begin();
+        entityManager.contains("bar");
+        Map props = entityManager.find(Map.class, "properties");
+        assertSame("Props are not null", props, null);
+        tm.commit();
+    }
+
+    /**
+     * section 5.9.1
+     * @throws Exception
+     */
+    public void testPropertiesUsed() throws Exception {
+        Map properties = new HashMap();
+        CMPEntityManager entityManager = new CMPEntityManager(tm, persistenceUnit, entityManagerFactory,
properties, true);
+        tm.begin();
+        entityManager.contains("bar");
+        Map props = entityManager.find(Map.class, "properties");
+        assertSame("Props are not what was passed in", props, properties);
+        tm.commit();
+    }
+}

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/CMPEntityManagerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/CMPEntityManagerTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jpa/CMPEntityManagerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/TransactionSynchronizationRegistryTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/TransactionSynchronizationRegistryTest.java?rev=434468&view=auto
==============================================================================
--- geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/TransactionSynchronizationRegistryTest.java
(added)
+++ geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/TransactionSynchronizationRegistryTest.java
Thu Aug 24 11:49:35 2006
@@ -0,0 +1,162 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed 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.geronimo.transaction.jta11;
+
+import javax.transaction.Synchronization;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+import javax.transaction.NotSupportedException;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev:$ $Date:$
+ */
+public class TransactionSynchronizationRegistryTest extends TestCase {
+
+    private static int beforeCounter = 0;
+    private static int afterCounter = 0;
+
+
+
+    private GeronimoTransactionManagerJTA11 tm;
+
+    private CountingSync interposedSync;
+    private CountingSync normalSync;
+
+    protected void setUp() throws Exception {
+        tm  = new GeronimoTransactionManagerJTA11();
+    }
+
+    private void setUpInterposedSync() throws NotSupportedException, SystemException {
+        interposedSync = new CountingSync();
+        tm.begin();
+        tm.registerInterposedSynchronization(interposedSync);
+    }
+
+    private void setUpSyncs() throws Exception {
+        normalSync = new CountingSync();
+        setUpInterposedSync();
+        tm.getTransaction().registerSynchronization(normalSync);
+    }
+
+
+    public void testInterposedSynchIsCalledOnCommit() throws Exception {
+        setUpInterposedSync();
+        tm.commit();
+        checkInterposedSyncCalled();
+    }
+
+    private void checkInterposedSyncCalled() {
+        assertTrue("interposedSync beforeCompletion was not called", interposedSync.getBeforeCount()
!= -1);
+        assertTrue("interposedSync afterCompletion was not called", interposedSync.getAfterCount()
!= -1);
+    }
+
+    public void testInterposedSynchIsCalledOnRollback() throws Exception {
+        setUpInterposedSync();
+        tm.rollback();
+        checkInterposedSyncCalled();
+    }
+
+    public void testInterposedSynchIsCalledOnMarkRollback() throws Exception {
+        setUpInterposedSync();
+        tm.setRollbackOnly();
+        try {
+            tm.commit();
+            fail("expected a RollbackException");
+        } catch (HeuristicMixedException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        } catch (HeuristicRollbackException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        } catch (IllegalStateException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        } catch (RollbackException e) {
+
+        } catch (SecurityException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        } catch (SystemException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        }
+        checkInterposedSyncCalled();
+    }
+
+    public void testSynchCallOrderOnCommit() throws Exception {
+        setUpSyncs();
+        tm.commit();
+        checkSyncCallOrder();
+    }
+
+    private void checkSyncCallOrder() {
+        checkInterposedSyncCalled();
+        assertTrue("interposedSync beforeCompletion was not called after normalSync beforeCompletion",
interposedSync.getBeforeCount() > normalSync.getBeforeCount());
+        assertTrue("interposedSync afterCompletion was not called before normalSync beforeCompletion",
interposedSync.getAfterCount() < normalSync.getAfterCount());
+    }
+
+    public void testSynchCallOrderOnRollback() throws Exception {
+        setUpSyncs();
+        tm.rollback();
+        checkSyncCallOrder();
+    }
+
+    public void testSynchCallOrderOnMarkRollback() throws Exception {
+        setUpSyncs();
+        tm.setRollbackOnly();
+        try {
+            tm.commit();
+            fail("expected a RollbackException");
+        } catch (HeuristicMixedException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        } catch (HeuristicRollbackException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        } catch (IllegalStateException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        } catch (RollbackException e) {
+
+        } catch (SecurityException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        } catch (SystemException e) {
+            fail("expected a RollbackException not " + e.getClass());
+        }
+        checkSyncCallOrder();
+    }
+
+    private class CountingSync implements Synchronization {
+
+        private int beforeCount = -1;
+        private int afterCount = -1;
+
+        public void beforeCompletion() {
+            beforeCount = beforeCounter++;
+        }
+
+        public void afterCompletion(int i) {
+            afterCount = afterCounter++;
+        }
+
+        public int getBeforeCount() {
+            return beforeCount;
+        }
+
+        public int getAfterCount() {
+            return afterCount;
+        }
+    }
+
+}

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/TransactionSynchronizationRegistryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/TransactionSynchronizationRegistryTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/jta11/TransactionSynchronizationRegistryTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManager.java?rev=434468&view=auto
==============================================================================
--- geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManager.java
(added)
+++ geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManager.java
Thu Aug 24 11:49:35 2006
@@ -0,0 +1,143 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed 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.geronimo.transaction.mockjpa;
+
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.FlushModeType;
+import javax.persistence.LockModeType;
+import javax.persistence.Query;
+import javax.persistence.EntityTransaction;
+
+/**
+ * @version $Rev:$ $Date:$
+ */
+public class MockEntityManager implements EntityManager {
+
+    private final Map properties;
+    private boolean closed = false;
+    private boolean cleared = false;
+
+    public MockEntityManager() {
+        properties = null;
+    }
+
+    public MockEntityManager(Map properties) {
+        this.properties = properties;
+    }
+
+    public void persist(Object object) {
+    }
+
+    public <T> T merge(T t) {
+        return null;
+    }
+
+    public void remove(Object object) {
+    }
+
+    public <T> T find(Class<T> aClass, Object object) {
+        if (aClass == Map.class && "properties".equals(object)) {
+            return (T)properties;
+        }
+        if (aClass == EntityManager.class && "this".equals(object)) {
+            return (T)this;
+        }
+        return null;
+   }
+
+    public <T> T getReference(Class<T> aClass, Object object) {
+        return null;
+    }
+
+    public void flush() {
+    }
+
+    public void setFlushMode(FlushModeType flushModeType) {
+    }
+
+    public FlushModeType getFlushMode() {
+        return null;
+    }
+
+    public void lock(Object object, LockModeType lockModeType) {
+    }
+
+    public void refresh(Object object) {
+    }
+
+    public void clear() {
+        cleared = true;
+    }
+
+    public boolean contains(Object object) {
+        return false;
+    }
+
+    public Query createQuery(String string) {
+        return null;
+    }
+
+    public Query createNamedQuery(String string) {
+        return null;
+    }
+
+    public Query createNativeQuery(String string) {
+        return null;
+    }
+
+    public Query createNativeQuery(String string, Class aClass) {
+        return null;
+    }
+
+    public Query createNativeQuery(String string, String string1) {
+        return null;
+    }
+
+    public void close() {
+        closed = true;
+    }
+
+    public boolean isOpen() {
+        return !closed;
+    }
+
+    public EntityTransaction getTransaction() {
+        return null;
+    }
+
+    public void joinTransaction() {
+    }
+
+    public Object getDelegate() {
+        return null;
+    }
+
+    public Map getProperties() {
+        return properties;
+    }
+
+    public boolean isClosed() {
+        return closed;
+    }
+
+    public boolean isCleared() {
+        return cleared;
+    }
+}

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManager.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManager.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManagerFactory.java?rev=434468&view=auto
==============================================================================
--- geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManagerFactory.java
(added)
+++ geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManagerFactory.java
Thu Aug 24 11:49:35 2006
@@ -0,0 +1,53 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed 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.geronimo.transaction.mockjpa;
+
+import java.util.Map;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityManager;
+
+/**
+ * @version $Rev:$ $Date:$
+ */
+public class MockEntityManagerFactory implements EntityManagerFactory {
+
+    private boolean open = true;
+
+    public EntityManager createEntityManager() {
+        if (!open) {
+            throw new IllegalStateException("closed");
+        }
+        return new MockEntityManager();
+    }
+
+    public EntityManager createEntityManager(Map map) {
+        if (!open) {
+            throw new IllegalStateException("closed");
+        }
+        return new MockEntityManager(map);
+    }
+
+    public void close() {
+        open = false;
+    }
+
+    public boolean isOpen() {
+        return open;
+    }
+}

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManagerFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManagerFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/jee5-jta/transaction/src/test/org/apache/geronimo/transaction/mockjpa/MockEntityManagerFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message