001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.student.common.test.spring;
017    
018    import java.io.File;
019    
020    import org.junit.internal.runners.InitializationError;
021    import org.junit.internal.runners.JUnit4ClassRunner;
022    import org.junit.runner.notification.RunNotifier;
023    import org.mortbay.jetty.Connector;
024    import org.mortbay.jetty.Handler;
025    import org.mortbay.jetty.Server;
026    import org.mortbay.jetty.handler.DefaultHandler;
027    import org.mortbay.jetty.handler.HandlerCollection;
028    import org.mortbay.jetty.nio.SelectChannelConnector;
029    import org.mortbay.jetty.webapp.WebAppContext;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    @Deprecated
034    public class IntegrationServiceTestClassRunner extends JUnit4ClassRunner {
035            final static Logger logger = LoggerFactory.getLogger(IntegrationServiceTestClassRunner.class);
036    
037            private Class<?> testClass;
038            private Server server;
039            private String webAppPath;
040            private String contextPath;
041            private int port = 9090;
042    
043            public IntegrationServiceTestClassRunner(Class<?> klass) throws InitializationError {
044                    super(klass);
045                    testClass = klass;
046            }
047    
048            private void getAnnotations() {
049                    IntegrationServer webapp = this.testClass.getAnnotation(IntegrationServer.class);
050                    this.port = webapp.port();
051                    this.webAppPath = webapp.webappPath();
052                    this.contextPath = webapp.contextPath();
053    
054                    if (logger.isDebugEnabled()) {
055                            logger.debug("port="+this.port);
056                            logger.debug("webAppPath="+this.webAppPath);
057                            logger.debug("contextPath="+this.contextPath);
058                    }
059            }
060            
061            private void setProperties() {
062                    if (System.getProperty("catalina.base") == null) {
063                            System.setProperty("catalina.base", "./target");
064                    }
065    
066                    SystemProperties systemProperties = this.testClass.getAnnotation(SystemProperties.class);
067                    if (systemProperties != null) {
068                            for(Property property : systemProperties.properties()) {
069                                    System.setProperty(property.key(), property.value());
070                            }
071                    }
072            }
073    
074            @Override
075            public void run(RunNotifier notifier) {
076                    startServer();
077                    super.run(notifier);
078                    stopServer();
079            }
080            
081            private void startServer() {
082                    getAnnotations();
083                    setProperties();
084                    
085                    this.server = new Server();
086                    Connector connector = new SelectChannelConnector();
087                    connector.setPort(this.port);
088                    this.server.setConnectors(new Connector[] { connector });
089    
090                    // Metro: Additional debugging info to console
091                    // com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true;      
092    
093                    String root = IntegrationServiceTestClassRunner.class.getResource("/").getPath();
094                    File webAppsPath = new File(root + this.webAppPath);
095    
096                    try {
097                        if(!webAppsPath.isDirectory()) {
098                            throw new RuntimeException("Webapps directory does not exist. " +
099                                            "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    }