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: * HttpDaemonTest.java
020: *
021: * JUnit based test
022: */
023:
024: // package path
025: package com.rift.coad.lib.httpd;
026:
027: // java imports
028: import com.rift.coad.lib.interceptor.InterceptorFactory;
029: import com.rift.coad.lib.naming.NamingDirector;
030: import com.rift.coad.lib.security.ThreadPermissionSession;
031: import com.rift.coad.lib.security.UserSession;
032: import com.rift.coad.lib.transaction.TransactionDirector;
033: import java.io.BufferedReader;
034: import java.io.InputStreamReader;
035: import java.net.URL;
036: import java.net.URLConnection;
037: import java.net.InetAddress;
038: import java.util.HashSet;
039: import java.util.Set;
040: import javax.xml.rpc.Service;
041: import javax.xml.rpc.JAXRPCException;
042: import javax.xml.namespace.QName;
043: import javax.xml.rpc.ServiceFactory;
044: import javax.xml.rpc.Stub;
045:
046: // junit imports
047: import junit.framework.*;
048:
049: // log 4 j imports
050: import org.apache.log4j.Logger;
051:
052: // coadunation imports
053: import com.rift.coad.lib.thread.CoadunationThreadGroup;
054: import com.rift.coad.lib.security.ThreadsPermissionContainer;
055: import com.rift.coad.lib.configuration.Configuration;
056: import com.rift.coad.lib.configuration.ConfigurationFactory;
057: import com.rift.coad.lib.deployment.DeploymentLoader;
058: import com.rift.coad.lib.deployment.webservice.WebServiceManager;
059: import com.rift.coad.lib.deployment.webservice.WebServiceConnector;
060: import com.rift.coad.lib.thirdparty.axis.AxisManager;
061: import com.rift.coad.lib.security.RoleManager;
062: import com.rift.coad.lib.security.SessionManager;
063: import com.rift.coad.lib.security.ThreadsPermissionContainer;
064: import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
065: import com.rift.coad.lib.security.user.UserStoreManager;
066: import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
067: import com.rift.coad.lib.security.user.UserSessionManager;
068: import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
069: import com.rift.coad.lib.security.login.LoginManager;
070:
071: /**
072: *
073: * @author Brett Chaldecott
074: */
075: public class HttpDaemonTest extends TestCase {
076:
077: /**
078: * The service end point interface for the web service
079: */
080: public interface WebServiceSEI extends java.rmi.Remote {
081:
082: /**
083: * This method will be called to test the web service end point interface.
084: *
085: * @param msg The string containing the message for the server.
086: * @return The containing the message from the server.
087: */
088: public String helloWorld(String msg);
089: }
090:
091: /**
092: * Web service client
093: */
094: public class POJOWebServiceClient {
095:
096: // class static variables
097: private final static String SERVICE_NAME = "urn:WebService/wsdl";
098:
099: // member variables
100: private String url = null;
101: private WebServiceSEI proxy = null;
102:
103: /**
104: * Creates a new instance of POJOWebServiceClient
105: */
106: public POJOWebServiceClient(String url) {
107: this .url = url;
108: }
109:
110: /**
111: * This method returns a reference to the web service interface.
112: *
113: * @return The reference ot the test web service interface.
114: * @exception Exception
115: */
116: public WebServiceSEI connect() throws Exception {
117: // the proxy reference
118: if (proxy != null) {
119: return proxy;
120: }
121:
122: // make the necessary connection
123: try {
124:
125: URL serviceUrl = new URL(url);
126:
127: ServiceFactory serviceFactory = ServiceFactory
128: .newInstance();
129:
130: Service testService = serviceFactory.createService(
131: serviceUrl, new QName(SERVICE_NAME,
132: "WebService"));
133:
134: proxy = (WebServiceSEI) testService.getPort(new QName(
135: SERVICE_NAME, "WebServiceSEIPort"),
136: WebServiceSEI.class);
137:
138: Stub stub = (Stub) proxy;
139: stub._setProperty(Stub.USERNAME_PROPERTY, "test");
140: stub._setProperty(Stub.PASSWORD_PROPERTY, "112233");
141:
142: return proxy;
143:
144: } catch (Exception ex) {
145: throw new Exception(
146: "Failed to make a connection to the test service : "
147: + ex.getMessage(), ex);
148: }
149: }
150:
151: }
152:
153: public HttpDaemonTest(String testName) {
154: super (testName);
155: }
156:
157: protected void setUp() throws Exception {
158: }
159:
160: protected void tearDown() throws Exception {
161: }
162:
163: public static Test suite() {
164: TestSuite suite = new TestSuite(HttpDaemonTest.class);
165:
166: return suite;
167: }
168:
169: /**
170: * Test of shutdown method, of class com.rift.coad.lib.httpd.HttpDaemon.
171: */
172: public void testDaemon() throws Exception {
173: System.out.println("Daemon");
174:
175: // instanciate the deployment loader
176: ThreadsPermissionContainer permissionContainer = new ThreadsPermissionContainer();
177: ThreadsPermissionContainerAccessor.init(permissionContainer);
178: SessionManager.init(permissionContainer);
179: UserStoreManager userStoreManager = new UserStoreManager();
180: UserStoreManagerAccessor.init(userStoreManager);
181: UserSessionManager sessionManager = new UserSessionManager(
182: permissionContainer, userStoreManager);
183: UserSessionManagerAccessor.init(sessionManager);
184: LoginManager.init(sessionManager, userStoreManager);
185:
186: // add a user to the session for the current thread
187: RoleManager.getInstance();
188:
189: // instanciate the thread manager
190: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
191: sessionManager, userStoreManager);
192:
193: InterceptorFactory.init(permissionContainer, sessionManager,
194: userStoreManager);
195:
196: // add a new user object and add to the permission
197: Set set = new HashSet();
198: set.add("test");
199: UserSession user = new UserSession("test1", set);
200: permissionContainer.putSession(new Long(Thread.currentThread()
201: .getId()), new ThreadPermissionSession(new Long(Thread
202: .currentThread().getId()), user));
203:
204: // init the naming director
205: NamingDirector.init(threadGroup);
206:
207: // instanciate the transaction director
208: TransactionDirector transactionDirector = TransactionDirector
209: .init();
210:
211: // instanciate the deployment loader
212: DeploymentLoader deploymentLoader = new DeploymentLoader(
213: new java.io.File(System.getProperty("test.jar")));
214:
215: // instanciate the axis engine
216: AxisManager.init();
217:
218: // instanciate the web service manager
219: WebServiceManager webServiceManager = new WebServiceManager();
220: WebServiceConnector.init(webServiceManager);
221: // load the files
222: webServiceManager.load(deploymentLoader);
223:
224: // instanciate the http daemon
225: HttpDaemon httpDaemon = new HttpDaemon(threadGroup);
226:
227: // retrieve full host name
228: String fqdn = InetAddress.getLocalHost().getCanonicalHostName();
229:
230: httpConnection(fqdn);
231: wsdlConnection(fqdn);
232: POJOWebServiceClient pojoWebServiceClient = new POJOWebServiceClient(
233: "http://" + fqdn + ":8085/WebServiceTest?WSDL");
234: rpcConnection(pojoWebServiceClient);
235:
236: httpDaemon.shutdown();
237: }
238:
239: /**
240: * This method makes a standard http connection and query
241: */
242: private void httpConnection(String fqdn) throws Exception {
243: try {
244: URL url = new URL("http://" + fqdn + ":8085/");
245: BufferedReader in = new BufferedReader(
246: new InputStreamReader(url.openStream()));
247: String inputLine;
248: while ((inputLine = in.readLine()) != null)
249: System.out.println(inputLine);
250: in.close();
251: fail("No out put should be retrieve here");
252: } catch (java.io.FileNotFoundException ex) {
253: // ignore
254: }
255: }
256:
257: /**
258: * This makes a wsdl connection
259: */
260: private void wsdlConnection(String fqdn) throws Exception {
261: URL url = new URL("http://" + fqdn
262: + ":8085/WebServiceTest?WSDL");
263: BufferedReader in = new BufferedReader(new InputStreamReader(
264: url.openStream()));
265: String inputLine;
266: while ((inputLine = in.readLine()) != null)
267: System.out.println(inputLine);
268: in.close();
269: }
270:
271: /**
272: * This method makes a standard http connection and query
273: */
274: private void rpcConnection(POJOWebServiceClient pojoWebServiceClient)
275: throws Exception {
276: try {
277: WebServiceSEI webServiceSEI = pojoWebServiceClient
278: .connect();
279: webServiceSEI.helloWorld("This is a test call");
280: } catch (Exception ex) {
281: ex.printStackTrace(System.out);
282: throw ex;
283: }
284: }
285:
286: }
|