1 22 package org.jboss.test.web.test; 23 24 import java.net.HttpURLConnection ; 25 import java.net.URL ; 26 import javax.management.ObjectName ; 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 50 public class WebIntegrationUnitTestCase extends JBossTestCase 51 { 52 private static String REALM = "JBossTest Servlets"; 53 private String baseURL = HttpUtils.getBaseURL(); 54 private String baseURLNoAuth = HttpUtils.getBaseURLNoAuth(); 55 56 public WebIntegrationUnitTestCase(String name) 57 { 58 super(name); 59 } 60 61 64 public void testRealPath() throws Exception 65 { 66 URL url = new URL (baseURL+"jbosstest/APIServlet?op=testGetRealPath"); 67 HttpUtils.accessURL(url); 68 } 69 70 73 public void testHttpSessionListener() throws Exception 74 { 75 URL url = new URL (baseURL+"jbosstest/APIServlet?op=testSessionListener"); 76 HttpUtils.accessURL(url); 77 } 78 79 81 public void testEJBOnStartupServlet() throws Exception 82 { 83 URL url = new URL (baseURL+"jbosstest/EJBOnStartupServlet"); 84 HttpUtils.accessURL(url); 85 } 86 88 public void testENCServlet() throws Exception 89 { 90 URL url = new URL (baseURL+"jbosstest/ENCServlet"); 91 HttpUtils.accessURL(url); 92 } 93 95 public void testENCServletViaInvoker() throws Exception 96 { 97 URL url = new URL (baseURL+"jbosstest/servlet/org.jboss.test.web.servlets.ENCServlet"); 98 HttpUtils.accessURL(url); 99 } 100 101 105 public void testServletInJar() throws Exception 106 { 107 URL url = new URL (baseURL+"jbosstest/SimpleServlet"); 108 HttpUtils.accessURL(url); 109 } 110 111 113 public void testEJBServlet() throws Exception 114 { 115 URL url = new URL (baseURL+"jbosstest/EJBServlet"); 116 HttpUtils.accessURL(url); 117 } 118 120 public void testEntityServlet() throws Exception 121 { 122 URL url = new URL (baseURL+"jbosstest/EntityServlet"); 123 HttpUtils.accessURL(url); 124 } 125 127 public void testStatefulSessionServlet() throws Exception 128 { 129 URL url = new URL (baseURL+"jbosstest/StatefulSessionServlet"); 130 HttpUtils.accessURL(url); 131 HttpUtils.accessURL(url); 133 } 134 136 public void testUserTransactionServlet() throws Exception 137 { 138 URL url = new URL (baseURL+"jbosstest/UserTransactionServlet"); 139 HttpUtils.accessURL(url); 140 } 141 143 public void testSpeedServlet() throws Exception 144 { 145 URL url = new URL (baseURL+"jbosstest/SpeedServlet"); 146 HttpUtils.accessURL(url); 147 } 148 150 public void testSnoopJSP() throws Exception 151 { 152 URL url = new URL (baseURL+"jbosstest/snoop.jsp"); 153 HttpUtils.accessURL(url); 154 } 155 157 public void testSnoopJSPByPattern() throws Exception 158 { 159 URL url = new URL (baseURL+"jbosstest/test-snoop.snp"); 160 HttpUtils.accessURL(url); 161 } 162 164 public void testSnoopJSPByMapping() throws Exception 165 { 166 URL url = new URL (baseURL+"jbosstest/test-jsp-mapping"); 167 HttpUtils.accessURL(url); 168 } 169 171 public void testJSPClasspath() throws Exception 172 { 173 URL url = new URL (baseURL+"jbosstest/classpath.jsp"); 174 HttpUtils.accessURL(url); 175 } 176 177 179 public void testClientLoginServlet() throws Exception 180 { 181 URL url = new URL (baseURL+"jbosstest/ClientLoginServlet"); 182 HttpUtils.accessURL(url); 183 } 184 187 public void testUserInRoleServlet() throws Exception 188 { 189 URL url = new URL (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 200 public void testSecureServlet() throws Exception 201 { 202 URL url = new URL (baseURL+"jbosstest/restricted/SecureServlet"); 203 HttpUtils.accessURL(url); 204 } 205 207 public void testSecureServlet2() throws Exception 208 { 209 URL url = new URL (baseURL+"jbosstest/restricted2/SecureServlet"); 210 HttpUtils.accessURL(url); 211 } 212 214 public void testSubjectServlet() throws Exception 215 { 216 URL url = new URL (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 230 public void testSecureServletAndUnsecureAccess() throws Exception 231 { 232 getLog().info("+++ testSecureServletAndUnsecureAccess"); 233 URL url = new URL (baseURL+"jbosstest/restricted/SecureServlet"); 234 getLog().info("Accessing SecureServlet with valid login"); 235 HttpUtils.accessURL(url); 236 String baseURL2 = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/'; 237 URL url2 = new URL (baseURL2+"jbosstest/restricted/UnsecureEJBServlet"); 238 getLog().info("Accessing SecureServlet with no login"); 239 HttpUtils.accessURL(url2, REALM, HttpURLConnection.HTTP_UNAUTHORIZED); 240 } 241 243 public void testSecureServletWithBadPass() throws Exception 244 { 245 String baseURL = "http://jduke:badpass@" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/'; 246 URL url = new URL (baseURL+"jbosstest/restricted/SecureServlet"); 247 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_UNAUTHORIZED); 248 } 249 251 public void testSecureServletWithNoLogin() throws Exception 252 { 253 String baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/'; 254 URL url = new URL (baseURL+"jbosstest/restricted/SecureServlet"); 255 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_UNAUTHORIZED); 256 } 257 259 public void testNotJbosstest() throws Exception 260 { 261 String baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/'; 262 URL url = new URL (baseURL+"jbosstest-not/unrestricted/SecureServlet"); 263 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK); 264 } 265 267 public void testSecuredEntityFacadeServlet() throws Exception 268 { 269 URL url = new URL (baseURL+"jbosstest/restricted/SecuredEntityFacadeServlet"); 270 HttpUtils.accessURL(url); 271 } 272 274 public void testSecureEJBAccess() throws Exception 275 { 276 URL url = new URL (baseURL+"jbosstest/restricted/SecureEJBAccess"); 277 HttpUtils.accessURL(url); 278 } 279 281 public void testIncludeEJB() throws Exception 282 { 283 URL url = new URL (baseURL+"jbosstest/restricted/include_ejb.jsp"); 284 HttpUtils.accessURL(url); 285 } 286 290 public void testUnsecureEJBAccess() throws Exception 291 { 292 URL url = new URL (baseURLNoAuth+"jbosstest/UnsecureEJBAccess?method=echo"); 293 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_INTERNAL_ERROR); 294 } 295 300 public void testUnsecureAnonEJBAccess() throws Exception 301 { 302 URL url = new URL (baseURLNoAuth+"jbosstest/UnsecureEJBAccess?method=unchecked"); 303 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK); 304 } 305 306 public void testUnsecureRunAsServlet() throws Exception 307 { 308 URL url = new URL (baseURLNoAuth+"jbosstest/UnsecureRunAsServlet?method=checkRunAs"); 309 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK); 310 } 311 312 318 public void testUnsecureRunAsServletWithPrincipalName() throws Exception 319 { 320 URL url = new URL (baseURLNoAuth+"jbosstest/UnsecureRunAsServletWithPrincipalName?ejbName=ejb/UnsecureRunAsServletWithPrincipalNameTarget"); 321 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK); 322 } 323 324 331 public void testUnsecureRunAsServletWithPrincipalNameAndRoles() throws Exception 332 { 333 URL url = new URL (baseURLNoAuth+"jbosstest/UnsecureRunAsServletWithPrincipalNameAndRoles?ejbName=ejb/UnsecureRunAsServletWithPrincipalNameAndRolesTarget"); 334 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK); 335 } 336 337 341 public void testNotJbosstest2() throws Exception 342 { 343 try 344 { 345 deploy("jbosstest-web2.ear"); 346 String baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/'; 347 URL url = new URL (baseURL+"jbosstest-not2/unrestricted/SecureServlet"); 348 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK); 349 } 350 finally 351 { 352 undeploy("jbosstest-web2.ear"); 353 } } 355 356 360 public void testBadWarRedeploy() throws Exception 361 { 362 try 363 { 364 deploy("bad-web.war"); 365 fail("The bad-web.war deployment did not fail"); 366 } 367 catch(Exception e) 368 { 369 getLog().debug("bad-web.war failed as expected", e); 370 } 371 finally 372 { 373 undeploy("bad-web.war"); 374 } try 376 { 377 deploy("good-web.war"); 378 String baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/'; 379 URL url = new URL (baseURL+"redeploy/index.html"); 380 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK); 381 } 382 finally 383 { 384 undeploy("good-web.war"); 385 } } 387 388 391 public void testWarManifest() throws Exception 392 { 393 deploy("manifest-web.ear"); 394 try 395 { 396 String baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/'; 397 URL url = new URL (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 410 { 411 try 412 { 413 deploy("jbosstest-bad.ear"); 414 fail("The jbosstest-bad.ear deployment did not fail"); 415 } 416 catch(Exception e) 417 { 418 getLog().debug("jbosstest-bad.ear failed as expected", e); 419 } 420 finally 421 { 422 undeploy("jbosstest-bad.ear"); 423 } try 425 { 426 deploy("jbosstest-good.ear"); 427 String baseURL = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + '/'; 428 URL url = new URL (baseURL+"redeploy/index.html"); 429 HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK); 430 } 431 finally 432 { 433 undeploy("jbosstest-good.ear"); 434 } 436 } 437 438 441 public static Test suite() throws Exception 442 { 443 TestSuite suite = new TestSuite(); 444 suite.addTest(new TestSuite(WebIntegrationUnitTestCase.class)); 445 446 Test wrapper = new JBossTestSetup(suite) 448 { 449 protected void setUp() throws Exception 450 { 451 super.setUp(); 452 redeploy("jbosstest-web.ear"); 453 flushAuthCache("jbosstest-web"); 454 } 455 protected void tearDown() throws Exception 456 { 457 undeploy("jbosstest-web.ear"); 458 super.tearDown(); 459 460 getServer().invoke 462 ( 463 new ObjectName ("jboss.mq.destination:service=Queue,name=testQueue"), 464 "removeAllMessages", 465 new Object [0], 466 new String [0] 467 ); 468 469 } 470 }; 471 return wrapper; 472 } 473 474 } 475 | Popular Tags |