KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > MessageQueueTest


1 /*
2  * MessageQueueClient: The message queue client library
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * MessageQueueTest.java
20  */

21
22 // package path
23
package com.rift.coad.daemon.messageservice;
24
25 import junit.framework.*;
26 import java.lang.ThreadLocal JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Queue JavaDoc;
29 import java.util.PriorityQueue JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.concurrent.ConcurrentHashMap JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.transaction.xa.XAException JavaDoc;
39 import javax.transaction.xa.XAResource JavaDoc;
40 import javax.transaction.xa.Xid JavaDoc;
41 import javax.transaction.UserTransaction JavaDoc;
42 import org.apache.log4j.Logger;
43 import org.apache.log4j.BasicConfigurator;
44
45 // object web imports
46
import org.objectweb.jotm.Jotm;
47
48 // coadunation imports
49
import com.rift.coad.lib.naming.NamingDirector;
50 import com.rift.coad.lib.naming.ContextManager;
51 import com.rift.coad.lib.db.DBSourceManager;
52
53 import com.rift.coad.lib.interceptor.InterceptorFactory;
54 import com.rift.coad.lib.security.RoleManager;
55 import com.rift.coad.lib.security.ThreadsPermissionContainer;
56 import com.rift.coad.lib.security.ThreadPermissionSession;
57 import com.rift.coad.lib.security.UserSession;
58 import com.rift.coad.lib.security.user.UserSessionManager;
59 import com.rift.coad.lib.security.user.UserStoreManager;
60 import com.rift.coad.lib.security.SessionManager;
61 import com.rift.coad.lib.security.login.LoginManager;
62 import com.rift.coad.lib.thread.CoadunationThreadGroup;
63
64 import com.rift.coad.lib.configuration.Configuration;
65 import com.rift.coad.lib.configuration.ConfigurationFactory;
66 import com.rift.coad.util.lock.LockRef;
67 import com.rift.coad.util.lock.ObjectLockFactory;
68 import com.rift.coad.lib.transaction.TransactionDirector;
69 import com.rift.coad.util.transaction.TransactionManager;
70 import com.rift.coad.util.lock.LockRef;
71 import com.rift.coad.util.lock.ObjectLockFactory;
72
73 /**
74  * This object tests the Message queue
75  *
76  * @author Brett Chaldecott
77  */

78 public class MessageQueueTest extends TestCase {
79     
80     /**
81      * This object represents a test message manager.
82      */

83     public class TestMessageManager implements MessageManager {
84         // the classes private member variables
85
public String JavaDoc id = null;
86         public Date JavaDoc nextProcessTime = new Date JavaDoc();
87         public int priority = 0;
88         /**
89          * The constructor that returns the test message.
90          */

91         public TestMessageManager (String JavaDoc id, int priority) {
92             this.id = id;
93             this.priority = priority;
94         }
95         
96         
97         /**
98          * Returns the id of the test message.
99          */

100         public String JavaDoc 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) throws MessageServiceException {
115             
116         }
117         
118         /**
119          * This method returns the next process time.
120          */

121         public Date JavaDoc nextProcessTime() {
122             return nextProcessTime;
123         }
124         
125         /**
126          * This method returns the next process time.
127          */

128         public void setNextProcessTime(Date JavaDoc nextProcessTime) {
129             this.nextProcessTime = nextProcessTime;
130         }
131
132         /**
133          * This message returns the priority.
134          */

135         public int getPriority() {
136             return priority;
137         }
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 JavaDoc o) {
147             TestMessageManager tmsg =(TestMessageManager)o;
148             if (tmsg.nextProcessTime().getTime() > nextProcessTime().getTime()) {
149                 return -1;
150             } else if (nextProcessTime().getTime() > tmsg.nextProcessTime().getTime()) {
151                 return 1;
152             } else if (tmsg.getPriority() > getPriority()) {
153                 return -1;
154             } else if (getPriority() > tmsg.getPriority()) {
155                 return 1;
156             }
157             return 0;
158         }
159         
160         
161         public void commit(Xid JavaDoc xid, boolean b) throws XAException JavaDoc {
162         }
163
164         public void end(Xid JavaDoc xid, int i) throws XAException JavaDoc {
165         }
166
167         public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
168         }
169
170         public int getTransactionTimeout() throws XAException JavaDoc {
171             return -1;
172         }
173
174         public boolean isSameRM(XAResource JavaDoc xAResource) throws XAException JavaDoc {
175             return this == xAResource;
176         }
177
178         public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
179             return 0;
180         }
181
182         public Xid JavaDoc[] recover(int i) throws XAException JavaDoc {
183             return null;
184         }
185
186         public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
187         }
188
189         public boolean setTransactionTimeout(int i) throws XAException JavaDoc {
190             return true;
191         }
192
193         public void start(Xid JavaDoc xid, int i) throws XAException JavaDoc {
194         }
195
196         public String JavaDoc getMessageQueueName() {
197             return "test";
198         }
199
200         public void remove() throws MessageServiceException {
201         }
202         
203     }
204     
205     public MessageQueueTest(String JavaDoc testName) {
206         super(testName);
207         //BasicConfigurator.configure();
208
}
209
210     protected void setUp() throws Exception JavaDoc {
211     }
212
213     protected void tearDown() throws Exception JavaDoc {
214     }
215
216     /**
217      * Test of com.rift.coad.daemon.messageservice.MessageQueue.
218      */

