package net.sourceforge.javautil.developer.web.unit.mockserver;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import net.sourceforge.javautil.common.io.VirtualDirectory;
import net.sourceforge.javautil.developer.web.unit.TestWebApplication;
import net.sourceforge.javautil.web.server.WebServerConnector;
import net.sourceforge.javautil.web.server.WebServerEvent;
import net.sourceforge.javautil.web.server.WebServerException;
import net.sourceforge.javautil.web.server.WebServerHost;
import net.sourceforge.javautil.web.server.WebServerListener;
import net.sourceforge.javautil.web.server.application.WebApplication;
import net.sourceforge.javautil.web.server.impl.WebServerAbstract;
/**
* MockServer allows one to truly simulate a javax.servlet environment on a pragmatic basis
* without dealing with http or other socket connections.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class MockServer extends WebServerAbstract {
protected final String name;
public MockServer(String name) {
this.name = name;
}
/**
* @param name The name of the application
* @param context The context (root path)
* @param root The root for source files
* @return The new web application
*/
public TestWebApplication create (String name, String context, VirtualDirectory root) {
TestWebApplication twa = new TestWebApplication(name, context, root);
return twa;
}
public void shutdown() {
for (WebServerHost host : this.hosts.values()) {
((MockServerHost)host).destroy();
}
}
public String getName() { return this.name; }
public void addConnector(WebServerConnector connector) throws WebServerException {
this.connectors.put(connector.getName(), connector);
}
public MockServerHost createHost(String name, String basePath, boolean defaultHost) throws WebServerException {
if (this.hosts.containsKey(name))
throw new WebServerException(this, "There is already a host with this name: " + name);
MockServerHost host = new MockServerHost(this, defaultHost, name, basePath);
this.hosts.put(name, host);
if (host.isDefault()) this.defaultHost = host;
return host;
}
public void initialize() throws WebServerException {}
public boolean start() { this.handler.serverStarted(new WebServerEvent(this)); return true; }
public boolean stop() { this.handler.serverStopped(new WebServerEvent(this)); return true; }
@Override public void removeHost(String name) {
if (!this.hosts.containsKey(name))
throw new WebServerException(this, "No such host: " + name);
((MockServerHost)hosts.get(name)).destroy();
super.removeHost(name);
}
/**
* @return Access to the handler
*/
protected WebServerListener getHandler () { return this.handler; }
}