KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > foedeployer > test > MessageConversionTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.foedeployer.test;
8
9 import java.io.IOException JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import java.rmi.RemoteException JavaDoc;
12 import java.util.Set JavaDoc;
13 import javax.ejb.CreateException JavaDoc;
14 import javax.ejb.Handle JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16 import javax.naming.InitialContext JavaDoc;
17 import javax.naming.NamingException JavaDoc;
18 import javax.rmi.PortableRemoteObject JavaDoc;
19
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.Message JavaDoc;
22 import javax.jms.Session JavaDoc;
23 import javax.jms.ObjectMessage JavaDoc;
24 import javax.jms.Topic JavaDoc;
25 import javax.jms.TopicConnection JavaDoc;
26 import javax.jms.TopicConnectionFactory JavaDoc;
27 import javax.jms.TopicPublisher JavaDoc;
28 import javax.jms.TopicSession JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.QueueConnection JavaDoc;
31 import javax.jms.QueueConnectionFactory JavaDoc;
32 import javax.jms.QueueReceiver JavaDoc;
33 import javax.jms.QueueSession JavaDoc;
34 import javax.jms.MessageListener JavaDoc;
35
36 import junit.extensions.TestSetup;
37 import junit.framework.Test;
38 import junit.framework.TestCase;
39 import junit.framework.TestSuite;
40
41 import org.jboss.test.JBossTestCase;
42 import org.jboss.test.JBossTestSetup;
43
44 import org.jboss.test.foedeployer.ejb.message.QuoteMessage;
45 import org.apache.log4j.Category;
46
47
48 /**
49  * Test of a message driven bean WebLogic Application Conversion
50  *
51  * @author <a HREF="mailto:loubyansky@hotmail.com>Alex Loubyansky</a>
52  * @version $Revision: 1.4 $
53  *
54  * Note: example that comes with WebLogic 6.1 has the following not compliant
55  * with JBoss issues:
56  * - WL's client didn't close session and connection after finishing
57  * its publishing.
58  * JBoss produced "SocketException: connection reset by peer..." for this,
59  * which seems like a right behaviour.
60  */

