Title: [497] trunk/rails-integration: Tidied up code.
Revision
497
Author
tantalon
Date
2007-04-25 01:27:22 -0400 (Wed, 25 Apr 2007)

Log Message

Tidied up code.
Removed samples for old Rails versions, focus on the latest.
Changed FileServletTest to use Tomcat 4 + HttpUnit rather than EasyMock.
Changed HttpOutputTest to use EasyMock 1.2 for JDK 1.3, so we can run the tests under 1.4.
Changed RailsServletTest to only test against Rails 1.2.3.

Modified Paths

Added Paths

Removed Paths

  • trunk/rails-integration/samples/helloworld-1.1.6/
  • trunk/rails-integration/samples/helloworld-1.2.1/
  • trunk/rails-integration/samples/helloworld-1.2.2/

Diff

Modified: trunk/rails-integration/pom.xml (496 => 497)


--- trunk/rails-integration/pom.xml	2007-04-25 01:42:41 UTC (rev 496)
+++ trunk/rails-integration/pom.xml	2007-04-25 05:27:22 UTC (rev 497)
@@ -83,10 +83,10 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>org.easymock</groupId>
-      <artifactId>easymock</artifactId>
-      <version>2.0</version>
-      <scope>test</scope>
+        <groupId>easymock</groupId>
+        <artifactId>easymock</artifactId>
+        <version>1.2_Java1.3</version>
+        <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>httpunit</groupId>

Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java (496 => 497)


--- trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java	2007-04-25 01:42:41 UTC (rev 496)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java	2007-04-25 05:27:22 UTC (rev 497)
@@ -42,12 +42,12 @@
 		ServletContext context = getServletContext();
 		rootPath = context.getRealPath("/");
 		if (rootPath == null) {
-			throw new ServletException(
-					"Cannot find the real path of this webapp, probably using a non-extracted WAR");
+			throw new ServletException("Cannot find the real path of this webapp, probably using a non-extracted WAR");
 		}
 		if (rootPath.endsWith("/")) {
 			rootPath = rootPath.substring(0, rootPath.length() - 1);
 		}
+
 		// check for default fallback servlet
 		String defaultServletName = context.getInitParameter(FileServlet.FALLBACK_SERVLET_PROPERTY);
 		if (defaultServletName != null && defaultServletName.length() != 0) {
@@ -71,14 +71,10 @@
 
 			// find the location of the file
 			String contextPath = request.getContextPath();
-			String relativePath = request.getRequestURI().substring(
-					contextPath.length());
-//			log("FileServlet :: contextPath " + contextPath);
-//			log("FileServlet :: relativePath " + relativePath);
+			String relativePath = request.getRequestURI().substring(contextPath.length());
 
 			String realPath = rootPath + prefix + relativePath;
 			realPath = realPath.replaceAll("\\\\", "/");
-//			log("FileServlet :: realPath " + realPath);
 
 			// check the file and open it
 			File file = new File(realPath);
@@ -148,8 +144,7 @@
 	/**
 	 * Static files treat GET and POST requests the same way.
 	 */
-	protected void doPost(HttpServletRequest request,
-			HttpServletResponse response) throws ServletException, IOException {
+	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 		doGet(request, response);
 	}
 

Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/RailsServletJavaDispatcher.java (496 => 497)


--- trunk/rails-integration/src/main/java/org/jruby/webapp/RailsServletJavaDispatcher.java	2007-04-25 01:42:41 UTC (rev 496)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/RailsServletJavaDispatcher.java	2007-04-25 05:27:22 UTC (rev 497)
@@ -36,15 +36,11 @@
 		setupEnvironment(runtime, request);
 
 		// setup the default session
-		String value = 
-                    getServletContext().getInitParameter("jruby.session_store");
-                if( value == null ) {
+		String value = getServletContext().getInitParameter("jruby.session_store");
+		if(value == null || value.intern() != "db") {
 		    setJavaSessionStoreAsDefault(runtime, request);
-		} else if ( value.intern() == "db".intern() ) {
-		    setJavaRequestSymbol(null, runtime, request);
-		} else {
-		    setJavaSessionStoreAsDefault(runtime, request);
 		}
+		setJavaRequestSymbol(runtime, request);
 
 		// create a cgi instance to store the data
 		IRubyObject cgi = createCgi(runtime);
@@ -58,30 +54,6 @@
 		JavaEmbedUtils.invokeMethod(runtime, dispatcher, "dispatch", args, void.class);
 	}
 
-        protected void setJavaRequestSymbol(RubyHash defaultSessionOptions,
-            Ruby runtime, HttpServletRequest request) throws ServletException {
-
-
-	    if( defaultSessionOptions == null ) {
-   	        RubyModule cgiRequestClass = 
-                    runtime.getClassFromPath("ActionController::CgiRequest");
-		if (cgiRequestClass == null) {
-			log("JavaServletStore disabled because ActionController::CgiRequest could not be found");
-		    return;
-		}
-		defaultSessionOptions = 
-                    (RubyHash)cgiRequestClass.getConstant("DEFAULT_SESSION_OPTIONS");
-	    }
-
-	    // store the java request
-	    RubySymbol javaRequestSymbol = RubySymbol.newSymbol(runtime, "java_request");
-	    IRubyObject javaRequest = 
-                JavaEmbedUtils.javaToRuby(runtime, new SessionHolder(request));
-		
-            defaultSessionOptions.aset(javaRequestSymbol, javaRequest);
-	}
-
-
 	protected IRubyObject createCgi(Ruby runtime) {
 		RubyClass cgiClass = runtime.getClass("CGI");
 		IRubyObject[] cgiArgs = { JavaEmbedUtils.javaToRuby(runtime, "query") };
@@ -137,14 +109,19 @@
 		}
 	}
 
