Commit a2b0e68e4dd0903193124a9337275771362159a9

Authored by Luciano Borges
1 parent 7b8c96ca
Exists in master

Adicionado novos testes no JDBCTransaction

impl/extension/jdbc/src/test/java/transaction/DDL.java 0 → 100644
... ... @@ -0,0 +1,50 @@
  1 +package transaction;
  2 +
  3 +import java.sql.Connection;
  4 +import java.sql.PreparedStatement;
  5 +import java.sql.Statement;
  6 +
  7 +import javax.inject.Inject;
  8 +
  9 +import br.gov.frameworkdemoiselle.annotation.Name;
  10 +import br.gov.frameworkdemoiselle.transaction.Transactional;
  11 +
  12 +public class DDL {
  13 +
  14 + @Name("conn1")
  15 + @Inject
  16 + private Connection connection;
  17 +
  18 + @Transactional
  19 + public void dropAndCreate() throws Exception {
  20 + dropTable();
  21 + createTable();
  22 + }
  23 +
  24 + private void dropTable() throws Exception {
  25 +
  26 + Statement st = connection.createStatement();
  27 +
  28 + try {
  29 + String sql = "DROP TABLE myentity";
  30 + st.executeUpdate(sql);
  31 + st.close();
  32 + } catch (Exception e) {
  33 +
  34 + }
  35 + }
  36 +
  37 + private void createTable() throws Exception {
  38 + StringBuffer sql = new StringBuffer();
  39 +
  40 + sql.append("CREATE TABLE myentity ( ");
  41 + sql.append(" id int NOT NULL, ");
  42 + sql.append(" description varchar(10) NOT NULL, ");
  43 + sql.append("CONSTRAINT myentity_pk PRIMARY KEY (id) ");
  44 + sql.append("); ");
  45 +
  46 + PreparedStatement pstmt = connection.prepareStatement(sql.toString());
  47 + pstmt.execute();
  48 + pstmt.close();
  49 + }
  50 +}
... ...
impl/extension/jdbc/src/test/java/transaction/MyEntity1.java 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +package transaction;
  2 +
  3 +
  4 +public class MyEntity1 {
  5 +
  6 + private int id;
  7 +
  8 + private String description;
  9 +
  10 + public int getId() {
  11 + return id;
  12 + }
  13 +
  14 + public void setId(int id) {
  15 + this.id = id;
  16 + }
  17 +
  18 + public String getDescription() {
  19 + return description;
  20 + }
  21 +
  22 + public void setDescription(String description) {
  23 + this.description = description;
  24 + }
  25 +}
... ...
impl/extension/jdbc/src/test/java/transaction/TransactionTest.java
... ... @@ -37,21 +37,76 @@
37 37  
38 38 package transaction;
39 39  
  40 +import javax.enterprise.context.RequestScoped;
  41 +import javax.inject.Inject;
  42 +
  43 +import junit.framework.Assert;
  44 +
40 45 import org.jboss.arquillian.container.test.api.Deployment;
41 46 import org.jboss.arquillian.junit.Arquillian;
42 47 import org.jboss.shrinkwrap.api.spec.WebArchive;
  48 +import org.junit.After;
  49 +import org.junit.Before;
  50 +import org.junit.Test;
43 51 import org.junit.runner.RunWith;
44 52  
45 53 import test.Tests;
  54 +import br.gov.frameworkdemoiselle.internal.context.ContextManager;
  55 +import br.gov.frameworkdemoiselle.internal.context.ManagedContext;
46 56  
47 57 @RunWith(Arquillian.class)
48 58 public class TransactionTest {
49 59  
  60 + private static String PATH = "src/test/resources/transaction";
  61 +
  62 + @Inject
  63 + private TransactionalBusiness tb;
50 64  
51 65 @Deployment
52 66 public static WebArchive createDeployment() {
53 67 WebArchive deployment = Tests.createDeployment(TransactionTest.class);
  68 + deployment.addAsResource(Tests.createFileAsset(PATH + "/demoiselle.properties"), "demoiselle.properties");
54 69 return deployment;
55 70 }
56 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 +
  88 + @Test
  89 + public void isTransactionActiveWithInterceptor(){
  90 + Assert.assertTrue(tb.isTransactionActiveWithInterceptor());
  91 + }
  92 +
  93 + @Test
  94 + public void isTransactionActiveWithoutInterceptor(){
  95 + Assert.assertFalse(tb.isTransactionActiveWithoutInterceptor());
  96 + }
  97 +
  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 +
  110 +
57 111 }
  112 +
58 113 \ No newline at end of file
... ...
impl/extension/jdbc/src/test/java/transaction/TransactionalBusiness.java 0 → 100644
... ... @@ -0,0 +1,63 @@
  1 +package transaction;
  2 +
  3 +import java.sql.Connection;
  4 +import java.sql.ResultSet;
  5 +import java.sql.Statement;
  6 +
  7 +import javax.inject.Inject;
  8 +
  9 +import br.gov.frameworkdemoiselle.annotation.Name;
  10 +import br.gov.frameworkdemoiselle.transaction.TransactionContext;
  11 +import br.gov.frameworkdemoiselle.transaction.Transactional;
  12 +
  13 +public class TransactionalBusiness {
  14 +
  15 + @Inject
  16 + private MyEntity1 m1;
  17 +
  18 + @Inject
  19 + @Name("conn1")
  20 + private Connection conn1;
  21 +
  22 + @Inject
  23 + private TransactionContext transactionContext;
  24 +
  25 + @Transactional
  26 + public boolean isTransactionActiveWithInterceptor(){
  27 + return transactionContext.getCurrentTransaction().isActive();
  28 + }
  29 +
  30 + public boolean isTransactionActiveWithoutInterceptor(){
  31 + return transactionContext.getCurrentTransaction().isActive();
  32 + }
  33 +
  34 + @Transactional
  35 + public void insert() throws Exception {
  36 + String sql = "insert into myentity (id, description) values (1, 'Entidade 1')";
  37 + Statement st = conn1.createStatement();
  38 + st.executeUpdate(sql);
  39 + st.close();
  40 + }
  41 +
  42 + @Transactional
  43 + public void delete() throws Exception {
  44 + String sql = "delete from myentity where id = 1";
  45 + Statement st = conn1.createStatement();
  46 + st.executeUpdate(sql);
  47 + st.close();
  48 + }
  49 +
  50 + @Transactional
  51 + public MyEntity1 find(int id) throws Exception {
  52 + String sql = "select * from myentity where id = " + id;
  53 + Statement st = conn1.createStatement();
  54 + ResultSet rs = st.executeQuery(sql);
  55 + rs.next();
  56 + m1.setId(rs.getInt(0));
  57 + m1.setDescription(rs.getString(1));
  58 + rs.close();
  59 + st.close();
  60 + return m1;
  61 + }
  62 +
  63 +}
... ...
impl/extension/jdbc/src/test/resources/transaction/demoiselle.properties
... ... @@ -33,4 +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.jndi.name=jdbc/arquillian
37 36 \ No newline at end of file
  37 +frameworkdemoiselle.persistence.conn1.driver.class=org.hsqldb.jdbcDriver
  38 +frameworkdemoiselle.persistence.conn1.url=jdbc:hsqldb:hsql
  39 +frameworkdemoiselle.persistence.conn1.username=sa
  40 +frameworkdemoiselle.persistence.conn1.password=
38 41 \ No newline at end of file
... ...