KickJava   Java API By Example, From Geeks To Geeks.

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


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  * HttpDaemonTest.java
20  *
21  * JUnit based test
22  */

23
24 // package path
25
package com.rift.coad.lib.httpd;
26
27 // java imports
28
import com.rift.coad.lib.interceptor.InterceptorFactory;
29 import com.rift.coad.lib.naming.NamingDirector;
30 import com.rift.coad.lib.security.ThreadPermissionSession;
31 import com.rift.coad.lib.security.UserSession;
32 import com.rift.coad.lib.transaction.TransactionDirector;
33 import java.io.BufferedReader JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.net.URLConnection JavaDoc;
37 import java.net.InetAddress JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import java.util.Set JavaDoc;
40 import javax.xml.rpc.Service JavaDoc;
41 import javax.xml.rpc.JAXRPCException JavaDoc;
42 import javax.xml.namespace.QName JavaDoc;
43 import javax.xml.rpc.ServiceFactory JavaDoc;
44 import javax.xml.rpc.Stub JavaDoc;
45
46 // junit imports
47
import junit.framework.*;
48
49 // log 4 j imports
50
import org.apache.log4j.Logger;
51
52 // coadunation imports
53
import com.rift.coad.lib.thread.CoadunationThreadGroup;
54 import com.rift.coad.lib.security.ThreadsPermissionContainer;
55 import com.rift.coad.lib.configuration.Configuration;
56 import com.rift.coad.lib.configuration.ConfigurationFactory;
57 import com.rift.coad.lib.deployment.DeploymentLoader;
58 import com.rift.coad.lib.deployment.webservice.WebServiceManager;
59 import com.rift.coad.lib.deployment.webservice.WebServiceConnector;
60 import com.rift.coad.lib.thirdparty.axis.AxisManager;
61 import com.rift.coad.lib.security.RoleManager;
62 import com.rift.coad.lib.security.SessionManager;
63 import com.rift.coad.lib.security.ThreadsPermissionContainer;
64 import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
65 import com.rift.coad.lib.security.user.UserStoreManager;
66 import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
67 import com.rift.coad.lib.security.user.UserSessionManager;
68 import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
69 import com.rift.coad.lib.security.login.LoginManager;
70
71
72 /**
73  *
74  * @author Brett Chaldecott
75  */

