01: package com.mockrunner.example.ejb;
02:
03: import org.mockejb.TransactionPolicy;
04:
05: import com.mockrunner.ejb.EJBTestCaseAdapter;
06: import com.mockrunner.example.ejb.interfaces.UserLoginSession;
07: import com.mockrunner.jdbc.PreparedStatementResultSetHandler;
08: import com.mockrunner.mock.jdbc.MockResultSet;
09:
10: /**
11: * Example test for {@link UserLoginSessionBean} and {@link UserEntityBean}.
12: * This example demonstrated how to combine the testing of BMP entity beans
13: * with the JDBC test framework. We prepare the primary key the database should
14: * return and MockEJB handles create and finder calls. No interceptors
15: * are involved here.
16: * In the first test, we prepare a test user and try to login.
17: * In the second test, we create a user and simulate the SQLException,
18: * the real database would throw because of the duplicate primary key.
19: */
20: public class UserLoginSessionTest extends EJBTestCaseAdapter {
21: private UserLoginSession bean;
22: private PreparedStatementResultSetHandler handler;
23:
24: protected void setUp() throws Exception {
25: super .setUp();
26: setInterfacePackage("com.mockrunner.example.ejb.interfaces");
27: deploySessionBean("com/mockrunner/example/UserLoginSession",
28: UserLoginSessionBean.class, TransactionPolicy.REQUIRED);
29: bean = (UserLoginSession) createBean("com/mockrunner/example/UserLoginSession");
30: deployEntityBean("java:comp/env/ejb/UserEntity",
31: UserEntityBean.class, TransactionPolicy.REQUIRED);
32: bindToContext("java:comp/env/jdbc/MySQLDB",
33: getJDBCMockObjectFactory().getMockDataSource());
34: handler = getJDBCMockObjectFactory().getMockConnection()
35: .getPreparedStatementResultSetHandler();
36: }
37:
38: private void prepareFindByPrimaryKeyResult() {
39: MockResultSet resultSet = handler.createResultSet();
40: resultSet.addRow(new Object[] { "TestUser" });
41: handler.prepareResultSet("select username from usertable",
42: resultSet);
43: createEntityBean("java:comp/env/ejb/UserEntity", new Object[] {
44: "TestUser", "CorrectPassword" }, "TestUser");
45: }
46:
47: private void prepareSQLExceptionForCreate() {
48: handler.prepareThrowsSQLException("insert into usertable");
49: }
50:
51: public void testLoginUser() throws Exception {
52: prepareFindByPrimaryKeyResult();
53: assertFalse(bean.loginUser("TestUser", "WrongPassword"));
54: assertTrue(bean.loginUser("TestUser", "CorrectPassword"));
55: assertFalse(bean.loginUser("UnknownUser", "APassword"));
56: }
57:
58: public void testCreateUser() throws Exception {
59: assertTrue(bean.createUser("TestUser", "APassword"));
60: prepareSQLExceptionForCreate();
61: assertFalse(bean.createUser("TestUser", "APassword"));
62: }
63: }
|