Commit 9b0c4238d5eb35d64c39adec5383fda08aa36112
1 parent
b119a612
Exists in
master
Adicionados testes de transação usando a anotação @Transactional
Showing
7 changed files
with
356 additions
and
1 deletions
Show diff stats
impl/extension/jpa/src/test/java/transaction/interceptor/JPATransactionTest.java
0 → 100644
| ... | ... | @@ -0,0 +1,93 @@ |
| 1 | +package transaction.interceptor; | |
| 2 | + | |
| 3 | +import javax.inject.Inject; | |
| 4 | +import javax.persistence.EntityManager; | |
| 5 | + | |
| 6 | +import junit.framework.Assert; | |
| 7 | + | |
| 8 | +import org.jboss.arquillian.container.test.api.Deployment; | |
| 9 | +import org.jboss.arquillian.junit.Arquillian; | |
| 10 | +import org.jboss.shrinkwrap.api.spec.WebArchive; | |
| 11 | +import org.junit.Before; | |
| 12 | +import org.junit.Test; | |
| 13 | +import org.junit.runner.RunWith; | |
| 14 | + | |
| 15 | +import test.Tests; | |
| 16 | +import br.gov.frameworkdemoiselle.annotation.Name; | |
| 17 | +import br.gov.frameworkdemoiselle.transaction.TransactionContext; | |
| 18 | + | |
| 19 | +@RunWith(Arquillian.class) | |
| 20 | +public class JPATransactionTest { | |
| 21 | + | |
| 22 | + private static final String PATH = "src/test/resources/transaction/interceptor"; | |
| 23 | + | |
| 24 | + @Inject | |
| 25 | + private TransactionalBusiness tb; | |
| 26 | + | |
| 27 | + @Inject | |
| 28 | + @Name("pu1") | |
| 29 | + private EntityManager em1; | |
| 30 | + | |
| 31 | + @Inject | |
| 32 | + @Name("pu2") | |
| 33 | + private EntityManager em2; | |
| 34 | + | |
| 35 | + @Inject | |
| 36 | + private TransactionContext transactionContext; | |
| 37 | + | |
| 38 | + @Deployment | |
| 39 | + public static WebArchive createDeployment() { | |
| 40 | + WebArchive deployment = Tests.createDeployment(JPATransactionTest.class); | |
| 41 | + deployment.addAsResource(Tests.createFileAsset(PATH + "/persistence.xml"), "META-INF/persistence.xml"); | |
| 42 | + | |
| 43 | + return deployment; | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Before | |
| 47 | + public void eraseDatabases(){ | |
| 48 | + transactionContext.getCurrentTransaction().begin(); | |
| 49 | + em1.createQuery("DELETE FROM MyEntity1").executeUpdate(); | |
| 50 | + em2.createQuery("DELETE FROM MyEntity2").executeUpdate(); | |
| 51 | + transactionContext.getCurrentTransaction().commit(); | |
| 52 | + } | |
| 53 | + | |
| 54 | + @Test | |
| 55 | + public void isTransactionActiveWithInterceptor(){ | |
| 56 | + Assert.assertTrue(tb.isTransactionActiveWithInterceptor()); | |
| 57 | + } | |
| 58 | + | |
| 59 | + @Test | |
| 60 | + public void isTransactionActiveWithoutInterceptor(){ | |
| 61 | + Assert.assertFalse(tb.isTransactionActiveWithoutInterceptor()); | |
| 62 | + } | |
| 63 | + | |
| 64 | + @Test | |
| 65 | + public void commitWithSuccess() { | |
| 66 | + | |
| 67 | + tb.commitWithSuccess(); | |
| 68 | + | |
| 69 | + MyEntity1 entity1 = em1.find(MyEntity1.class, tb.createId("id-1")); | |
| 70 | + MyEntity2 entity2 = em2.find(MyEntity2.class, tb.createId("id-2")); | |
| 71 | + | |
| 72 | + Assert.assertEquals("desc-1", entity1.getDescription()); | |
| 73 | + Assert.assertEquals("desc-2", entity2.getDescription()); | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Test | |
| 77 | + public void rollbackWithSuccess() { | |
| 78 | + | |
| 79 | + try{ | |
| 80 | + tb.rollbackWithSuccess(); | |
| 81 | + } catch (Exception e) { | |
| 82 | + Assert.assertEquals("Exceção criada para marcar transação para rollback", e.getMessage()); | |
| 83 | + } | |
| 84 | + finally{ | |
| 85 | + MyEntity1 entity1 = em1.find(MyEntity1.class, tb.createId("id-1")); | |
| 86 | + MyEntity2 entity2 = em2.find(MyEntity2.class, tb.createId("id-2")); | |
| 87 | + | |
| 88 | + Assert.assertNull(entity1); | |
| 89 | + Assert.assertNull(entity2); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | +} | |
| 0 | 94 | \ No newline at end of file | ... | ... |
impl/extension/jpa/src/test/java/transaction/interceptor/MyEntity1.java
0 → 100644
| ... | ... | @@ -0,0 +1,29 @@ |
| 1 | +package transaction.interceptor; | |
| 2 | + | |
| 3 | +import javax.persistence.Entity; | |
| 4 | +import javax.persistence.Id; | |
| 5 | + | |
| 6 | +@Entity | |
| 7 | +public class MyEntity1 { | |
| 8 | + | |
| 9 | + @Id | |
| 10 | + private String id; | |
| 11 | + | |
| 12 | + private String description; | |
| 13 | + | |
| 14 | + public String getId() { | |
| 15 | + return id; | |
| 16 | + } | |
| 17 | + | |
| 18 | + public void setId(String id) { | |
| 19 | + this.id = id; | |
| 20 | + } | |
| 21 | + | |
| 22 | + public String getDescription() { | |
| 23 | + return description; | |
| 24 | + } | |
| 25 | + | |
| 26 | + public void setDescription(String description) { | |
| 27 | + this.description = description; | |
| 28 | + } | |
| 29 | +} | ... | ... |
impl/extension/jpa/src/test/java/transaction/interceptor/MyEntity2.java
0 → 100644
| ... | ... | @@ -0,0 +1,29 @@ |
| 1 | +package transaction.interceptor; | |
| 2 | + | |
| 3 | +import javax.persistence.Entity; | |
| 4 | +import javax.persistence.Id; | |
| 5 | + | |
| 6 | +@Entity | |
| 7 | +public class MyEntity2 { | |
| 8 | + | |
| 9 | + @Id | |
| 10 | + private String id; | |
| 11 | + | |
| 12 | + private String description; | |
| 13 | + | |
| 14 | + public String getId() { | |
| 15 | + return id; | |
| 16 | + } | |
| 17 | + | |
| 18 | + public void setId(String id) { | |
| 19 | + this.id = id; | |
| 20 | + } | |
| 21 | + | |
| 22 | + public String getDescription() { | |
| 23 | + return description; | |
| 24 | + } | |
| 25 | + | |
| 26 | + public void setDescription(String description) { | |
| 27 | + this.description = description; | |
| 28 | + } | |
| 29 | +} | ... | ... |
impl/extension/jpa/src/test/java/transaction/interceptor/TransactionalBusiness.java
0 → 100644
| ... | ... | @@ -0,0 +1,64 @@ |
| 1 | +package transaction.interceptor; | |
| 2 | + | |
| 3 | +import javax.inject.Inject; | |
| 4 | +import javax.persistence.EntityManager; | |
| 5 | + | |
| 6 | +import br.gov.frameworkdemoiselle.annotation.Name; | |
| 7 | +import br.gov.frameworkdemoiselle.transaction.TransactionContext; | |
| 8 | +import br.gov.frameworkdemoiselle.transaction.Transactional; | |
| 9 | + | |
| 10 | +public class TransactionalBusiness { | |
| 11 | + | |
| 12 | + @Inject | |
| 13 | + @Name("pu1") | |
| 14 | + private EntityManager em1; | |
| 15 | + | |
| 16 | + @Inject | |
| 17 | + @Name("pu2") | |
| 18 | + private EntityManager em2; | |
| 19 | + | |
| 20 | + @Inject | |
| 21 | + private TransactionContext transactionContext; | |
| 22 | + | |
| 23 | + @Transactional | |
| 24 | + public boolean isTransactionActiveWithInterceptor(){ | |
| 25 | + return transactionContext.getCurrentTransaction().isActive(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public boolean isTransactionActiveWithoutInterceptor(){ | |
| 29 | + return transactionContext.getCurrentTransaction().isActive(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + @Transactional | |
| 33 | + public void commitWithSuccess() { | |
| 34 | + MyEntity1 entity1 = new MyEntity1(); | |
| 35 | + entity1.setId(createId("id-1")); | |
| 36 | + entity1.setDescription("desc-1"); | |
| 37 | + | |
| 38 | + MyEntity2 entity2 = new MyEntity2(); | |
| 39 | + entity2.setId(createId("id-2")); | |
| 40 | + entity2.setDescription("desc-2"); | |
| 41 | + | |
| 42 | + em1.persist(entity1); | |
| 43 | + em2.persist(entity2); | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Transactional | |
| 47 | + public void rollbackWithSuccess() throws Exception { | |
| 48 | + MyEntity1 entity1 = new MyEntity1(); | |
| 49 | + entity1.setId(createId("id-3")); | |
| 50 | + | |
| 51 | + MyEntity2 entity2 = new MyEntity2(); | |
| 52 | + entity2.setId(createId("id-4")); | |
| 53 | + | |
| 54 | + em1.persist(entity1); | |
| 55 | + em2.persist(entity2); | |
| 56 | + | |
| 57 | + throw new Exception("Exceção criada para marcar transação para rollback"); | |
| 58 | + } | |
| 59 | + | |
| 60 | + String createId(String id) { | |
| 61 | + return this.getClass().getName() + "_" + id; | |
| 62 | + } | |
| 63 | + | |
| 64 | +} | ... | ... |
impl/extension/jpa/src/test/java/transaction/manual/JPATransactionTest.java
| ... | ... | @@ -9,6 +9,8 @@ import javax.inject.Inject; |
| 9 | 9 | import javax.persistence.EntityManager; |
| 10 | 10 | import javax.persistence.TransactionRequiredException; |
| 11 | 11 | |
| 12 | +import junit.framework.Assert; | |
| 13 | + | |
| 12 | 14 | import org.jboss.arquillian.container.test.api.Deployment; |
| 13 | 15 | import org.jboss.arquillian.junit.Arquillian; |
| 14 | 16 | import org.jboss.shrinkwrap.api.spec.WebArchive; |
| ... | ... | @@ -20,6 +22,8 @@ import br.gov.frameworkdemoiselle.annotation.Name; |
| 20 | 22 | import br.gov.frameworkdemoiselle.transaction.JPATransaction; |
| 21 | 23 | import br.gov.frameworkdemoiselle.transaction.Transaction; |
| 22 | 24 | import br.gov.frameworkdemoiselle.transaction.TransactionContext; |
| 25 | +import br.gov.frameworkdemoiselle.util.Beans; | |
| 26 | +import br.gov.frameworkdemoiselle.util.NameQualifier; | |
| 23 | 27 | |
| 24 | 28 | @RunWith(Arquillian.class) |
| 25 | 29 | public class JPATransactionTest { |
| ... | ... | @@ -37,7 +41,7 @@ public class JPATransactionTest { |
| 37 | 41 | @Name("pu2") |
| 38 | 42 | private EntityManager em2; |
| 39 | 43 | |
| 40 | - @Deployment(name = "2") | |
| 44 | + @Deployment | |
| 41 | 45 | public static WebArchive createDeployment() { |
| 42 | 46 | WebArchive deployment = Tests.createDeployment(JPATransactionTest.class); |
| 43 | 47 | deployment.addAsResource(Tests.createFileAsset(PATH + "/persistence.xml"), "META-INF/persistence.xml"); |
| ... | ... | @@ -120,6 +124,33 @@ public class JPATransactionTest { |
| 120 | 124 | assertNull(persisted1); |
| 121 | 125 | assertNull(persisted2); |
| 122 | 126 | } |
| 127 | + | |
| 128 | + @Test | |
| 129 | + public void checkEntityManagerCreatedAfterTransaction(){ | |
| 130 | + Transaction transaction = transactionContext.getCurrentTransaction(); | |
| 131 | + | |
| 132 | + String id = createId("id-5"); | |
| 133 | + MyEntity1 entity1 = new MyEntity1(); | |
| 134 | + entity1.setId(id); | |
| 135 | + entity1.setDescription("Test description"); | |
| 136 | + | |
| 137 | + Assert.assertFalse(transaction.isActive()); | |
| 138 | + transaction.begin(); | |
| 139 | + Assert.assertTrue(transaction.isActive()); | |
| 140 | + | |
| 141 | + EntityManager em1 = Beans.getReference(EntityManager.class, new NameQualifier("pu3")); | |
| 142 | + | |
| 143 | + try{ | |
| 144 | + em1.persist(entity1); | |
| 145 | + transaction.commit(); | |
| 146 | + } | |
| 147 | + catch(TransactionRequiredException te){ | |
| 148 | + Assert.fail("Entity Manager não ingressou em transação já em curso: "+te.getMessage()); | |
| 149 | + } | |
| 150 | + | |
| 151 | + entity1 = em1.find(MyEntity1.class, id); | |
| 152 | + Assert.assertEquals("Test description", entity1.getDescription()); | |
| 153 | + } | |
| 123 | 154 | |
| 124 | 155 | private String createId(String id) { |
| 125 | 156 | return this.getClass().getName() + "_" + id; | ... | ... |
impl/extension/jpa/src/test/resources/transaction/interceptor/persistence.xml
0 → 100644
| ... | ... | @@ -0,0 +1,96 @@ |
| 1 | +<!-- | |
| 2 | + Demoiselle Framework | |
| 3 | + Copyright (C) 2010 SERPRO | |
| 4 | + ============================================================================ | |
| 5 | + This file is part of Demoiselle Framework. | |
| 6 | + | |
| 7 | + Demoiselle Framework is free software; you can redistribute it and/or | |
| 8 | + modify it under the terms of the GNU Lesser General Public License version 3 | |
| 9 | + as published by the Free Software Foundation. | |
| 10 | + | |
| 11 | + This program is distributed in the hope that it will be useful, | |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | + GNU General Public License for more details. | |
| 15 | + | |
| 16 | + You should have received a copy of the GNU Lesser General Public License version 3 | |
| 17 | + along with this program; if not, see <http://www.gnu.org/licenses/> | |
| 18 | + or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
| 19 | + Fifth Floor, Boston, MA 02110-1301, USA. | |
| 20 | + ============================================================================ | |
| 21 | + Este arquivo é parte do Framework Demoiselle. | |
| 22 | + | |
| 23 | + O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
| 24 | + modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
| 25 | + do Software Livre (FSF). | |
| 26 | + | |
| 27 | + Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
| 28 | + GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
| 29 | + APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
| 30 | + para maiores detalhes. | |
| 31 | + | |
| 32 | + Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
| 33 | + "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
| 34 | + ou escreva para a Fundação do Software Livre (FSF) Inc., | |
| 35 | + 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
| 36 | +--> | |
| 37 | +<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 38 | + xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> | |
| 39 | + | |
| 40 | + <!-- | |
| 41 | + <persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL"> | |
| 42 | + <non-jta-data-source>jdbc/arquillian1</non-jta-data-source> | |
| 43 | + | |
| 44 | + <class>transaction.manual.MyEntity1</class> | |
| 45 | + <class>transaction.manual.MyEntity2</class> | |
| 46 | + | |
| 47 | + <properties> | |
| 48 | + <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> | |
| 49 | + <property name="eclipselink.logging.level.sql" value="FINE" /> | |
| 50 | + <property name="eclipselink.logging.parameters" value="true" /> | |
| 51 | + </properties> | |
| 52 | + </persistence-unit> | |
| 53 | + | |
| 54 | + <persistence-unit name="pu2" transaction-type="RESOURCE_LOCAL"> | |
| 55 | + <non-jta-data-source>jdbc/arquillian2</non-jta-data-source> | |
| 56 | + | |
| 57 | + <class>transaction.manual.MyEntity1</class> | |
| 58 | + <class>transaction.manual.MyEntity2</class> | |
| 59 | + | |
| 60 | + <properties> | |
| 61 | + <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> | |
| 62 | + <property name="eclipselink.logging.level.sql" value="FINE" /> | |
| 63 | + <property name="eclipselink.logging.parameters" value="true" /> | |
| 64 | + </properties> | |
| 65 | + </persistence-unit> | |
| 66 | + --> | |
| 67 | + | |
| 68 | + <!-- | |
| 69 | + --> | |
| 70 | + <persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL"> | |
| 71 | + <non-jta-data-source>java:jboss/datasources/ExampleDS</non-jta-data-source> | |
| 72 | + | |
| 73 | + <class>transaction.interceptor.MyEntity1</class> | |
| 74 | + <class>transaction.interceptor.MyEntity2</class> | |
| 75 | + | |
| 76 | + <properties> | |
| 77 | + <property name="hibernate.show_sql" value="true" /> | |
| 78 | + <property name="hibernate.format_sql" value="false" /> | |
| 79 | + <property name="hibernate.hbm2ddl.auto" value="create-drop" /> | |
| 80 | + </properties> | |
| 81 | + </persistence-unit> | |
| 82 | + | |
| 83 | + <persistence-unit name="pu2" transaction-type="RESOURCE_LOCAL"> | |
| 84 | + <non-jta-data-source>java:jboss/datasources/ExampleDS</non-jta-data-source> | |
| 85 | + | |
| 86 | + <class>transaction.interceptor.MyEntity1</class> | |
| 87 | + <class>transaction.interceptor.MyEntity2</class> | |
| 88 | + | |
| 89 | + <properties> | |
| 90 | + <property name="hibernate.show_sql" value="true" /> | |
| 91 | + <property name="hibernate.format_sql" value="false" /> | |
| 92 | + <property name="hibernate.hbm2ddl.auto" value="create-drop" /> | |
| 93 | + </properties> | |
| 94 | + </persistence-unit> | |
| 95 | + | |
| 96 | +</persistence> | |
| 0 | 97 | \ No newline at end of file | ... | ... |
impl/extension/jpa/src/test/resources/transaction/manual/persistence.xml
| ... | ... | @@ -92,5 +92,18 @@ |
| 92 | 92 | <property name="hibernate.hbm2ddl.auto" value="create-drop" /> |
| 93 | 93 | </properties> |
| 94 | 94 | </persistence-unit> |
| 95 | + | |
| 96 | + <persistence-unit name="pu3" transaction-type="RESOURCE_LOCAL"> | |
| 97 | + <non-jta-data-source>java:jboss/datasources/ExampleDS</non-jta-data-source> | |
| 98 | + | |
| 99 | + <class>transaction.manual.MyEntity1</class> | |
| 100 | + <class>transaction.manual.MyEntity2</class> | |
| 101 | + | |
| 102 | + <properties> | |
| 103 | + <property name="hibernate.show_sql" value="true" /> | |
| 104 | + <property name="hibernate.format_sql" value="false" /> | |
| 105 | + <property name="hibernate.hbm2ddl.auto" value="create-drop" /> | |
| 106 | + </properties> | |
| 107 | + </persistence-unit> | |
| 95 | 108 | |
| 96 | 109 | </persistence> |
| 97 | 110 | \ No newline at end of file | ... | ... |