Source Code Cross Referenced for TestContainerTransactionBase.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » tests » transaction » containermanaged » base » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » ow2 easybeans » org.ow2.easybeans.tests.transaction.containermanaged.base 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006 Bull S.A.S.
004:         * Contact: easybeans@ow2.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: TestContainerTransactionBase.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.tests.transaction.containermanaged.base;
025:
026:        import java.sql.SQLException;
027:
028:        import javax.naming.NamingException;
029:        import javax.transaction.Status;
030:        import javax.transaction.UserTransaction;
031:
032:        import org.ow2.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction;
033:        import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager;
034:        import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager;
035:        import org.ow2.easybeans.tests.common.exception.TransactionException;
036:        import org.ow2.easybeans.tests.common.helper.EJBHelper;
037:        import org.ow2.easybeans.tests.common.helper.EmbeddedHelper;
038:        import org.ow2.easybeans.tests.common.helper.TransactionHelper;
039:        import org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged;
040:        import org.ow2.util.log.Log;
041:        import org.ow2.util.log.LogFactory;
042:
043:        /**
044:         * Contains all base methods for using during the tests about the
045:         * container-managed.
046:         * @author Gisele Pinheiro Souza
047:         * @author Eduardo Studzinski Estima de Castro
048:         */
049:        public class TestContainerTransactionBase implements 
050:                ItfTestContainerManaged {
051:
052:            /**
053:             * Constant that defines the first database name.
054:             */
055:            protected static final String DATABASE_1 = "jdbc_1";
056:
057:            /**
058:             * Constant that defines the second database name.
059:             */
060:            protected static final String DATABASE_2 = "jdbc_2";
061:
062:            /**
063:             * Bean used during the tests.
064:             */
065:            private ItfContainerTransaction sfsbContainerTransaction = null;
066:
067:            /**
068:             * Bean used to manage the database in the server side.
069:             */
070:            private ItfDatabaseManager slsbDatabaseManager;
071:
072:            /**
073:             * Logger.
074:             */
075:            private static Log logger = LogFactory
076:                    .getLog(TestContainerTransactionBase.class);
077:
078:            /**
079:             * Verifies if the table was created in the database.
080:             * @param database the database name.
081:             * @param tableName the table name verified.
082:             * @throws NamingException if a lookup error occurs.
083:             * @throws SQLException if a database error occurs.
084:             */
085:            protected void verifyTable(final String database,
086:                    final String tableName) throws NamingException,
087:                    SQLException {
088:                slsbDatabaseManager.verifyTable(database, tableName);
089:            }
090:
091:            /**
092:             * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#setup()
093:             * @throws Exception if a database error occurs.
094:             */
095:            public void setup() throws Exception {
096:                // Inserts all database before execute the test
097:                // used because the container does not provide this feature yet
098:                EmbeddedHelper.bindDatasource();
099:                // creates the bean used to manages the databse in the server site.
100:                slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(
101:                        SLSBDatabaseManager.class, ItfDatabaseManager.class);
102:                //cleans the transaction active in other test
103:                cleanTransaction();
104:            }
105:
106:            /**
107:             * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#createBean(java.lang.Class)
108:             * @param beanClass the bean class name used to make the lookup.
109:             * @throws Exception if a lookup error occurs.
110:             */
111:            public void createBean(final Class beanClass) throws Exception {
112:                sfsbContainerTransaction = EJBHelper.getBeanRemoteInstance(
113:                        beanClass, ItfContainerTransaction.class);
114:            }
115:
116:            /**
117:             * Creates a transaction, executes a method in the bean that creates a
118:             * database and after forces a rollback. The specification says that the
119:             * bean and the client, in this case, must to use the same transaction. So,
120:             * the rollback must to be done in the bean also.
121:             * @throws Exception if an error during the transaction occurs.
122:             */
123:            private void executeErrorTransaction() throws Exception {
124:                UserTransaction utx = getUserTransaction();
125:                // starts the transaction
126:                utx.begin();
127:                // call the method that must have the same transaction context
128:                sfsbContainerTransaction.insertCorrectTableInBothDB(DATABASE_1,
129:                        DATABASE_2);
130:                // rollback the transaction
131:                utx.rollback();
132:            }
133:
134:            /**
135:             * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#testSetRollbackOnly()
136:             * @throws Exception if an error during the tests occurs.
137:             */
138:            public void testSetRollbackOnly() throws Exception {
139:                // calls the setRollbackOnly
140:                try {
141:                    sfsbContainerTransaction.setRollbackOnly(DATABASE_1,
142:                            DATABASE_2);
143:                } catch (TransactionException e) {
144:                    throw e.getParentException();
145:                }
146:                /*
147:                 * Verifies if the tables were inserted. The transaction attributes,
148:                 * that supports the setRollbackOnly, must to make a roll back when the
149:                 * method invocation completes.
150:                 */
151:                verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
152:                verifyTable(DATABASE_2, ItfContainerTransaction.TABLE);
153:            }
154:
155:            /**
156:             * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged
157:             * @throws Exception if an error during the tests occurs.
158:             */
159:            public void testGetRollbackOnly() throws Exception {
160:                try {
161:                    sfsbContainerTransaction.getRollbackOnly();
162:                } catch (TransactionException e) {
163:                    throw e.getParentException();
164:                }
165:            }
166:
167:            /**
168:             * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged#testUsingClientTrans()
169:             * @throws Exception if an error during the tests occurs.
170:             */
171:            public void testUsingClientTrans() throws Exception {
172:                executeErrorTransaction();
173:
174:                // verifies if the table was created in the DATABASE_1
175:                verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
176:                // verifies if the table was created in the DATABASE_2
177:                verifyTable(DATABASE_2, ItfContainerTransaction.TABLE);
178:            }
179:
180:            /**
181:             * @see org.ow2.easybeans.tests.common.interfaces.ItfTestContainerManaged
182:             */
183:            public void deleteTable() {
184:                // deletes the table after each test to avoid errors.
185:                deleteTable(DATABASE_1, ItfContainerTransaction.TABLE);
186:                deleteTable(DATABASE_2, ItfContainerTransaction.TABLE);
187:            }
188:
189:            /**
190:             * Deletes the table in the database.
191:             * @param dbName the database name in the registry.
192:             * @param tableName the table name.
193:             */
194:            protected void deleteTable(final String dbName,
195:                    final String tableName) {
196:                // deletes the table after each test to avoid errors.
197:                try {
198:                    slsbDatabaseManager.deleteTable(dbName, tableName);
199:                } catch (SQLException e) {
200:                    logger
201:                            .debug(
202:                                    "The table delete threw an error during the execution {0}",
203:                                    e);
204:                } catch (NamingException e) {
205:                    logger
206:                            .debug(
207:                                    "The table delete threw an error during the execution {0}",
208:                                    e);
209:                }
210:            }
211:
212:            /**
213:             * Returns the bean used during the tests.
214:             * @return the bean.
215:             */
216:            protected ItfContainerTransaction getBean() {
217:                return sfsbContainerTransaction;
218:            }
219:
220:            /**
221:             * Verifies if the container throws an IllegalStateException when a bean with
222:             * container-managed transaction tries to get an UserTransaction. The bean
223:             * gets the UserTransaction by using a EJBContext.
224:             * @input -
225:             * @output an IllegalStateException.
226:             * @throws Exception if an error during the tests occurs.
227:             */
228:            public void testGetUserTransactionWithLookup() throws Exception {
229:                try {
230:                    sfsbContainerTransaction.getUserTransactionWithLookup();
231:                } catch (TransactionException e) {
232:                    throw e.getParentException();
233:                }
234:            }
235:
236:            /**
237:             * Verifies if the container throws a NameNotFoundException when a bean with
238:             * container-managed transaction tries to get an UserTransaction. The bean
239:             * gets the UserTransaction by using a lookup.
240:             * @input -
241:             * @output an IllegalStateException.
242:             * @throws Exception if an error during the tests occurs.
243:             */
244:            public void testGetUserTransactionWithEJBContext() throws Exception {
245:                try {
246:                    sfsbContainerTransaction.getUserTransactionWithEJBContext();
247:                } catch (TransactionException e) {
248:                    throw e.getParentException();
249:                }
250:            }
251:
252:            /**
253:             * Gets a new transaction.
254:             * @return the class UserTransaction.
255:             * @throws Exception if a lookup error occurs.
256:             */
257:            protected UserTransaction getUserTransaction() throws Exception {
258:                UserTransaction utx = TransactionHelper
259:                        .getInternalUserTransaction();
260:                logger.debug("Transaction status after get = {0} ",
261:                        new Integer(utx.getStatus()));
262:                return utx;
263:            }
264:
265:            /**
266:             * Makes a rollback in the transaction that is active.
267:             * @throws Exception if an error occurs during the rollback.
268:             */
269:            public void cleanTransaction() throws Exception {
270:                UserTransaction utx = TransactionHelper
271:                        .getInternalUserTransaction();
272:                try {
273:                    if (transactionIsActive()) {
274:                        utx.rollback();
275:                    }
276:                } catch (Exception e) {
277:                    throw new Exception(
278:                            "Cannot clean the transaction. The test cannot be started",
279:                            e);
280:                }
281:            }
282:
283:            /**
284:             * Verifies if the transaction in the client side is active.
285:             * @return true if the transaction is active, false otherwise.
286:             * @throws Exception id a lookup error occurs
287:             */
288:            protected boolean transactionIsActive() throws Exception {
289:                UserTransaction utx = TransactionHelper
290:                        .getInternalUserTransaction();
291:                boolean bolResult = false;
292:                if (utx != null) {
293:                    if (utx.getStatus() == Status.STATUS_ACTIVE) {
294:                        bolResult = true;
295:                    }
296:                }
297:                return bolResult;
298:            }
299:
300:        }
w___ww_.___ja_v__a__2___s.___c__om___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.