-	protected void setJavaSessionStoreAsDefault(Ruby runtime, HttpServletRequest request) throws ServletException {
+	protected RubyHash getDefaultSessionOptions(Ruby runtime) {
 		RubyModule cgiRequestClass = runtime.getClassFromPath("ActionController::CgiRequest");
 		if (cgiRequestClass == null) {
 			log("JavaServletStore disabled because ActionController::CgiRequest could not be found");
-			return;
+			return null;
 		}
-		RubyHash defaultSessionOptions = (RubyHash)cgiRequestClass.getConstant("DEFAULT_SESSION_OPTIONS");
+		return (RubyHash)cgiRequestClass.getConstant("DEFAULT_SESSION_OPTIONS");
+	}
 
+	protected void setJavaSessionStoreAsDefault(Ruby runtime, HttpServletRequest request) throws ServletException {
+		RubyHash defaultSessionOptions = getDefaultSessionOptions(runtime);
+		if (defaultSessionOptions == null) return;
+
 		runtime.getLoadService().require("builtin/rails-integration/session/java_servlet_store");
 		RubyModule javaServletStore = runtime.getClassFromPath("CGI::Session::JavaServletStore");
 		if (javaServletStore == null) {
@@ -155,9 +132,17 @@
 		// store the default database manager
 		RubySymbol databaseManagerSymbol = RubySymbol.newSymbol(runtime, "database_manager");
 		defaultSessionOptions.aset(databaseManagerSymbol, javaServletStore);
+	}
 
+	protected void setJavaRequestSymbol(Ruby runtime, HttpServletRequest request) throws ServletException {
+		RubyHash defaultSessionOptions = getDefaultSessionOptions(runtime);
+		if (defaultSessionOptions == null) return;
+
 		// store the java request
-                setJavaRequestSymbol(defaultSessionOptions, runtime, request);
+		RubySymbol javaRequestSymbol = RubySymbol.newSymbol(runtime, "java_request");
+		IRubyObject javaRequest = JavaEmbedUtils.javaToRuby(runtime, new SessionHolder(request));
+
+		defaultSessionOptions.aset(javaRequestSymbol, javaRequest);
 	}
 
 	protected void setEnv(RubyHash env, String name, String value, String defaultValue) {

Added: trunk/rails-integration/src/test/java/org/jruby/webapp/AbstractTomcat4Test.java (0 => 497)


--- trunk/rails-integration/src/test/java/org/jruby/webapp/AbstractTomcat4Test.java	                        (rev 0)
+++ trunk/rails-integration/src/test/java/org/jruby/webapp/AbstractTomcat4Test.java	2007-04-25 05:27:22 UTC (rev 497)
@@ -0,0 +1,83 @@
+package org.jruby.webapp;
+import junit.framework.TestCase;
+import junit.framework.TestResult;
+import org.apache.catalina.startup.Embedded;
+import org.apache.catalina.Host;
+import org.apache.catalina.Logger;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Connector;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.logger.SystemOutLogger;
+import java.net.InetAddress;
+/**
+ * Test out RailsServlet under Tomcat 4.
+ *
+ * @author Robert Egglestone
+ */
+public class AbstractTomcat4Test extends TestCase {
+
+	private static int lastPort = 8926;
+	private int port;
+	protected Embedded embedded;
+	protected Host host;
+	protected Logger logger;
+
+	protected void setUp() throws Exception {
+		super.setUp();
+
+		logger = new SystemOutLogger();
+
+		// find an available port
+		port = lastPort++;
+
+		// Set the home directory
+		String tomcatHome = "target/tomcat";
+		System.setProperty("catalina.home", tomcatHome);
+
+		// Create an embedded server
+		embedded = new Embedded();
+		embedded.setDebug(0);
+		embedded.setLogger(logger);
+
+		// Create an engine
+		Engine engine = embedded.createEngine();
+		engine.setDefaultHost("localhost");
+		engine.setParentClassLoader(AbstractTomcat4Test.class.getClassLoader());
+
+		// Create a default virtual host
+		host = embedded.createHost("localhost", "webapps");
+		engine.addChild(host);
+
+		embedded.addEngine(engine);
+
+		// Assemble and install a default HTTP connector
+		InetAddress localhost = InetAddress.getByName("localhost");
+		Connector connector = embedded.createConnector(localhost, port, false);
+		embedded.addConnector(connector);
+
+		// Start the embedded server
+		embedded.start();
+	}
+
+	public void run(TestResult testResult) {
+		// we must always try to stop the embedded Tomcat instance, even on failure
+		try {
+			super.run(testResult);
+		} finally {
+			try {
+				if (embedded != null) embedded.stop();
+			} catch (LifecycleException e) {
+				if (logger != null) logger.log("Failed to stop Tomcat", e);
+			}
+		}
+	}
+
+	protected String getServerUrl() {
+		return "http://localhost:" + getPort();
+	}
+
+	protected int getPort() {
+		return port;
+	}
+
+}

Modified: trunk/rails-integration/src/test/java/org/jruby/webapp/FileServletTest.java (496 => 497)


--- trunk/rails-integration/src/test/java/org/jruby/webapp/FileServletTest.java	2007-04-25 01:42:41 UTC (rev 496)
+++ trunk/rails-integration/src/test/java/org/jruby/webapp/FileServletTest.java	2007-04-25 05:27:22 UTC (rev 497)
@@ -1,144 +1,87 @@
 package org.jruby.webapp;
 
-import org.easymock.EasyMock;
-import org.easymock.IMocksControl;
-import junit.framework.TestCase;
+import org.xml.sax.SAXException;
+import org.apache.catalina.Context;
+import org.apache.catalina.Wrapper;
 import java.io.IOException;
 import java.io.File;
-import java.util.Date;
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
-import javax.servlet.ServletOutputStream;
-import javax.servlet.RequestDispatcher;
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import com.meterware.httpunit.HttpNotFoundException;
+import com.meterware.httpunit.WebConversation;
+import com.meterware.httpunit.WebResponse;
+import com.meterware.httpunit.WebRequest;
+import com.meterware.httpunit.GetMethodWebRequest;
 
 /**
  * Test case for FileServlet.
  * @author Robert Egglestone
  */
-public class FileServletTest extends TestCase {
+public class FileServletTest extends AbstractTomcat4Test {
 
+	private Context context;
+
+	protected void setUp() throws Exception {
+		super.setUp();
+		context = embedded.createContext("/context", getRoot());
+		host.addChild(context);
+
+		addFileServlet();
+	}
+
 	private String getRoot() {
 		String classFilePath = getClass().getResource("FileServletTest.class").getPath();
 		File classFile = new File(classFilePath);
 		return classFile.getParent();
 	}
 
-	public void testNotFound() throws ServletException, IOException {
-		IMocksControl mockControl = EasyMock.createControl();
-		ServletConfig config = (ServletConfig)mockControl.createMock(ServletConfig.class);
-		ServletContext context = (ServletContext)mockControl.createMock(ServletContext.class);
-		HttpServletRequest request = (HttpServletRequest)mockControl.createMock(HttpServletRequest.class);
-		HttpServletResponse response = (HttpServletResponse)mockControl.createMock(HttpServletResponse.class);
-		// setup expectations
-		EasyMock.expect(config.getServletName()).andStubReturn("Rails");
-		EasyMock.expect(config.getServletContext()).andStubReturn(context);
-		EasyMock.expect(context.getInitParameter(FileServlet.FALLBACK_SERVLET_PROPERTY)).andReturn(null);
-		EasyMock.expect(request.getMethod()).andReturn("GET");
-		EasyMock.expect(request.getContextPath()).andReturn("/context");
-		EasyMock.expect(request.getRequestURI()).andReturn("/context/no_such_file.txt");
-		EasyMock.expect(context.getRealPath("/")).andReturn(getRoot());
-		response.sendError(HttpServletResponse.SC_NOT_FOUND);
-		// logging
-		context.log((String)EasyMock.anyObject());
-		// "Rails: File not found: .../no_such_file.txt"
-		EasyMock.expectLastCall().atLeastOnce();
-		// run the test
-		mockControl.replay();
-		FileServlet servlet = new FileServlet();
-		servlet.init(config);
-		servlet.service(request, response);
-		mockControl.verify();
+	public void testNotFound() throws ServletException, IOException, SAXException {
+		WebConversation sc = new WebConversation();
+		try {
+			sc.getResponse(getContextUrl() + "/no_such_file.txt");
+			fail("File not found was not thrown for a non-existant file");
+		} catch (HttpNotFoundException e) {
+			// expected
+		}
 	}
 
+	public void testFound() throws ServletException, IOException, SAXException {
+		WebConversation sc = new WebConversation();
+		WebResponse response = sc.getResponse(getContextUrl() + "/file.txt");
+		assertEquals(200, response.getResponseCode());
+		assertTrue(response.getContentLength() > 0);
+	}
+
 	/**
 	 * Files that are not found are passed on to the default servlet.
 	 */
-	public void testDefaultServlet() throws IOException, ServletException {
-		IMocksControl mockControl = EasyMock.createControl();
-		ServletConfig config = (ServletConfig)mockControl.createMock(ServletConfig.class);
-		ServletContext context = (ServletContext)mockControl.createMock(ServletContext.class);
-		HttpServletRequest request = (HttpServletRequest)mockControl.createMock(HttpServletRequest.class);
-		HttpServletResponse response = (HttpServletResponse)mockControl.createMock(HttpServletResponse.class);
-		RequestDispatcher dispatcher = (RequestDispatcher)mockControl.createMock(RequestDispatcher.class);
-		// setup expectations
-		EasyMock.expect(config.getServletName()).andStubReturn("Rails");
-		EasyMock.expect(config.getServletContext()).andStubReturn(context);
-		EasyMock.expect(context.getInitParameter(FileServlet.FALLBACK_SERVLET_PROPERTY)).andReturn("default-servlet");
-		EasyMock.expect(context.getNamedDispatcher("default-servlet")).andReturn(dispatcher);
-		EasyMock.expect(request.getMethod()).andReturn("GET");
-		EasyMock.expect(request.getContextPath()).andReturn("/context");
-		EasyMock.expect(request.getRequestURI()).andReturn("/context/no_such_file.txt");
-		EasyMock.expect(context.getRealPath("/")).andReturn(getRoot());
-		dispatcher.forward(request, response);
-		EasyMock.expectLastCall().once();
-		// logging
-		context.log((String)EasyMock.anyObject());
-		EasyMock.expectLastCall().atLeastOnce();
-		// run the test
-		mockControl.replay();
-		FileServlet servlet = new FileServlet();
-		servlet.init(config);
-		servlet.service(request, response);
-		mockControl.verify();
+	public void testDefaultServletNew() throws IOException, SAXException, ServletException {
+		context.addParameter(FileServlet.FALLBACK_SERVLET_PROPERTY, "mock");
+		addMockServlet();
+
+		WebConversation sc = new WebConversation();
+		WebResponse response = sc.getResponse(getContextUrl() + "/no_such_file.txt");
+		assertEquals(MockServlet.MESSAGE, response.getText());
 	}
 
-	public void testFound() throws ServletException, IOException {
-		IMocksControl mockControl = EasyMock.createControl();
-		ServletConfig config = (ServletConfig)mockControl.createMock(ServletConfig.class);
-		ServletContext context = (ServletContext)mockControl.createMock(ServletContext.class);
-		HttpServletRequest request = (HttpServletRequest)mockControl.createMock(HttpServletRequest.class);
-		HttpServletResponse response = (HttpServletResponse)mockControl.createMock(HttpServletResponse.class);
-		// setup expectations
-		EasyMock.expect(config.getServletName()).andStubReturn("Rails");
-		EasyMock.expect(config.getServletContext()).andStubReturn(context);
-		EasyMock.expect(context.getInitParameter(FileServlet.FALLBACK_SERVLET_PROPERTY)).andReturn(null);
-		EasyMock.expect(request.getMethod()).andReturn("GET");
-		EasyMock.expect(request.getContextPath()).andReturn("/context");
-		EasyMock.expect(request.getRequestURI()).andReturn("/context/file.txt");
-		EasyMock.expect(context.getRealPath("/")).andReturn(getRoot());
-		EasyMock.expect(new Long(request.getDateHeader("If-Modified-Since"))).andReturn(new Long(-1));
-		response.setContentType("text/plain");
-		EasyMock.expect(response.getOutputStream()).andReturn(new FakeServletOutputStream());
-		// logging
-		context.log((String)EasyMock.anyObject());
-		EasyMock.expectLastCall().anyTimes();
-		// run the test
-		mockControl.replay();
-		FileServlet servlet = new FileServlet();
-		servlet.init(config);
-		servlet.service(request, response);
-		mockControl.verify();
+	public void testNotModified() throws ServletException, IOException, SAXException {
+		WebConversation sc = new WebConversation();
+
+		WebRequest request = new GetMethodWebRequest(getContextUrl() + "/file.txt");
+		request.setHeaderField("If-Modified-Since", "Sat, 29 Oct 2094 19:43:31 GMT");
+
+		WebResponse response = sc.getResponse(request);
+		assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getResponseCode());
 	}
 
-	public void testNotModified() throws ServletException, IOException {
-		IMocksControl mockControl = EasyMock.createControl();
-		ServletConfig config = (ServletConfig)mockControl.createMock(ServletConfig.class);
-		ServletContext context = (ServletContext)mockControl.createMock(ServletContext.class);
-		HttpServletRequest request = (HttpServletRequest)mockControl.createMock(HttpServletRequest.class);
-		HttpServletResponse response = (HttpServletResponse)mockControl.createMock(HttpServletResponse.class);
-		// setup expectations
-		EasyMock.expect(config.getServletName()).andStubReturn("Rails");
-		EasyMock.expect(config.getServletContext()).andStubReturn(context);
-		EasyMock.expect(context.getInitParameter(FileServlet.FALLBACK_SERVLET_PROPERTY)).andReturn(null);
-		EasyMock.expect(request.getMethod()).andReturn("GET");
-		EasyMock.expect(request.getContextPath()).andReturn("/context");
-		EasyMock.expect(request.getRequestURI()).andReturn("/context/file.txt");
-		EasyMock.expect(context.getRealPath("/")).andReturn(getRoot());
-		Long now = new Long(new Date().getTime());
-		EasyMock.expect(new Long(request.getDateHeader("If-Modified-Since"))).andReturn(now);
-		response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
-		// logging
-		context.log((String)EasyMock.anyObject());
-		EasyMock.expectLastCall().anyTimes();
-		// run the test
-		mockControl.replay();
-		FileServlet servlet = new FileServlet();
-		servlet.init(config);
-		servlet.service(request, response);
-		mockControl.verify();
+	public void testWelcomeFile() throws IOException, SAXException {
+		context.addParameter("org.apache.catalina.WELCOME_FILES", "file.txt");
+
+		WebConversation sc = new WebConversation();
+
+		WebResponse response = sc.getResponse(getContextUrl() + "/file.txt");
+		assertEquals(200, response.getResponseCode());
+		assertTrue(response.getContentLength() > 0);
 	}
 
 	public void testMimeTypes() throws ServletException, IOException {
@@ -153,40 +96,27 @@
 		// assertEquals("text/js", servlet.getContentTypeFor("script.js"));
 	}
 
-	public void testWelcomeFile() throws IOException, ServletException {
-		IMocksControl mockControl = EasyMock.createControl();
-		ServletConfig config = (ServletConfig)mockControl.createMock(ServletConfig.class);
-		ServletContext context = (ServletContext)mockControl.createMock(ServletContext.class);
-		HttpServletRequest request = (HttpServletRequest)mockControl.createMock(HttpServletRequest.class);
-		HttpServletResponse response = (HttpServletResponse)mockControl.createMock(HttpServletResponse.class);
-		// setup expectations
-		EasyMock.expect(config.getServletName()).andStubReturn("Rails");
-		EasyMock.expect(config.getServletContext()).andStubReturn(context);
-		EasyMock.expect(context.getAttribute("org.apache.catalina.WELCOME_FILES")).andReturn(new String[] { "file.txt" });
-		EasyMock.expect(context.getInitParameter(FileServlet.FALLBACK_SERVLET_PROPERTY)).andReturn(null);
-		EasyMock.expect(request.getMethod()).andReturn("GET");
-		EasyMock.expect(request.getContextPath()).andReturn("/context");
-		EasyMock.expect(request.getRequestURI()).andReturn("/context/");
-		EasyMock.expect(context.getRealPath("/")).andReturn(getRoot());
-		EasyMock.expect(new Long(request.getDateHeader("If-Modified-Since"))).andReturn(new Long(-1));
-		response.setContentType("text/plain");
-		EasyMock.expect(response.getOutputStream()).andReturn(new FakeServletOutputStream());
-		// logging
-		context.log((String)EasyMock.anyObject());
-		EasyMock.expectLastCall().anyTimes();
-		// run the test
-		mockControl.replay();
-		FileServlet servlet = new FileServlet();
-		servlet.init(config);
-		servlet.service(request, response);
-		mockControl.verify();
+	private Wrapper addFileServlet() {
+		Wrapper servlet = context.createWrapper();
+		servlet.setLogger(logger);
+		servlet.setName("files");
+		servlet.setServletClass(FileServlet.class.getName());
+		context.addChild(servlet);
+		context.addServletMapping("/", "files");
+		return servlet;
 	}
 
+	private Wrapper addMockServlet() {
+		Wrapper servlet = context.createWrapper();
+		servlet.setLogger(logger);
+		servlet.setName("mock");
+		servlet.setServletClass(MockServlet.class.getName());
+		context.addChild(servlet);
+		return servlet;
+	}
 
-	private static class FakeServletOutputStream extends ServletOutputStream {
-		public void write(int i) {
-			// do nothing
-		}
+	private String getContextUrl() {
+		return getServerUrl() + "/context";
 	}
 
 }

Modified: trunk/rails-integration/src/test/java/org/jruby/webapp/HttpOutputTest.java (496 => 497)


--- trunk/rails-integration/src/test/java/org/jruby/webapp/HttpOutputTest.java	2007-04-25 01:42:41 UTC (rev 496)
+++ trunk/rails-integration/src/test/java/org/jruby/webapp/HttpOutputTest.java	2007-04-25 05:27:22 UTC (rev 497)
@@ -1,64 +1,69 @@
 package org.jruby.webapp;
 import junit.framework.TestCase;
 import javax.servlet.http.HttpServletResponse;
-import org.easymock.EasyMock;
+import org.easymock.MockControl;
 import java.io.IOException;
 /**
  * Tests for HttpOutput
  */
 public class HttpOutputTest extends TestCase {
 
+	private MockControl control;
+	private HttpServletResponse response;
 
+	protected void setUp() throws Exception {
+		super.setUp();
+		control = MockControl.createControl(HttpServletResponse.class);
+		response = (HttpServletResponse)control.getMock();
+	}
+
 	public void testSimple() throws IOException {
-		HttpServletResponse response = (HttpServletResponse)EasyMock.createMock(HttpServletResponse.class);
 		String input = "Status: 200 Ok\r\nContent-Type: text/plain\r\n\r\nThis is the body";
 		BufferServletOutputStream out = new BufferServletOutputStream();
 		// setup expectations
 		response.setStatus(200);
 		response.setContentType("text/plain");
-		EasyMock.expect(response.getOutputStream()).andReturn(out);
+		control.expectAndReturn(response.getOutputStream(), out);
 		response.flushBuffer();
-		EasyMock.replay(response);
+		control.replay();
 		// execute the action
 		HttpOutput httpOutput = new HttpOutput(response);
 		httpOutput.write(input.getBytes());
 		httpOutput.close();
 		// verify the results
-		EasyMock.verify(response);
+		control.verify();
 		assertEquals("This is the body", out.getContent());
 	}
 
 	public void testError() throws IOException {
-		HttpServletResponse response = (HttpServletResponse)EasyMock.createMock(HttpServletResponse.class);
 		String input = "Status: 404 File Not Found\r\n\r\nFile not found";
 		BufferServletOutputStream out = new BufferServletOutputStream();
 		// setup expectations
 		response.setStatus(404);
-		EasyMock.expect(response.getOutputStream()).andReturn(out);
-		EasyMock.replay(response);
+		control.expectAndReturn(response.getOutputStream(), out);
+		control.replay();
 		// execute the action
 		HttpOutput httpOutput = new HttpOutput(response);
 		httpOutput.write(input.getBytes());
 		// verify the results
-		EasyMock.verify(response);
+		control.verify();
 		assertEquals("File not found", out.getContent());
 	}
 
 	public void testNoBody() throws IOException {
-		HttpServletResponse response = (HttpServletResponse)EasyMock.createMock(HttpServletResponse.class);
 		String input = "Status: 200 Ok\r\n\r\n";
 		BufferServletOutputStream out = new BufferServletOutputStream();
 		// setup expectations
 		response.setStatus(200);
-		EasyMock.expect(response.getOutputStream()).andReturn(out);
+		control.expectAndReturn(response.getOutputStream(), out);
 		response.flushBuffer();
-		EasyMock.replay(response);
+		control.replay();
 		// execute the action
 		HttpOutput httpOutput = new HttpOutput(response);
 		httpOutput.write(input.getBytes());
 		httpOutput.close();
 		// verify the results
-		EasyMock.verify(response);
+		control.verify();
 		assertEquals("", out.getContent());
 	}
 

Added: trunk/rails-integration/src/test/java/org/jruby/webapp/MockServlet.java (0 => 497)


--- trunk/rails-integration/src/test/java/org/jruby/webapp/MockServlet.java	                        (rev 0)
+++ trunk/rails-integration/src/test/java/org/jruby/webapp/MockServlet.java	2007-04-25 05:27:22 UTC (rev 497)
@@ -0,0 +1,22 @@
+package org.jruby.webapp;
+import java.io.IOException;
+import java.io.PrintWriter;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.ServletException;
+/**
+ * @author Robert Egglestone
+ */
+public class MockServlet extends HttpServlet {
+
+	public static final String MESSAGE = "MockServlet says hi";
+
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		response.setStatus(200);
+		PrintWriter out = response.getWriter();
+		out.print(MESSAGE);
+		out.close();
+	}
+
+}

Modified: trunk/rails-integration/src/test/java/org/jruby/webapp/RailsServletTest.java (496 => 497)


--- trunk/rails-integration/src/test/java/org/jruby/webapp/RailsServletTest.java	2007-04-25 01:42:41 UTC (rev 496)
+++ trunk/rails-integration/src/test/java/org/jruby/webapp/RailsServletTest.java	2007-04-25 05:27:22 UTC (rev 497)
@@ -18,18 +18,16 @@
 		super.setUp();
 	}
 
-	public void testHelloWorld116() throws Exception {
-		checkHelloWorld("1.1.6", "web-test.xml");
+	public File getWebXml() {
+		return new File("samples/helloworld-" + getRailsVersion() + "/WEB-INF/web-test.xml");
 	}
 
-	public void testHelloWorld122() throws Exception {
-		checkHelloWorld("1.2.2", "web-test.xml");
+	protected String getRailsVersion() {
+		return "1.2.3";
 	}
 
-	private void checkHelloWorld(String version, String configuration) throws Exception {
-		File webxml = new File("samples/helloworld-" + version + "/WEB-INF/" + configuration);
-
-		ServletRunner sr = new ServletRunner(webxml, "/rails");
+	public void testHelloWorld() throws Exception {
+		ServletRunner sr = new ServletRunner(getWebXml(), "/rails");
 		sc = sr.newClient();
 
 		WebRequest request = new GetMethodWebRequest("http://localhost/rails/");
@@ -41,12 +39,7 @@
 	}
 
 	public void testSessionPost() throws Exception {
-		String version = "1.2.2";
-		String configuration = "web-test.xml";
-
-		File webxml = new File("samples/helloworld-" + version + "/WEB-INF/" + configuration);
-
-		ServletRunner sr = new ServletRunner(webxml, "/rails");
+		ServletRunner sr = new ServletRunner(getWebXml(), "/rails");
 		sc = sr.newClient();
 
 		WebRequest sessionPostHello = new PostMethodWebRequest("http://localhost/rails/session/set");

Modified: trunk/rails-integration/src/test/java/org/jruby/webapp/RailsServletTomcat4Test.java (496 => 497)


--- trunk/rails-integration/src/test/java/org/jruby/webapp/RailsServletTomcat4Test.java	2007-04-25 01:42:41 UTC (rev 496)
+++ trunk/rails-integration/src/test/java/org/jruby/webapp/RailsServletTomcat4Test.java	2007-04-25 05:27:22 UTC (rev 497)
@@ -1,79 +1,28 @@
 package org.jruby.webapp;
-import junit.framework.TestCase;
-import org.apache.catalina.Host;
-import org.apache.catalina.Engine;
-import org.apache.catalina.logger.SystemOutLogger;
-import org.apache.catalina.startup.Embedded;
 import java.io.File;
 import java.io.IOException;
-import java.net.InetAddress;
-import org.apache.catalina.Connector;
 import org.apache.catalina.Context;
 import org.apache.catalina.Wrapper;
 import org.apache.catalina.LifecycleException;
-import org.apache.catalina.Logger;
 import org.xml.sax.SAXException;
 import com.meterware.httpunit.WebRequest;
 import com.meterware.httpunit.GetMethodWebRequest;
 import com.meterware.httpunit.WebResponse;
 import com.meterware.httpunit.WebConversation;
-import com.meterware.httpunit.WebForm;
-import com.meterware.httpunit.WebTable;
 
 /**
  * Test out RailsServlet under Tomcat 4.
  *
  * @author Robert Egglestone
  */
-public class RailsServletTomcat4Test extends TestCase {
+public class RailsServletTomcat4Test extends AbstractTomcat4Test {
 
-	private static final int PORT = 8926;
-	private Embedded embedded;
-	private Host host;
-	private Logger logger;
-
-	protected void setUp() throws Exception {
-		super.setUp();
-
-		logger = new SystemOutLogger();
-
-		// Set the home directory
-		String tomcatHome = "target/tomcat";
-		System.setProperty("catalina.home", tomcatHome);
-
-		// Create an embedded server
-		embedded = new Embedded();
-		embedded.setDebug(0);
-		embedded.setLogger(logger);
-
-		// Create an engine
-		Engine engine = embedded.createEngine();
-		engine.setDefaultHost("localhost");
-		engine.setParentClassLoader(RailsServletTomcat4Test.class.getClassLoader());
-
-		// Create a default virtual host
-		host = embedded.createHost("localhost", "webapps");
-		engine.addChild(host);
-
-		embedded.addEngine(engine);
-
-		// Assemble and install a default HTTP connector
-		InetAddress localhost = InetAddress.getByName("localhost");
-		Connector connector = embedded.createConnector(localhost, PORT, false);
-		embedded.addConnector(connector);
-
-		// Start the embedded server
-		embedded.start();
+	protected String getRailsVersion() {
+		return "1.2.3";
 	}
 
-	protected void tearDown() throws Exception {
-		embedded.stop();
-		super.tearDown();
-	}
-
 	public void testHelloWorld() throws IOException, SAXException, InterruptedException, LifecycleException {
-		String version = "1.2.2";
-		File documentRoot = new File("samples/helloworld-" + version);
+		File documentRoot = new File("samples/helloworld-" + getRailsVersion());
 
 		Context context = embedded.createContext("/hw", documentRoot.getAbsolutePath());
 		context.addParameter("jruby.pool.maxActive", "1");
@@ -91,11 +40,11 @@
 
 		WebConversation sc = new WebConversation();
 
-		WebRequest request = new GetMethodWebRequest("http://localhost:" + PORT + "/hw/");
+		WebRequest request = new GetMethodWebRequest(getServerUrl() + "/hw/");
 		WebResponse response = sc.getResponse(request);
 		assertEquals("response code", 200, response.getResponseCode());
 		assertEquals("content type", "text/html", response.getContentType());
-		assertTrue("welcome message", response.getText().contains("Welcome to JRuby on Rails!"));
+		assertTrue("welcome message", response.getText().indexOf("Welcome to JRuby on Rails!") != -1);
 	}
 
 	/*
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to