KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > test > WebIntegrationUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.web.test;
23
24 import java.net.HttpURLConnection JavaDoc;
25 import java.net.URL JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 import org.jboss.test.JBossTestCase;
32 import org.jboss.test.JBossTestSetup;
33 import org.jboss.test.util.web.HttpUtils;
34 import org.apache.commons.httpclient.HttpMethodBase;
35 import org.apache.commons.httpclient.Header;
36
37 /** Tests of servlet container integration into the JBoss server. This test
38  requires than a web container be integrated into the JBoss server. The tests
39  currently do NOT use the java.net.HttpURLConnection and associated http client
40  and these do not return valid HTTP error codes so if a failure occurs it
41  is best to connect the webserver using a browser to look for additional error
42  info.
43
44  The secure access tests require a user named 'jduke' with a password of 'theduke'
45  with a role of 'AuthorizedUser' in the servlet container.
46  
47  @author Scott.Stark@jboss.org
48  @version $Revision: 42698 $
49  */

50 public class WebIntegrationUnitTestCase extends JBossTestCase
51 {
52    private static String JavaDoc REALM = "JBossTest Servlets";
53    private String JavaDoc baseURL = HttpUtils.getBaseURL();
54    private String JavaDoc baseURLNoAuth = HttpUtils.getBaseURLNoAuth();
55    
56    public WebIntegrationUnitTestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60    
61    /** Access the http://{host}/jbosstest/APIServlet to test the
62     * getRealPath method
63     */

64    public void testRealPath() throws Exception JavaDoc
65    {
66       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/APIServlet?op=testGetRealPath");
67       HttpUtils.accessURL(url);
68    }
69
70    /** Access the http://{host}/jbosstest/APIServlet to test the
71     * HttpSessionListener events
72     */

73    public void testHttpSessionListener() throws Exception JavaDoc
74    {
75       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/APIServlet?op=testSessionListener");
76       HttpUtils.accessURL(url);
77    }
78
79    /** Access the http://{host}/jbosstest/EJBOnStartupServlet
80     */

81    public void testEJBOnStartupServlet() throws Exception JavaDoc
82    {
83       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/EJBOnStartupServlet");
84       HttpUtils.accessURL(url);
85    }
86    /** Access the http://{host}/jbosstest/ENCServlet
87     */

88    public void testENCServlet() throws Exception JavaDoc
89    {
90       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/ENCServlet");
91       HttpUtils.accessURL(url);
92    }
93    /** Access the http://{host}/jbosstest/servlet/org.jboss.test.web.servlets.ENCServlet
94     */

95    public void testENCServletViaInvoker() throws Exception JavaDoc
96    {
97       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/servlet/org.jboss.test.web.servlets.ENCServlet");
98       HttpUtils.accessURL(url);
99    }
100
101    /** Access the http://{host}/jbosstest/SimpleServlet to test that servlets
102     * in the WEB-INF/lib jar.
103     *
104     */

105    public void testServletInJar() throws Exception JavaDoc
106    {
107       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/SimpleServlet");
108       HttpUtils.accessURL(url);
109    }
110
111    /** Access the http://{host}/jbosstest/EJBServlet
112     */

113    public void testEJBServlet() throws Exception JavaDoc
114    {
115       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/EJBServlet");
116       HttpUtils.accessURL(url);
117    }
118    /** Access the http://{host}/jbosstest/EntityServlet
119     */

120    public void testEntityServlet() throws Exception JavaDoc
121    {
122       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/EntityServlet");
123       HttpUtils.accessURL(url);
124    }
125    /** Access the http://{host}/jbosstest/StatefulSessionServlet
126     */

127    public void testStatefulSessionServlet() throws Exception JavaDoc
128    {
129       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/StatefulSessionServlet");
130       HttpUtils.accessURL(url);
131       // Need a mechanism to force passivation...
132
HttpUtils.accessURL(url);
133    }
134    /** Access the http://{host}/jbosstest/UserTransactionServlet
135     */

136    public void testUserTransactionServlet() throws Exception JavaDoc
137    {
138       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/UserTransactionServlet");
139       HttpUtils.accessURL(url);
140    }
141    /** Access the http://{host}/jbosstest/SpeedServlet
142     */

143    public void testSpeedServlet() throws Exception JavaDoc
144    {
145       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/SpeedServlet");
146       HttpUtils.accessURL(url);
147    }
148    /** Access the http://{host}/jbosstest/snoop.jsp
149     */

150    public void testSnoopJSP() throws Exception JavaDoc
151    {
152       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/snoop.jsp");
153       HttpUtils.accessURL(url);
154    }
155    /** Access the http://{host}/jbosstest/snoop.jsp
156     */

157    public void testSnoopJSPByPattern() throws Exception JavaDoc
158    {
159       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/test-snoop.snp");
160       HttpUtils.accessURL(url);
161    }
162    /** Access the http://{host}/jbosstest/test-jsp-mapping
163     */

164    public void testSnoopJSPByMapping() throws Exception JavaDoc
165    {
166       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/test-jsp-mapping");
167       HttpUtils.accessURL(url);
168    }
169    /** Access the http://{host}/jbosstest/classpath.jsp
170     */

171    public void testJSPClasspath() throws Exception JavaDoc
172    {
173       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/classpath.jsp");
174       HttpUtils.accessURL(url);
175    }
176
177    /** Access the http://{host}/jbosstest/ClientLoginServlet
178     */

179    public void testClientLoginServlet() throws Exception JavaDoc
180    {
181       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/ClientLoginServlet");
182       HttpUtils.accessURL(url);
183    }
184    /** Access the http://{host}/jbosstest/restricted/UserInRoleServlet to
185     * test isUserInRole.
186     */

187    public void testUserInRoleServlet() throws Exception JavaDoc
188    {
189       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/UserInRoleServlet");
190       HttpMethodBase request = HttpUtils.accessURL(url);
191       Header errors = request.getResponseHeader("X-ExpectedUserRoles-Errors");
192       log.info("X-ExpectedUserRoles-Errors: "+errors);
193       assertTrue("X-ExpectedUserRoles-Errors("+errors+") is null", errors == null);
194       errors = request.getResponseHeader("X-UnexpectedUserRoles-Errors");
195       log.info("X-UnexpectedUserRoles-Errors: "+errors);
196       assertTrue("X-UnexpectedUserRoles-Errors("+errors+") is null", errors == null);
197    }
198    /** Access the http://{host}/jbosstest/restricted/SecureServlet
199     */

200    public void testSecureServlet() throws Exception JavaDoc
201    {
202       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/SecureServlet");
203       HttpUtils.accessURL(url);
204    }
205    /** Access the http://{host}/jbosstest/restricted2/SecureServlet
206     */

207    public void testSecureServlet2() throws Exception JavaDoc
208    {
209       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted2/SecureServlet");
210       HttpUtils.accessURL(url);
211    }
212    /** Access the http://{host}/jbosstest/restricted/SubjectServlet
213     */

214    public void testSubjectServlet() throws Exception JavaDoc
215    {
216       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/SubjectServlet");
217       HttpMethodBase request = HttpUtils.accessURL(url);
218       Header hdr = request.getResponseHeader("X-SubjectServlet");
219       log.info("X-SubjectServlet: "+hdr);
220       assertTrue("X-SubjectServlet("+hdr+") is NOT null", hdr != null);
221       hdr = request.getResponseHeader("X-SubjectFilter-ENC");
222       log.info("X-SubjectFilter-ENC: "+hdr);
223       assertTrue("X-SubjectFilter-ENC("+hdr+") is NOT null", hdr != null);
224       hdr = request.getResponseHeader("X-SubjectFilter-SubjectSecurityManager");
225       log.info("X-SubjectFilter-SubjectSecurityManager: "+hdr);
226       assertTrue("X-SubjectFilter-SubjectSecurityManager("+hdr+") is NOT null", hdr != null);
227    }
228    /** Access the http://{host}/jbosstest/restricted/SecureServlet
229     */

230    public void testSecureServletAndUnsecureAccess() throws Exception JavaDoc
231    {
232       getLog().info("+++ testSecureServletAndUnsecureAccess");
233       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/SecureServlet");
234       getLog().info("Accessing SecureServlet with valid login");
235       HttpUtils.accessURL(url);
236       String JavaDoc baseURL2 = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/';
237       URL JavaDoc url2 = new URL JavaDoc(baseURL2+"jbosstest/restricted/UnsecureEJBServlet");
238       getLog().info("Accessing SecureServlet with no login");
239       HttpUtils.accessURL(url2, REALM, HttpURLConnection.HTTP_UNAUTHORIZED);
240    }
241    /** Access the http://{host}/jbosstest/restricted/SecureServlet
242     */

243    public void testSecureServletWithBadPass() throws Exception JavaDoc
244    {
245       String JavaDoc baseURL = "http://jduke:badpass@" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/';
246       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/SecureServlet");
247       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_UNAUTHORIZED);
248    }
249    /** Access the http://{host}/jbosstest/restricted/SecureServlet
250     */

