<commit_before>package org.codehaus.enunciate.test.integration;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.httpclient.HttpClient;
import org.codehaus.enunciate.test.integration.util.FileUtil;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

/**
 * JUnit base class for running lightweight integration tests on webapps with an embedded 
 * web server (Jetty).
 * 
 * This class provides static setUpBeforeClass() and tearDownAfterClass() methods to 
 * start and stop the web server.  JUnit 4 testers will call these methods from their
 * @BeforeClass and @AfterClass methods.
 * 
 * A shared Apache Commons HttpClient instance is provided and tests can retrieve the webapp's full URL by 
 * calling getBaseContextAsString():
 * <pre>
 * public void testServiceIsRunning() throws Exception {
 *   GetMethod method = new GetMethod(GenericWebServerTestCase.getBaseContextAsString());
 *   try {
 *     int statusCode = GenericWebServerTestCase.getHttpClient().executeMethod(method);
 *     assertEquals("HttpStatus.SC_OK", HttpStatus.SC_OK, statusCode);
 *   } finally {
 *     method.releaseConnection();
 *   }
 * }
 * </pre>
 * 
 * Minimally the relativeContext and webappHome properties must be set before
 * calling setUpBeforeClass():
 * <pre>
 * public static void beforeTest() {
 *   GenericWebServerTestCase.setRelativeContext("/my-webapp");
 *   GenericWebServerTestCase.setWebappHome("target/my-webapp");
 *   GenericWebServerTestCase.setUpBeforeClass();
 * }
 * </pre>
 * 
 * Alternatively, configuration can be loaded from a properties file.
 * The default, Maven-friendly location of the 'jetty.properties' file is the 
 * './target/test-classes/jetty.properties', but the location can be changed
 * at startup:
 * <pre>
 * public static void beforeTest() {
 *   GenericWebServerTestCase.setPropertyFileLocation("src/config/webapp-config.properties");
 *   GenericWebServerTestCase.setUpBeforeClass();
 * }
 * </pre>
 * 
 * One motivation for supporting property file configuration is to allow filtering by
 * Maven.  Here is how that might look:
 * <pre>
 * context=/${project.build.finalName}
 * webapp.home=${project.build.directory}/${project.build.finalName}
 * host=localhost
 * protocol=http
 * port=0
 * </pre>
 * 
 * The default port is <code>0</code> which tells the web server to pick an arbitrary port.
 * The actual port used is available from the relevant static property methods,
 * such as <code>getActualPort</code>.  Tests expecting a specific port can set this
 * via the <code>setSpecifiedPort</code> methhod or <code>port</code> property.
 * 
 * @author reaster
 */
abstract public class GenericWebServerTestCase {

	public static String DEFAULT_JETTY_PROPERTIES_PATH = "./target/test-classes/jetty.properties";

	protected final static int DEFAULT_PORT = 0;

	protected final static String DEFAULT_PROTOCOL = "http";

	protected final static String DEFAULT_HOST = "localhost";

	protected static int specifiedPort = DEFAULT_PORT;

	protected static String protocol = DEFAULT_PROTOCOL;

	protected static String host = DEFAULT_HOST;

	protected static int actualPort;

	protected static String relativeContext;

	protected static File webappHome;

	protected static URL baseContext;

	protected static String baseContextString;

	protected static String jettyPropertiesPath = DEFAULT_JETTY_PROPERTIES_PATH;
	
	protected static Properties properties;

	protected static Server jettyServer = null;

	protected static HttpClient httpClient;

	private static boolean _debug = true;

	private static boolean _verbose = false;

	/**
	 * Start embedded web server and static properties including baseContext.
	 * 
	 * @throws Exception
	 */
	// @org.junit.BeforeClass
	public static void setUpBeforeClass() throws Exception {
		if (httpClient == null) {
			loadAndVerifyJettyProperties();
			jettyServer = new Server(specifiedPort);
			jettyServer.addHandler(new WebAppContext(webappHome.getAbsolutePath(), relativeContext));
			//org.mortbay.log.Log.getLog().setDebugEnabled(false); // TODO too verbose
			jettyServer.start();
			actualPort = jettyServer.getConnectors()[0].getLocalPort();
			baseContext = new URL(protocol, host, actualPort, relativeContext);
			info("Loading WebAppContext[%s] from %s",  baseContext.toExternalForm(), webappHome.getAbsolutePath());
			httpClient = new HttpClient();
		}
	}

