cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1417953 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/tx/ test/java/org/apache/cayenne/configuration/server/ test/java/org/apache/cayenne/tx/
Date Thu, 06 Dec 2012 15:43:04 GMT
Author: aadamchik
Date: Thu Dec  6 15:43:02 2012
New Revision: 1417953

URL: http://svn.apache.org/viewvc?rev=1417953&view=rev
Log:
CAY-1778 TransactionManager to simplify user-managed transactions

fixing circular dependency issue (some API change from the last commit)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/DefaultTransactionManager.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/TransactionalOperation.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/tx/DefaultTransactionManagerTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/DefaultTransactionManager.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/DefaultTransactionManager.java?rev=1417953&r1=1417952&r2=1417953&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/DefaultTransactionManager.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/DefaultTransactionManager.java
Thu Dec  6 15:43:02 2012
@@ -19,8 +19,8 @@
 package org.apache.cayenne.tx;
 
 import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.access.Transaction;
-import org.apache.cayenne.configuration.server.ServerRuntime;
 import org.apache.cayenne.di.Inject;
 
 /**
@@ -28,10 +28,10 @@ import org.apache.cayenne.di.Inject;
  */
 public class DefaultTransactionManager implements TransactionManager {
 
-    private ServerRuntime runtime;
+    private DataDomain dataDomain;
 
-    public DefaultTransactionManager(@Inject ServerRuntime runtime) {
-        this.runtime = runtime;
+    public DefaultTransactionManager(@Inject DataDomain dataDomain) {
+        this.dataDomain = dataDomain;
     }
 
     public <T> T performInTransaction(TransactionalOperation<T> op) {
@@ -40,14 +40,14 @@ public class DefaultTransactionManager i
         // commit or roll it back
         Transaction currentTx = Transaction.getThreadTransaction();
         if (currentTx != null) {
-            return op.perform(runtime);
+            return op.perform();
         }
 
         // start a new tx and manage it till the end
-        Transaction tx = runtime.getDataDomain().createTransaction();
+        Transaction tx = dataDomain.createTransaction();
         Transaction.bindThreadTransaction(tx);
         try {
-            return op.perform(runtime);
+            return op.perform();
         } catch (Exception ex) {
             tx.setRollbackOnly();
             throw new CayenneRuntimeException(ex);

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/TransactionalOperation.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/TransactionalOperation.java?rev=1417953&r1=1417952&r2=1417953&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/TransactionalOperation.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tx/TransactionalOperation.java
Thu Dec  6 15:43:02 2012
@@ -18,8 +18,6 @@
  ****************************************************************/
 package org.apache.cayenne.tx;
 
-import org.apache.cayenne.configuration.CayenneRuntime;
-
 /**
  * @since 3.2
  */
@@ -30,5 +28,5 @@ public interface TransactionalOperation<
      * {@link TransactionManager} will wrap this method call in a single
      * thread-bound transaction.
      */
-    T perform(CayenneRuntime runtime);
+    T perform();
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeTest.java?rev=1417953&r1=1417952&r2=1417953&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeTest.java
Thu Dec  6 15:43:02 2012
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.server;
 
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 import java.util.Arrays;
 import java.util.List;
 
@@ -27,6 +30,8 @@ import org.apache.cayenne.DataChannel;
 import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.QueryResponse;
 import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.access.DataDomain;
+import org.apache.cayenne.access.Transaction;
 import org.apache.cayenne.configuration.Constants;
 import org.apache.cayenne.configuration.ObjectContextFactory;
 import org.apache.cayenne.di.Binder;
@@ -36,9 +41,41 @@ import org.apache.cayenne.event.EventMan
 import org.apache.cayenne.graph.GraphDiff;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.query.Query;
+import org.apache.cayenne.tx.TransactionalOperation;
 
 public class ServerRuntimeTest extends TestCase {
 
+    public void testPerformInTransaction() {
+
+        final Transaction tx = mock(Transaction.class);
+        final DataDomain domain = mock(DataDomain.class);
+        when(domain.createTransaction()).thenReturn(tx);
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(DataDomain.class).toInstance(domain);
+            }
+        };
+
+        ServerRuntime runtime = new ServerRuntime("xxxx", module);
+        try {
+
+            final Object expectedResult = new Object();
+            Object result = runtime.performInTransaction(new TransactionalOperation<Object>()
{
+                public Object perform() {
+                    assertSame(tx, Transaction.getThreadTransaction());
+                    return expectedResult;
+                }
+            });
+
+            assertSame(expectedResult, result);
+        } finally {
+            runtime.shutdown();
+        }
+
+    }
+
     public void testDefaultConstructor_SingleLocation() {
         ServerRuntime runtime = new ServerRuntime("xxxx");
 
@@ -55,9 +92,7 @@ public class ServerRuntimeTest extends T
     }
 
     public void testDefaultConstructor_MultipleLocations() {
-        ServerRuntime runtime = new ServerRuntime(new String[] {
-                "xxxx", "yyyy"
-        });
+        ServerRuntime runtime = new ServerRuntime(new String[] { "xxxx", "yyyy" });
 
         List<?> locations = runtime.getInjector().getInstance(
                 Key.get(List.class, Constants.SERVER_PROJECT_LOCATIONS_LIST));
@@ -112,10 +147,7 @@ public class ServerRuntimeTest extends T
                 return null;
             }
 
-            public GraphDiff onSync(
-                    ObjectContext originatingContext,
-                    GraphDiff changes,
-                    int syncType) {
+            public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes,
int syncType) {
                 return null;
             }
         };

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/tx/DefaultTransactionManagerTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/tx/DefaultTransactionManagerTest.java?rev=1417953&r1=1417952&r2=1417953&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/tx/DefaultTransactionManagerTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/tx/DefaultTransactionManagerTest.java
Thu Dec  6 15:43:02 2012
@@ -19,26 +19,27 @@
 package org.apache.cayenne.tx;
 
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
+import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.access.Transaction;
-import org.apache.cayenne.configuration.CayenneRuntime;
-import org.apache.cayenne.configuration.server.ServerRuntime;
-import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.unit.di.server.ServerCase;
 import org.apache.cayenne.unit.di.server.UseServerRuntime;
 
 @UseServerRuntime(ServerCase.TESTMAP_PROJECT)
 public class DefaultTransactionManagerTest extends ServerCase {
 
-    @Inject
-    private ServerRuntime runtime;
-
     public void testPerformInTransaction_NoTx() {
-        DefaultTransactionManager txManager = new DefaultTransactionManager(runtime);
+        
+        final Transaction tx = mock(Transaction.class);
+        final DataDomain domain = mock(DataDomain.class);
+        when(domain.createTransaction()).thenReturn(tx);
+        
+        DefaultTransactionManager txManager = new DefaultTransactionManager(domain);
 
         final Object expectedResult = new Object();
         Object result = txManager.performInTransaction(new TransactionalOperation<Object>()
{
-            public Object perform(CayenneRuntime runtime) {
+            public Object perform() {
                 assertNotNull(Transaction.getThreadTransaction());
                 return expectedResult;
             }
@@ -48,16 +49,21 @@ public class DefaultTransactionManagerTe
     }
 
     public void testPerformInTransaction_ExistingTx() {
-        DefaultTransactionManager txManager = new DefaultTransactionManager(runtime);
+        
+        final Transaction tx1 = mock(Transaction.class);
+        final DataDomain domain = mock(DataDomain.class);
+        when(domain.createTransaction()).thenReturn(tx1);
+        
+        DefaultTransactionManager txManager = new DefaultTransactionManager(domain);
 
-        final Transaction tx = mock(Transaction.class);
-        Transaction.bindThreadTransaction(tx);
+        final Transaction tx2 = mock(Transaction.class);
+        Transaction.bindThreadTransaction(tx2);
         try {
 
             final Object expectedResult = new Object();
             Object result = txManager.performInTransaction(new TransactionalOperation<Object>()
{
-                public Object perform(CayenneRuntime runtime) {
-                    assertSame(tx, Transaction.getThreadTransaction());
+                public Object perform() {
+                    assertSame(tx2, Transaction.getThreadTransaction());
                     return expectedResult;
                 }
             });



Mime
View raw message