251    public void testSecureServletWithNoLogin() throws Exception JavaDoc
252    {
253       String JavaDoc baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/';
254       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/SecureServlet");
255       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_UNAUTHORIZED);
256    }
257    /** Access the http://{host}/jbosstest-not/unrestricted/SecureServlet
258     */

259    public void testNotJbosstest() throws Exception JavaDoc
260    {
261       String JavaDoc baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/';
262       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest-not/unrestricted/SecureServlet");
263       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
264    }
265    /** Access the http://{host}/jbosstest/restricted/SecuredEntityFacadeServlet
266     */

267    public void testSecuredEntityFacadeServlet() throws Exception JavaDoc
268    {
269       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/SecuredEntityFacadeServlet");
270       HttpUtils.accessURL(url);
271    }
272    /** Access the http://{host}/jbosstest/restricted/SecureEJBAccess
273     */

274    public void testSecureEJBAccess() throws Exception JavaDoc
275    {
276       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/SecureEJBAccess");
277       HttpUtils.accessURL(url);
278    }
279    /** Access the http://{host}/jbosstest/restricted/include_ejb.jsp
280     */

281    public void testIncludeEJB() throws Exception JavaDoc
282    {
283       URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest/restricted/include_ejb.jsp");
284       HttpUtils.accessURL(url);
285    }
286    /** Access the http://{host}/jbosstest/UnsecureEJBAccess with method=echo
287     * to test that an unsecured servlet cannot access a secured EJB method
288     * that requires a valid permission. This should fail.
289     */