	// @org.junit.AfterClass
	public static void tearDownAfterClass() {
		if (jettyServer != null) {
			try {
				if (jettyServer.isRunning()) {
					jettyServer.stop();
				}
			} catch (Exception e) {
				warn("%s on shutting down Jetty server", e.getMessage());
			} finally {
				jettyServer = null;<commit_msg>fixed ClassCastException when accessing Spring WebApplicationContext<commit_after>package org.codehaus.enunciate.test.integration;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.httpclient.HttpClient;
import org.codehaus.enunciate.test.integration.util.FileUtil;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

/**
 * JUnit base class for running lightweight integration tests on webapps with an embedded 
 * web server (Jetty).
 * 
 * This class provides static setUpBeforeClass() and tearDownAfterClass() methods to 
 * start and stop the web server.  JUnit 4 testers will call these methods from their
 * @BeforeClass and @AfterClass methods.
 * 
 * A shared Apache Commons HttpClient instance is provided and tests can retrieve the webapp's full URL by 
 * calling getBaseContextAsString():
 * <pre>
 * public void testServiceIsRunning() throws Exception {
 *   GetMethod method = new GetMethod(GenericWebServerTestCase.getBaseContextAsString());
 *   try {
 *     int statusCode = GenericWebServerTestCase.getHttpClient().executeMethod(method);
 *     assertEquals("HttpStatus.SC_OK", HttpStatus.SC_OK, statusCode);
 *   } finally {
 *     method.releaseConnection();
 *   }
 * }
 * </pre>
 * 
 * Minimally the relativeContext and webappHome properties must be set before
 * calling setUpBeforeClass():
 * <pre>
 * public static void beforeTest() {
 *   GenericWebServerTestCase.setRelativeContext("/my-webapp");
 *   GenericWebServerTestCase.setWebappHome("target/my-webapp");
 *   GenericWebServerTestCase.setUpBeforeClass();
 * }
 * </pre>
 * 
 * Alternatively, configuration can be loaded from a properties file.
 * The default, Maven-friendly location of the 'jetty.properties' file is the 
 * './target/test-classes/jetty.properties', but the location can be changed
 * at startup:
 * <pre>
 * public static void beforeTest() {
 *   GenericWebServerTestCase.setPropertyFileLocation("src/config/webapp-config.properties");
 *   GenericWebServerTestCase.setUpBeforeClass();
 * }
 * </pre>
 * 
 * One motivation for supporting property file configuration is to allow filtering by
 * Maven.  Here is how that might look:
 * <pre>
 * context=/${project.build.finalName}
 * webapp.home=${project.build.directory}/${project.build.finalName}
 * host=localhost
 * protocol=http
 * port=0
 * </pre>
 * 
 * The default port is <code>0</code> which tells the web server to pick an arbitrary port.
 * The actual port used is available from the relevant static property methods,
 * such as <code>getActualPort</code>.  Tests expecting a specific port can set this
 * via the <code>setSpecifiedPort</code> methhod or <code>port</code> property.
 * 
 * @author reaster
 */
abstract public class GenericWebServerTestCase {

	public static String DEFAULT_JETTY_PROPERTIES_PATH = "./target/test-classes/jetty.properties";

	protected final static int DEFAULT_PORT = 0;

	protected final static String DEFAULT_PROTOCOL = "http";

	protected final static String DEFAULT_HOST = "localhost";

	protected static int specifiedPort = DEFAULT_PORT;

	protected static String protocol = DEFAULT_PROTOCOL;

	protected static String host = DEFAULT_HOST;

	protected static int actualPort;

	protected static String relativeContext;

	protected static File webappHome;

	protected static URL baseContext;

	protected static String baseContextString;

	protected static String jettyPropertiesPath = DEFAULT_JETTY_PROPERTIES_PATH;
	
	protected static Properties properties;

	protected static Server jettyServer = null;

	protected static HttpClient httpClient;

	private static boolean _debug = true;

	private static boolean _verbose = false;

	/**
	 * Start embedded web server and static properties including baseContext.
	 * 
	 * @throws Exception
	 */
	// @org.junit.BeforeClass
	public static void setUpBeforeClass() throws Exception {
		if (httpClient == null) {
			loadAndVerifyJettyProperties();
			jettyServer = new Server(specifiedPort);
			WebAppContext context = new WebAppContext(webappHome.getAbsolutePath(), relativeContext);
			context.setParentLoaderPriority(true);
			jettyServer.addHandler(context);
			//org.mortbay.log.Log.getLog().setDebugEnabled(false); // TODO too verbose
			jettyServer.start();
			actualPort = jettyServer.getConnectors()[0].getLocalPort();
			baseContext = new URL(protocol, host, actualPort, relativeContext);
			info("Loading WebAppContext[%s] from %s",  baseContext.toExternalForm(), webappHome.getAbsolutePath());
			httpClient = new HttpClient();
		}
	}

	// @org.junit.AfterClass
	public static void tearDownAfterClass() {
		if (jettyServer != null) {
			try {
				if (jettyServer.isRunning()) {
					jettyServer.stop();
				}
			} catch (Exception e) {
				warn("%s on shutting down Jetty server", e.getMessage());
			} finally {
				jettyServer = null;
{'fraction_non_alphanumeric': 0.09777138749101366, 'fraction_numerical': 0.0010270103728047654, 'mean_word_length': 4.641946697566628, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '988845', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2644, 'n_words': 804}