package org.mockejb.test;
import javax.jms.MessageListener;
import javax.naming.*;
import junit.framework.TestCase;
import org.mockejb.*;
import org.mockejb.interceptor.*;
import org.mockejb.jndi.*;
public class MDBTest extends TestCase {
private MockContainer mockContainer;
private Context context;
public MDBTest(String name) {
super(name);
}
public void setUp() throws Exception {
MockContextFactory.setAsInitial();
context = new InitialContext();
mockContainer = new MockContainer( context );
SessionBeanDescriptor sampleBeanDescriptor =
new SessionBeanDescriptor( SampleService.JNDI_NAME,
SampleServiceHome.class, SampleService.class, new SampleServiceBean() );
mockContainer.deploy( sampleBeanDescriptor );
}
public void testMessageBean( ) throws Exception {
MDBDescriptor sampleMDBDescriptor =
new MDBDescriptor( "SampleConnectionFactory", "SampleTopic", new SampleMessageBean() );
sampleMDBDescriptor.setIsTopic( true );
mockContainer.deploy( sampleMDBDescriptor );
mockContainer.deploy( new MDBDescriptor( "SampleQueueConnectionFactory", "SampleQueue",
new SampleMessageBean() ));
context.rebind( "AnotherSampleQueueConnectionFactory", new org.mockejb.jms.QueueConnectionFactoryImpl() );
context.rebind( "AnotherSampleQueue", new org.mockejb.jms.MockQueue( "AnotherSampleQueue" ) );
MDBDescriptor foreignProviderMDBDescriptor =
new MDBDescriptor( "AnotherSampleQueueConnectionFactory", "AnotherSampleQueue",
new SampleMessageBean() );
foreignProviderMDBDescriptor.setIsAlreadyBound( true );
mockContainer.deploy(foreignProviderMDBDescriptor);
InvocationRecorder recorder = new InvocationRecorder();
AspectSystem aspectSystem = AspectSystemFactory.getAspectSystem();
aspectSystem.add( new ClassPointcut( SampleService.class, false), recorder );
aspectSystem.add( new ClassPointcut( MessageListener.class, false), recorder );
SampleServiceHome sampleServiceHome = (SampleServiceHome)context.lookup( SampleService.JNDI_NAME );
SampleService sampleService = sampleServiceHome.create();
sampleService.sendMessage( "Test message" );
assertNotNull( recorder.findByTargetMethod( "SampleMessageBean.onMessage") );
InvocationContext echoStringInvocation = recorder.findByTargetMethod( "echoString");
assertNotNull(echoStringInvocation);
assertEquals( "Test message", echoStringInvocation.getParamVals()[0]);
}
}