290    public void testUnsecureEJBAccess() throws Exception JavaDoc
291    {
292       URL JavaDoc url = new URL JavaDoc(baseURLNoAuth+"jbosstest/UnsecureEJBAccess?method=echo");
293       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_INTERNAL_ERROR);
294    }
295    /** Access the http://{host}/jbosstest/UnsecureEJBAccess with method=unchecked
296     * to test that an unsecured servlet can access a secured EJB method that
297     * only requires an authenticated user. This requires unauthenticated
298     * identity support by the web security domain.
299     */

300    public void testUnsecureAnonEJBAccess() throws Exception JavaDoc
301    {
302       URL JavaDoc url = new URL JavaDoc(baseURLNoAuth+"jbosstest/UnsecureEJBAccess?method=unchecked");
303       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
304    }
305
306    public void testUnsecureRunAsServlet() throws Exception JavaDoc
307    {
308       URL JavaDoc url = new URL JavaDoc(baseURLNoAuth+"jbosstest/UnsecureRunAsServlet?method=checkRunAs");
309       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
310    }
311
312    /** Access the http://{host}/jbosstest/UnsecureRunAsServletWithPrincipalName
313     * to test that an unsecured servlet can access a secured EJB method by using
314     * a run-as role. This should also have a custom run-as principal name.
315     *
316     * @throws Exception
317     */

318    public void testUnsecureRunAsServletWithPrincipalName() throws Exception JavaDoc
319    {
320       URL JavaDoc url = new URL JavaDoc(baseURLNoAuth+"jbosstest/UnsecureRunAsServletWithPrincipalName?ejbName=ejb/UnsecureRunAsServletWithPrincipalNameTarget");
321       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
322    }
323
324    /** Access the http://{host}/jbosstest/UnsecureRunAsServletWithPrincipalNameAndRoles
325     * to test that an unsecured servlet can access a secured EJB method by using
326     * a run-as role. This should also have a custom run-as principal name and
327     * additional roles.
328     *
329     * @throws Exception
330     */

