KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > transaction > beanmanaged > TestBeanManagedException


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: TestBeanManagedException.java 989 2006-07-31 12:23:28Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.transaction.beanmanaged;
26
27 import static org.testng.Assert.fail;
28
29 import java.sql.SQLException JavaDoc;
30
31 import javax.ejb.EJBException JavaDoc;
32 import javax.ejb.NoSuchEJBException JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37 import org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.ItfBeanManagedException;
38 import org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.SFSBBeanManagedException;
39 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
40 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
41 import org.objectweb.easybeans.tests.common.exception.RollbackAppRuntimeException;
42 import org.objectweb.easybeans.tests.common.helper.EJBHelper;
43 import org.testng.annotations.BeforeMethod;
44 import org.testng.annotations.Test;
45
46 /**
47  * Verifies if the bean-managed transaction and the exceptions are correctly
48  * managed by the container.The item couvered in this test is: 14.3.1(table 15).
49  * @reference JSR 220- FINAL RELEASE
50  * @requirement Application Server must be running; the bean
51  * SFSBBeanManagedException and SLSBDatabaseManager. must be
52  * deployed.
53  * @setup gets the reference of SFSBBeanManagedException and
54  * SLSBDatabaseManager, cleans the database.
55  * @author Gisele Pinheiro Souza
56  * @author Eduardo Studzinski Estima de Castro
57  */

58 public class TestBeanManagedException {
59
60     /**
61      * Bean used during the tests.
62      */

63     private ItfBeanManagedException bean;
64
65     /**
66      * Bean used to manage the database in the server side.
67      */

68     private ItfDatabaseManager slsbDatabaseManager;
69
70     /**
71      * The database used during the tests.
72      */

73     private static final String JavaDoc DATABASE = "jdbc_1";
74
75     /**
76      * Logger.
77      */

78     private static JLog logger = JLogFactory.getLog(TestBeanManagedException.class);
79
80     /**
81      * Creates the bean used during the tests and binds the databases used.
82      * @throws Exception if an error during the test startup occurs.
83      */

84     @BeforeMethod
85     public void setup() throws Exception JavaDoc {
86         // creates the bean used to manages the databse in the server site.
87
slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class);
88
89         //deletes the table
90
deleteTable();
91
92         //creates a bean
93
bean = EJBHelper.getBeanRemoteInstance(SFSBBeanManagedException.class, ItfBeanManagedException.class);
94         bean.startup(DATABASE);
95     }
96
97     /**
98      * Verifies if the container only throws the application exception when it
99      * occurs in a bean-managed exception.
100      * @throws Exception if an error occurs.
101      * @input -
102      * @output the correct method execution
103      */

104     @Test
105     public void insertTableWithAppException() throws Exception JavaDoc {
106         try {
107             bean.insertTableWithAppException();
108             fail("The container did not throw the application exception");
109         } catch (RollbackAppRuntimeException e) {
110             logger.debug("The table delete threw an error during the execution {0}", e);
111         }
112         //The transaction must not be rolled back.
113
slsbDatabaseManager.verifyTable(DATABASE, ItfBeanManagedException.TABLE);
114     }
115
116     /**
117      * Verifies if the container manages a runtime exception in a bean-management transaction. The container must:
118      * <li>Log the exception</li>
119      * <li>Mark the transaction for rollback</li>
120      * <li>Discard the bean</li>
121      * <li>Throw EJBEXception</li>
122      * @throws Exception if an error occurs.
123      */

124     @Test
125     public void insertTableWithRuntimeException() throws Exception JavaDoc {
126         try {
127             bean.insertTableWithRuntimeException();
128             fail("The container did not throw EJBEexception");
129         } catch (EJBException JavaDoc e) {
130             logger.debug("The table delete threw an error during the execution {0}", e);
131         }
132         //verifies if the instance is discarded
133
try {
134             bean.emptyMethod();
135             fail("The instance was not discarded");
136         } catch (NoSuchEJBException JavaDoc e) {
137             logger.debug("The bean threw an expected error during the execution {0}", e);
138         }
139
140         //TODO - verify log
141

142         //verifies if the transaction was rolled back
143
try {
144             slsbDatabaseManager.verifyTable(DATABASE, ItfBeanManagedException.TABLE);
145             fail("The transaction was not rolled back");
146         } catch (SQLException JavaDoc e) {
147             logger.debug("The bean threw an expected error during the execution {0}", e);
148         }
149
150     }
151
152     /**
153      * Deletes the table used during the test.
154      *
155      */

156     public void deleteTable() {
157         // deletes the table after each test to avoid errors.
158
try {
159             // verifies if the table was created
160
slsbDatabaseManager.deleteTable(DATABASE, ItfBeanManagedException.TABLE);
161         } catch (SQLException JavaDoc e) {
162             logger.debug("The table delete threw an error during the execution {0}", e);
163         } catch (NamingException JavaDoc e) {
164             logger.debug("The table delete threw an error during the execution {0}", e);
165         }
166     }
167 }
168
Popular Tags