001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 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: * HttpRequestManagerTest.java
020: *
021: * JUnit based test
022: */
023:
024: // package imports
025: package com.rift.coad.lib.httpd;
026:
027: // java imports
028: import java.lang.reflect.InvocationHandler;
029: import java.lang.reflect.Method;
030: import java.lang.reflect.Proxy;
031: import java.util.Vector;
032:
033: // junit imports
034: import junit.framework.*;
035:
036: // login imports
037: import org.apache.log4j.Logger;
038: import org.apache.log4j.BasicConfigurator;
039: import org.apache.log4j.ConsoleAppender;
040: import org.apache.log4j.Priority;
041:
042: // coadunation imports
043: import com.rift.coad.lib.thread.BasicThread;
044: import com.rift.coad.lib.thread.CoadunationThreadGroup;
045: import com.rift.coad.lib.thread.ThreadStateMonitor;
046: import com.rift.coad.lib.configuration.Configuration;
047: import com.rift.coad.lib.configuration.ConfigurationFactory;
048: import com.rift.coad.lib.security.RoleManager;
049: import com.rift.coad.lib.security.SessionManager;
050: import com.rift.coad.lib.security.ThreadsPermissionContainer;
051: import com.rift.coad.lib.security.user.UserStoreManager;
052: import com.rift.coad.lib.security.user.UserSessionManager;
053: import com.rift.coad.lib.security.login.LoginManager;
054:
055: /**
056: *
057: * @author mincemeat
058: */
059: public class HttpRequestManagerTest extends TestCase {
060:
061: public class TestHandler implements InvocationHandler {
062:
063: // number of tests
064: private int numTests = 0;
065: private int countTests = 0;
066:
067: /**
068: * The default handler
069: */
070: public TestHandler() {
071:
072: }
073:
074: /**
075: * The constructor of the handler
076: *
077: * @param numTests The number of tests.
078: */
079: public TestHandler(int numTests) {
080: this .numTests = numTests * 2;
081: }
082:
083: /**
084: * This method will be called to invoke the call.
085: */
086: public Object invoke(Object proxy, Method method, Object[] args) {
087: System.out.println("Handler has been called");
088: synchronized (this ) {
089: countTests++;
090: notify();
091: }
092: return null;
093: }
094:
095: /**
096: * This method will be called to wait on the test
097: *
098: * @exception Exception
099: */
100: public synchronized void waitOnTest() throws Exception {
101: while (countTests < numTests) {
102: wait();
103: }
104: }
105: }
106:
107: public HttpRequestManagerTest(String testName) {
108: super (testName);
109: BasicConfigurator.configure();
110: }
111:
112: protected void setUp() throws Exception {
113: }
114:
115: protected void tearDown() throws Exception {
116: }
117:
118: public static Test suite() {
119: TestSuite suite = new TestSuite(HttpRequestManagerTest.class);
120:
121: return suite;
122: }
123:
124: /**
125: * Test of addRequest method, of class com.rift.coad.lib.httpd.HttpRequestManager.
126: */
127: public void testAddRequest() throws Exception {
128: System.out.println("addRequest");
129:
130: // instanciate the deployment loader
131: ThreadsPermissionContainer permissionContainer = new ThreadsPermissionContainer();
132: SessionManager.init(permissionContainer);
133: UserStoreManager userStoreManager = new UserStoreManager();
134: UserSessionManager sessionManager = new UserSessionManager(
135: permissionContainer, userStoreManager);
136: LoginManager.init(sessionManager, userStoreManager);
137:
138: // add a user to the session for the current thread
139: RoleManager.getInstance();
140:
141: // instanciate the thread manager
142: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
143: sessionManager, userStoreManager);
144:
145: HttpRequestManager instance = new HttpRequestManager(
146: threadGroup);
147:
148: // Init the test handler
149: TestHandler testHandler = new TestHandler(100);
150: for (int index = 0; index < 100; index++) {
151: // add the proxy object
152: RequestInterface requestInterface = (RequestInterface) Proxy
153: .newProxyInstance(RequestInterface.class
154: .getClassLoader(),
155: new Class[] { RequestInterface.class },
156: testHandler);
157: System.out.println("Add entry to request [" + (index + 1)
158: + "]");
159: instance.addRequest(requestInterface);
160: }
161:
162: System.out.println("Wait on the test.");
163: testHandler.waitOnTest();
164: }
165:
166: }
|