331    public void testUnsecureRunAsServletWithPrincipalNameAndRoles() throws Exception JavaDoc
332    {
333       URL JavaDoc url = new URL JavaDoc(baseURLNoAuth+"jbosstest/UnsecureRunAsServletWithPrincipalNameAndRoles?ejbName=ejb/UnsecureRunAsServletWithPrincipalNameAndRolesTarget");
334       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
335    }
336    
337    /** Deploy a second ear that include a notjbosstest-web.war to test ears
338     with the same war names conflicting.
339     Access the http://{host}/jbosstest-not2/unrestricted/SecureServlet
340     */

341    public void testNotJbosstest2() throws Exception JavaDoc
342    {
343       try
344       {
345          deploy("jbosstest-web2.ear");
346          String JavaDoc baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/';
347          URL JavaDoc url = new URL JavaDoc(baseURL+"jbosstest-not2/unrestricted/SecureServlet");
348          HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
349       }
350       finally
351       {
352          undeploy("jbosstest-web2.ear");
353       } // end of try-finally
354
}
355
356    /** Deploy a bad war and then redploy with a fixed war to test failed war
357     * cleanup.
358     * Access the http://{host}/redeploy/index.html
359     */

360    public void testBadWarRedeploy() throws Exception JavaDoc
361    {
362       try
363       {
364          deploy("bad-web.war");
365          fail("The bad-web.war deployment did not fail");
366       }
367       catch(Exception JavaDoc e)
368       {
369          getLog().debug("bad-web.war failed as expected", e);
370       }
371       finally
372       {
373          undeploy("bad-web.war");
374       } // end of try-finally
375
try
376       {
377          deploy("good-web.war");
378          String JavaDoc baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/';
379          URL JavaDoc url = new URL JavaDoc(baseURL+"redeploy/index.html");
380          HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
381       }
382       finally
383       {
384          undeploy("good-web.war");
385       } // end of try-finally
386
}
387
388    /** Test of a war that accesses classes referred to via the war manifest
389     * classpath. Access the http://{host}/manifest/classpath.jsp
390     */

391    public void testWarManifest() throws Exception JavaDoc
392    {
393       deploy("manifest-web.ear");
394       try
395       {
396          String JavaDoc baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/';
397          URL JavaDoc url = new URL JavaDoc(baseURL+"manifest/classpath.jsp");
398          HttpMethodBase request = HttpUtils.accessURL(url);
399          Header errors = request.getResponseHeader("X-Exception");
400          log.info("X-Exception: "+errors);
401          assertTrue("X-Exception("+errors+") is null", errors == null);
402       }
403       finally
404       {
405          undeploy("manifest-web.ear");
406       }
407    }
408
409    public void testBadEarRedeploy() throws Exception JavaDoc
410    {
411       try
412       {
413          deploy("jbosstest-bad.ear");
414          fail("The jbosstest-bad.ear deployment did not fail");
415       }
416       catch(Exception JavaDoc e)
417       {
418          getLog().debug("jbosstest-bad.ear failed as expected", e);
419       }
420       finally
421       {
422          undeploy("jbosstest-bad.ear");
423       } // end of finally
424
try
425       {
426          deploy("jbosstest-good.ear");
427          String JavaDoc baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/';
428          URL JavaDoc url = new URL JavaDoc(baseURL+"redeploy/index.html");
429          HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
430       }
431       finally
432       {
433          undeploy("jbosstest-good.ear");
434       } // end of try-finally
435

436    }
437
438    /**
439     * Setup the test suite.
440     */

441    public static Test suite() throws Exception JavaDoc
442    {
443       TestSuite suite = new TestSuite();
444       suite.addTest(new TestSuite(WebIntegrationUnitTestCase.class));
445
446       // Create an initializer for the test suite
447
Test wrapper = new JBossTestSetup(suite)
448       {
449          protected void setUp() throws Exception JavaDoc
450          {
451             super.setUp();
452             redeploy("jbosstest-web.ear");
453             flushAuthCache("jbosstest-web");
454          }
455          protected void tearDown() throws Exception JavaDoc
456          {
457             undeploy("jbosstest-web.ear");
458             super.tearDown();
459
460             // Remove all the messages created during this test
461
getServer().invoke
462             (
463                new ObjectName JavaDoc("jboss.mq.destination:service=Queue,name=testQueue"),
464                "removeAllMessages",
465                new Object JavaDoc[0],
466                new String JavaDoc[0]
467             );
468          
469          }
470       };
471       return wrapper;
472    }
473
474 }
475
Popular Tags