KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > httpd > HttpRequestManagerTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 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  * HttpRequestManagerTest.java
20  *
21  * JUnit based test
22  */

23
24 // package imports
25
package com.rift.coad.lib.httpd;
26
27 // java imports
28
import java.lang.reflect.InvocationHandler JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.lang.reflect.Proxy JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 // junit imports
34
import junit.framework.*;
35
36 // login imports
37
import org.apache.log4j.Logger;
38 import org.apache.log4j.BasicConfigurator;
39 import org.apache.log4j.ConsoleAppender;
40 import org.apache.log4j.Priority;
41
42 // coadunation imports
43
import com.rift.coad.lib.thread.BasicThread;
44 import com.rift.coad.lib.thread.CoadunationThreadGroup;
45 import com.rift.coad.lib.thread.ThreadStateMonitor;
46 import com.rift.coad.lib.configuration.Configuration;
47 import com.rift.coad.lib.configuration.ConfigurationFactory;
48 import com.rift.coad.lib.security.RoleManager;
49 import com.rift.coad.lib.security.SessionManager;
50 import com.rift.coad.lib.security.ThreadsPermissionContainer;
51 import com.rift.coad.lib.security.user.UserStoreManager;
52 import com.rift.coad.lib.security.user.UserSessionManager;
53 import com.rift.coad.lib.security.login.LoginManager;
54
55 /**
56  *
57  * @author mincemeat
58  */

59 public class HttpRequestManagerTest extends TestCase {
60     
61     public class TestHandler implements InvocationHandler JavaDoc {
62         
63         // number of tests
64
private int numTests = 0;
65         private int countTests = 0;
66         
67         
68         /**
69          * The default handler
70          */

71         public TestHandler() {
72             
73         }
74         
75         /**
76          * The constructor of the handler
77          *
78          * @param numTests The number of tests.
79          */

80         public TestHandler(int numTests) {
81             this.numTests = numTests * 2;
82         }
83         
84         
85         /**
86          * This method will be called to invoke the call.
87          */

88         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) {
89             System.out.println("Handler has been called");
90             synchronized (this) {
91                 countTests++;
92                 notify();
93             }
94             return null;
95         }
96         
97         
98         /**
99          * This method will be called to wait on the test
100          *
101          * @exception Exception
102          */

103         public synchronized void waitOnTest() throws Exception JavaDoc {
104             while (countTests < numTests) {
105                 wait();
106             }
107         }
108     }
109     
110     public HttpRequestManagerTest(String JavaDoc testName) {
111         super(testName);
112         BasicConfigurator.configure();
113     }
114
115     protected void setUp() throws Exception JavaDoc {
116     }
117
118     protected void tearDown() throws Exception JavaDoc {
119     }
120
121     public static Test suite() {
122         TestSuite suite = new TestSuite(HttpRequestManagerTest.class);
123         
124         return suite;
125     }
126
127     /**
128      * Test of addRequest method, of class com.rift.coad.lib.httpd.HttpRequestManager.
129      */

130     public void testAddRequest() throws Exception JavaDoc {
131         System.out.println("addRequest");
132         
133         // instanciate the deployment loader
134
ThreadsPermissionContainer permissionContainer =
135                 new ThreadsPermissionContainer();
136         SessionManager.init(permissionContainer);
137         UserStoreManager userStoreManager = new UserStoreManager();
138         UserSessionManager sessionManager = new UserSessionManager(
139                 permissionContainer,userStoreManager);
140         LoginManager.init(sessionManager,userStoreManager);
141         
142         // add a user to the session for the current thread
143
RoleManager.getInstance();
144         
145         // instanciate the thread manager
146
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
147             userStoreManager);
148         
149         
150         HttpRequestManager instance = new HttpRequestManager(threadGroup);
151         
152         // Init the test handler
153
TestHandler testHandler = new TestHandler(100);
154         for (int index = 0; index < 100; index++) {
155             // add the proxy object
156
RequestInterface requestInterface =
157                     (RequestInterface)Proxy.newProxyInstance(
158                     RequestInterface.class.getClassLoader(),
159                     new Class JavaDoc[] {RequestInterface.class},
160                     testHandler);
161             System.out.println("Add entry to request [" + (index + 1) + "]");
162             instance.addRequest(requestInterface);
163         }
164         
165         System.out.println("Wait on the test.");
166         testHandler.waitOnTest();
167     }
168     
169 }
170
Popular Tags