001: /*
002: * MessageQueueClient: The message queue client library
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: * MessageQueueTest.java
020: */
021:
022: // package path
023: package com.rift.coad.daemon.messageservice;
024:
025: import junit.framework.*;
026: import java.lang.ThreadLocal;
027: import java.util.Date;
028: import java.util.Queue;
029: import java.util.PriorityQueue;
030: import java.util.HashMap;
031: import java.util.HashSet;
032: import java.util.Map;
033: import java.util.ArrayList;
034: import java.util.Set;
035: import java.util.concurrent.ConcurrentHashMap;
036: import javax.naming.Context;
037: import javax.naming.InitialContext;
038: import javax.transaction.xa.XAException;
039: import javax.transaction.xa.XAResource;
040: import javax.transaction.xa.Xid;
041: import javax.transaction.UserTransaction;
042: import org.apache.log4j.Logger;
043: import org.apache.log4j.BasicConfigurator;
044:
045: // object web imports
046: import org.objectweb.jotm.Jotm;
047:
048: // coadunation imports
049: import com.rift.coad.lib.naming.NamingDirector;
050: import com.rift.coad.lib.naming.ContextManager;
051: import com.rift.coad.lib.db.DBSourceManager;
052:
053: import com.rift.coad.lib.interceptor.InterceptorFactory;
054: import com.rift.coad.lib.security.RoleManager;
055: import com.rift.coad.lib.security.ThreadsPermissionContainer;
056: import com.rift.coad.lib.security.ThreadPermissionSession;
057: import com.rift.coad.lib.security.UserSession;
058: import com.rift.coad.lib.security.user.UserSessionManager;
059: import com.rift.coad.lib.security.user.UserStoreManager;
060: import com.rift.coad.lib.security.SessionManager;
061: import com.rift.coad.lib.security.login.LoginManager;
062: import com.rift.coad.lib.thread.CoadunationThreadGroup;
063:
064: import com.rift.coad.lib.configuration.Configuration;
065: import com.rift.coad.lib.configuration.ConfigurationFactory;
066: import com.rift.coad.util.lock.LockRef;
067: import com.rift.coad.util.lock.ObjectLockFactory;
068: import com.rift.coad.lib.transaction.TransactionDirector;
069: import com.rift.coad.util.transaction.TransactionManager;
070: import com.rift.coad.util.lock.LockRef;
071: import com.rift.coad.util.lock.ObjectLockFactory;
072:
073: /**
074: * This object tests the Message queue
075: *
076: * @author Brett Chaldecott
077: */
078: public class MessageQueueTest extends TestCase {
079:
080: /**
081: * This object represents a test message manager.
082: */
083: public class TestMessageManager implements MessageManager {
084: // the classes private member variables
085: public String id = null;
086: public Date nextProcessTime = new Date();
087: public int priority = 0;
088:
089: /**
090: * The constructor that returns the test message.
091: */
092: public TestMessageManager(String id, int priority) {
093: this .id = id;
094: this .priority = priority;
095: }
096:
097: /**
098: * Returns the id of the test message.
099: */
100: public String getID() {
101: return id;
102: }
103:
104: /**
105: * Returns null for this test.
106: */
107: public Message getMessage() throws MessageServiceException {
108: return null;
109: }
110:
111: /**
112: * does nothing for this test.
113: */
114: public void updateMessage(Message updatedMessage)
115: throws MessageServiceException {
116:
117: }
118:
119: /**
120: * This method returns the next process time.
121: */
122: public Date nextProcessTime() {
123: return nextProcessTime;
124: }
125:
126: /**
127: * This method returns the next process time.
128: */
129: public void setNextProcessTime(Date nextProcessTime) {
130: this .nextProcessTime = nextProcessTime;
131: }
132:
133: /**
134: * This message returns the priority.
135: */
136: public int getPriority() {
137: return priority;
138: }
139:
140: /**
141: * Perform the comparision between the object.
142: *
143: * @return The integer value of message to perform the comparison on.
144: * @param o The object to perform the comparison on.
145: */
146: public int compareTo(Object o) {
147: TestMessageManager tmsg = (TestMessageManager) o;
148: if (tmsg.nextProcessTime().getTime() > nextProcessTime()
149: .getTime()) {
150: return -1;
151: } else if (nextProcessTime().getTime() > tmsg
152: .nextProcessTime().getTime()) {
153: return 1;
154: } else if (tmsg.getPriority() > getPriority()) {
155: return -1;
156: } else if (getPriority() > tmsg.getPriority()) {
157: return 1;
158: }
159: return 0;
160: }
161:
162: public void commit(Xid xid, boolean b) throws XAException {
163: }
164:
165: public void end(Xid xid, int i) throws XAException {
166: }
167:
168: public void forget(Xid xid) throws XAException {
169: }
170:
171: public int getTransactionTimeout() throws XAException {
172: return -1;
173: }
174:
175: public boolean isSameRM(XAResource xAResource)
176: throws XAException {
177: return this == xAResource;
178: }
179:
180: public int prepare(Xid xid) throws XAException {
181: return 0;
182: }
183:
184: public Xid[] recover(int i) throws XAException {
185: return null;
186: }
187:
188: public void rollback(Xid xid) throws XAException {
189: }
190:
191: public boolean setTransactionTimeout(int i) throws XAException {
192: return true;
193: }
194:
195: public void start(Xid xid, int i) throws XAException {
196: }
197:
198: public String getMessageQueueName() {
199: return "test";
200: }
201:
202: public void remove() throws MessageServiceException {
203: }
204:
205: }
206:
207: public MessageQueueTest(String testName) {
208: super (testName);
209: //BasicConfigurator.configure();
210: }
211:
212: protected void setUp() throws Exception {
213: }
214:
215: protected void tearDown() throws Exception {
216: }
217:
218: /**
219: * Test of com.rift.coad.daemon.messageservice.MessageQueue.
220: */
221: public void testMessageQueue() throws Exception {
222: System.out.println("MessageQueue");
223:
224: // init the session information
225: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
226: SessionManager.init(permissions);
227: UserStoreManager userStoreManager = new UserStoreManager();
228: UserSessionManager sessionManager = new UserSessionManager(
229: permissions, userStoreManager);
230: LoginManager.init(sessionManager, userStoreManager);
231: // instanciate the thread manager
232: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
233: sessionManager, userStoreManager);
234:
235: // add a user to the session for the current thread
236: RoleManager.getInstance();
237:
238: InterceptorFactory.init(permissions, sessionManager,
239: userStoreManager);
240:
241: // add a new user object and add to the permission
242: Set set = new HashSet();
243: set.add("test");
244: UserSession user = new UserSession("test1", set);
245: permissions.putSession(
246: new Long(Thread.currentThread().getId()),
247: new ThreadPermissionSession(new Long(Thread
248: .currentThread().getId()), user));
249:
250: // init the naming director
251: NamingDirector.init(threadGroup);
252:
253: // instanciate the transaction director
254: TransactionDirector transactionDirector = TransactionDirector
255: .init();
256: ObjectLockFactory.init();
257: TransactionManager.init();
258:
259: // retrieve the user transaction
260: Context context = new InitialContext();
261: UserTransaction ut = (UserTransaction) context
262: .lookup("java:comp/UserTransaction");
263:
264: ut.begin();
265:
266: MessageQueue messageQueue = new MessageQueue("test");
267:
268: TestMessageManager message1 = new TestMessageManager("test1", 1);
269: TransactionManager.getInstance().bindResource(message1, true);
270: messageQueue.addMessage(message1);
271:
272: ut.rollback();
273:
274: ut.begin();
275: try {
276: messageQueue.getMessage("test1");
277: fail("Retrieved a message that should not be there");
278: } catch (MessageServiceException ex) {
279: System.out.println("Failed to find the message : "
280: + ex.getMessage());
281: }
282: ut.commit();
283:
284: ut.begin();
285:
286: TestMessageManager message2 = new TestMessageManager("test2", 1);
287: TransactionManager.getInstance().bindResource(message1, true);
288: messageQueue.addMessage(message1);
289: TransactionManager.getInstance().bindResource(message2, true);
290: messageQueue.addMessage(message2);
291:
292: ut.commit();
293:
294: ut.begin();
295:
296: if (message1 != messageQueue.getMessage("test1")) {
297: fail("Test message 1 not found");
298: }
299: ut.rollback();
300:
301: Date nextRunTime = new Date();
302: TestMessageManager testRunMessage = (TestMessageManager) messageQueue
303: .popFrontMessage(nextRunTime);
304: if (testRunMessage == null) {
305: fail("Failed to pop a message from the list");
306: }
307: if (!testRunMessage.getID().equals("test1")) {
308: fail("The pop order is wrong");
309: }
310:
311: ut.begin();
312: TransactionManager.getInstance().bindResource(testRunMessage,
313: true);
314: testRunMessage.setNextProcessTime(new Date(testRunMessage
315: .nextProcessTime().getTime() + 1000));
316: ut.commit();
317: messageQueue.pushBackMessage(testRunMessage);
318:
319: testRunMessage = (TestMessageManager) messageQueue
320: .popFrontMessage(nextRunTime);
321: if (testRunMessage == null) {
322: fail("Failed to pop a message from the list");
323: }
324: if (!testRunMessage.getID().equals("test2")) {
325: fail("The pop order is wrong");
326: }
327:
328: ut.begin();
329: TransactionManager.getInstance().bindResource(testRunMessage,
330: true);
331: testRunMessage.setNextProcessTime(new Date(testRunMessage
332: .nextProcessTime().getTime() + 1000));
333: ut.commit();
334: messageQueue.pushBackMessage(testRunMessage);
335:
336: testRunMessage = (TestMessageManager) messageQueue
337: .popFrontMessage(nextRunTime);
338: if (testRunMessage != null) {
339: fail("Popped a message from the list");
340: }
341:
342: Thread.sleep(1500);
343:
344: testRunMessage = (TestMessageManager) messageQueue
345: .popFrontMessage(nextRunTime);
346: if (testRunMessage == null) {
347: fail("Failed to pop a message from the list");
348: }
349: if (!testRunMessage.getID().equals("test1")) {
350: fail("The pop order is wrong");
351: }
352:
353: ut.begin();
354: TransactionManager.getInstance().bindResource(testRunMessage,
355: true);
356: testRunMessage.setNextProcessTime(new Date(testRunMessage
357: .nextProcessTime().getTime() + 1000));
358: ut.commit();
359: messageQueue.pushBackMessage(testRunMessage);
360:
361: testRunMessage = (TestMessageManager) messageQueue
362: .popFrontMessage(nextRunTime);
363: if (testRunMessage == null) {
364: fail("Failed to pop a message from the list");
365: }
366: if (!testRunMessage.getID().equals("test2")) {
367: fail("The pop order is wrong");
368: }
369:
370: ut.begin();
371: messageQueue.removeMessage("test2");
372: ut.commit();
373:
374: ut.begin();
375: try {
376: messageQueue.getMessage("test2");
377: fail("Retrieved a message that should not be there");
378: } catch (MessageServiceException ex) {
379: System.out.println("Failed to find the message : "
380: + ex.getMessage());
381: }
382: ut.commit();
383:
384: ut.begin();
385: messageQueue.removeMessage("test1");
386: ut.commit();
387:
388: ut.begin();
389: try {
390: messageQueue.getMessage("test1");
391: fail("Retrieved a message that should not be there");
392: } catch (MessageServiceException ex) {
393: System.out.println("Failed to find the message : "
394: + ex.getMessage());
395: }
396: ut.commit();
397: }
398:
399: }
|