001: /*
002: * MessageManagerFactoryTest.java
003: * JUnit based test
004: *
005: * Created on January 15, 2007, 9:31 PM
006: */
007:
008: package com.rift.coad.daemon.messageservice.message;
009:
010: // java imports
011: import com.rift.coad.lib.thread.ThreadGroupManager;
012: import com.rift.coad.util.change.ChangeLog;
013: import javax.naming.InitialContext;
014: import javax.naming.Context;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.Statement;
018: import javax.sql.DataSource;
019: import java.util.Set;
020: import java.util.HashSet;
021: import javax.transaction.UserTransaction;
022: import java.sql.Timestamp;
023: import java.util.ArrayList;
024: import java.util.Date;
025: import java.util.Enumeration;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Set;
030: import javax.transaction.xa.XAException;
031: import javax.transaction.xa.XAResource;
032: import javax.transaction.xa.Xid;
033: import org.hibernate.*;
034: import org.hibernate.cfg.*;
035: import org.apache.log4j.Logger;
036: import org.apache.log4j.BasicConfigurator;
037:
038: // junit imports
039: import junit.framework.*;
040:
041: // object web imports
042: import org.objectweb.jotm.Jotm;
043:
044: // coadunation imports
045: import com.rift.coad.lib.naming.NamingDirector;
046: import com.rift.coad.lib.naming.ContextManager;
047: import com.rift.coad.lib.db.DBSourceManager;
048: import com.rift.coad.lib.common.ObjectSerializer;
049: import com.rift.coad.lib.interceptor.InterceptorFactory;
050: import com.rift.coad.lib.security.RoleManager;
051: import com.rift.coad.lib.security.ThreadsPermissionContainer;
052: import com.rift.coad.lib.security.ThreadPermissionSession;
053: import com.rift.coad.lib.security.UserSession;
054: import com.rift.coad.lib.security.user.UserSessionManager;
055: import com.rift.coad.lib.security.user.UserStoreManager;
056: import com.rift.coad.lib.security.SessionManager;
057: import com.rift.coad.lib.security.login.LoginManager;
058: import com.rift.coad.lib.thread.CoadunationThreadGroup;
059: import com.rift.coad.lib.transaction.TransactionDirector;
060: import com.rift.coad.daemon.messageservice.Message;
061: import com.rift.coad.daemon.messageservice.RPCMessage;
062: import com.rift.coad.daemon.messageservice.TextMessage;
063: import com.rift.coad.daemon.messageservice.MessageManager;
064: import com.rift.coad.daemon.messageservice.MessageServiceException;
065: import com.rift.coad.daemon.messageservice.MessageServiceManager;
066: import com.rift.coad.daemon.messageservice.db.*;
067: import com.rift.coad.daemon.messageservice.message.MessageImpl;
068: import com.rift.coad.daemon.messageservice.message.RPCMessageImpl;
069: import com.rift.coad.daemon.messageservice.message.TextMessageImpl;
070: import com.rift.coad.hibernate.util.HibernateUtil;
071: import com.rift.coad.util.lock.ObjectLockFactory;
072: import com.rift.coad.util.transaction.TransactionManager;
073:
074: /**
075: *
076: * @author mincemeat
077: */
078: public class MessageManagerFactoryTest extends TestCase {
079:
080: public MessageManagerFactoryTest(String testName) {
081: super (testName);
082: }
083:
084: protected void setUp() throws Exception {
085: }
086:
087: protected void tearDown() throws Exception {
088: }
089:
090: /**
091: * Test of class com.rift.coad.daemon.messageservice.message.MessageManagerFactory.
092: */
093: public void testMessageManagerFactory() throws Exception {
094: System.out.println("MessageManagerFactory");
095:
096: Thread.currentThread().setContextClassLoader(
097: this .getClass().getClassLoader());
098:
099: // init the session information
100: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
101: SessionManager.init(permissions);
102: UserStoreManager userStoreManager = new UserStoreManager();
103: UserSessionManager sessionManager = new UserSessionManager(
104: permissions, userStoreManager);
105: LoginManager.init(sessionManager, userStoreManager);
106: // instanciate the thread manager
107: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
108: sessionManager, userStoreManager);
109:
110: // add a user to the session for the current thread
111: RoleManager.getInstance();
112:
113: InterceptorFactory.init(permissions, sessionManager,
114: userStoreManager);
115:
116: // add a new user object and add to the permission
117: Set set = new HashSet();
118: set.add("test");
119: UserSession user = new UserSession("test1", set);
120: permissions.putSession(
121: new Long(Thread.currentThread().getId()),
122: new ThreadPermissionSession(new Long(Thread
123: .currentThread().getId()), user));
124:
125: // instanciate the thread manager
126: ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
127:
128: // init the naming director
129: NamingDirector.init(threadGroup);
130:
131: // instanciate the transaction director
132: TransactionDirector transactionDirector = TransactionDirector
133: .init();
134:
135: // init the database source
136: DBSourceManager.init();
137: ObjectLockFactory.init();
138: TransactionManager.init();
139: ChangeLog.init(MessageServiceManager.class);
140: ChangeLog.getInstance().start();
141:
142: Context context = new InitialContext();
143:
144: List principals = new ArrayList(set);
145: Date currentTime = new Date();
146: TextMessageImpl textMessage = new TextMessageImpl("test",
147: "test", "test", principals, Message.UNDELIVERED);
148: textMessage.setFrom("test");
149: textMessage.setTextBody("test text");
150: textMessage.setStringProperty("TESTString", "TESTvalue");
151: textMessage.setBooleanProperty("TESTBoolean", true);
152: textMessage.setByteProperty("TESTByte", (byte) 1);
153: textMessage.setCorrelationId("TESTCorrelationId");
154: textMessage.setDoubleProperty("TESTDouble", 1.1);
155: textMessage.setFloatProperty("TESTFloat", (float) 2.2);
156: textMessage.setFrom("test@test.com");
157: textMessage.setIntProperty("TESTInt", 100);
158: textMessage.setLongProperty("TESTLong", (long) 2000);
159: textMessage.setTargetNamedQueue("QUEUEName");
160: textMessage.setReplyNamedQueue("ReplyQUEUEName");
161: textMessage.setNextProcessDate(currentTime);
162: textMessage.setObjectProperty("TESTObject",
163: "This is a test obj");
164: textMessage.setPriority(1000);
165: textMessage.setProcessedDate(currentTime);
166: textMessage.setReply(true);
167: textMessage.setServices(new String[] { "test1", "test2",
168: "test3" });
169: textMessage.setTarget("target");
170: UserTransaction ut = (UserTransaction) context
171: .lookup("java:comp/UserTransaction");
172:
173: ut.begin();
174:
175: MessageManager instance = MessageManagerFactory.getInstance()
176: .getMessageManager(textMessage);
177:
178: ut.commit();
179:
180: Thread.sleep(30000);
181:
182: ut.begin();
183:
184: MessageManager instance2 = MessageManagerFactory.getInstance()
185: .getMessageManager("test");
186:
187: ut.commit();
188:
189: assertEquals(instance2.getID(), instance.getID());
190:
191: ut.begin();
192:
193: instance2.remove();
194:
195: ut.commit();
196:
197: Thread.sleep(30000);
198:
199: ChangeLog.getInstance().terminate();
200: }
201:
202: }
|