Source Code Cross Referenced for JdbcAndTransactionTest.java in  » Testing » MockEJB » org » mockejb » test » 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 » Testing » MockEJB » org.mockejb.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.mockejb.test;
002:
003:        import java.util.Collection;
004:
005:        import javax.naming.*;
006:        import com.mockrunner.mock.ejb.MockUserTransaction;
007:
008:        import org.mockejb.*;
009:        import org.mockejb.jndi.*;
010:        import org.mockejb.interceptor.*;
011:
012:        /**
013:         * Demonstrates the use of JDBC and transactions with MockEJB. 
014:         * 
015:         * @author Alexander Ananiev
016:         */
017:        public class JdbcAndTransactionTest extends OptionalCactusTestCase {
018:
019:            // State of this test case. These variables are initialized by setUp method
020:            private SampleService sampleService;
021:            private SampleServiceHome sampleServiceHome;
022:            private Context context;
023:
024:            // Aspect system used by this test
025:            private AspectSystem aspectSystem;
026:
027:            private MockUserTransaction mockTransaction;
028:
029:            /**
030:             * Constructor for JdbcTransactionTest.
031:             * @param name name of the test
032:             */
033:            public JdbcAndTransactionTest(String name) {
034:                super (name);
035:            }
036:
037:            /**
038:             * Deploys and creates EJBs needed for our tests.
039:             */
040:            public void setUp() throws Exception {
041:
042:                aspectSystem = AspectSystemFactory.getAspectSystem();
043:
044:                // inside the app server use its InitialContext
045:                if (isRunningOnServer()) {
046:                    context = new InitialContext();
047:                }
048:                // if the test runs outside of the app server
049:                else {
050:                    MockContextFactory.setAsInitial();
051:                    // create the initial context that will be used for binding EJBs
052:                    context = new InitialContext();
053:                    // Create an instance of the MockContainer
054:                    MockContainer mockContainer = new MockContainer(context);
055:
056:                    /* Create deployment descriptor of our sample bean.
057:                     * MockEjb does not support XML descriptors.
058:                     */
059:                    SessionBeanDescriptor sampleBeanDescriptor = new SessionBeanDescriptor(
060:                            SampleService.JNDI_NAME, SampleServiceHome.class,
061:                            SampleService.class, SampleServiceBean.class);
062:                    // Deploy operation simply creates Home and binds it to JNDI
063:                    mockContainer.deploy(sampleBeanDescriptor);
064:
065:                    // StatelssSampleBean calls SampleHelperBean, so we need to deploy it too
066:
067:                    SessionBeanDescriptor helperBeanDescriptor = new SessionBeanDescriptor(
068:                            SampleServiceBean.HELPER_BEAN_JNDI_NAME,
069:                            SampleHelperHome.class, SampleHelper.class,
070:                            SampleHelperBean.class);
071:                    mockContainer.deploy(helperBeanDescriptor);
072:
073:                    /* Prepare the data source if we are not running in the app server.
074:                     * We assume that the app server has the data source pre-configured.
075:                     * You don't have to do it that way, the code will work in both cases, 
076:                     * however it is "cleaner" to rely on the infrastructure provided by the 
077:                     * app server for in-container testing. Also, the data source inside the 
078:                     * app server may point to a completely different database. 
079:                     */
080:
081:                    // Instantiate DataSource implementation
082:                    // You can use Jakarta DBCP
083:                    //org.apache.commons.dbcp.BasicDataSource ds = new org.apache.commons.dbcp.BasicDataSource();
084:                    //ds.setDriverClassName("com.mysql.jdbc.Driver");
085:                    // You can also use DataSource implementation that comes with MySQL driver
086:                    com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
087:
088:                    // Or, in case of Oracle driver:
089:                    // oracle.jdbc.pool.OracleDataSource ds = oracle.jdbc.pool.OracleDataSource();
090:
091:                    // point to the database - we use the default MySql database.
092:                    ds.setUrl("jdbc:mysql://localhost:3306/test");
093:
094:                    // add to the context
095:                    context.rebind("java:comp/env/jdbc/SampleDataSource", ds);
096:
097:                    // we use MockTransaction outside of the app server            
098:                    mockTransaction = new MockUserTransaction();
099:                    context.rebind("javax.transaction.UserTransaction",
100:                            mockTransaction);
101:                }
102:
103:                // All EJBs are now deployed
104:
105:                // To get the Sample bean we use the standard J2EE routine
106:
107:                // Lookup the home
108:                SampleServiceHome sampleHome = (SampleServiceHome) context
109:                        .lookup(SampleService.JNDI_NAME);
110:
111:                // create the bean
112:                sampleService = sampleHome.create();
113:
114:            }
115:
116:            /**
117:             * Performs the necessary cleanup by restoring the system properties that
118:             * were modified by MockContextFactory.setAsInitial().
119:             * This is needed in case if the test runs inside the container, so it would
120:             * not affect the tests that run after it.  
121:             */
122:            public void tearDown() {
123:
124:                MockContextFactory.revertSetAsInitial();
125:            }
126:
127:            /**
128:             * Tests EJB interaction with the database without transactions, i.e, 
129:             * the transactional policy is "Suppports".
130:             */
131:            public void testJdbc() throws Exception {
132:
133:                /* Read something from the database
134:                 * We assume the existence of the "test_table" with a column "name".
135:                 */
136:                Collection values = sampleService.selectFromTable("test_table",
137:                        "name");
138:                assertNotNull(values);
139:            }
140:
141:            /**
142:             * Demonstrates the use of transactions with MockEJB. 
143:             * Outside of the container, we use MockTransaction, inside we can rely on 
144:             * the real transaction support. Inside the container we can't really test much. 
145:             */
146:            public void testTransactions() throws Exception {
147:
148:                // it does not make sense to run this test on the server since we can't test much 
149:                if (isRunningOnServer()) {
150:                    return;
151:                }
152:
153:                // set our policy
154:                aspectSystem.add(new ClassPatternPointcut("org.mockejb.test"),
155:                        new TransactionManager(TransactionPolicy.REQUIRED));
156:
157:                Collection values = sampleService.selectFromTable("test_table",
158:                        "name");
159:                assertNotNull(values);
160:
161:                assertTrue(mockTransaction.wasBeginCalled());
162:                assertTrue(mockTransaction.wasCommitCalled());
163:
164:                // Call the method that explicitly rolls back the transaction using ejb context        
165:                mockTransaction.reset();
166:
167:                sampleService.rollbackSampleTransaction();
168:
169:                assertTrue(mockTransaction.wasBeginCalled());
170:                assertTrue(mockTransaction.wasRollbackOnlyCalled());
171:                assertTrue(mockTransaction.wasRollbackCalled());
172:
173:                // TODO: test other policies
174:
175:            }
176:
177:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.