| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.test.spring; | 
  | 17 |  |   | 
  | 18 |  |  import java.io.File; | 
  | 19 |  |   | 
  | 20 |  |  import org.junit.internal.runners.InitializationError; | 
  | 21 |  |  import org.junit.internal.runners.JUnit4ClassRunner; | 
  | 22 |  |  import org.junit.runner.notification.RunNotifier; | 
  | 23 |  |  import org.mortbay.jetty.Connector; | 
  | 24 |  |  import org.mortbay.jetty.Handler; | 
  | 25 |  |  import org.mortbay.jetty.Server; | 
  | 26 |  |  import org.mortbay.jetty.handler.DefaultHandler; | 
  | 27 |  |  import org.mortbay.jetty.handler.HandlerCollection; | 
  | 28 |  |  import org.mortbay.jetty.nio.SelectChannelConnector; | 
  | 29 |  |  import org.mortbay.jetty.webapp.WebAppContext; | 
  | 30 |  |  import org.slf4j.Logger; | 
  | 31 |  |  import org.slf4j.LoggerFactory; | 
  | 32 |  |   | 
  | 33 |  |  public class IntegrationServiceTestClassRunner extends JUnit4ClassRunner { | 
  | 34 | 0 |          final static Logger logger = LoggerFactory.getLogger(IntegrationServiceTestClassRunner.class); | 
  | 35 |  |   | 
  | 36 |  |          private Class<?> testClass; | 
  | 37 |  |          private Server server; | 
  | 38 |  |          private String webAppPath; | 
  | 39 |  |          private String contextPath; | 
  | 40 | 0 |          private int port = 9090; | 
  | 41 |  |   | 
  | 42 |  |          public IntegrationServiceTestClassRunner(Class<?> klass) throws InitializationError { | 
  | 43 | 0 |                  super(klass); | 
  | 44 | 0 |                  testClass = klass; | 
  | 45 | 0 |          } | 
  | 46 |  |   | 
  | 47 |  |          private void getAnnotations() { | 
  | 48 | 0 |                  IntegrationServer webapp = this.testClass.getAnnotation(IntegrationServer.class); | 
  | 49 | 0 |                  this.port = webapp.port(); | 
  | 50 | 0 |                  this.webAppPath = webapp.webappPath(); | 
  | 51 | 0 |                  this.contextPath = webapp.contextPath(); | 
  | 52 |  |   | 
  | 53 | 0 |                  if (logger.isDebugEnabled()) { | 
  | 54 | 0 |                          logger.debug("port="+this.port); | 
  | 55 | 0 |                          logger.debug("webAppPath="+this.webAppPath); | 
  | 56 | 0 |                          logger.debug("contextPath="+this.contextPath); | 
  | 57 |  |                  } | 
  | 58 | 0 |          } | 
  | 59 |  |           | 
  | 60 |  |          private void setProperties() { | 
  | 61 | 0 |                  if (System.getProperty("catalina.base") == null) { | 
  | 62 | 0 |                          System.setProperty("catalina.base", "./target"); | 
  | 63 |  |                  } | 
  | 64 |  |   | 
  | 65 | 0 |                  SystemProperties systemProperties = this.testClass.getAnnotation(SystemProperties.class); | 
  | 66 | 0 |                  if (systemProperties != null) { | 
  | 67 | 0 |                          for(Property property : systemProperties.properties()) { | 
  | 68 | 0 |                                  System.setProperty(property.key(), property.value()); | 
  | 69 |  |                          } | 
  | 70 |  |                  } | 
  | 71 | 0 |          } | 
  | 72 |  |   | 
  | 73 |  |          @Override | 
  | 74 |  |          public void run(RunNotifier notifier) { | 
  | 75 | 0 |                  startServer(); | 
  | 76 | 0 |                  super.run(notifier); | 
  | 77 | 0 |                  stopServer(); | 
  | 78 | 0 |          } | 
  | 79 |  |           | 
  | 80 |  |          private void startServer() { | 
  | 81 | 0 |                  getAnnotations(); | 
  | 82 | 0 |                  setProperties(); | 
  | 83 |  |                   | 
  | 84 | 0 |                  this.server = new Server(); | 
  | 85 | 0 |                  Connector connector = new SelectChannelConnector(); | 
  | 86 | 0 |                  connector.setPort(this.port); | 
  | 87 | 0 |                  this.server.setConnectors(new Connector[] { connector }); | 
  | 88 |  |   | 
  | 89 |  |                   | 
  | 90 |  |                   | 
  | 91 |  |   | 
  | 92 | 0 |                  String root = IntegrationServiceTestClassRunner.class.getResource("/").getPath(); | 
  | 93 | 0 |                  File webAppsPath = new File(root + this.webAppPath); | 
  | 94 |  |   | 
  | 95 |  |                  try { | 
  | 96 | 0 |                      if(!webAppsPath.isDirectory()) { | 
  | 97 | 0 |                              throw new RuntimeException("Webapps directory does not exist. " + | 
  | 98 |  |                                              "Webapps directory must be an exploded (war) directory: " +  | 
  | 99 |  |                                              webAppsPath.getCanonicalPath()); | 
  | 100 |  |                      } | 
  | 101 |  |                                           | 
  | 102 | 0 |                          if (logger.isDebugEnabled()) { | 
  | 103 | 0 |                                  logger.debug("WebApps Path="+webAppsPath.getCanonicalPath()); | 
  | 104 |  |                          } | 
  | 105 |  |           | 
  | 106 | 0 |                          WebAppContext webAppcontext = new WebAppContext(); | 
  | 107 | 0 |                          webAppcontext.setParentLoaderPriority(true); | 
  | 108 | 0 |                          webAppcontext.setContextPath(this.contextPath);  | 
  | 109 | 0 |                          webAppcontext.setWar(webAppsPath.getCanonicalPath()); | 
  | 110 |  |                           | 
  | 111 |  |           | 
  | 112 | 0 |                          HandlerCollection handlers = new HandlerCollection(); | 
  | 113 | 0 |                          handlers.setHandlers(new Handler[] { webAppcontext, new DefaultHandler() }); | 
  | 114 | 0 |                          this.server.setHandler(handlers); | 
  | 115 |  |   | 
  | 116 | 0 |                          this.server.start(); | 
  | 117 | 0 |                  } catch (Exception e) { | 
  | 118 | 0 |                          throw new RuntimeException("Starting Jetty server failed", e); | 
  | 119 | 0 |                  } | 
  | 120 | 0 |          } | 
  | 121 |  |   | 
  | 122 |  |          private void stopServer() { | 
  | 123 |  |                  try { | 
  | 124 | 0 |                          this.server.stop(); | 
  | 125 | 0 |                  } catch (Exception e) { | 
  | 126 | 0 |                          throw new RuntimeException("Stopping Jetty server failed", e); | 
  | 127 | 0 |                  } | 
  | 128 | 0 |          } | 
  | 129 |  |  } |