From d5f6b8aecee8d40d8d6b1896720d905d093ccc2b Mon Sep 17 00:00:00 2001 From: Konstantin Krivopustov Date: Fri, 10 Feb 2012 09:21:29 +0000 Subject: [PATCH] Declarative transaction support --- modules/core/src/cuba-spring.xml | 7 +++- ...cServiceTest.java => DataServiceTest.java} | 2 +- .../haulmont/cuba/core/TransactionTest.java | 35 ++++++++++++------- 3 files changed, 30 insertions(+), 14 deletions(-) rename modules/core/test/com/haulmont/cuba/core/{BasicServiceTest.java => DataServiceTest.java} (94%) diff --git a/modules/core/src/cuba-spring.xml b/modules/core/src/cuba-spring.xml index 6e3f2f234c..1859ba2711 100644 --- a/modules/core/src/cuba-spring.xml +++ b/modules/core/src/cuba-spring.xml @@ -4,10 +4,13 @@ xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" + xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd - http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> + http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd + http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd + "> @@ -32,6 +35,8 @@ + + diff --git a/modules/core/test/com/haulmont/cuba/core/BasicServiceTest.java b/modules/core/test/com/haulmont/cuba/core/DataServiceTest.java similarity index 94% rename from modules/core/test/com/haulmont/cuba/core/BasicServiceTest.java rename to modules/core/test/com/haulmont/cuba/core/DataServiceTest.java index 3588b0f073..75dad50c16 100644 --- a/modules/core/test/com/haulmont/cuba/core/BasicServiceTest.java +++ b/modules/core/test/com/haulmont/cuba/core/DataServiceTest.java @@ -21,7 +21,7 @@ import java.util.Collections; import java.util.List; import java.util.UUID; -public class BasicServiceTest extends CubaTestCase +public class DataServiceTest extends CubaTestCase { public void test() { DataService bs = Locator.lookup(DataService.NAME); diff --git a/modules/core/test/com/haulmont/cuba/core/TransactionTest.java b/modules/core/test/com/haulmont/cuba/core/TransactionTest.java index 58a1bbe64d..333cb55d09 100644 --- a/modules/core/test/com/haulmont/cuba/core/TransactionTest.java +++ b/modules/core/test/com/haulmont/cuba/core/TransactionTest.java @@ -19,9 +19,18 @@ public class TransactionTest extends CubaTestCase { private static final String TEST_EXCEPTION_MSG = "test exception"; + public void testNoTransaction() { + try { + EntityManager em = PersistenceProvider.getEntityManager(); + fail(); + } catch (Exception e) { + assertTrue(e instanceof IllegalStateException); + } + } + public void testCommit() { UUID id; - Transaction tx = Locator.createTransaction(); + Transaction tx = PersistenceProvider.createTransaction(); try { EntityManager em = PersistenceProvider.getEntityManager(); assertNotNull(em); @@ -37,7 +46,7 @@ public class TransactionTest extends CubaTestCase tx.end(); } - tx = Locator.createTransaction(); + tx = PersistenceProvider.createTransaction(); try { EntityManager em = PersistenceProvider.getEntityManager(); Server server = em.find(Server.class, id); @@ -52,7 +61,7 @@ public class TransactionTest extends CubaTestCase public void testCommitRetaining() { UUID id; - Transaction tx = Locator.createTransaction(); + Transaction tx = PersistenceProvider.createTransaction(); try { EntityManager em = PersistenceProvider.getEntityManager(); assertNotNull(em); @@ -86,7 +95,7 @@ public class TransactionTest extends CubaTestCase } private void __testRollback() { - Transaction tx = Locator.createTransaction(); + Transaction tx = PersistenceProvider.createTransaction(); try { EntityManager em = PersistenceProvider.getEntityManager(); assertNotNull(em); @@ -112,7 +121,7 @@ public class TransactionTest extends CubaTestCase } private void __testRollbackAndCatch() { - Transaction tx = Locator.createTransaction(); + Transaction tx = PersistenceProvider.createTransaction(); try { EntityManager em = PersistenceProvider.getEntityManager(); assertNotNull(em); @@ -142,7 +151,7 @@ public class TransactionTest extends CubaTestCase private void __testCommitRetainingAndRollback() { UUID id; - Transaction tx = Locator.createTransaction(); + Transaction tx = PersistenceProvider.createTransaction(); try { EntityManager em = PersistenceProvider.getEntityManager(); assertNotNull(em); @@ -170,10 +179,10 @@ public class TransactionTest extends CubaTestCase public void testNestedRollback() { try { - Transaction tx = Locator.createTransaction(); + Transaction tx = PersistenceProvider.createTransaction(); try { - Transaction tx1 = Locator.getTransaction(); + Transaction tx1 = PersistenceProvider.getTransaction(); try { throwException(); fail(); @@ -195,7 +204,7 @@ public class TransactionTest extends CubaTestCase } public void testSuspend() { - Transaction tx = Locator.getTransaction(); + Transaction tx = PersistenceProvider.getTransaction(); try { EntityManager em = PersistenceProvider.getEntityManager(); Server server = new Server(); @@ -204,7 +213,7 @@ public class TransactionTest extends CubaTestCase server.setRunning(true); em.persist(server); - Transaction tx1 = Locator.createTransaction(); + Transaction tx1 = PersistenceProvider.createTransaction(); try { EntityManager em1 = PersistenceProvider.getEntityManager(); assertTrue(em != em1); @@ -225,7 +234,7 @@ public class TransactionTest extends CubaTestCase } public void testSuspendRollback() { - Transaction tx = Locator.getTransaction(); + Transaction tx = PersistenceProvider.getTransaction(); try { EntityManager em = PersistenceProvider.getEntityManager(); Server server = new Server(); @@ -234,10 +243,12 @@ public class TransactionTest extends CubaTestCase server.setRunning(true); em.persist(server); - Transaction tx1 = Locator.createTransaction(); + Transaction tx1 = PersistenceProvider.createTransaction(); try { EntityManager em1 = PersistenceProvider.getEntityManager(); assertTrue(em != em1); + Server server1 = em1.find(Server.class, server.getId()); + assertNull(server1); throwException(); tx1.commit(); } catch (Exception e) {