Source Code Cross Referenced for ThreadPoolManagerTest.java in  » Net » Coadunation_1.0.1 » com » rift » coad » lib » thread » pool » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Coadunation_1.0.1 » com.rift.coad.lib.thread.pool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * CoadunationLib: The coadunation core 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:         * ThreadPoolManagerTest.java
020:         */
021:
022:        package com.rift.coad.lib.thread.pool;
023:
024:        import junit.framework.*;
025:        import java.util.Vector;
026:        import java.util.HashSet;
027:        import java.util.List;
028:        import java.util.Date;
029:        import java.util.ArrayList;
030:        import java.util.Set;
031:        import java.util.concurrent.atomic.AtomicInteger;
032:        import java.net.URLClassLoader;
033:        import java.net.URL;
034:
035:        import org.apache.log4j.Logger;
036:        import org.apache.log4j.BasicConfigurator;
037:
038:        import com.rift.coad.lib.common.ClassUtil;
039:        import com.rift.coad.lib.thread.CoadunationThread;
040:        import com.rift.coad.lib.thread.ThreadStateMonitor;
041:        import com.rift.coad.lib.security.UserSession;
042:        import com.rift.coad.lib.security.user.UserSessionManager;
043:        import com.rift.coad.lib.configuration.ConfigurationFactory;
044:        import com.rift.coad.lib.configuration.Configuration;
045:        import com.rift.coad.lib.security.user.UserStoreManager;
046:        import com.rift.coad.lib.security.ThreadsPermissionContainer;
047:        import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
048:        import com.rift.coad.lib.security.SessionManager;
049:        import com.rift.coad.lib.security.RoleManager;
050:        import com.rift.coad.lib.security.Validator;
051:        import com.rift.coad.lib.security.login.LoginManager;
052:        import com.rift.coad.lib.thread.CoadunationThread;
053:        import com.rift.coad.lib.thread.CoadunationThreadGroup;
054:        import com.rift.coad.lib.thread.ThreadGroupManager;
055:        import com.rift.coad.lib.security.ThreadPermissionSession;
056:
057:        /**
058:         * The test of the thread pool manager.
059:         *
060:         * @author Brett Chaldecott
061:         */
062:        public class ThreadPoolManagerTest extends TestCase {
063:
064:            /**
065:             * This object is called by the test task object.
066:             */
067:            public static class TestMonitor {
068:
069:                private int waitCount = 0;
070:                private int callCount = 0;
071:
072:                /**
073:                 * This class is used to monitor the test
074:                 */
075:                public TestMonitor() {
076:
077:                }
078:
079:                /**
080:                 * This method is called to by the threads to wait indefinitly.
081:                 */
082:                public synchronized void threadWait() {
083:                    waitCount++;
084:                    callCount++;
085:                    try {
086:                        wait();
087:                    } catch (Exception ex) {
088:                        System.out.println("Failed to wait : "
089:                                + ex.getMessage());
090:                        ex.printStackTrace(System.out);
091:                    }
092:                    waitCount--;
093:                }
094:
095:                /**
096:                 * This returns the wait count
097:                 */
098:                public synchronized int getWaitCount() {
099:                    return waitCount;
100:                }
101:
102:                /**
103:                 * This method returns the call count
104:                 */
105:                public synchronized int getCallCount() {
106:                    return callCount;
107:                }
108:
109:                /**
110:                 * This method resets the called count
111:                 */
112:                public synchronized void resetCalledCount() {
113:                    callCount = 0;
114:                }
115:
116:                /**
117:                 * This method is called to release all waiting threads
118:                 */
119:                public synchronized void notifyAllWaitingThreads() {
120:                    notifyAll();
121:                }
122:            }
123:
124:            /**
125:             * This class is responsible for processing as a task.
126:             */
127:            public static class TestTask implements  Task {
128:
129:                /**
130:                 * The process method used by the task object.
131:                 */
132:                public void process(ThreadPoolManager pool) throws Exception {
133:                    testMonitor.threadWait();
134:                }
135:
136:            }
137:
138:            public static TestMonitor testMonitor = new TestMonitor();
139:
140:            public ThreadPoolManagerTest(String testName) {
141:                super (testName);
142:                BasicConfigurator.configure();
143:            }
144:
145:            protected void setUp() throws Exception {
146:            }
147:
148:            protected void tearDown() throws Exception {
149:            }
150:
151:            /**
152:             * Test com.rift.coad.lib.thread.pool.ThreadPoolManager.
153:             */
154:            public void testThreadPoolManager() throws Exception {
155:                System.out.println("ThreadPoolManager");
156:
157:                // initialize the thread permissions
158:                ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
159:                SessionManager.init(permissions);
160:                UserStoreManager userStoreManager = new UserStoreManager();
161:                UserSessionManager sessionManager = new UserSessionManager(
162:                        permissions, userStoreManager);
163:                LoginManager.init(sessionManager, userStoreManager);
164:
165:                // add a user to the session for the current thread
166:                RoleManager.getInstance();
167:
168:                // instanciate the thread manager
169:                CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
170:                        sessionManager, userStoreManager);
171:                ClassLoader loader = new URLClassLoader(new URL[0], this 
172:                        .getClass().getClassLoader());
173:                Thread.currentThread().setContextClassLoader(loader);
174:                ThreadGroupManager.getInstance().initThreadGroup(threadGroup);
175:
176:                // add a new user object and add to the permission
177:                Set set = new HashSet();
178:                set.add("test");
179:                UserSession user = new UserSession("test1", set);
180:                permissions.putSession(
181:                        new Long(Thread.currentThread().getId()),
182:                        new ThreadPermissionSession(new Long(Thread
183:                                .currentThread().getId()), user));
184:
185:                // start the thread pool
186:                ThreadPoolManager instance1 = new ThreadPoolManager(2,
187:                        TestTask.class, "test");
188:                ThreadPoolManager instance2 = new ThreadPoolManager(3, 5,
189:                        TestTask.class, "test");
190:
191:                assertEquals(2, instance1.getSize());
192:                instance1.setSize(3);
193:                assertEquals(3, instance1.getSize());
194:                assertEquals(3, instance2.getMinSize());
195:                instance2.setMinSize(4);
196:                assertEquals(4, instance2.getMinSize());
197:                try {
198:                    instance2.setMinSize(6);
199:                    fail("Should not be able to set the pool size to greater than max.");
200:                } catch (PoolException ex) {
201:                    // ignore correct
202:                }
203:                assertEquals(5, instance2.getMaxSize());
204:                try {
205:                    instance2.setMaxSize(3);
206:                    fail("Should not be able to set the pool size max to smaller "
207:                            + "than max.");
208:                } catch (PoolException ex) {
209:                    // ignore correct
210:                }
211:
212:                assertEquals(2, testMonitor.getWaitCount());
213:                assertEquals(2, testMonitor.getCallCount());
214:
215:                instance1.releaseThread();
216:                instance2.releaseThread();
217:                Thread.sleep(500);
218:
219:                assertEquals(4, testMonitor.getCallCount());
220:                assertEquals(4, testMonitor.getWaitCount());
221:
222:                instance1.releaseThread();
223:                instance1.releaseThread();
224:                instance2.releaseThread();
225:                instance2.releaseThread();
226:                instance2.releaseThread();
227:                instance2.releaseThread();
228:                Thread.sleep(500);
229:
230:                assertEquals(8, testMonitor.getCallCount());
231:                assertEquals(8, testMonitor.getWaitCount());
232:
233:                testMonitor.notifyAllWaitingThreads();
234:
235:                instance1.releaseThread();
236:                instance1.releaseThread();
237:                instance2.releaseThread();
238:                instance2.releaseThread();
239:                Thread.sleep(500);
240:
241:                assertEquals(6, testMonitor.getWaitCount());
242:                assertEquals(14, testMonitor.getCallCount());
243:
244:                instance1.terminate();
245:                instance2.terminate();
246:            }
247:
248:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.