Commit f3b395619bf11ec0574cb77adddb0a69265ab81e
1 parent
a99f52a3
Exists in
master
Adição de testes e refatoração de classes relacionadas aos testes de
transação
Showing
5 changed files
with
115 additions
and
91 deletions
Show diff stats
impl/extension/jdbc/src/test/java/transaction/MyEntity.java
0 → 100644
... | ... | @@ -0,0 +1,60 @@ |
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 | +package transaction; | |
38 | + | |
39 | +public class MyEntity { | |
40 | + | |
41 | + private int id; | |
42 | + | |
43 | + private String description; | |
44 | + | |
45 | + public int getId() { | |
46 | + return id; | |
47 | + } | |
48 | + | |
49 | + public void setId(int id) { | |
50 | + this.id = id; | |
51 | + } | |
52 | + | |
53 | + public String getDescription() { | |
54 | + return description; | |
55 | + } | |
56 | + | |
57 | + public void setDescription(String description) { | |
58 | + this.description = description; | |
59 | + } | |
60 | +} | ... | ... |
impl/extension/jdbc/src/test/java/transaction/MyEntity1.java
... | ... | @@ -1,60 +0,0 @@ |
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 | -package transaction; | |
38 | - | |
39 | -public class MyEntity1 { | |
40 | - | |
41 | - private int id; | |
42 | - | |
43 | - private String description; | |
44 | - | |
45 | - public int getId() { | |
46 | - return id; | |
47 | - } | |
48 | - | |
49 | - public void setId(int id) { | |
50 | - this.id = id; | |
51 | - } | |
52 | - | |
53 | - public String getDescription() { | |
54 | - return description; | |
55 | - } | |
56 | - | |
57 | - public void setDescription(String description) { | |
58 | - this.description = description; | |
59 | - } | |
60 | -} |
impl/extension/jdbc/src/test/java/transaction/TransactionTest.java
... | ... | @@ -37,6 +37,9 @@ |
37 | 37 | |
38 | 38 | package transaction; |
39 | 39 | |
40 | +import java.sql.Connection; | |
41 | +import java.sql.SQLException; | |
42 | + | |
40 | 43 | import javax.inject.Inject; |
41 | 44 | |
42 | 45 | import junit.framework.Assert; |
... | ... | @@ -52,6 +55,8 @@ import test.Tests; |
52 | 55 | import br.gov.frameworkdemoiselle.transaction.JDBCTransaction; |
53 | 56 | import br.gov.frameworkdemoiselle.transaction.Transaction; |
54 | 57 | import br.gov.frameworkdemoiselle.transaction.TransactionContext; |
58 | +import br.gov.frameworkdemoiselle.util.Beans; | |
59 | +import br.gov.frameworkdemoiselle.util.NameQualifier; | |
55 | 60 | |
56 | 61 | @RunWith(Arquillian.class) |
57 | 62 | public class TransactionTest { |
... | ... | @@ -107,18 +112,17 @@ public class TransactionTest { |
107 | 112 | |
108 | 113 | @Test |
109 | 114 | public void commitWithSuccess() throws Exception{ |
115 | + MyEntity m = new MyEntity(); | |
116 | + m.setId(1); | |
117 | + m.setDescription("desc-1"); | |
110 | 118 | |
111 | - MyEntity1 m1 = new MyEntity1(); | |
112 | - m1.setId(1); | |
113 | - m1.setDescription("desc-1"); | |
114 | - | |
115 | - tb.insert(m1); | |
119 | + tb.insert(m); | |
116 | 120 | |
117 | - Assert.assertEquals("desc-1", tb.find(m1.getId()).getDescription()); | |
121 | + Assert.assertEquals("desc-1", tb.find(m.getId()).getDescription()); | |
118 | 122 | |
119 | - tb.delete(m1); | |
123 | + tb.delete(m); | |
120 | 124 | |
121 | - Assert.assertNull(tb.find(m1.getId()).getDescription()); | |
125 | + Assert.assertNull(tb.find(m.getId()).getDescription()); | |
122 | 126 | } |
123 | 127 | |
124 | 128 | @Test |
... | ... | @@ -129,8 +133,22 @@ public class TransactionTest { |
129 | 133 | Assert.assertEquals("Exceção criada para marcar transação para rollback", e.getMessage()); |
130 | 134 | } |
131 | 135 | finally{ |
132 | - MyEntity1 m1 = tb.find(3); | |
133 | - Assert.assertNull(tb.find(m1.getId()).getDescription()); | |
136 | + MyEntity m = tb.find(3); | |
137 | + Assert.assertNull(tb.find(m.getId()).getDescription()); | |
134 | 138 | } |
135 | 139 | } |
140 | + | |
141 | + @Test(expected=SQLException.class) | |
142 | + public void closedConnection() throws Exception{ | |
143 | + MyEntity m = new MyEntity(); | |
144 | + m.setId(1); | |
145 | + m.setDescription("desc-1"); | |
146 | + | |
147 | + tb.insertWithouTransaction(m); | |
148 | + | |
149 | + Connection conn = Beans.getReference(Connection.class, new NameQualifier("conn")); | |
150 | + conn.close(); | |
151 | + | |
152 | + tb.find(m.getId()); | |
153 | + } | |
136 | 154 | } | ... | ... |
impl/extension/jdbc/src/test/java/transaction/TransactionalBusiness.java
... | ... | @@ -49,8 +49,8 @@ import br.gov.frameworkdemoiselle.transaction.Transactional; |
49 | 49 | public class TransactionalBusiness { |
50 | 50 | |
51 | 51 | @Inject |
52 | - @Name("conn1") | |
53 | - private Connection conn1; | |
52 | + @Name("conn") | |
53 | + private Connection conn; | |
54 | 54 | |
55 | 55 | @Inject |
56 | 56 | private TransactionContext transactionContext; |
... | ... | @@ -65,47 +65,53 @@ public class TransactionalBusiness { |
65 | 65 | } |
66 | 66 | |
67 | 67 | @Transactional |
68 | - public void insert(MyEntity1 m) throws Exception { | |
68 | + public void insert(MyEntity m) throws Exception { | |
69 | 69 | String sql = "insert into myentity (id, description) values (" + m.getId() + ", '" + m.getDescription() + "')"; |
70 | - Statement st = conn1.createStatement(); | |
70 | + Statement st = conn.createStatement(); | |
71 | 71 | st.executeUpdate(sql); |
72 | 72 | st.close(); |
73 | 73 | } |
74 | + | |
75 | + public void insertWithouTransaction(MyEntity m) throws Exception { | |
76 | + String sql = "insert into myentity (id, description) values (" + m.getId() + ", '" + m.getDescription() + "')"; | |
77 | + Statement st = conn.createStatement(); | |
78 | + st.executeUpdate(sql); | |
79 | + st.close(); | |
80 | + } | |
81 | + | |
74 | 82 | |
75 | 83 | @Transactional |
76 | - public void delete(MyEntity1 m1) throws Exception { | |
84 | + public void delete(MyEntity m1) throws Exception { | |
77 | 85 | String sql = "delete from myentity where id = " + m1.getId(); |
78 | - Statement st = conn1.createStatement(); | |
86 | + Statement st = conn.createStatement(); | |
79 | 87 | st.executeUpdate(sql); |
80 | 88 | st.close(); |
81 | 89 | } |
82 | 90 | |
83 | - @Transactional | |
84 | - public MyEntity1 find(int id) throws Exception { | |
91 | + public MyEntity find(int id) throws Exception { | |
85 | 92 | String sql = "select * from myentity where id = " + id; |
86 | - Statement st = conn1.createStatement(); | |
93 | + Statement st = conn.createStatement(); | |
87 | 94 | ResultSet rs = st.executeQuery(sql); |
88 | 95 | |
89 | - MyEntity1 m1 = new MyEntity1(); | |
96 | + MyEntity m = new MyEntity(); | |
90 | 97 | while (rs.next()) { |
91 | - m1.setId(rs.getInt(1)); | |
92 | - m1.setDescription(rs.getString(2)); | |
98 | + m.setId(rs.getInt(1)); | |
99 | + m.setDescription(rs.getString(2)); | |
93 | 100 | } |
94 | 101 | |
95 | 102 | rs.close(); |
96 | 103 | st.close(); |
97 | 104 | |
98 | - return m1; | |
105 | + return m; | |
99 | 106 | } |
100 | 107 | |
101 | 108 | @Transactional |
102 | 109 | public void rollbackWithSuccess() throws Exception { |
103 | - MyEntity1 m1 = new MyEntity1(); | |
104 | - m1.setId(3); | |
110 | + MyEntity m = new MyEntity(); | |
111 | + m.setId(3); | |
105 | 112 | |
106 | - this.insert(m1); | |
113 | + this.insert(m); | |
107 | 114 | |
108 | 115 | throw new Exception("Exceção criada para marcar transação para rollback"); |
109 | 116 | } |
110 | - | |
111 | 117 | } | ... | ... |
impl/extension/jdbc/src/test/resources/transaction/demoiselle.properties
... | ... | @@ -33,7 +33,7 @@ |
33 | 33 | # ou escreva para a Fundação do Software Livre (FSF) Inc., |
34 | 34 | # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
35 | 35 | |
36 | -frameworkdemoiselle.persistence.conn1.driver.class=org.hsqldb.jdbcDriver | |
37 | -frameworkdemoiselle.persistence.conn1.url=jdbc:hsqldb:hsql | |
38 | -frameworkdemoiselle.persistence.conn1.username=sa | |
39 | -frameworkdemoiselle.persistence.conn1.password= | |
40 | 36 | \ No newline at end of file |
37 | +frameworkdemoiselle.persistence.conn.driver.class=org.hsqldb.jdbcDriver | |
38 | +frameworkdemoiselle.persistence.conn.url=jdbc:hsqldb:hsql | |
39 | +frameworkdemoiselle.persistence.conn.username=sa | |
40 | +frameworkdemoiselle.persistence.conn.password= | |
41 | 41 | \ No newline at end of file | ... | ... |