76 public class HttpDaemonTest extends TestCase {
77     
78     
79     /**
80      * The service end point interface for the web service
81      */

82     public interface WebServiceSEI extends java.rmi.Remote JavaDoc {
83
84         /**
85          * This method will be called to test the web service end point interface.
86          *
87          * @param msg The string containing the message for the server.
88          * @return The containing the message from the server.
89          */

90         public String JavaDoc helloWorld(String JavaDoc msg);
91     }
92     
93     
94     /**
95      * Web service client
96      */

97     public class POJOWebServiceClient {
98
99         // class static variables
100
private final static String JavaDoc SERVICE_NAME = "urn:WebService/wsdl";
101
102         // member variables
103
private String JavaDoc url = null;
104         private WebServiceSEI proxy = null;
105         
106         
107         /**
108          * Creates a new instance of POJOWebServiceClient
109          */

110         public POJOWebServiceClient(String JavaDoc url) {
111             this.url = url;
112         }
113
114         /**
115          * This method returns a reference to the web service interface.
116          *
117          * @return The reference ot the test web service interface.
118          * @exception Exception
119          */

120         public WebServiceSEI connect() throws Exception JavaDoc {
121             // the proxy reference
122
if (proxy != null) {
123                 return proxy;
124             }
125
126             // make the necessary connection
127
try {
128
129                 URL JavaDoc serviceUrl = new URL JavaDoc(url);
130
131                 ServiceFactory JavaDoc serviceFactory = ServiceFactory.newInstance();
132
133                 Service JavaDoc testService =
134                         serviceFactory.createService(serviceUrl,
135                         new QName JavaDoc(SERVICE_NAME,"WebService"));
136
137                 proxy = (WebServiceSEI)testService.getPort(
138                         new QName JavaDoc(SERVICE_NAME,"WebServiceSEIPort"),
139                         WebServiceSEI.class);
140                 
141                 Stub JavaDoc stub = (Stub JavaDoc)proxy;
142                 stub._setProperty(Stub.USERNAME_PROPERTY,"test");
143                 stub._setProperty(Stub.PASSWORD_PROPERTY,"112233");
144
145                 return proxy;
146
147             } catch (Exception JavaDoc ex) {
148                 throw new Exception JavaDoc(
149                         "Failed to make a connection to the test service : " +
150                         ex.getMessage(),ex);
151             }
152         }
153
154     }
155     
156     public HttpDaemonTest(String JavaDoc testName) {
157         super(testName);
158     }
159
160     protected void setUp() throws Exception JavaDoc {
161     }
162
163     protected void tearDown() throws Exception JavaDoc {
164     }
165
166     public static Test suite() {
167         TestSuite suite = new TestSuite(HttpDaemonTest.class);
168         
169         return suite;
170     }
171
172     /**
173      * Test of shutdown method, of class com.rift.coad.lib.httpd.HttpDaemon.
174      */

175     public void testDaemon() throws Exception JavaDoc {
176         System.out.println("Daemon");
177         
178         // instanciate the deployment loader
179
ThreadsPermissionContainer permissionContainer =
180                 new ThreadsPermissionContainer();
181         ThreadsPermissionContainerAccessor.init(permissionContainer);
182         SessionManager.init(permissionContainer);
183         UserStoreManager userStoreManager = new UserStoreManager();
184         UserStoreManagerAccessor.init(userStoreManager);
185         UserSessionManager sessionManager = new UserSessionManager(
186                 permissionContainer,userStoreManager);
187         UserSessionManagerAccessor.init(sessionManager);
188         LoginManager.init(sessionManager,userStoreManager);
189         
190         // add a user to the session for the current thread
191
RoleManager.getInstance();
192         
193         // instanciate the thread manager
194
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
195             userStoreManager);
196         
197         InterceptorFactory.init(permissionContainer,sessionManager,userStoreManager);
198         
199         // add a new user object and add to the permission
200
Set JavaDoc set = new HashSet JavaDoc();
201         set.add("test");
202         UserSession user = new UserSession("test1", set);
203         permissionContainer.putSession(new Long JavaDoc(Thread.currentThread().getId()),
204                 new ThreadPermissionSession(
205                 new Long JavaDoc(Thread.currentThread().getId()),user));
206         
207         // init the naming director
208
NamingDirector.init(threadGroup);
209         
210         // instanciate the transaction director
211
TransactionDirector transactionDirector = TransactionDirector.init();
212         
213         // instanciate the deployment loader
214
DeploymentLoader deploymentLoader = new DeploymentLoader(
215                 new java.io.File JavaDoc(System.getProperty("test.jar")));
216         
217         // instanciate the axis engine
218
AxisManager.init();
219         
220         // instanciate the web service manager
221
WebServiceManager webServiceManager = new WebServiceManager();
222         WebServiceConnector.init(webServiceManager);
223         // load the files
224
webServiceManager.load(deploymentLoader);
225         
226         // instanciate the http daemon
227
HttpDaemon httpDaemon = new HttpDaemon(threadGroup);
228         
229         // retrieve full host name
230
String JavaDoc fqdn = InetAddress.getLocalHost().getCanonicalHostName();
231         
232         httpConnection(fqdn);
233         wsdlConnection(fqdn);
234         POJOWebServiceClient pojoWebServiceClient = new POJOWebServiceClient(
235                 "http://" + fqdn + ":8085/WebServiceTest?WSDL");
236         rpcConnection(pojoWebServiceClient);
237         
238         httpDaemon.shutdown();
239     }
240     
241     
242     /**
243      * This method makes a standard http connection and query
244      */

245     private void httpConnection(String JavaDoc fqdn) throws Exception JavaDoc {
246         try {
247             URL JavaDoc url = new URL JavaDoc("http://" + fqdn + ":8085/");
248             BufferedReader JavaDoc in = new BufferedReader JavaDoc(
249                                     new InputStreamReader JavaDoc(
250                                     url.openStream()));
251             String JavaDoc inputLine;
252             while ((inputLine = in.readLine()) != null)
253                 System.out.println(inputLine);
254             in.close();
255             fail("No out put should be retrieve here");
256         } catch (java.io.FileNotFoundException JavaDoc ex) {
257             // ignore
258
}
259     }
260     
261     /**
262      * This makes a wsdl connection
263      */

264     private void wsdlConnection(String JavaDoc fqdn) throws Exception JavaDoc {
265         URL JavaDoc url = new URL JavaDoc("http://" + fqdn + ":8085/WebServiceTest?WSDL");
266         BufferedReader JavaDoc in = new BufferedReader JavaDoc(
267                                 new InputStreamReader JavaDoc(
268                                 url.openStream()));
269         String JavaDoc inputLine;
270         while ((inputLine = in.readLine()) != null)
271             System.out.println(inputLine);
272         in.close();
273     }
274     
275     
276     /**
277      * This method makes a standard http connection and query
278      */

279     private void rpcConnection(POJOWebServiceClient pojoWebServiceClient)
280             throws Exception JavaDoc {
281         try {
282             WebServiceSEI webServiceSEI = pojoWebServiceClient.connect();
283             webServiceSEI.helloWorld("This is a test call");
284         } catch (Exception JavaDoc ex) {
285             ex.printStackTrace(System.out);
286             throw ex;
287         }
288     }
289     
290     
291     
292 }
293
Popular Tags