001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2007 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: * ChangeLogTest.java
020: */
021:
022: package com.rift.coad.util.change;
023:
024: import com.rift.coad.lib.thread.ThreadGroupManager;
025: import java.io.File;
026: import java.net.URL;
027: import java.net.URLClassLoader;
028: import junit.framework.*;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.Queue;
034: import java.util.Set;
035: import java.util.HashSet;
036: import java.util.concurrent.ConcurrentHashMap;
037: import java.util.concurrent.ConcurrentLinkedQueue;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import javax.transaction.xa.XAException;
041: import javax.transaction.xa.XAResource;
042: import javax.transaction.xa.Xid;
043: import javax.transaction.UserTransaction;
044:
045: // logger
046: import org.apache.log4j.Logger;
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: import com.rift.coad.lib.thread.CoadunationThread;
053: import com.rift.coad.lib.thread.ThreadStateMonitor;
054: import com.rift.coad.util.transaction.TransactionManager;
055: import com.rift.coad.lib.interceptor.InterceptorFactory;
056: import com.rift.coad.lib.security.RoleManager;
057: import com.rift.coad.lib.security.ThreadsPermissionContainer;
058: import com.rift.coad.lib.security.ThreadPermissionSession;
059: import com.rift.coad.lib.security.UserSession;
060: import com.rift.coad.lib.security.user.UserSessionManager;
061: import com.rift.coad.lib.security.user.UserStoreManager;
062: import com.rift.coad.lib.security.SessionManager;
063: import com.rift.coad.lib.security.login.LoginManager;
064: import com.rift.coad.lib.thread.CoadunationThreadGroup;
065:
066: import com.rift.coad.lib.configuration.Configuration;
067: import com.rift.coad.lib.configuration.ConfigurationFactory;
068: import com.rift.coad.util.lock.LockRef;
069: import com.rift.coad.util.lock.ObjectLockFactory;
070: import com.rift.coad.lib.transaction.TransactionDirector;
071:
072: /**
073: * This object is responsible for testing the change log object.
074: *
075: * @author Brett Chaldecott
076: */
077: public class ChangeLogTest extends TestCase {
078:
079: /**
080: * This object is responsible for representing a change.
081: */
082: public static class TestChange implements Change {
083:
084: /**
085: * The constructor of the test change.
086: */
087: public TestChange() {
088:
089: }
090:
091: /**
092: * The definition of the apply method.
093: */
094: public void applyChanges() throws ChangeException {
095: changeCount++;
096: }
097: }
098:
099: public ChangeLogTest(String testName) {
100: super (testName);
101: }
102:
103: protected void setUp() throws Exception {
104: }
105:
106: protected void tearDown() throws Exception {
107: }
108:
109: public static int changeCount = 0;
110:
111: /**
112: * Test of class com.rift.coad.util.change.ChangeLog.
113: */
114: public void testChangeLog() throws Exception {
115: System.out.println("testChangeLog");
116:
117: // init the session information
118: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
119: SessionManager.init(permissions);
120: UserStoreManager userStoreManager = new UserStoreManager();
121: UserSessionManager sessionManager = new UserSessionManager(
122: permissions, userStoreManager);
123: LoginManager.init(sessionManager, userStoreManager);
124: // instanciate the thread manager
125: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
126: sessionManager, userStoreManager);
127:
128: // add a user to the session for the current thread
129: RoleManager.getInstance();
130:
131: InterceptorFactory.init(permissions, sessionManager,
132: userStoreManager);
133:
134: // add a new user object and add to the permission
135: Set set = new HashSet();
136: set.add("test");
137: UserSession user = new UserSession("test1", set);
138: permissions.putSession(
139: new Long(Thread.currentThread().getId()),
140: new ThreadPermissionSession(new Long(Thread
141: .currentThread().getId()), user));
142:
143: // instanciate the thread manager
144: Thread.currentThread().setContextClassLoader(
145: this .getClass().getClassLoader());
146: ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
147:
148: // init the naming director
149: NamingDirector.init(threadGroup);
150:
151: // instanciate the transaction director
152: TransactionDirector transactionDirector = TransactionDirector
153: .init();
154:
155: // init the transaction manager
156: ObjectLockFactory.init();
157: TransactionManager.init();
158:
159: String username = "";
160:
161: ChangeLog.init(ChangeLog.class);
162:
163: // retrieve the user transaction
164: Context context = new InitialContext();
165: UserTransaction ut = (UserTransaction) context
166: .lookup("java:comp/UserTransaction");
167:
168: for (int index = 0; index < 1000; index++) {
169: ut.begin();
170: for (int count = 0; count < 10; count++) {
171: ChangeLog.getInstance().addChange(new TestChange());
172: }
173: ut.commit();
174: }
175:
176: ChangeLog.terminate();
177:
178: File dumpFile = new File("./tmp/changelog.dmp");
179: if (!dumpFile.exists()) {
180: fail("The dump file does not exist changeCount : "
181: + changeCount);
182: }
183:
184: // init the change log
185: ChangeLog.init(ChangeLog.class);
186: ChangeLog.getInstance().start();
187:
188: Thread.sleep(500);
189:
190: if (changeCount != 10000) {
191: fail("Failed to apply 10000 changes");
192: }
193:
194: ChangeLog.terminate();
195:
196: if (dumpFile.exists()) {
197: fail("The dump file does exist should have been removed");
198: }
199: }
200:
201: }
|