001: /*
002: * MessageService: The message service daemon
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * MessageQueueManagerTest.java
020: */
021:
022: package com.rift.coad.daemon.messageservice;
023:
024: // java imports
025: import javax.naming.InitialContext;
026: import javax.naming.Context;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.Statement;
030: import javax.sql.DataSource;
031: import java.util.Set;
032: import java.util.HashSet;
033: import javax.transaction.UserTransaction;
034: import java.sql.Timestamp;
035: import java.util.ArrayList;
036: import java.util.Date;
037: import java.util.Enumeration;
038: import java.util.HashSet;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Set;
042: import javax.transaction.xa.XAException;
043: import javax.transaction.xa.XAResource;
044: import javax.transaction.xa.Xid;
045: import org.hibernate.*;
046: import org.hibernate.cfg.*;
047: import org.apache.log4j.Logger;
048: import org.apache.log4j.BasicConfigurator;
049:
050: // junit imports
051: import junit.framework.*;
052:
053: // object web imports
054: import org.objectweb.jotm.Jotm;
055:
056: // coadunation imports
057: import com.rift.coad.lib.naming.NamingDirector;
058: import com.rift.coad.lib.naming.ContextManager;
059: import com.rift.coad.lib.db.DBSourceManager;
060: import com.rift.coad.lib.common.ObjectSerializer;
061: import com.rift.coad.lib.interceptor.InterceptorFactory;
062: import com.rift.coad.lib.security.RoleManager;
063: import com.rift.coad.lib.security.ThreadsPermissionContainer;
064: import com.rift.coad.lib.security.ThreadPermissionSession;
065: import com.rift.coad.lib.security.UserSession;
066: import com.rift.coad.lib.security.user.UserSessionManager;
067: import com.rift.coad.lib.security.user.UserStoreManager;
068: import com.rift.coad.lib.security.SessionManager;
069: import com.rift.coad.lib.security.login.LoginManager;
070: import com.rift.coad.lib.thread.CoadunationThreadGroup;
071: import com.rift.coad.lib.transaction.TransactionDirector;
072: import com.rift.coad.daemon.messageservice.Message;
073: import com.rift.coad.daemon.messageservice.RPCMessage;
074: import com.rift.coad.daemon.messageservice.TextMessage;
075: import com.rift.coad.daemon.messageservice.MessageManager;
076: import com.rift.coad.daemon.messageservice.MessageServiceException;
077: import com.rift.coad.daemon.messageservice.MessageServiceManager;
078: import com.rift.coad.daemon.messageservice.db.*;
079: import com.rift.coad.daemon.messageservice.message.MessageImpl;
080: import com.rift.coad.daemon.messageservice.message.RPCMessageImpl;
081: import com.rift.coad.daemon.messageservice.message.TextMessageImpl;
082: import com.rift.coad.hibernate.util.HibernateUtil;
083: import com.rift.coad.util.lock.ObjectLockFactory;
084: import com.rift.coad.util.transaction.TransactionManager;
085:
086: /**
087: * A test of the message queue manager
088: *
089: * @author Brett Chaldecott
090: */
091: public class MessageQueueManagerTest extends TestCase {
092:
093: /**
094: * This object represents a test message manager.
095: */
096: public class TestMessageManager implements MessageManager {
097: // the classes private member variables
098: public String id = null;
099: public Date nextProcessTime = new Date();
100: public int priority = 0;
101:
102: /**
103: * The constructor that returns the test message.
104: */
105: public TestMessageManager(String id, int priority) {
106: this .id = id;
107: this .priority = priority;
108: }
109:
110: /**
111: * Returns the id of the test message.
112: */
113: public String getID() {
114: return id;
115: }
116:
117: /**
118: * Returns null for this test.
119: */
120: public Message getMessage() throws MessageServiceException {
121: return null;
122: }
123:
124: /**
125: * does nothing for this test.
126: */
127: public void updateMessage(Message updatedMessage)
128: throws MessageServiceException {
129:
130: }
131:
132: /**
133: * This method returns the next process time.
134: */
135: public Date nextProcessTime() {
136: return nextProcessTime;
137: }
138:
139: /**
140: * This method returns the next process time.
141: */
142: public void setNextProcessTime(Date nextProcessTime) {
143: this .nextProcessTime = nextProcessTime;
144: }
145:
146: /**
147: * This message returns the priority.
148: */
149: public int getPriority() {
150: return priority;
151: }
152:
153: /**
154: * Perform the comparision between the object.
155: *
156: * @return The integer value of message to perform the comparison on.
157: * @param o The object to perform the comparison on.
158: */
159: public int compareTo(Object o) {
160: TestMessageManager tmsg = (TestMessageManager) o;
161: if (tmsg.nextProcessTime().getTime() > nextProcessTime()
162: .getTime()) {
163: return -1;
164: } else if (nextProcessTime().getTime() > tmsg
165: .nextProcessTime().getTime()) {
166: return 1;
167: } else if (tmsg.getPriority() > getPriority()) {
168: return -1;
169: } else if (getPriority() > tmsg.getPriority()) {
170: return 1;
171: }
172: return 0;
173: }
174:
175: public void commit(Xid xid, boolean b) throws XAException {
176: }
177:
178: public void end(Xid xid, int i) throws XAException {
179: }
180:
181: public void forget(Xid xid) throws XAException {
182: }
183:
184: public int getTransactionTimeout() throws XAException {
185: return -1;
186: }
187:
188: public boolean isSameRM(XAResource xAResource)
189: throws XAException {
190: return this == xAResource;
191: }
192:
193: public int prepare(Xid xid) throws XAException {
194: return 0;
195: }
196:
197: public Xid[] recover(int i) throws XAException {
198: return null;
199: }
200:
201: public void rollback(Xid xid) throws XAException {
202: }
203:
204: public boolean setTransactionTimeout(int i) throws XAException {
205: return true;
206: }
207:
208: public void start(Xid xid, int i) throws XAException {
209: }
210:
211: public String getMessageQueueName() {
212: return "test";
213: }
214:
215: public void remove() throws MessageServiceException {
216: }
217:
218: }
219:
220: boolean gotRef = false;
221:
222: public MessageQueueManagerTest(String testName) {
223: super (testName);
224: //BasicConfigurator.configure();
225: }
226:
227: protected void setUp() throws Exception {
228: }
229:
230: protected void tearDown() throws Exception {
231: }
232:
233: /**
234: * Test of class com.rift.coad.daemon.messageservice.MessageQueueManager.
235: */
236: public void testMessageQueueManager() throws Exception {
237: System.out.println("testMessageQueueManager");
238:
239: // init the session information
240: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
241: SessionManager.init(permissions);
242: UserStoreManager userStoreManager = new UserStoreManager();
243: UserSessionManager sessionManager = new UserSessionManager(
244: permissions, userStoreManager);
245: LoginManager.init(sessionManager, userStoreManager);
246: // instanciate the thread manager
247: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
248: sessionManager, userStoreManager);
249:
250: // add a user to the session for the current thread
251: RoleManager.getInstance();
252:
253: InterceptorFactory.init(permissions, sessionManager,
254: userStoreManager);
255:
256: // add a new user object and add to the permission
257: Set set = new HashSet();
258: set.add("test");
259: UserSession user = new UserSession("test1", set);
260: permissions.putSession(
261: new Long(Thread.currentThread().getId()),
262: new ThreadPermissionSession(new Long(Thread
263: .currentThread().getId()), user));
264:
265: // init the naming director
266: NamingDirector.init(threadGroup);
267:
268: // instanciate the transaction director
269: TransactionDirector transactionDirector = TransactionDirector
270: .init();
271:
272: // init the database source
273: DBSourceManager.init();
274: Context context = new InitialContext();
275: ObjectLockFactory.init();
276: TransactionManager.init();
277:
278: MessageQueueManager expResult = MessageQueueManager
279: .getInstance();
280: MessageQueueManager result = MessageQueueManager.getInstance();
281: assertEquals(expResult, result);
282:
283: UserTransaction ut = (UserTransaction) context
284: .lookup("java:comp/UserTransaction");
285:
286: ut.begin();
287:
288: MessageQueue queue = result.getQueue("test");
289:
290: ut.commit();
291:
292: ut.begin();
293: assertEquals(queue, result.getQueue("test"));
294: ut.commit();
295:
296: ut.begin();
297: Session session = HibernateUtil.getInstance(
298: MessageServiceManager.class).getSession();
299: com.rift.coad.daemon.messageservice.db.MessageQueue messageQueue = new com.rift.coad.daemon.messageservice.db.MessageQueue(
300: "test2");
301: session.persist(messageQueue);
302: ut.commit();
303:
304: ut.begin();
305:
306: MessageQueue queue2 = result.getQueue("test2");
307:
308: ut.commit();
309:
310: ut.begin();
311: assertEquals(queue2, result.getQueue("test2"));
312: ut.commit();
313:
314: ut.begin();
315: result.getQueue("test3");
316: Thread testThread = new Thread(new Runnable() {
317: public void run() {
318: try {
319: MessageQueueManager.getInstance().getQueue("test3");
320: gotRef = true;
321: } catch (Exception ex) {
322: System.out
323: .println("Failed to get the queue reference : "
324: + ex.getMessage());
325: ex.printStackTrace(System.out);
326: }
327: }
328: });
329: testThread.start();
330: Thread.sleep(500);
331: if (gotRef) {
332: fail("Managed aquire the queue reference");
333: }
334: ut.commit();
335: Thread.sleep(500);
336: if (gotRef == false) {
337: fail("Failed aquire the queue reference");
338: }
339:
340: ut.begin();
341: TestMessageManager message1 = new TestMessageManager("test1", 1);
342: queue.addMessage(message1);
343: TestMessageManager message2 = new TestMessageManager("test2", 1);
344: queue2.addMessage(message2);
345: ut.commit();
346:
347: Date nextDate = new Date();
348: System.out.println("Get message");
349: MessageProcessInfo messageProcessInfo = result
350: .getNextMessage(nextDate);
351: System.out.println("After retrieving the message");
352: if ((messageProcessInfo == null)
353: || ((messageProcessInfo.getMessageManager() != message1) && (messageProcessInfo
354: .getMessageManager() != message2))) {
355: fail("Failed to retrieve the next message");
356: }
357:
358: System.out.println("Get message");
359: messageProcessInfo = result.getNextMessage(nextDate);
360: System.out.println("After retrieving the message");
361: if ((messageProcessInfo == null)
362: || ((messageProcessInfo.getMessageManager() != message1) && (messageProcessInfo
363: .getMessageManager() != message2))) {
364: fail("Failed to retrieve the next message");
365: }
366:
367: System.out.println("Get message");
368: messageProcessInfo = result.getNextMessage(nextDate);
369: System.out.println("After retrieving the message");
370: if (messageProcessInfo != null) {
371: fail("Succeeded in retrieving a message");
372: }
373:
374: queue.pushBackMessage(message1);
375:
376: messageProcessInfo = result.getNextMessage(nextDate);
377: if ((messageProcessInfo == null)
378: || ((messageProcessInfo.getMessageManager() != message1) && (messageProcessInfo
379: .getMessageManager() != message2))) {
380: fail("Failed to retrieve the next message");
381: }
382:
383: ut.begin();
384: session = HibernateUtil
385: .getInstance(MessageServiceManager.class).getSession();
386: com.rift.coad.daemon.messageservice.db.MessageQueue messageQueue2 = new com.rift.coad.daemon.messageservice.db.MessageQueue(
387: "test5");
388: messageQueue2.setNamed(new Integer(1));
389: session.persist(messageQueue2);
390: ut.commit();
391:
392: ut.begin();
393:
394: try {
395: result.getQueue("test5");
396: fail("Was able to retrieve the named queue");
397: } catch (MessageServiceException ex) {
398: System.out.println(ex.getMessage());
399: }
400:
401: ut.commit();
402: }
403:
404: }
|