61 public class MessageConversionTestCase
62    extends JBossTestCase
63 {
64    // Constants -----------------------------------------------------
65
public static final String JavaDoc FOE_DEPLOYER = "foe-deployer-3.2.sar";
66    public static final String JavaDoc FOE_DEPLOYER_NAME = "jboss:service=FoeDeployer";
67    public static final String JavaDoc CONVERTOR_DEPLOYER_QUERY_NAME = "jboss:service=Convertor,*";
68    public static final String JavaDoc MESSAGE_APPLICATION = "foe-deployer-message-test";
69    public static final String JavaDoc TOPIC = "topic/testTopic";
70    public static final String JavaDoc TOPIC_FACTORY = "ConnectionFactory";
71    public static final String JavaDoc QUEUE = "queue/testQueue";
72    public static final String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
73
74    public static final int MESSAGES_NUMBER = 10;
75    public static final int WAIT_ITERATIONS = 3;
76
77    // Attributes ----------------------------------------------------
78
private static TopicConnection JavaDoc topicConnection;
79    private static QueueConnection JavaDoc queueConnection;
80    private static QueueSession JavaDoc queueSession;
81    private static Queue JavaDoc queue;
82    private static TopicSession JavaDoc topicSession;
83    private static Topic JavaDoc topic;
84    private static TopicPublisher JavaDoc topicPublisher;
85    private static QuoteMessageListener listener;
86
87    // Static --------------------------------------------------------
88
/**
89     * Setup the test suite.
90     */

91    public static Test suite() throws Exception JavaDoc
92    {
93       TestSuite lSuite = new TestSuite();
94       lSuite.addTest(new TestSuite(MessageConversionTestCase.class));
95
96       // Create an initializer for the test suite
97
TestSetup lWrapper = new JBossTestSetup(lSuite)
98       {
99          protected void setUp() throws Exception JavaDoc
100          {
101             super.setUp();
102          }
103
104          protected void tearDown() throws Exception JavaDoc
105          {
106             super.tearDown();
107          }
108       };
109       return lWrapper;
110    }
111
112    // Constructors --------------------------------------------------
113
public MessageConversionTestCase(String JavaDoc pName)
114    {
115       super(pName);
116    }
117
118    // Public --------------------------------------------------------
119
/**
120     * Test an MDB conversion
121     */

122    public void testMessageConversion()
123       throws Exception JavaDoc
124    {
125       try
126       {
127          log.debug("+++ testMessageConversion");
128
129          // First check if foe-deployer is deployed
130
boolean lIsInitiallyDeployed = getServer().isRegistered(new ObjectName JavaDoc(FOE_DEPLOYER_NAME));
131          if(!lIsInitiallyDeployed)
132             deploy(FOE_DEPLOYER);
133
134          boolean lIsDeployed = getServer().isRegistered(new ObjectName JavaDoc(FOE_DEPLOYER_NAME));
135          assertTrue("Foe-Deployer is not deployed", lIsDeployed);
136
137          // Count number of converters (must be a list one)
138
int lCount = getServer().queryNames(new ObjectName JavaDoc(CONVERTOR_DEPLOYER_QUERY_NAME), null).size();
139          assertTrue("No Converter found on web server", lCount > 0);
140
141          // Deploy WL application
142
deploy(MESSAGE_APPLICATION + ".wlar");
143
144          // Because the Foe-Deployer copies the converted JAR back to the original place
145
// it has to be deployed from here again
146
deploy(MESSAGE_APPLICATION + ".jar");
147
148          log.debug("getting intial naming context");
149          InitialContext JavaDoc ic = new InitialContext JavaDoc();
150
151          //
152
// initialize queue stuff
153
//
154
log.debug("looking for queue connection factory");
155          QueueConnectionFactory JavaDoc qcf = (QueueConnectionFactory JavaDoc)ic.lookup( QUEUE_FACTORY );
156
157          log.debug("creating queue connection");
158          queueConnection = qcf.createQueueConnection();
159
160          log.debug("creating queue session");
161          queueSession = queueConnection.
162             createQueueSession( false, Session.AUTO_ACKNOWLEDGE );
163
164          log.debug("looking for queue");
165          queue = ( Queue JavaDoc ) ic.lookup( QUEUE );
166
167          log.debug( "creating queue receiver" );
168          QueueReceiver JavaDoc receiver = queueSession.createReceiver( queue );
169
170          log.debug( "creating message listener" );
171          listener = new QuoteMessageListener();
172
173          log.debug( "registering listener with receiver" );
174          receiver.setMessageListener( listener );
175
176          log.debug( "starting queue connection" );
177          queueConnection.start();
178
179          //
180
// Prepare topic stuff
181
//
182
log.debug("looking for topic connection factory");
183          TopicConnectionFactory JavaDoc cf = (TopicConnectionFactory JavaDoc)ic.lookup(TOPIC_FACTORY);
184
185          log.debug("creating topic connection");
186          topicConnection = cf.createTopicConnection();
187
188          log.debug("creating topic session");
189          topicSession = topicConnection.
190             createTopicSession( false, Session.AUTO_ACKNOWLEDGE );
191
192          log.debug("looking for topic");
193          topic = (Topic JavaDoc)ic.lookup( TOPIC );
194
195          log.debug("creating topic publisher");
196          topicPublisher = topicSession.createPublisher(topic);
197
198          log.debug("starting topic connection");
199          topicConnection.start();
200
201          log.debug("testMessageConversion: sending " +
202             MESSAGES_NUMBER + " messages");
203
204          for(int i = 0; i < MESSAGES_NUMBER; ++i)
205          {
206             QuoteMessage quoteMsg = new QuoteMessage("Topic message no." + i);
207
208             log.debug("publishing message: " + quoteMsg.getQuote() );
209
210             ObjectMessage JavaDoc message = topicSession.createObjectMessage();
211             message.setObject(quoteMsg);
212             topicPublisher.publish(message);
213          }
214
215          log.debug( "waiting for messages to be processed" );
216          int i = 0;
217          while( (i++ < WAIT_ITERATIONS)
218             && (listener.getCount() < MESSAGES_NUMBER) )
219          {
220             try
221             {
222                Thread.currentThread().sleep(1000);
223             }
224             catch(Exception JavaDoc e) {}
225          }
226
227          log.debug("Messages received: " + listener.getCount());
228          assertTrue("Number of sent messages ("
229             + MESSAGES_NUMBER
230             + ") isn't equal to number of received ("
231             + listener.getCount() + ")",
232             MESSAGES_NUMBER == listener.getCount());
233
234          // close connections
235
if(topicConnection != null) topicConnection.close();
236          if(queueConnection != null) queueConnection.close();
237
238          // undeploy converted application to clean up
239
undeploy(MESSAGE_APPLICATION + ".jar" );
240
241          // undeploy WL application
242
undeploy(MESSAGE_APPLICATION + ".wlar" );
243
244          // Only undeploy if deployed here
245
if(!lIsInitiallyDeployed)
246          {
247             undeploy(FOE_DEPLOYER);
248          }
249       }
250       catch( Exception JavaDoc e )
251       {
252          e.printStackTrace();
253          throw e;
254       }
255    }
256
257    // Inner classes -------------------------------------------------
258
public class QuoteMessageListener
259       implements MessageListener JavaDoc
260    {
261       // Attributes -------------------------------------------------
262
Category log = Category.getInstance( QuoteMessageListener.class );
263       public int count = 0;
264
265       // Constructor ------------------------------------------------
266
public QuoteMessageListener()
267       {
268          log.debug( "created" );
269          count = 0;
270       }
271
272       // Public methods ---------------------------------------------
273
public int getCount()
274       {
275          return count;
276       }
277
278       // MessageListener implementation -----------------------------
279
public void onMessage(Message JavaDoc msg)
280       {
281          QuoteMessage quoteMsg = null;
282          try
283          {
284             quoteMsg = (QuoteMessage)((ObjectMessage JavaDoc)msg).getObject();
285          }
286          catch(ClassCastException JavaDoc cce)
287          {
288             log.error("Received message isn't of type QuoteMessage: ", cce);
289          }
290          catch(JMSException JavaDoc jmse)
291          {
292             log.error("Couldn't fetch message: ", jmse);
293          }
294
295          log.debug("received message: " + quoteMsg.getQuote() );
296          ++count;
297       }
298    }
299 }
300
Popular Tags