1 20 package org.apache.cactus.extension.jetty; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.HttpURLConnection ; 26 import java.net.URL ; 27 28 import junit.extensions.TestSetup; 29 import junit.framework.Protectable; 30 import junit.framework.Test; 31 import junit.framework.TestResult; 32 33 import org.apache.cactus.internal.configuration.BaseConfiguration; 34 import org.apache.cactus.internal.configuration.Configuration; 35 import org.apache.cactus.internal.configuration.DefaultFilterConfiguration; 36 import org.apache.cactus.internal.configuration.DefaultServletConfiguration; 37 import org.apache.cactus.internal.configuration.FilterConfiguration; 38 import org.apache.cactus.internal.configuration.ServletConfiguration; 39 import org.apache.cactus.internal.util.ClassLoaderUtils; 40 import org.apache.cactus.server.FilterTestRedirector; 41 import org.apache.cactus.server.ServletTestRedirector; 42 43 55 public class JettyTestSetup extends TestSetup 56 { 57 61 private static final String CACTUS_JETTY_CONFIG_PROPERTY = 62 "cactus.jetty.config"; 63 64 68 private static final String CACTUS_JETTY_RESOURCE_DIR_PROPERTY = 69 "cactus.jetty.resourceDir"; 70 71 74 private File configFile; 75 76 79 private File resourceDir; 80 81 85 private Object server; 86 87 90 private boolean alreadyRunning; 91 92 95 private boolean isRunning = false; 96 97 101 private boolean forceShutdown = false; 102 103 106 private ServletConfiguration servletConfiguration; 107 108 111 private FilterConfiguration filterConfiguration; 112 113 116 private Configuration baseConfiguration; 117 118 121 public JettyTestSetup(Test theTest) 122 { 123 super(theTest); 124 this.baseConfiguration = new BaseConfiguration(); 125 this.servletConfiguration = new DefaultServletConfiguration(); 126 this.filterConfiguration = new DefaultFilterConfiguration(); 127 } 128 129 138 public JettyTestSetup(Test theTest, 139 Configuration theBaseConfiguration, 140 ServletConfiguration theServletConfiguration, 141 FilterConfiguration theFilterConfiguration) 142 { 143 this(theTest); 144 this.baseConfiguration = theBaseConfiguration; 145 this.servletConfiguration = theServletConfiguration; 146 this.filterConfiguration = theFilterConfiguration; 147 } 148 149 157 public void run(final TestResult theResult) 158 { 159 Protectable p = new Protectable() 160 { 161 public void protect() throws Exception 162 { 163 try 164 { 165 setUp(); 166 basicRun(theResult); 167 } 168 finally 169 { 170 tearDown(); 171 } 172 } 173 }; 174 theResult.runProtected(this, p); 175 } 176 177 185 protected void setUp() throws Exception 186 { 187 URL contextURL = new URL (this.baseConfiguration.getContextURL() 190 + "/" + this.servletConfiguration.getDefaultRedirectorName() 191 + "?Cactus_Service=RUN_TEST"); 192 this.alreadyRunning = isAvailable(testConnectivity(contextURL)); 193 if (this.alreadyRunning) 194 { 195 this.isRunning = true; 198 return; 199 } 200 201 208 this.server = createServer(this.baseConfiguration); 210 211 Object context = createContext(this.server, this.baseConfiguration); 213 214 addServletRedirector(context, this.servletConfiguration); 216 217 addJspRedirector(context); 219 220 addFilterRedirector(context, this.filterConfiguration); 222 223 if (getConfigFile() != null) 226 { 227 this.server.getClass().getMethod("configure", 228 new Class [] {String .class}).invoke( 229 this.server, new Object [] {getConfigFile().toString()}); 230 } 231 232 this.server.getClass().getMethod("start", null).invoke( 234 this.server, null); 235 236 this.isRunning = true; 237 } 238 239 244 protected void tearDown() throws Exception 245 { 246 if (!this.forceShutdown && this.alreadyRunning) 248 { 249 return; 250 } 251 252 if (this.server != null) 253 { 254 boolean started = ((Boolean ) this.server.getClass().getMethod( 256 "isStarted", null).invoke(this.server, null)).booleanValue(); 257 258 if (started) 260 { 261 this.server.getClass().getMethod("stop", null).invoke( 263 this.server, null); 264 265 this.server.getClass().getMethod("destroy", null).invoke( 268 this.server, null); 269 } 270 } 271 272 this.isRunning = false; 273 } 274 275 280 public final void setConfigFile(File theConfigFile) 281 { 282 this.configFile = theConfigFile; 283 } 284 285 291 public final void setResourceDir(File theResourceDir) 292 { 293 this.resourceDir = theResourceDir; 294 } 295 296 300 public final void setForceShutdown(boolean isForcedShutdown) 301 { 302 this.forceShutdown = isForcedShutdown; 303 } 304 305 309 protected final File getConfigFile() 310 { 311 if (this.configFile == null) 312 { 313 String configFileProperty = System.getProperty( 314 CACTUS_JETTY_CONFIG_PROPERTY); 315 if (configFileProperty != null) 316 { 317 this.configFile = new File (configFileProperty); 318 } 319 } 320 return this.configFile; 321 } 322 323 327 protected final File getResourceDir() 328 { 329 if (this.resourceDir == null) 330 { 331 String resourceDirProperty = System.getProperty( 332 CACTUS_JETTY_RESOURCE_DIR_PROPERTY); 333 if (resourceDirProperty != null) 334 { 335 this.resourceDir = new File (resourceDirProperty); 336 } 337 } 338 return this.resourceDir; 339 } 340 341 350 private Object createServer(Configuration theConfiguration) 351 throws Exception 352 { 353 Class serverClass = ClassLoaderUtils.loadClass( 355 "org.mortbay.jetty.Server", this.getClass()); 356 Object server = serverClass.newInstance(); 357 358 URL contextURL = new URL (theConfiguration.getContextURL()); 359 360 server.getClass().getMethod("addListener", 362 new Class [] {String .class}) 363 .invoke(server, new Object [] {"" + contextURL.getPort()}); 364 365 return server; 366 } 367 368 378 private Object createContext(Object theServer, 379 Configuration theConfiguration) throws Exception 380 { 381 URL contextURL = new URL (theConfiguration.getContextURL()); 385 386 if (getResourceDir() != null) 387 { 388 theServer.getClass().getMethod("addWebApplication", 389 new Class [] {String .class, String .class}) 390 .invoke(theServer, new Object [] {contextURL.getPath(), 391 getResourceDir().toString()}); 392 } 393 394 Object context = theServer.getClass().getMethod( 398 "getContext", new Class [] {String .class}) 399 .invoke(theServer, new Object [] {contextURL.getPath()}); 400 401 return context; 402 } 403 404 412 private void addServletRedirector(Object theContext, 413 ServletConfiguration theConfiguration) throws Exception 414 { 415 theContext.getClass().getMethod("addServlet", 416 new Class [] {String .class, String .class, String .class}) 417 .invoke(theContext, 418 new Object [] {theConfiguration.getDefaultRedirectorName(), 419 "/" + theConfiguration.getDefaultRedirectorName(), 420 ServletTestRedirector.class.getName()}); 421 } 422 423 432 private void addJspRedirector(Object theContext) throws Exception 433 { 434 if (getResourceDir() != null) 435 { 436 theContext.getClass().getMethod("addServlet", 437 new Class [] {String .class, String .class}) 438 .invoke(theContext, 439 new Object [] {"*.jsp", 440 "org.apache.jasper.servlet.JspServlet"}); 441 442 Object handler = theContext.getClass().getMethod( 445 "getWebApplicationHandler", 446 new Class [] {}).invoke(theContext, new Object [] {}); 447 448 handler.getClass().getMethod("addServlet", 449 new Class [] {String .class, String .class, String .class, 450 String .class}) 451 .invoke(handler, 452 new Object [] { 453 "JspRedirector", 454 "/JspRedirector", 455 "org.apache.jasper.servlet.JspServlet", 456 "/jspRedirector.jsp"}); 457 } 458 } 459 460 471 private void addFilterRedirector(Object theContext, 472 FilterConfiguration theConfiguration) throws Exception 473 { 474 if (getResourceDir() != null) 475 { 476 Object handler = theContext.getClass().getMethod( 479 "getWebApplicationHandler", 480 new Class [] {}).invoke(theContext, new Object [] {}); 481 482 Object filterHolder = handler.getClass().getMethod("defineFilter", 483 new Class [] {String .class, String .class}) 484 .invoke(handler, 485 new Object [] {theConfiguration.getDefaultRedirectorName(), 486 FilterTestRedirector.class.getName()}); 487 488 filterHolder.getClass().getMethod("addAppliesTo", 489 new Class [] {String .class}) 490 .invoke(filterHolder, new Object [] {"REQUEST"}); 491 492 handler.getClass().getMethod("mapPathToFilter", 494 new Class [] {String .class, String .class}) 495 .invoke(handler, 496 new Object [] {"/" 497 + theConfiguration.getDefaultRedirectorName(), 498 theConfiguration.getDefaultRedirectorName()}); 499 } 500 } 501 502 510 protected int testConnectivity(URL theUrl) 511 { 512 int code; 513 try 514 { 515 HttpURLConnection connection = 516 (HttpURLConnection ) theUrl.openConnection(); 517 connection.setRequestProperty("Connection", "close"); 518 connection.connect(); 519 readFully(connection); 520 connection.disconnect(); 521 code = connection.getResponseCode(); 522 } 523 catch (IOException e) 524 { 525 code = -1; 526 } 527 return code; 528 } 529 530 538 protected boolean isAvailable(int theCode) 539 { 540 boolean result; 541 if ((theCode != -1) && (theCode < 300)) 542 { 543 result = true; 544 } 545 else 546 { 547 result = false; 548 } 549 return result; 550 } 551 552 559 protected void readFully(HttpURLConnection theConnection) 560 throws IOException 561 { 562 if (theConnection.getContentLength() != 0) 568 { 569 byte[] buf = new byte[256]; 570 InputStream in = theConnection.getInputStream(); 571 while (in.read(buf) != -1) 572 { 573 } 575 } 576 } 577 578 581 protected boolean isRunning() 582 { 583 return this.isRunning; 584 } 585 } 586
| Popular Tags
|