/*
* Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
* Distributed under the terms of either:
* - the common development and distribution license (CDDL), v1.0; or
* - the GNU Lesser General Public License, v2.1 or later
* $Id: TestCaseServerside.java 3643 2007-01-12 15:29:45Z gbevin $
*/
package com.uwyn.rife;
import com.uwyn.rife.engine.Gate;
import com.uwyn.rife.rep.Rep;
import com.uwyn.rife.rep.Repository;
import com.uwyn.rife.resources.ResourceFinderClasspath;
import com.uwyn.rife.servlet.RifeFilter;
import com.uwyn.rife.servlet.RifeServlet;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Stack;
import junit.framework.TestCase;
import org.mortbay.http.SocketListener;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.FilterHolder;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.servlet.WebApplicationContext;
import org.mortbay.jetty.servlet.WebApplicationHandler;
import org.mortbay.util.Code;
import org.mortbay.util.Frame;
import org.mortbay.util.Log;
import org.mortbay.util.LogSink;
public abstract class TestCaseServerside extends TestCase
{
public final static int SITE_SERVLET = 1;
public final static int SITE_FILTER = 2;
private int mSiteType = SITE_SERVLET;
private String mWebAppName = "empty";
private String mWebXmlPath = "/WEB-INF/web.xml";
private Server mServer = null;
private CollectingLogSink mLogSink = null;
public TestCaseServerside(int siteType, String name)
{
super(name);
setSiteType(siteType);
}
protected CollectingLogSink getLogSink()
{
return mLogSink;
}
private WebApplicationContext prepareWebapp(String context)
throws Exception
{
// stop previous server if it was set up
stopServer();
// Disable debug output
Log.instance().disableLog();
mLogSink = new CollectingLogSink();
mLogSink.start();
Log.instance().add(mLogSink);
// Create the server
mServer = new Server();
mServer.setResolveRemoteHost(true);
// Create a port listener
SocketListener listener = new SocketListener();
listener.setHost("localhost");
listener.setPort(8181);
mServer.addListener(listener);
// Find the absolute path of the web application in the class path
ResourceFinderClasspath resource_finder = ResourceFinderClasspath.getInstance();
URL webapp_resource = resource_finder.getResource(mWebAppName+mWebXmlPath);
String webapp_resource_path = URLDecoder.decode(webapp_resource.getFile());
webapp_resource_path = webapp_resource_path.substring(0, webapp_resource_path.length()-mWebXmlPath.length());
// Setup a new web application
WebApplicationContext webapp_context = mServer.addWebApplication("localhost", context, webapp_resource_path);
webapp_context.setDefaultsDescriptor(null);
return webapp_context;
}
private void startServer()
throws Exception
{
mServer.start();
}
public void setSiteType(int type)
{
if (type != SITE_SERVLET && type != SITE_FILTER) throw new IllegalArgumentException("invalid site type.");
mSiteType = type;
}
public void setWebAppName(String name)
{
mWebAppName = name;
}
public void setWebXmlPath(String path)
{
mWebXmlPath = path;
}
protected Gate setupSite(String siteXmlPath)
throws Exception
{
return setupSite("/", siteXmlPath, null);
}
protected Gate setupSite(String siteXmlPath, String[][] initParams)
throws Exception
{
return setupSite("/", siteXmlPath, initParams);
}
protected Gate setupSite(String context, String siteXmlPath)
throws Exception
{
return setupSite(context, siteXmlPath, null);
}
protected Gate setupSite(String context, String siteXmlPath, String[][] initParams)
throws Exception
{
switch (mSiteType)
{
case SITE_SERVLET:
return setupServlet(context, siteXmlPath, initParams);
case SITE_FILTER:
return setupFilter(context, siteXmlPath, initParams);
default:
return null;
}
}
private Gate setupServlet(String context, String siteXmlPath, String[][] initParams)
throws Exception
{
// Register the servlet
WebApplicationContext webapp_context = prepareWebapp(context);
ServletHandler servlet_handler = webapp_context.getServletHandler();
ServletHolder servlet_holder = servlet_handler.addServlet("GATE", "/*", "com.uwyn.rife.servlet.RifeServlet");
servlet_holder.setInitOrder(1);
// Setup the site if the xml path was provided
if (siteXmlPath != null)
{
servlet_holder.setInitParameter("site.xml.path", siteXmlPath);
}
// add the provided init parameters
if (initParams != null)
{
for (String[] param : initParams)
{
servlet_holder.setInitParameter(param[0], param[1]);
}
}
startServer();
return (Gate)((RifeServlet)servlet_holder.getServlet()).getGate();
}
private Gate setupFilter(String context, String siteXmlPath, String[][] initParams)
throws Exception
{
// Register the filter
WebApplicationContext webapp_context = prepareWebapp(context);
ServletHandler servlet_handler = webapp_context.getServletHandler();
ServletHolder servlet_holder = servlet_handler.addServlet("default", "/", "org.mortbay.jetty.servlet.Default");
WebApplicationHandler webapp_handler = webapp_context.getWebApplicationHandler();
FilterHolder filter_holder = webapp_handler.defineFilter("GATE", "com.uwyn.rife.servlet.RifeFilter");
filter_holder.addAppliesTo("REQUEST");
webapp_handler.mapPathToFilter("/*", "GATE");
// Configure the default servlet
servlet_holder.setInitParameter("acceptRanges", "true");
servlet_holder.setInitParameter("dirAllowed", "true");
servlet_holder.setInitParameter("putAllowed", "false");
servlet_holder.setInitParameter("delAllowed", "false");
servlet_holder.setInitParameter("redirectWelcome", "false");
servlet_holder.setInitOrder(0);
// Setup the site if the xml path was provided
if (siteXmlPath != null)
{
filter_holder.setInitParameter("site.xml.path", siteXmlPath);
}
// add the provided init parameters
if (initParams != null)
{
for (String[] param : initParams)
{
filter_holder.setInitParameter(param[0], param[1]);
}
}
startServer();
return (Gate)((RifeFilter)filter_holder.getFilter()).getGate();
}
private void stopServer()
throws Exception
{
if (mServer != null)
{
// disconnect default repository, otherwise it will be shutdown
// by the RIFE's lifecycle cleanup
Repository rep = Rep.getDefaultRepository();
Rep.setDefaultRepository(null);
// Stop the http server
mServer.stop();
mServer = null;
// put the default repository back
Rep.setDefaultRepository(rep);
}
}
public void tearDown()
throws Exception
{
stopServer();
}
protected class CollectingLogSink implements LogSink
{
private Stack<Object> mLog = null;
public Stack<Object> getLog()
{
return mLog;
}
public void start()
throws Exception
{
mLog = new Stack<Object>();
}
public void stop()
throws InterruptedException
{
mLog = null;
}
public boolean isStarted()
{
return mLog != null;
}
public void log(String tag, Object message, Frame frame, long time)
{
mLog.push(message);
}
public void setOptions(String options)
{
}
public String getOptions()
{
return "";
}
public void log(String formattedLog)
{
mLog.push(formattedLog);
}
public Throwable getInternalException()
{
Object last_entry = getLogSink().getLog().peek();
if (!(last_entry instanceof Code.LogMsg))
{
return null;
}
return ((Code.LogMsg)last_entry).getThrowable();
}
}
}
|