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: }
|