From f3b395619bf11ec0574cb77adddb0a69265ab81e Mon Sep 17 00:00:00 2001 From: lusabo Date: Tue, 27 Aug 2013 15:31:24 -0300 Subject: [PATCH] Adição de testes e refatoração de classes relacionadas aos testes de transação --- impl/extension/jdbc/src/test/java/transaction/MyEntity.java | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ impl/extension/jdbc/src/test/java/transaction/MyEntity1.java | 60 ------------------------------------------------------------ impl/extension/jdbc/src/test/java/transaction/TransactionTest.java | 38 ++++++++++++++++++++++++++++---------- impl/extension/jdbc/src/test/java/transaction/TransactionalBusiness.java | 40 +++++++++++++++++++++++----------------- impl/extension/jdbc/src/test/resources/transaction/demoiselle.properties | 8 ++++---- 5 files changed, 115 insertions(+), 91 deletions(-) create mode 100644 impl/extension/jdbc/src/test/java/transaction/MyEntity.java delete mode 100644 impl/extension/jdbc/src/test/java/transaction/MyEntity1.java diff --git a/impl/extension/jdbc/src/test/java/transaction/MyEntity.java b/impl/extension/jdbc/src/test/java/transaction/MyEntity.java new file mode 100644 index 0000000..99b66bd --- /dev/null +++ b/impl/extension/jdbc/src/test/java/transaction/MyEntity.java @@ -0,0 +1,60 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package transaction; + +public class MyEntity { + + private int id; + + private String description; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/impl/extension/jdbc/src/test/java/transaction/MyEntity1.java b/impl/extension/jdbc/src/test/java/transaction/MyEntity1.java deleted file mode 100644 index ae63a57..0000000 --- a/impl/extension/jdbc/src/test/java/transaction/MyEntity1.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package transaction; - -public class MyEntity1 { - - private int id; - - private String description; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } -} diff --git a/impl/extension/jdbc/src/test/java/transaction/TransactionTest.java b/impl/extension/jdbc/src/test/java/transaction/TransactionTest.java index 819e6f1..56b32c4 100644 --- a/impl/extension/jdbc/src/test/java/transaction/TransactionTest.java +++ b/impl/extension/jdbc/src/test/java/transaction/TransactionTest.java @@ -37,6 +37,9 @@ package transaction; +import java.sql.Connection; +import java.sql.SQLException; + import javax.inject.Inject; import junit.framework.Assert; @@ -52,6 +55,8 @@ import test.Tests; import br.gov.frameworkdemoiselle.transaction.JDBCTransaction; import br.gov.frameworkdemoiselle.transaction.Transaction; import br.gov.frameworkdemoiselle.transaction.TransactionContext; +import br.gov.frameworkdemoiselle.util.Beans; +import br.gov.frameworkdemoiselle.util.NameQualifier; @RunWith(Arquillian.class) public class TransactionTest { @@ -107,18 +112,17 @@ public class TransactionTest { @Test public void commitWithSuccess() throws Exception{ + MyEntity m = new MyEntity(); + m.setId(1); + m.setDescription("desc-1"); - MyEntity1 m1 = new MyEntity1(); - m1.setId(1); - m1.setDescription("desc-1"); - - tb.insert(m1); + tb.insert(m); - Assert.assertEquals("desc-1", tb.find(m1.getId()).getDescription()); + Assert.assertEquals("desc-1", tb.find(m.getId()).getDescription()); - tb.delete(m1); + tb.delete(m); - Assert.assertNull(tb.find(m1.getId()).getDescription()); + Assert.assertNull(tb.find(m.getId()).getDescription()); } @Test @@ -129,8 +133,22 @@ public class TransactionTest { Assert.assertEquals("Exceção criada para marcar transação para rollback", e.getMessage()); } finally{ - MyEntity1 m1 = tb.find(3); - Assert.assertNull(tb.find(m1.getId()).getDescription()); + MyEntity m = tb.find(3); + Assert.assertNull(tb.find(m.getId()).getDescription()); } } + + @Test(expected=SQLException.class) + public void closedConnection() throws Exception{ + MyEntity m = new MyEntity(); + m.setId(1); + m.setDescription("desc-1"); + + tb.insertWithouTransaction(m); + + Connection conn = Beans.getReference(Connection.class, new NameQualifier("conn")); + conn.close(); + + tb.find(m.getId()); + } } diff --git a/impl/extension/jdbc/src/test/java/transaction/TransactionalBusiness.java b/impl/extension/jdbc/src/test/java/transaction/TransactionalBusiness.java index a3551bf..a98a64c 100644 --- a/impl/extension/jdbc/src/test/java/transaction/TransactionalBusiness.java +++ b/impl/extension/jdbc/src/test/java/transaction/TransactionalBusiness.java @@ -49,8 +49,8 @@ import br.gov.frameworkdemoiselle.transaction.Transactional; public class TransactionalBusiness { @Inject - @Name("conn1") - private Connection conn1; + @Name("conn") + private Connection conn; @Inject private TransactionContext transactionContext; @@ -65,47 +65,53 @@ public class TransactionalBusiness { } @Transactional - public void insert(MyEntity1 m) throws Exception { + public void insert(MyEntity m) throws Exception { String sql = "insert into myentity (id, description) values (" + m.getId() + ", '" + m.getDescription() + "')"; - Statement st = conn1.createStatement(); + Statement st = conn.createStatement(); st.executeUpdate(sql); st.close(); } + + public void insertWithouTransaction(MyEntity m) throws Exception { + String sql = "insert into myentity (id, description) values (" + m.getId() + ", '" + m.getDescription() + "')"; + Statement st = conn.createStatement(); + st.executeUpdate(sql); + st.close(); + } + @Transactional - public void delete(MyEntity1 m1) throws Exception { + public void delete(MyEntity m1) throws Exception { String sql = "delete from myentity where id = " + m1.getId(); - Statement st = conn1.createStatement(); + Statement st = conn.createStatement(); st.executeUpdate(sql); st.close(); } - @Transactional - public MyEntity1 find(int id) throws Exception { + public MyEntity find(int id) throws Exception { String sql = "select * from myentity where id = " + id; - Statement st = conn1.createStatement(); + Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); - MyEntity1 m1 = new MyEntity1(); + MyEntity m = new MyEntity(); while (rs.next()) { - m1.setId(rs.getInt(1)); - m1.setDescription(rs.getString(2)); + m.setId(rs.getInt(1)); + m.setDescription(rs.getString(2)); } rs.close(); st.close(); - return m1; + return m; } @Transactional public void rollbackWithSuccess() throws Exception { - MyEntity1 m1 = new MyEntity1(); - m1.setId(3); + MyEntity m = new MyEntity(); + m.setId(3); - this.insert(m1); + this.insert(m); throw new Exception("Exceção criada para marcar transação para rollback"); } - } diff --git a/impl/extension/jdbc/src/test/resources/transaction/demoiselle.properties b/impl/extension/jdbc/src/test/resources/transaction/demoiselle.properties index 354503e..4fdb5f3 100644 --- a/impl/extension/jdbc/src/test/resources/transaction/demoiselle.properties +++ b/impl/extension/jdbc/src/test/resources/transaction/demoiselle.properties @@ -33,7 +33,7 @@ # ou escreva para a Fundação do Software Livre (FSF) Inc., # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. -frameworkdemoiselle.persistence.conn1.driver.class=org.hsqldb.jdbcDriver -frameworkdemoiselle.persistence.conn1.url=jdbc:hsqldb:hsql -frameworkdemoiselle.persistence.conn1.username=sa -frameworkdemoiselle.persistence.conn1.password= \ No newline at end of file +frameworkdemoiselle.persistence.conn.driver.class=org.hsqldb.jdbcDriver +frameworkdemoiselle.persistence.conn.url=jdbc:hsqldb:hsql +frameworkdemoiselle.persistence.conn.username=sa +frameworkdemoiselle.persistence.conn.password= \ No newline at end of file -- libgit2 0.21.2