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 @Deprecated
34 public class IntegrationServiceTestClassRunner extends JUnit4ClassRunner {
35 final static Logger logger = LoggerFactory.getLogger(IntegrationServiceTestClassRunner.class);
36
37 private Class<?> testClass;
38 private Server server;
39 private String webAppPath;
40 private String contextPath;
41 private int port = 9090;
42
43 public IntegrationServiceTestClassRunner(Class<?> klass) throws InitializationError {
44 super(klass);
45 testClass = klass;
46 }
47
48 private void getAnnotations() {
49 IntegrationServer webapp = this.testClass.getAnnotation(IntegrationServer.class);
50 this.port = webapp.port();
51 this.webAppPath = webapp.webappPath();
52 this.contextPath = webapp.contextPath();
53
54 if (logger.isDebugEnabled()) {
55 logger.debug("port="+this.port);
56 logger.debug("webAppPath="+this.webAppPath);
57 logger.debug("contextPath="+this.contextPath);
58 }
59 }
60
61 private void setProperties() {
62 if (System.getProperty("catalina.base") == null) {
63 System.setProperty("catalina.base", "./target");
64 }
65
66 SystemProperties systemProperties = this.testClass.getAnnotation(SystemProperties.class);
67 if (systemProperties != null) {
68 for(Property property : systemProperties.properties()) {
69 System.setProperty(property.key(), property.value());
70 }
71 }
72 }
73
74 @Override
75 public void run(RunNotifier notifier) {
76 startServer();
77 super.run(notifier);
78 stopServer();
79 }
80
81 private void startServer() {
82 getAnnotations();
83 setProperties();
84
85 this.server = new Server();
86 Connector connector = new SelectChannelConnector();
87 connector.setPort(this.port);
88 this.server.setConnectors(new Connector[] { connector });
89
90
91
92
93 String root = IntegrationServiceTestClassRunner.class.getResource("/").getPath();
94 File webAppsPath = new File(root + this.webAppPath);
95
96 try {
97 if(!webAppsPath.isDirectory()) {
98 throw new RuntimeException("Webapps directory does not exist. " +
99 "Webapps directory must be an exploded (war) directory: " +
100 webAppsPath.getCanonicalPath());
101 }
102
103 if (logger.isDebugEnabled()) {
104 logger.debug("WebApps Path="+webAppsPath.getCanonicalPath());
105 }
106
107 WebAppContext webAppcontext = new WebAppContext();
108 webAppcontext.setParentLoaderPriority(true);
109 webAppcontext.setContextPath(this.contextPath);
110 webAppcontext.setWar(webAppsPath.getCanonicalPath());
111
112
113 HandlerCollection handlers = new HandlerCollection();
114 handlers.setHandlers(new Handler[] { webAppcontext, new DefaultHandler() });
115 this.server.setHandler(handlers);
116
117 this.server.start();
118 } catch (Exception e) {
119 throw new RuntimeException("Starting Jetty server failed", e);
120 }
121 }
122
123 private void stopServer() {
124 try {
125 this.server.stop();
126 } catch (Exception e) {
127 throw new RuntimeException("Stopping Jetty server failed", e);
128 }
129 }
130 }