KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > util > change > ChangeLogTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2007 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  * ChangeLogTest.java
20  */

21
22 package com.rift.coad.util.change;
23
24 import com.rift.coad.lib.thread.ThreadGroupManager;
25 import java.io.File JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLClassLoader JavaDoc;
28 import junit.framework.*;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Queue JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.concurrent.ConcurrentHashMap JavaDoc;
37 import java.util.concurrent.ConcurrentLinkedQueue JavaDoc;
38 import javax.naming.Context JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.transaction.xa.XAException JavaDoc;
41 import javax.transaction.xa.XAResource JavaDoc;
42 import javax.transaction.xa.Xid JavaDoc;
43 import javax.transaction.UserTransaction JavaDoc;
44
45 // logger
46
import org.apache.log4j.Logger;
47
48
49 // coadunation imports
50
import com.rift.coad.lib.naming.NamingDirector;
51 import com.rift.coad.lib.naming.ContextManager;
52 import com.rift.coad.lib.db.DBSourceManager;
53 import com.rift.coad.lib.thread.CoadunationThread;
54 import com.rift.coad.lib.thread.ThreadStateMonitor;
55 import com.rift.coad.util.transaction.TransactionManager;
56 import com.rift.coad.lib.interceptor.InterceptorFactory;
57 import com.rift.coad.lib.security.RoleManager;
58 import com.rift.coad.lib.security.ThreadsPermissionContainer;
59 import com.rift.coad.lib.security.ThreadPermissionSession;
60 import com.rift.coad.lib.security.UserSession;
61 import com.rift.coad.lib.security.user.UserSessionManager;
62 import com.rift.coad.lib.security.user.UserStoreManager;
63 import com.rift.coad.lib.security.SessionManager;
64 import com.rift.coad.lib.security.login.LoginManager;
65 import com.rift.coad.lib.thread.CoadunationThreadGroup;
66
67 import com.rift.coad.lib.configuration.Configuration;
68 import com.rift.coad.lib.configuration.ConfigurationFactory;
69 import com.rift.coad.util.lock.LockRef;
70 import com.rift.coad.util.lock.ObjectLockFactory;
71 import com.rift.coad.lib.transaction.TransactionDirector;
72
73
74 /**
75  * This object is responsible for testing the change log object.
76  *
77  * @author Brett Chaldecott
78  */

79 public class ChangeLogTest extends TestCase {
80     
81     /**
82      * This object is responsible for representing a change.
83      */

84     public static class TestChange implements Change {
85         
86         /**
87          * The constructor of the test change.
88          */

89         public TestChange() {
90             
91         }
92         
93         
94         /**
95          * The definition of the apply method.
96          */

97         public void applyChanges() throws ChangeException {
98             changeCount++;
99         }
100     }
101     
102     public ChangeLogTest(String JavaDoc testName) {
103         super(testName);
104     }
105
106     protected void setUp() throws Exception JavaDoc {
107     }
108
109     protected void tearDown() throws Exception JavaDoc {
110     }
111     
112     public static int changeCount = 0;
113     /**
114      * Test of class com.rift.coad.util.change.ChangeLog.
115      */

116     public void testChangeLog() throws Exception JavaDoc {
117         System.out.println("testChangeLog");
118         
119         // init the session information
120
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
121         SessionManager.init(permissions);
122         UserStoreManager userStoreManager = new UserStoreManager();
123         UserSessionManager sessionManager = new UserSessionManager(permissions,
124                 userStoreManager);
125         LoginManager.init(sessionManager,userStoreManager);
126         // instanciate the thread manager
127
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
128                 userStoreManager);
129         
130         // add a user to the session for the current thread
131
RoleManager.getInstance();
132         
133         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
134         
135         // add a new user object and add to the permission
136
Set JavaDoc set = new HashSet JavaDoc();
137         set.add("test");
138         UserSession user = new UserSession("test1", set);
139         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
140                 new ThreadPermissionSession(
141                 new Long JavaDoc(Thread.currentThread().getId()),user));
142         
143         // instanciate the thread manager
144
Thread.currentThread().setContextClassLoader(this.getClass().
145                 getClassLoader());
146         ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
147         
148         
149         // init the naming director
150
NamingDirector.init(threadGroup);
151         
152         // instanciate the transaction director
153
TransactionDirector transactionDirector = TransactionDirector.init();
154         
155         // init the transaction manager
156
ObjectLockFactory.init();
157         TransactionManager.init();
158         
159         String JavaDoc username = "";
160         
161         ChangeLog.init(ChangeLog.class);
162         
163         // retrieve the user transaction
164
Context JavaDoc context = new InitialContext JavaDoc();
165         UserTransaction JavaDoc ut =
166                 (UserTransaction JavaDoc)context.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 JavaDoc dumpFile = new File JavaDoc("./tmp/changelog.dmp");
179         if (!dumpFile.exists()) {
180             fail("The dump file does not exist changeCount : " + changeCount);
181         }
182         
183         // init the change log
184
ChangeLog.init(ChangeLog.class);
185         ChangeLog.getInstance().start();
186         
187         Thread.sleep(500);
188         
189         if (changeCount != 10000) {
190             fail("Failed to apply 10000 changes");
191         }
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     
202 }
203
Popular Tags