219     public void testMessageQueue() throws Exception JavaDoc {
220         System.out.println("MessageQueue");
221         
222         // init the session information
223
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
224         SessionManager.init(permissions);
225         UserStoreManager userStoreManager = new UserStoreManager();
226         UserSessionManager sessionManager = new UserSessionManager(permissions,
227                 userStoreManager);
228         LoginManager.init(sessionManager,userStoreManager);
229         // instanciate the thread manager
230
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
231                 userStoreManager);
232         
233         // add a user to the session for the current thread
234
RoleManager.getInstance();
235         
236         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
237         
238         // add a new user object and add to the permission
239
Set JavaDoc set = new HashSet JavaDoc();
240         set.add("test");
241         UserSession user = new UserSession("test1", set);
242         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
243                 new ThreadPermissionSession(
244                 new Long JavaDoc(Thread.currentThread().getId()),user));
245         
246         // init the naming director
247
NamingDirector.init(threadGroup);
248         
249         // instanciate the transaction director
250
TransactionDirector transactionDirector = TransactionDirector.init();
251         ObjectLockFactory.init();
252         TransactionManager.init();
253
254         // retrieve the user transaction
255
Context JavaDoc context = new InitialContext JavaDoc();
256         UserTransaction JavaDoc ut =
257                 (UserTransaction JavaDoc)context.lookup("java:comp/UserTransaction");
258         
259         ut.begin();
260         
261         MessageQueue messageQueue = new MessageQueue("test");
262         
263         TestMessageManager message1 = new TestMessageManager("test1",1);
264         TransactionManager.getInstance().bindResource(message1,true);
265         messageQueue.addMessage(message1);
266         
267         ut.rollback();
268         
269         ut.begin();
270         try {
271             messageQueue.getMessage("test1");
272             fail("Retrieved a message that should not be there");
273         } catch (MessageServiceException ex) {
274             System.out.println("Failed to find the message : " +
275                     ex.getMessage());
276         }
277         ut.commit();
278         
279         
280         ut.begin();
281         
282         TestMessageManager message2 = new TestMessageManager("test2",1);
283         TransactionManager.getInstance().bindResource(message1,true);
284         messageQueue.addMessage(message1);
285         TransactionManager.getInstance().bindResource(message2,true);
286         messageQueue.addMessage(message2);
287         
288         ut.commit();
289         
290         ut.begin();
291         
292         if (message1 != messageQueue.getMessage("test1")) {
293             fail("Test message 1 not found");
294         }
295         ut.rollback();
296         
297         Date JavaDoc nextRunTime = new Date JavaDoc();
298         TestMessageManager testRunMessage =
299                 (TestMessageManager)messageQueue.popFrontMessage(nextRunTime);
300         if (testRunMessage == null) {
301             fail("Failed to pop a message from the list");
302         }
303         if (!testRunMessage.getID().equals("test1")) {
304             fail("The pop order is wrong");
305         }
306         
307         ut.begin();
308         TransactionManager.getInstance().bindResource(testRunMessage,true);
309         testRunMessage.setNextProcessTime(new Date JavaDoc(testRunMessage.
310                 nextProcessTime().getTime() + 1000));
311         ut.commit();
312         messageQueue.pushBackMessage(testRunMessage);
313         
314         testRunMessage =
315                 (TestMessageManager)messageQueue.popFrontMessage(nextRunTime);
316         if (testRunMessage == null) {
317             fail("Failed to pop a message from the list");
318         }
319         if (!testRunMessage.getID().equals("test2")) {
320             fail("The pop order is wrong");
321         }
322         
323         ut.begin();
324         TransactionManager.getInstance().bindResource(testRunMessage,true);
325         testRunMessage.setNextProcessTime(new Date JavaDoc(testRunMessage.
326                 nextProcessTime().getTime() + 1000));
327         ut.commit();
328         messageQueue.pushBackMessage(testRunMessage);
329         
330         
331         testRunMessage =
332                 (TestMessageManager)messageQueue.popFrontMessage(nextRunTime);
333         if (testRunMessage != null) {
334             fail("Popped a message from the list");
335         }
336         
337         Thread.sleep(1500);
338         
339         testRunMessage =
340                 (TestMessageManager)messageQueue.popFrontMessage(nextRunTime);
341         if (testRunMessage == null) {
342             fail("Failed to pop a message from the list");
343         }
344         if (!testRunMessage.getID().equals("test1")) {
345             fail("The pop order is wrong");
346         }
347         
348         ut.begin();
349         TransactionManager.getInstance().bindResource(testRunMessage,true);
350         testRunMessage.setNextProcessTime(new Date JavaDoc(testRunMessage.
351                 nextProcessTime().getTime() + 1000));
352         ut.commit();
353         messageQueue.pushBackMessage(testRunMessage);
354         
355         
356         testRunMessage =
357                 (TestMessageManager)messageQueue.popFrontMessage(nextRunTime);
358         if (testRunMessage == null) {
359             fail("Failed to pop a message from the list");
360         }
361         if (!testRunMessage.getID().equals("test2")) {
362             fail("The pop order is wrong");
363         }
364         
365         ut.begin();
366         messageQueue.removeMessage("test2");
367         ut.commit();
368         
369         ut.begin();
370         try {
371             messageQueue.getMessage("test2");
372             fail("Retrieved a message that should not be there");
373         } catch (MessageServiceException ex) {
374             System.out.println("Failed to find the message : " +
375                     ex.getMessage());
376         }
377         ut.commit();
378         
379         ut.begin();
380         messageQueue.removeMessage("test1");
381         ut.commit();
382         
383         ut.begin();
384         try {
385             messageQueue.getMessage("test1");
386             fail("Retrieved a message that should not be there");
387         } catch (MessageServiceException ex) {
388             System.out.println("Failed to find the message : " +
389                     ex.getMessage());
390         }
391         ut.commit();
392     }
393
394     
395 }
396
Popular Tags