001: /*
002: * $Id: JettyDecorator.java 461192 2006-06-28 08:37:16 +0200 (Wed, 28 Jun 2006)
003: * ehillenius $ $Revision: 464023 $ $Date: 2006-06-28 08:37:16 +0200 (Wed, 28 Jun
004: * 2006) $
005: *
006: * ====================================================================
007: * Copyright (c) 2003, Open Edge B.V. All rights reserved. Redistribution and
008: * use in source and binary forms, with or without modification, are permitted
009: * provided that the following conditions are met: Redistributions of source
010: * code must retain the above copyright notice, this list of conditions and the
011: * following disclaimer. Redistributions in binary form must reproduce the above
012: * copyright notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution. Neither
014: * the name of OpenEdge B.V. nor the names of its contributors may be used to
015: * endorse or promote products derived from this software without specific prior
016: * written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package nl.openedge.util.jetty;
031:
032: import junit.framework.Test;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.mortbay.jetty.Connector;
037: import org.mortbay.jetty.Server;
038: import org.mortbay.jetty.nio.SelectChannelConnector;
039: import org.mortbay.jetty.webapp.WebAppContext;
040:
041: /**
042: * JUnit decorator for starting and stopping a local instance Jetty for usage
043: * with test cases.
044: * <p>
045: * It uses property 'jettyConfig' to load the - mandatory - Jetty configuration
046: * document. Eg '/jetty-test-config.xml' is loaded from the classpath root (so
047: * providing the configuration document in the root of the test classes folder
048: * will suffice) but 'file://c:/mydir/myconfig.xml' should work as well. If
049: * property 'jettyConfig' is not provided (default == null), the properties
050: * 'port', 'webappContextRoot' and 'contextPath' are used to start a Jetty
051: * instance.
052: * </p>
053: * <p>
054: * Property useJettyPlus (default == false) is used to decide whether JettyPlus
055: * should be used, or just the basic version of Jetty. JettyPlus provides
056: * support for JNDI, datasources, transactions etc.
057: * </p>
058: * <p>
059: * Usage:
060: *
061: * <pre>
062: *
063: * ...
064: * public static Test suite()
065: * {
066: * TestSuite suite = new TestSuite();
067: * suite.addTest(new JettyDecoratorWithArgsTest("testPing"));
068: * JettyDecorator deco = new JettyDecorator(suite);
069: * deco.setPort(8098);
070: * deco.setWebappContextRoot("src/webapp");
071: * deco.setContextPath("/test");
072: * deco.setUseJettyPlus(false);
073: * return deco;
074: * }
075: * ...
076: *
077: * </pre>
078: *
079: * Jetty will be started before the tests are actually run, and will be stopped
080: * afterwards.
081: * </p>
082: *
083: * @author Eelco Hillenius
084: */
085: public class JettyDecorator extends AbstractJettyDecorator {
086: /** logger. */
087: private static final Log log = LogFactory
088: .getLog(JettyDecorator.class);
089:
090: /** instance of jetty server. */
091: private Server server = null;
092:
093: /**
094: * construct with test.
095: *
096: * @param test
097: * test case
098: */
099: public JettyDecorator(final Test test) {
100: super (test);
101: }
102:
103: /**
104: * Start Jetty.
105: *
106: * @throws Exception
107: * @see junit.extensions.TestSetup#setUp()
108: */
109: public void setUp() throws Exception {
110: server = new Server();
111: SelectChannelConnector connector = new SelectChannelConnector();
112: connector.setPort(getPort());
113: server.setConnectors(new Connector[] { connector });
114:
115: WebAppContext web = new WebAppContext();
116: web.setContextPath(getContextPath());
117: web.setWar(getWebappContextRoot());
118: server.addHandler(web);
119:
120: log.info("Starting Jetty");
121: server.start();
122: log.info("Jetty started");
123: }
124:
125: /**
126: * Stop Jetty.
127: *
128: * @see junit.extensions.TestSetup#tearDown()
129: */
130: public void tearDown() {
131: log.info("Stopping Jetty");
132: try {
133: server.stop();
134: log.info("Jetty stopped");
135: } catch (Exception e) {
136: log.error(e.getMessage(), e);
137: }
138: }
139: }
|