View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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  		// Metro: Additional debugging info to console
91  		// com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true;      
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); // e.g. /brms-ws-0.1.0-SNAPSHOT
110 			webAppcontext.setWar(webAppsPath.getCanonicalPath());
111 			//webAppcontext.setTempDirectory(new File(root));
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 }