Commit a99f52a3aa54a3c355d84960150214324a3891bf

Authored by Luciano Borges
1 parent d457968e
Exists in master

Teste de JDBC Transaction

impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/transaction/JDBCTransaction.java
... ... @@ -47,7 +47,6 @@ import br.gov.frameworkdemoiselle.internal.producer.ConnectionProducer;
47 47 import br.gov.frameworkdemoiselle.internal.producer.ConnectionProducer.Status;
48 48 import br.gov.frameworkdemoiselle.util.Beans;
49 49  
50   -
51 50 /**
52 51 * Represents the strategy destinated to manage JDBC transactions.
53 52 *
... ... @@ -61,22 +60,9 @@ public class JDBCTransaction implements Transaction {
61 60  
62 61 private ConnectionProducer producer;
63 62  
64   - //private Map<Connection, Status> cache = Collections.synchronizedMap(new HashMap<Connection, Status>());
65   - //private List<ConnectionProxy> cache = Collections.synchronizedList(new ArrayList<ConnectionProxy>());
66   -
67 63 private ConnectionProducer getProducer() {
68 64 if (producer == null) {
69 65 producer = Beans.getReference(ConnectionProducer.class);
70   -
71   - /*for (Connection connection : producer.getCache().values()) {
72   - if (!ConnectionProxy.class.isInstance(connection)) {
73   - continue;
74   - }
75   -
76   - if (!cache.contains(connection)) {
77   - cache.add((ConnectionProxy)connection);
78   - }
79   - }*/
80 66 }
81 67  
82 68 return producer;
... ... @@ -101,7 +87,7 @@ public class JDBCTransaction implements Transaction {
101 87 @Override
102 88 public void commit() {
103 89 Status status;
104   -
  90 +
105 91 for (Connection connection : getDelegate()) {
106 92 try {
107 93 connection.commit();
... ... @@ -119,7 +105,7 @@ public class JDBCTransaction implements Transaction {
119 105 @Override
120 106 public void rollback() {
121 107 Status status;
122   -
  108 +
123 109 for (Connection connection : getDelegate()) {
124 110 try {
125 111 connection.rollback();
... ... @@ -165,29 +151,4 @@ public class JDBCTransaction implements Transaction {
165 151  
166 152 return result;
167 153 }
168   -
169   - /*private static class Status implements Serializable {
170   -
171   - private static final long serialVersionUID = 1L;
172   -
173   - private boolean active = false;
174   -
175   - private boolean markedRollback = false;
176   -
177   - public boolean isActive() {
178   - return active;
179   - }
180   -
181   - public void setActive(boolean active) {
182   - this.active = active;
183   - }
184   -
185   - public boolean isMarkedRollback() {
186   - return markedRollback;
187   - }
188   -
189   - public void setRollbackOnly(boolean markedRollback) {
190   - this.markedRollback = markedRollback;
191   - }
192   - }*/
193 154 }
... ...
impl/extension/jdbc/src/test/java/connection/producer/ConnectionProducerNoConnectionDriverTest.java
... ... @@ -44,6 +44,7 @@ import javax.inject.Inject;
44 44 import org.jboss.arquillian.container.test.api.Deployment;
45 45 import org.jboss.arquillian.junit.Arquillian;
46 46 import org.jboss.shrinkwrap.api.spec.WebArchive;
  47 +import org.junit.Ignore;
47 48 import org.junit.Test;
48 49 import org.junit.runner.RunWith;
49 50  
... ... @@ -67,7 +68,7 @@ public class ConnectionProducerNoConnectionDriverTest {
67 68 }
68 69  
69 70 /**
70   - * TODO Refinar a exceção esperada
  71 + * TODO Refinar a exceção esperada
71 72 */
72 73 @Test(expected = Exception.class)
73 74 public void failOnCreateConnection() {
... ...
impl/extension/jdbc/src/test/java/connection/producer/ConnectionProducerNoConnectionUrlTest.java
... ... @@ -44,6 +44,7 @@ import javax.inject.Inject;
44 44 import org.jboss.arquillian.container.test.api.Deployment;
45 45 import org.jboss.arquillian.junit.Arquillian;
46 46 import org.jboss.shrinkwrap.api.spec.WebArchive;
  47 +import org.junit.Ignore;
47 48 import org.junit.Test;
48 49 import org.junit.runner.RunWith;
49 50  
... ...
impl/extension/jdbc/src/test/java/connection/producer/ConnectionProducerWithJndiTest.java
... ... @@ -46,6 +46,7 @@ import javax.inject.Inject;
46 46 import org.jboss.arquillian.container.test.api.Deployment;
47 47 import org.jboss.arquillian.junit.Arquillian;
48 48 import org.jboss.shrinkwrap.api.spec.WebArchive;
  49 +import org.junit.Ignore;
49 50 import org.junit.Test;
50 51 import org.junit.runner.RunWith;
51 52  
... ... @@ -73,4 +74,4 @@ public class ConnectionProducerWithJndiTest {
73 74 assertEquals(conn1.getMetaData().getURL(), "jdbc:derby:target/databases/derby");
74 75 }
75 76  
76   -}
77 77 \ No newline at end of file
  78 +}
... ...
impl/extension/jdbc/src/test/java/connection/producer/ConnectionProducerWithNameTest.java
... ... @@ -45,6 +45,7 @@ import javax.inject.Inject;
45 45 import org.jboss.arquillian.container.test.api.Deployment;
46 46 import org.jboss.arquillian.junit.Arquillian;
47 47 import org.jboss.shrinkwrap.api.spec.WebArchive;
  48 +import org.junit.Ignore;
48 49 import org.junit.Test;
49 50 import org.junit.runner.RunWith;
50 51  
... ...
impl/extension/jdbc/src/test/java/connection/producer/ConnectionProducerWithoutJndiTest.java
... ... @@ -45,6 +45,7 @@ import javax.inject.Inject;
45 45 import org.jboss.arquillian.container.test.api.Deployment;
46 46 import org.jboss.arquillian.junit.Arquillian;
47 47 import org.jboss.shrinkwrap.api.spec.WebArchive;
  48 +import org.junit.Ignore;
48 49 import org.junit.Test;
49 50 import org.junit.runner.RunWith;
50 51  
... ... @@ -67,9 +68,9 @@ public class ConnectionProducerWithoutJndiTest {
67 68 return deployment;
68 69 }
69 70  
70   - @Test(expected=Exception.class)
  71 + @Test(expected = Exception.class)
71 72 public void createConnection() throws SQLException {
72 73 conn1.get();
73 74 }
74 75  
75   -}
76 76 \ No newline at end of file
  77 +}
... ...
impl/extension/jdbc/src/test/java/connection/producer/ConnectionProducerWithoutNameTest.java
... ... @@ -45,6 +45,7 @@ import javax.inject.Inject;
45 45 import org.jboss.arquillian.container.test.api.Deployment;
46 46 import org.jboss.arquillian.junit.Arquillian;
47 47 import org.jboss.shrinkwrap.api.spec.WebArchive;
  48 +import org.junit.Ignore;
48 49 import org.junit.Test;
49 50 import org.junit.runner.RunWith;
50 51  
... ...
impl/extension/jdbc/src/test/java/transaction/DDL.java
  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 + */
1 37 package transaction;
2 38  
3 39 import java.sql.Connection;
... ...
impl/extension/jdbc/src/test/java/transaction/MyEntity1.java
  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 + */
1 37 package transaction;
2 38  
3   -
4 39 public class MyEntity1 {
5 40  
6 41 private int id;
... ...
impl/extension/jdbc/src/test/java/transaction/TransactionTest.java
... ... @@ -37,7 +37,6 @@
37 37  
38 38 package transaction;
39 39  
40   -import javax.enterprise.context.RequestScoped;
41 40 import javax.inject.Inject;
42 41  
43 42 import junit.framework.Assert;
... ... @@ -45,14 +44,14 @@ import junit.framework.Assert;
45 44 import org.jboss.arquillian.container.test.api.Deployment;
46 45 import org.jboss.arquillian.junit.Arquillian;
47 46 import org.jboss.shrinkwrap.api.spec.WebArchive;
48   -import org.junit.After;
49 47 import org.junit.Before;
50 48 import org.junit.Test;
51 49 import org.junit.runner.RunWith;
52 50  
53 51 import test.Tests;
54   -import br.gov.frameworkdemoiselle.internal.context.ContextManager;
55   -import br.gov.frameworkdemoiselle.internal.context.ManagedContext;
  52 +import br.gov.frameworkdemoiselle.transaction.JDBCTransaction;
  53 +import br.gov.frameworkdemoiselle.transaction.Transaction;
  54 +import br.gov.frameworkdemoiselle.transaction.TransactionContext;
56 55  
57 56 @RunWith(Arquillian.class)
58 57 public class TransactionTest {
... ... @@ -61,52 +60,77 @@ public class TransactionTest {
61 60  
62 61 @Inject
63 62 private TransactionalBusiness tb;
64   -
  63 +
  64 + private Transaction transaction;
  65 +
  66 + @Inject
  67 + private TransactionContext context;
  68 +
  69 + @Inject
  70 + private DDL ddl;
  71 +
65 72 @Deployment
66 73 public static WebArchive createDeployment() {
67 74 WebArchive deployment = Tests.createDeployment(TransactionTest.class);
68 75 deployment.addAsResource(Tests.createFileAsset(PATH + "/demoiselle.properties"), "demoiselle.properties");
69 76 return deployment;
70 77 }
71   -
72   -// @Before
73   -// public void init() throws Exception{
74   -//// transaction = context.getCurrentTransaction();
75   -//// ddl.dropAndCreate();
76   -// }
77   -
78   -// @Before
79   -// public void activeContext() {
80   -// ContextManager.activate(ManagedContext.class, RequestScoped.class);
81   -// }
82   -//
83   -// @After
84   -// public void deactiveContext() {
85   -// ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
86   -// }
87   -
  78 +
  79 + @Before
  80 + public void init() throws Exception {
  81 + transaction = context.getCurrentTransaction();
  82 + ddl.dropAndCreate();
  83 + transaction.commit();
  84 + }
  85 +
88 86 @Test
89   - public void isTransactionActiveWithInterceptor(){
  87 + public void isTransactionActiveWithInterceptor() {
90 88 Assert.assertTrue(tb.isTransactionActiveWithInterceptor());
91 89 }
92   -
  90 +
93 91 @Test
94   - public void isTransactionActiveWithoutInterceptor(){
  92 + public void isTransactionActiveWithoutInterceptor() {
95 93 Assert.assertFalse(tb.isTransactionActiveWithoutInterceptor());
96 94 }
97 95  
98   -// @Test
99   -// public void verifyIfTransactionIsJdbcTransaction() {
100   -// assertEquals(transaction.getClass(), JDBCTransaction.class);
101   -// }
102   -//
103   -// @Test
104   -// public void verifyIfTransactionIsActive() {
105   -// assertTrue(!transaction.isActive());
106   -// transaction.begin();
107   -// assertTrue(transaction.isActive());
108   -// }
109   -
  96 + @Test
  97 + public void verifyIfTransactionIsJdbcTransaction() {
  98 + Assert.assertEquals(transaction.getClass(), JDBCTransaction.class);
  99 + }
  100 +
  101 + @Test
  102 + public void verifyIfTransactionIsActive() {
  103 + Assert.assertTrue(!transaction.isActive());
  104 + transaction.begin();
  105 + Assert.assertTrue(transaction.isActive());
  106 + }
110 107  
  108 + @Test
  109 + public void commitWithSuccess() throws Exception{
  110 +
  111 + MyEntity1 m1 = new MyEntity1();
  112 + m1.setId(1);
  113 + m1.setDescription("desc-1");
  114 +
  115 + tb.insert(m1);
  116 +
  117 + Assert.assertEquals("desc-1", tb.find(m1.getId()).getDescription());
  118 +
  119 + tb.delete(m1);
  120 +
  121 + Assert.assertNull(tb.find(m1.getId()).getDescription());
  122 + }
  123 +
  124 + @Test
  125 + public void rollbackWithSuccess() throws Exception {
  126 + try{
  127 + tb.rollbackWithSuccess();
  128 + } catch (Exception e) {
  129 + Assert.assertEquals("Exceção criada para marcar transação para rollback", e.getMessage());
  130 + }
  131 + finally{
  132 + MyEntity1 m1 = tb.find(3);
  133 + Assert.assertNull(tb.find(m1.getId()).getDescription());
  134 + }
  135 + }
111 136 }
112   -
113 137 \ No newline at end of file
... ...
impl/extension/jdbc/src/test/java/transaction/TransactionalBusiness.java
  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 + */
1 37 package transaction;
2 38  
3 39 import java.sql.Connection;
... ... @@ -13,51 +49,63 @@ import br.gov.frameworkdemoiselle.transaction.Transactional;
13 49 public class TransactionalBusiness {
14 50  
15 51 @Inject
16   - private MyEntity1 m1;
17   -
18   - @Inject
19 52 @Name("conn1")
20 53 private Connection conn1;
21   -
  54 +
22 55 @Inject
23 56 private TransactionContext transactionContext;
24   -
  57 +
25 58 @Transactional
26   - public boolean isTransactionActiveWithInterceptor(){
  59 + public boolean isTransactionActiveWithInterceptor() {
27 60 return transactionContext.getCurrentTransaction().isActive();
28 61 }
29   -
30   - public boolean isTransactionActiveWithoutInterceptor(){
  62 +
  63 + public boolean isTransactionActiveWithoutInterceptor() {
31 64 return transactionContext.getCurrentTransaction().isActive();
32 65 }
33   -
  66 +
34 67 @Transactional
35   - public void insert() throws Exception {
36   - String sql = "insert into myentity (id, description) values (1, 'Entidade 1')";
  68 + public void insert(MyEntity1 m) throws Exception {
  69 + String sql = "insert into myentity (id, description) values (" + m.getId() + ", '" + m.getDescription() + "')";
37 70 Statement st = conn1.createStatement();
38 71 st.executeUpdate(sql);
39 72 st.close();
40 73 }
41   -
  74 +
42 75 @Transactional
43   - public void delete() throws Exception {
44   - String sql = "delete from myentity where id = 1";
  76 + public void delete(MyEntity1 m1) throws Exception {
  77 + String sql = "delete from myentity where id = " + m1.getId();
45 78 Statement st = conn1.createStatement();
46 79 st.executeUpdate(sql);
47 80 st.close();
48 81 }
49   -
  82 +
50 83 @Transactional
51 84 public MyEntity1 find(int id) throws Exception {
52   - String sql = "select * from myentity where id = " + id;
  85 + String sql = "select * from myentity where id = " + id;
53 86 Statement st = conn1.createStatement();
54 87 ResultSet rs = st.executeQuery(sql);
55   - rs.next();
56   - m1.setId(rs.getInt(0));
57   - m1.setDescription(rs.getString(1));
  88 +
  89 + MyEntity1 m1 = new MyEntity1();
  90 + while (rs.next()) {
  91 + m1.setId(rs.getInt(1));
  92 + m1.setDescription(rs.getString(2));
  93 + }
  94 +
58 95 rs.close();
59 96 st.close();
  97 +
60 98 return m1;
61   - }
62   -
  99 + }
  100 +
  101 + @Transactional
  102 + public void rollbackWithSuccess() throws Exception {
  103 + MyEntity1 m1 = new MyEntity1();
  104 + m1.setId(3);
  105 +
  106 + this.insert(m1);
  107 +
  108 + throw new Exception("Exceção criada para marcar transação para rollback");
  109 + }
  110 +
63 111 }
... ...