01: package org.mockejb.test;
02:
03: import javax.rmi.PortableRemoteObject;
04: import javax.naming.*;
05:
06: import junit.framework.TestCase;
07:
08: import org.mockejb.*;
09: import org.mockejb.jndi.*;
10:
11: /**
12: * Simple MockEJB test case, the place to start exploring MockEJB.
13: * Deploys a session bean and calls its method.
14: * This class uses only the most basic features of MockEJB, please
15: * see other test classes in this package to learn about more advanced MockEJB capabilities.
16: *
17: * @author Alexander Ananiev
18: */
19: public class HelloWorldTest extends TestCase {
20:
21: // State of this test case. These variables are initialized by setUp method
22: private Context context;
23:
24: public HelloWorldTest(String name) {
25: super (name);
26: }
27:
28: /**
29: * Deploys and creates EJBs needed for our tests.
30: */
31: public void setUp() throws Exception {
32:
33: /* We need to set MockContextFactory as our JNDI provider.
34: * This method sets the necessary system properties.
35: */
36: MockContextFactory.setAsInitial();
37:
38: // create the initial context that will be used for binding EJBs
39: context = new InitialContext();
40:
41: // Create an instance of the MockContainer
42: MockContainer mockContainer = new MockContainer(context);
43:
44: /* Create deployment descriptor of our sample bean.
45: * MockEjb uses it instead of XML deployment descriptors
46: */
47: SessionBeanDescriptor sampleServiceDescriptor = new SessionBeanDescriptor(
48: SampleService.JNDI_NAME, SampleServiceHome.class,
49: SampleService.class, new SampleServiceBean());
50: // Deploy operation creates Home and binds it to JNDI
51: mockContainer.deploy(sampleServiceDescriptor);
52:
53: }
54:
55: /**
56: * Looks up sample EJB and calls its method
57: */
58: public void testHelloWorld() throws Exception {
59:
60: // Lookup the home
61: Object sampleServiceHomeObj = context
62: .lookup(SampleService.JNDI_NAME);
63:
64: // PortableRemoteObject does not do anything in MockEJB but it does no harm to call it
65: SampleServiceHome sampleServiceHome = (SampleServiceHome) PortableRemoteObject
66: .narrow(sampleServiceHomeObj, SampleServiceHome.class);
67:
68: // create the bean
69: SampleService sampleService = sampleServiceHome.create();
70: // call the method
71: String result = sampleService.echoString("HelloWorld");
72: // is the return value correct?
73: assertEquals("HelloWorld", result);
74:
75: }
76:
77: }
|