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: * MessageProducerImplTest.java
020: */
021:
022: // package path
023: package com.rift.coad.daemon.messageservice;
024:
025: // java imports
026: import javax.naming.InitialContext;
027: import javax.naming.Context;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.Statement;
031: import javax.sql.DataSource;
032: import java.util.Set;
033: import java.util.HashSet;
034: import javax.transaction.UserTransaction;
035: import java.sql.Timestamp;
036: import java.util.ArrayList;
037: import java.util.Date;
038: import java.util.Enumeration;
039: import java.util.HashSet;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.Set;
043: import javax.transaction.xa.XAException;
044: import javax.transaction.xa.XAResource;
045: import javax.transaction.xa.Xid;
046: import org.hibernate.*;
047: import org.hibernate.cfg.*;
048: import org.apache.log4j.Logger;
049: import org.apache.log4j.BasicConfigurator;
050:
051: // junit imports
052: import junit.framework.*;
053:
054: // object web imports
055: import org.objectweb.jotm.Jotm;
056:
057: // coadunation imports
058: import com.rift.coad.lib.naming.NamingDirector;
059: import com.rift.coad.lib.naming.ContextManager;
060: import com.rift.coad.lib.db.DBSourceManager;
061: import com.rift.coad.lib.common.ObjectSerializer;
062: import com.rift.coad.lib.interceptor.InterceptorFactory;
063: import com.rift.coad.lib.security.RoleManager;
064: import com.rift.coad.lib.security.ThreadsPermissionContainer;
065: import com.rift.coad.lib.security.ThreadPermissionSession;
066: import com.rift.coad.lib.security.UserSession;
067: import com.rift.coad.lib.security.user.UserSessionManager;
068: import com.rift.coad.lib.security.user.UserStoreManager;
069: import com.rift.coad.lib.security.SessionManager;
070: import com.rift.coad.lib.security.login.LoginManager;
071: import com.rift.coad.lib.thread.CoadunationThreadGroup;
072: import com.rift.coad.lib.transaction.TransactionDirector;
073: import com.rift.coad.daemon.messageservice.Message;
074: import com.rift.coad.daemon.messageservice.RPCMessage;
075: import com.rift.coad.daemon.messageservice.TextMessage;
076: import com.rift.coad.daemon.messageservice.MessageManager;
077: import com.rift.coad.daemon.messageservice.MessageServiceException;
078: import com.rift.coad.daemon.messageservice.MessageServiceManager;
079: import com.rift.coad.daemon.messageservice.db.*;
080: import com.rift.coad.daemon.messageservice.message.MessageImpl;
081: import com.rift.coad.daemon.messageservice.message.RPCMessageImpl;
082: import com.rift.coad.daemon.messageservice.message.TextMessageImpl;
083: import com.rift.coad.hibernate.util.HibernateUtil;
084: import com.rift.coad.util.lock.ObjectLockFactory;
085: import com.rift.coad.util.transaction.TransactionManager;
086: import com.rift.coad.lib.thread.ThreadGroupManager;
087: import com.rift.coad.util.change.ChangeLog;
088:
089: /**
090: *
091: * @author Brett Chaldecott
092: */
093: public class MessageProducerImplTest extends TestCase {
094:
095: public MessageProducerImplTest(String testName) {
096: super (testName);
097: //BasicConfigurator.configure();
098: }
099:
100: protected void setUp() throws Exception {
101: }
102:
103: protected void tearDown() throws Exception {
104: }
105:
106: /**
107: * Test of com.rift.coad.daemon.messageservice.MessageProducerImpl.
108: */
109: public void testMessageProducer() throws Exception {
110: System.out.println("testMessageProducer");
111:
112: Thread.currentThread().setContextClassLoader(
113: this .getClass().getClassLoader());
114:
115: // init the session information
116: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
117: SessionManager.init(permissions);
118: UserStoreManager userStoreManager = new UserStoreManager();
119: UserSessionManager sessionManager = new UserSessionManager(
120: permissions, userStoreManager);
121: LoginManager.init(sessionManager, userStoreManager);
122: // instanciate the thread manager
123: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
124: sessionManager, userStoreManager);
125:
126: // add a user to the session for the current thread
127: RoleManager.getInstance();
128:
129: InterceptorFactory.init(permissions, sessionManager,
130: userStoreManager);
131:
132: // add a new user object and add to the permission
133: Set set = new HashSet();
134: set.add("test");
135: UserSession user = new UserSession("test1", set);
136: permissions.putSession(
137: new Long(Thread.currentThread().getId()),
138: new ThreadPermissionSession(new Long(Thread
139: .currentThread().getId()), user));
140:
141: // instanciate the thread manager
142: ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
143:
144: // init the naming director
145: NamingDirector.init(threadGroup);
146:
147: // instanciate the transaction director
148: TransactionDirector transactionDirector = TransactionDirector
149: .init();
150:
151: // init the database source
152: DBSourceManager.init();
153: Context context = new InitialContext();
154: ObjectLockFactory.init();
155: TransactionManager.init();
156: ChangeLog.init(MessageServiceManager.class);
157: ChangeLog.getInstance().start();
158:
159: MessageProducerImpl instance = new MessageProducerImpl();
160:
161: Producer result = instance.createProducer("test1");
162: Producer result2 = instance.createProducer("test1");
163: if (result == result2) {
164: fail("Should get a new producer every time");
165: }
166:
167: UserTransaction ut = (UserTransaction) context
168: .lookup("java:comp/UserTransaction");
169:
170: RPCMessage rpcMessage = result
171: .createRPCMessage(Message.POINT_TO_POINT);
172: if (rpcMessage.getProcessedDate() == null) {
173: fail("The processed date is equal to null");
174: }
175: rpcMessage.setMethodBodyXML("xml body");
176: ut.begin();
177: result.submit(rpcMessage);
178: ut.commit();
179:
180: TextMessage textMessage = result
181: .createTextMessage(Message.POINT_TO_POINT);
182: textMessage.setTextBody("Text body");
183: ut.begin();
184: result.submit(textMessage);
185: ut.commit();
186:
187: Thread.sleep(30000);
188:
189: ut.begin();
190: Session session = HibernateUtil.getInstance(
191: MessageServiceManager.class).getSession();
192: List messages = session.createQuery("From Message as msg")
193: .list();
194: if (messages.size() != 2) {
195: fail("Did not create enough messages");
196: }
197: ut.commit();
198:
199: Thread.sleep(30000);
200:
201: ChangeLog.getInstance().terminate();
202: }
203:
204: }
|