Custom JUnit test setup to use to automatically start Jetty. Example:
| Method from org.apache.cactus.extension.jetty.JettyTestSetup Detail: |
protected final File getConfigFile() {
if (this.configFile == null)
{
String configFileProperty = System.getProperty(
CACTUS_JETTY_CONFIG_PROPERTY);
if (configFileProperty != null)
{
this.configFile = new File(configFileProperty);
}
}
return this.configFile;
}
|
protected final File getResourceDir() {
if (this.resourceDir == null)
{
String resourceDirProperty = System.getProperty(
CACTUS_JETTY_RESOURCE_DIR_PROPERTY);
if (resourceDirProperty != null)
{
this.resourceDir = new File(resourceDirProperty);
}
}
return this.resourceDir;
}
|
protected boolean isAvailable(int theCode) {
boolean result;
if ((theCode != -1) && (theCode < 300))
{
result = true;
}
else
{
result = false;
}
return result;
}
Tests whether an HTTP return code corresponds to a valid connection
to the test URL or not. Success is 200 up to but excluding 300. |
protected boolean isRunning() {
return this.isRunning;
}
|
protected void readFully(HttpURLConnection theConnection) throws IOException {
// Only read if there is data to read ... The problem is that not
// all servers return a content-length header. If there is no header
// getContentLength() returns -1. It seems to work and it seems
// that all servers that return no content-length header also do
// not block on read() operations!
if (theConnection.getContentLength() != 0)
{
byte[] buf = new byte[256];
InputStream in = theConnection.getInputStream();
while (in.read(buf) != -1)
{
// Make sure we read all the data in the stream
}
}
}
Fully reads the input stream from the passed HTTP URL connection to
prevent (harmless) server-side exception. |
public void run(TestResult theResult) {
Protectable p = new Protectable()
{
public void protect() throws Exception
{
try
{
setUp();
basicRun(theResult);
}
finally
{
tearDown();
}
}
};
theResult.runProtected(this, p);
}
|
public final void setConfigFile(File theConfigFile) {
this.configFile = theConfigFile;
}
Sets the configuration file to use for initializing Jetty. |
public final void setForceShutdown(boolean isForcedShutdown) {
this.forceShutdown = isForcedShutdown;
}
|
public final void setResourceDir(File theResourceDir) {
this.resourceDir = theResourceDir;
}
Sets the directory in which Jetty will look for the web-application
resources. |
protected void setUp() throws Exception {
// Try connecting in case the server is already running. If so, does
// nothing
URL contextURL = new URL(this.baseConfiguration.getContextURL()
+ "/" + this.servletConfiguration.getDefaultRedirectorName()
+ "?Cactus_Service=RUN_TEST");
this.alreadyRunning = isAvailable(testConnectivity(contextURL));
if (this.alreadyRunning)
{
// Server is already running. Record this information so that we
// don't stop it afterwards.
this.isRunning = true;
return;
}
// Note: We are currently using reflection in order not to need Jetty
// to compile Cactus. If the code becomes more complex or we need to
// add other initializer, it will be worth considering moving them
// to a separate "extension" subproject which will need additional jars
// in its classpath (using the same mechanism as the Ant project is
// using to conditionally compile tasks).
// Create a Jetty Server object and configure a listener
this.server = createServer(this.baseConfiguration);
// Create a Jetty context.
Object context = createContext(this.server, this.baseConfiguration);
// Add the Cactus Servlet redirector
addServletRedirector(context, this.servletConfiguration);
// Add the Cactus Jsp redirector
addJspRedirector(context);
// Add the Cactus Filter redirector
addFilterRedirector(context, this.filterConfiguration);
// Configure Jetty with an XML file if one has been specified on the
// command line.
if (getConfigFile() != null)
{
this.server.getClass().getMethod("configure",
new Class[] {String.class}).invoke(
this.server, new Object[] {getConfigFile().toString()});
}
// Start the Jetty server
this.server.getClass().getMethod("start", null).invoke(
this.server, null);
this.isRunning = true;
}
Start an embedded Jetty server. It is allowed to pass a Jetty XML as
a system property (cactus.jetty.config) to further
configure Jetty. Example:
-Dcactus.jetty.config=./jetty.xml. |
protected void tearDown() throws Exception {
// Don't shut down a container that has not been started by us
if (!this.forceShutdown && this.alreadyRunning)
{
return;
}
if (this.server != null)
{
// First, verify if the server is running
boolean started = ((Boolean) this.server.getClass().getMethod(
"isStarted", null).invoke(this.server, null)).booleanValue();
// Stop and destroy the Jetty server, if started
if (started)
{
// Stop all listener and contexts
this.server.getClass().getMethod("stop", null).invoke(
this.server, null);
// Destroy a stopped server. Remove all components and send
// notifications to all event listeners.
this.server.getClass().getMethod("destroy", null).invoke(
this.server, null);
}
}
this.isRunning = false;
}
Stop the running Jetty server. |
protected int testConnectivity(URL theUrl) {
int code;
try
{
HttpURLConnection connection =
(HttpURLConnection) theUrl.openConnection();
connection.setRequestProperty("Connection", "close");
connection.connect();
readFully(connection);
connection.disconnect();
code = connection.getResponseCode();
}
catch (IOException e)
{
code = -1;
}
return code;
}
Tests whether we are able to connect to the HTTP server identified by the
specified URL. |