001: package org.mockejb.test;
002:
003: import javax.jms.MessageListener;
004: import javax.naming.*;
005:
006: import junit.framework.TestCase;
007:
008: import org.mockejb.*;
009: import org.mockejb.interceptor.*;
010: import org.mockejb.jndi.*;
011:
012: /**
013: * Demonstrates MDB testing with MockEJB.
014: * This class provides an example of the integration test where session bean
015: * sends a message to MDB and MDB calls another session bean.
016: * We use the capability of MockEJB JMS implementation to deliver messages
017: * synchronously to the receivers, so we don't need to mess with threads.
018: * This class relies on MockEJB and it won't run in the container.
019: *
020: * @author Alexander Ananiev
021: * @author Dimitar Gospodinov
022: */
023: public class MDBTest extends TestCase {
024:
025: // JNDI context and MockContainer instance used by all tests in this class
026: private MockContainer mockContainer;
027: private Context context;
028:
029: public MDBTest(String name) {
030: super (name);
031: }
032:
033: /**
034: * Sets up our mock container, JNDI context and deploy the beans that we need.
035: */
036: public void setUp() throws Exception {
037:
038: // MockContextFactory becomes the primary JNDI provider
039: MockContextFactory.setAsInitial();
040: context = new InitialContext();
041:
042: mockContainer = new MockContainer(context);
043:
044: // Create deployment descriptor of our sample session bean.
045: // This session bean is used as a client for MDB.
046: SessionBeanDescriptor sampleBeanDescriptor = new SessionBeanDescriptor(
047: SampleService.JNDI_NAME, SampleServiceHome.class,
048: SampleService.class, new SampleServiceBean());
049:
050: mockContainer.deploy(sampleBeanDescriptor);
051: }
052:
053: /**
054: * Deploys MDB and uses session bean to send a message to MDB.
055: */
056: public void testMessageBean() throws Exception {
057:
058: /* Specify connection factory and destination that MDB will listen to.
059: * We also provide an instance of MDB implementaion class.
060: */
061: MDBDescriptor sampleMDBDescriptor = new MDBDescriptor(
062: "SampleConnectionFactory", "SampleTopic",
063: new SampleMessageBean());
064: // queue is the default, in this test we use topic
065: sampleMDBDescriptor.setIsTopic(true);
066: // This will create connection factory and destination, create MDB and set
067: // it as the listener to the destination
068: mockContainer.deploy(sampleMDBDescriptor);
069:
070: // We can bind the same MDB to the queue as well
071: // Note that the connection factory must be different for queues and topics!
072: mockContainer.deploy(new MDBDescriptor(
073: "SampleQueueConnectionFactory", "SampleQueue",
074: new SampleMessageBean()));
075:
076: /*
077: * If you want to use some other JMS provider, you need to create the connection factory
078: * and destination separately and bind them to JNDI directly.
079: * Here we use MockEJB JMS as an example, but in reality you can use mockrunner,
080: * or any other JMS library (for this test though, the JMS provider must be synchronous).
081: */
082: context.rebind("AnotherSampleQueueConnectionFactory",
083: new org.mockejb.jms.QueueConnectionFactoryImpl());
084: context.rebind("AnotherSampleQueue",
085: new org.mockejb.jms.MockQueue("AnotherSampleQueue"));
086: MDBDescriptor foreignProviderMDBDescriptor = new MDBDescriptor(
087: "AnotherSampleQueueConnectionFactory",
088: "AnotherSampleQueue", new SampleMessageBean());
089: //Then method tell MockEJB not to create queues/factories
090: foreignProviderMDBDescriptor.setIsAlreadyBound(true);
091: mockContainer.deploy(foreignProviderMDBDescriptor);
092:
093: InvocationRecorder recorder = new InvocationRecorder();
094: AspectSystem aspectSystem = AspectSystemFactory
095: .getAspectSystem();
096: // Set our custom interceptor so it would handle all calls to Sample interface (w/o subclasses)
097: aspectSystem.add(new ClassPointcut(SampleService.class, false),
098: recorder);
099: // Intercept MDB too
100: aspectSystem.add(
101: new ClassPointcut(MessageListener.class, false),
102: recorder);
103:
104: // Obtain the session bean
105: SampleServiceHome sampleServiceHome = (SampleServiceHome) context
106: .lookup(SampleService.JNDI_NAME);
107: // create the bean
108: SampleService sampleService = sampleServiceHome.create();
109:
110: // send messages
111: sampleService.sendMessage("Test message");
112:
113: // make sure that onMessage was called.
114: assertNotNull(recorder
115: .findByTargetMethod("SampleMessageBean.onMessage"));
116:
117: // validate the call to the session bean and its parameters
118: InvocationContext echoStringInvocation = recorder
119: .findByTargetMethod("echoString");
120: // ok, it was indeed called
121: assertNotNull(echoStringInvocation);
122: // and the content of the message was indeed passed in
123: assertEquals("Test message", echoStringInvocation
124: .getParamVals()[0]);
125:
126: }
127:
128: }
|