| Classes in this File | Line Coverage | Branch Coverage | Complexity | |||||||
| JettyRunner |
|
| 0.0;0 |
| 1 | // Copyright 2006 Howard M. Lewis Ship |
|
| 2 | // |
|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 4 | // you may not use this file except in compliance with the License. |
|
| 5 | // You may obtain a copy of the License at |
|
| 6 | // |
|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
| 8 | // |
|
| 9 | // Unless required by applicable law or agreed to in writing, software |
|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 12 | // See the License for the specific language governing permissions and |
|
| 13 | // limitations under the License. |
|
| 14 | ||
| 15 | package com.javaforge.tapestry.testng; |
|
| 16 | ||
| 17 | import org.mortbay.jetty.Connector; |
|
| 18 | import org.mortbay.jetty.Server; |
|
| 19 | import org.mortbay.jetty.nio.BlockingChannelConnector; |
|
| 20 | import org.mortbay.jetty.webapp.WebAppContext; |
|
| 21 | ||
| 22 | import static java.lang.String.format; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * A utitilty class for running an instance of the <a |
|
| 26 | * href="http://jetty.mortbay.com/jetty/index.html">Jetty servlet container</a> from within a unit |
|
| 27 | * test. This code is based on the Jetty 6 code base (which is in beta at the time of writing). When |
|
| 28 | * combined with <a href="http://jwebunit.sourceforge.net/">jWebUnit</a>, this allows for basic |
|
| 29 | * <em>integration</em> testing of an application from within a unit test case. |
|
| 30 | * |
|
| 31 | * @author Howard M. Lewis Ship |
|
| 32 | */ |
|
| 33 | public class JettyRunner |
|
| 34 | { |
|
| 35 | public static final String DEFAULT_CONTEXT_PATH = "/"; |
|
| 36 | ||
| 37 | public static final int DEFAULT_PORT = 80; |
|
| 38 | ||
| 39 | public static final String DEFAULT_WAR_PATH = "src/main/webapp"; |
|
| 40 | ||
| 41 | private final String _contextPath; |
|
| 42 | ||
| 43 | private final int _port; |
|
| 44 | ||
| 45 | private final String _warPath; |
|
| 46 | ||
| 47 | private final Server _jetty; |
|
| 48 | ||
| 49 | /** |
|
| 50 | * Creates and starts a new instance of Jetty, using default configuration values. The |
|
| 51 | * contextPath will be /, the port will be 80, the warPath will be src/main/webapp. |
|
| 52 | */ |
|
| 53 | public JettyRunner() |
|
| 54 | { |
|
| 55 | 0 | this(DEFAULT_CONTEXT_PATH, DEFAULT_PORT, DEFAULT_WAR_PATH); |
| 56 | 0 | } |
| 57 | ||
| 58 | /** |
|
| 59 | * Creates and starts a new instance of Jetty. This should be done from a test case setup |
|
| 60 | * method. |
|
| 61 | * |
|
| 62 | * @param contextPath |
|
| 63 | * the context path for the deployed application |
|
| 64 | * @param port |
|
| 65 | * the port number used to access the application |
|
| 66 | * @param warPath |
|
| 67 | * the path to the exploded web application (typically, "src/main/webapp") |
|
| 68 | */ |
|
| 69 | public JettyRunner(String contextPath, int port, String warPath) |
|
| 70 | 3 | { |
| 71 | 3 | _contextPath = contextPath; |
| 72 | 3 | _port = port; |
| 73 | 3 | _warPath = warPath; |
| 74 | ||
| 75 | 3 | _jetty = new Server(); |
| 76 | ||
| 77 | 3 | startJetty(); |
| 78 | 3 | } |
| 79 | ||
| 80 | /** Stops the Jetty instance. This should be called from a test case tear down method. */ |
|
| 81 | public void stop() |
|
| 82 | { |
|
| 83 | try |
|
| 84 | { |
|
| 85 | 3 | _jetty.stop(); |
| 86 | } |
|
| 87 | 0 | catch (Exception ex) |
| 88 | { |
|
| 89 | 0 | throw new RuntimeException("Error stopping Jetty instance: " + ex.toString(), ex); |
| 90 | 3 | } |
| 91 | 3 | } |
| 92 | ||
| 93 | @Override |
|
| 94 | public String toString() |
|
| 95 | { |
|
| 96 | 0 | return format("<JettyRunner %s:%d (%s)>", _contextPath, _port, _warPath); |
| 97 | } |
|
| 98 | ||
| 99 | private void startJetty() |
|
| 100 | { |
|
| 101 | try |
|
| 102 | { |
|
| 103 | 3 | BlockingChannelConnector connector = new BlockingChannelConnector(); |
| 104 | 3 | connector.setPort(_port); |
| 105 | ||
| 106 | 3 | _jetty.setConnectors(new Connector[] |
| 107 | { connector }); |
|
| 108 | ||
| 109 | 3 | WebAppContext context = new WebAppContext(); |
| 110 | ||
| 111 | 3 | context.setContextPath(_contextPath); |
| 112 | 3 | context.setWar(_warPath); |
| 113 | ||
| 114 | 3 | _jetty.addHandler(context); |
| 115 | ||
| 116 | 3 | _jetty.start(); |
| 117 | } |
|
| 118 | 0 | catch (Exception ex) |
| 119 | { |
|
| 120 | 0 | throw new RuntimeException("Failure starting Jetty instance: " + ex.toString(), ex); |
| 121 | 3 | } |
| 122 | 3 | } |
| 123 | ||
| 124 | } |