001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.foedeployer.test;
023:
024: import java.io.IOException;
025: import java.net.InetAddress;
026: import java.rmi.RemoteException;
027: import java.util.Set;
028: import javax.ejb.CreateException;
029: import javax.ejb.Handle;
030: import javax.management.ObjectName;
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033: import javax.rmi.PortableRemoteObject;
034:
035: import javax.jms.JMSException;
036: import javax.jms.Message;
037: import javax.jms.Session;
038: import javax.jms.ObjectMessage;
039: import javax.jms.Topic;
040: import javax.jms.TopicConnection;
041: import javax.jms.TopicConnectionFactory;
042: import javax.jms.TopicPublisher;
043: import javax.jms.TopicSession;
044: import javax.jms.Queue;
045: import javax.jms.QueueConnection;
046: import javax.jms.QueueConnectionFactory;
047: import javax.jms.QueueReceiver;
048: import javax.jms.QueueSession;
049: import javax.jms.MessageListener;
050:
051: import junit.extensions.TestSetup;
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jboss.test.JBossTestCase;
057: import org.jboss.test.JBossTestSetup;
058:
059: import org.jboss.test.foedeployer.ejb.message.QuoteMessage;
060: import org.apache.log4j.Category;
061:
062: /**
063: * Test of a message driven bean WebLogic Application Conversion
064: *
065: * @author <a href="mailto:loubyansky@hotmail.com>Alex Loubyansky</a>
066: * @version $Revision: 57211 $
067: *
068: * Note: example that comes with WebLogic 6.1 has the following not compliant
069: * with JBoss issues:
070: * - WL's client didn't close session and connection after finishing
071: * its publishing.
072: * JBoss produced "SocketException: connection reset by peer..." for this,
073: * which seems like a right behaviour.
074: */
075: public class MessageConversionTestCase extends JBossTestCase {
076: // Constants -----------------------------------------------------
077: public static final String FOE_DEPLOYER = "foe-deployer-3.2.sar";
078: public static final String FOE_DEPLOYER_NAME = "jboss:service=FoeDeployer";
079: public static final String CONVERTOR_DEPLOYER_QUERY_NAME = "jboss:service=Convertor,*";
080: public static final String MESSAGE_APPLICATION = "foe-deployer-message-test";
081: public static final String TOPIC = "topic/testTopic";
082: public static final String TOPIC_FACTORY = "ConnectionFactory";
083: public static final String QUEUE = "queue/testQueue";
084: public static final String QUEUE_FACTORY = "ConnectionFactory";
085:
086: public static final int MESSAGES_NUMBER = 10;
087: public static final int WAIT_ITERATIONS = 3;
088:
089: // Attributes ----------------------------------------------------
090: private static TopicConnection topicConnection;
091: private static QueueConnection queueConnection;
092: private static QueueSession queueSession;
093: private static Queue queue;
094: private static TopicSession topicSession;
095: private static Topic topic;
096: private static TopicPublisher topicPublisher;
097: private static QuoteMessageListener listener;
098:
099: // Static --------------------------------------------------------
100: /**
101: * Setup the test suite.
102: */
103: public static Test suite() throws Exception {
104: TestSuite lSuite = new TestSuite();
105: lSuite.addTest(new TestSuite(MessageConversionTestCase.class));
106:
107: // Create an initializer for the test suite
108: TestSetup lWrapper = new JBossTestSetup(lSuite) {
109: protected void setUp() throws Exception {
110: super .setUp();
111: }
112:
113: protected void tearDown() throws Exception {
114: super .tearDown();
115: }
116: };
117: return lWrapper;
118: }
119:
120: // Constructors --------------------------------------------------
121: public MessageConversionTestCase(String pName) {
122: super (pName);
123: }
124:
125: // Public --------------------------------------------------------
126: /**
127: * Test an MDB conversion
128: */
129: public void testMessageConversion() throws Exception {
130: try {
131: log.debug("+++ testMessageConversion");
132:
133: // First check if foe-deployer is deployed
134: boolean lIsInitiallyDeployed = getServer().isRegistered(
135: new ObjectName(FOE_DEPLOYER_NAME));
136: if (!lIsInitiallyDeployed)
137: deploy(FOE_DEPLOYER);
138:
139: boolean lIsDeployed = getServer().isRegistered(
140: new ObjectName(FOE_DEPLOYER_NAME));
141: assertTrue("Foe-Deployer is not deployed", lIsDeployed);
142:
143: // Count number of converters (must be a list one)
144: int lCount = getServer()
145: .queryNames(
146: new ObjectName(
147: CONVERTOR_DEPLOYER_QUERY_NAME),
148: null).size();
149: assertTrue("No Converter found on web server", lCount > 0);
150:
151: // Deploy WL application
152: deploy(MESSAGE_APPLICATION + ".wlar");
153:
154: // Because the Foe-Deployer copies the converted JAR back to the original place
155: // it has to be deployed from here again
156: deploy(MESSAGE_APPLICATION + ".jar");
157:
158: log.debug("getting intial naming context");
159: InitialContext ic = new InitialContext();
160:
161: //
162: // initialize queue stuff
163: //
164: log.debug("looking for queue connection factory");
165: QueueConnectionFactory qcf = (QueueConnectionFactory) ic
166: .lookup(QUEUE_FACTORY);
167:
168: log.debug("creating queue connection");
169: queueConnection = qcf.createQueueConnection();
170:
171: log.debug("creating queue session");
172: queueSession = queueConnection.createQueueSession(false,
173: Session.AUTO_ACKNOWLEDGE);
174:
175: log.debug("looking for queue");
176: queue = (Queue) ic.lookup(QUEUE);
177:
178: log.debug("creating queue receiver");
179: QueueReceiver receiver = queueSession.createReceiver(queue);
180:
181: log.debug("creating message listener");
182: listener = new QuoteMessageListener();
183:
184: log.debug("registering listener with receiver");
185: receiver.setMessageListener(listener);
186:
187: log.debug("starting queue connection");
188: queueConnection.start();
189:
190: //
191: // Prepare topic stuff
192: //
193: log.debug("looking for topic connection factory");
194: TopicConnectionFactory cf = (TopicConnectionFactory) ic
195: .lookup(TOPIC_FACTORY);
196:
197: log.debug("creating topic connection");
198: topicConnection = cf.createTopicConnection();
199:
200: log.debug("creating topic session");
201: topicSession = topicConnection.createTopicSession(false,
202: Session.AUTO_ACKNOWLEDGE);
203:
204: log.debug("looking for topic");
205: topic = (Topic) ic.lookup(TOPIC);
206:
207: log.debug("creating topic publisher");
208: topicPublisher = topicSession.createPublisher(topic);
209:
210: log.debug("starting topic connection");
211: topicConnection.start();
212:
213: log.debug("testMessageConversion: sending "
214: + MESSAGES_NUMBER + " messages");
215:
216: for (int i = 0; i < MESSAGES_NUMBER; ++i) {
217: QuoteMessage quoteMsg = new QuoteMessage(
218: "Topic message no." + i);
219:
220: log.debug("publishing message: " + quoteMsg.getQuote());
221:
222: ObjectMessage message = topicSession
223: .createObjectMessage();
224: message.setObject(quoteMsg);
225: topicPublisher.publish(message);
226: }
227:
228: log.debug("waiting for messages to be processed");
229: int i = 0;
230: while ((i++ < WAIT_ITERATIONS)
231: && (listener.getCount() < MESSAGES_NUMBER)) {
232: try {
233: Thread.currentThread().sleep(1000);
234: } catch (Exception e) {
235: }
236: }
237:
238: log.debug("Messages received: " + listener.getCount());
239: assertTrue("Number of sent messages (" + MESSAGES_NUMBER
240: + ") isn't equal to number of received ("
241: + listener.getCount() + ")",
242: MESSAGES_NUMBER == listener.getCount());
243:
244: // close connections
245: if (topicConnection != null)
246: topicConnection.close();
247: if (queueConnection != null)
248: queueConnection.close();
249:
250: // undeploy converted application to clean up
251: undeploy(MESSAGE_APPLICATION + ".jar");
252:
253: // undeploy WL application
254: undeploy(MESSAGE_APPLICATION + ".wlar");
255:
256: // Only undeploy if deployed here
257: if (!lIsInitiallyDeployed) {
258: undeploy(FOE_DEPLOYER);
259: }
260: } catch (Exception e) {
261: e.printStackTrace();
262: throw e;
263: }
264: }
265:
266: // Inner classes -------------------------------------------------
267: public class QuoteMessageListener implements MessageListener {
268: // Attributes -------------------------------------------------
269: Category log = Category.getInstance(QuoteMessageListener.class);
270: public int count = 0;
271:
272: // Constructor ------------------------------------------------
273: public QuoteMessageListener() {
274: log.debug("created");
275: count = 0;
276: }
277:
278: // Public methods ---------------------------------------------
279: public int getCount() {
280: return count;
281: }
282:
283: // MessageListener implementation -----------------------------
284: public void onMessage(Message msg) {
285: QuoteMessage quoteMsg = null;
286: try {
287: quoteMsg = (QuoteMessage) ((ObjectMessage) msg)
288: .getObject();
289: } catch (ClassCastException cce) {
290: log
291: .error(
292: "Received message isn't of type QuoteMessage: ",
293: cce);
294: } catch (JMSException jmse) {
295: log.error("Couldn't fetch message: ", jmse);
296: }
297:
298: log.debug("received message: " + quoteMsg.getQuote());
299: ++count;
300: }
301: }
302: }
|