01: package org.mockejb.test;
02:
03: import javax.naming.*;
04:
05: import org.mockejb.*;
06: import org.mockejb.jndi.*;
07:
08: /**
09: * Demonstrates Stateful session bean usage with MockEJB.
10: *
11: * @author Alexander Ananiev
12: */
13: public class StatefulTest extends OptionalCactusTestCase {
14:
15: // State of this test case. These variables are initialized by setUp method
16: private SampleStatefulServiceHome statefulSampleServiceHome;
17:
18: public StatefulTest(String name) {
19: super (name);
20: }
21:
22: /**
23: * Deploy EJBs needed for our tests.
24: */
25: public void setUp() throws Exception {
26:
27: if (!isRunningOnServer()) {
28:
29: /* Deploy EJBs to the MockContainer if we run outside of the app server
30: * In cactus mode all but one EJB are deployed by the app server, so we don't need to
31: * do it.
32: */
33:
34: MockContextFactory.setAsInitial();
35:
36: // Create an instance of the MockContainer and pass the JNDI context that
37: // it will use to bind EJBs.
38: MockContainer mockContainer = new MockContainer(
39: new InitialContext());
40:
41: /*
42: * Create the deployment descriptor of the bean. Stateless and Stateful beans
43: * both use SessionBeanDescriptor.
44: */
45: SessionBeanDescriptor statefulSampleDescriptor = new SessionBeanDescriptor(
46: SampleStatefulService.JNDI_NAME,
47: SampleStatefulServiceHome.class,
48: SampleStatefulService.class,
49: SampleStatefulServiceBean.class);
50: // Mark this bean as stateful. Stateless is the default.
51: statefulSampleDescriptor.setStateful(true);
52:
53: mockContainer.deploy(statefulSampleDescriptor);
54: }
55:
56: // All EJBs are now deployed
57:
58: //Look up the home in JNDI
59: Context context = new InitialContext();
60: statefulSampleServiceHome = (SampleStatefulServiceHome) context
61: .lookup(SampleStatefulService.JNDI_NAME);
62:
63: }
64:
65: /**
66: * Simple stateful session bean test.
67: */
68: public void testStatefulSessionBean() throws Exception {
69:
70: String someState = "some state";
71:
72: // create the bean
73: SampleStatefulService sampleStatefulService = statefulSampleServiceHome
74: .create(someState);
75:
76: // Call the bean and make sure that it returns the same state
77: String returnedState = sampleStatefulService.getSampleState();
78: assertEquals(someState, returnedState);
79: // remove the bean
80: sampleStatefulService.remove();
81: }
82:
83: }
|