cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gkossakow...@apache.org
Subject svn commit: r570388 - in /cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl: pom.xml src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTest.java
Date Tue, 28 Aug 2007 11:51:14 GMT
Author: gkossakowski
Date: Tue Aug 28 04:51:13 2007
New Revision: 570388

URL: http://svn.apache.org/viewvc?rev=570388&view=rev
Log:
COCOON-2038: Added more complicated tests for advanced functionality (context handling and
true OO behaviour).
  * reimplemented testing of ServletServiceContext; mock objects didn't work well because
Servlets need to interact in more complicated tests.
  * removed dependency on easymock

Modified:
    cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/pom.xml
    cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTest.java

Modified: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/pom.xml?rev=570388&r1=570387&r2=570388&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/pom.xml (original)
+++ cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/pom.xml Tue Aug 28
04:51:13 2007
@@ -114,11 +114,6 @@
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>easymock</groupId>
-      <artifactId>easymock</artifactId>
-      <scope>test</scope>
-    </dependency>
   </dependencies>
   
   <build>

Modified: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTest.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTest.java?rev=570388&r1=570387&r2=570388&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTest.java
(original)
+++ cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTest.java
Tue Aug 28 04:51:13 2007
@@ -16,71 +16,277 @@
 */
 package org.apache.cocoon.servletservice;
 
+import java.io.IOException;
 import java.net.URI;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
 import javax.servlet.RequestDispatcher;
-import javax.servlet.Servlet;
 import javax.servlet.ServletConfig;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import junit.framework.TestCase;
 
 import org.apache.cocoon.servletservice.util.BlockCallHttpServletRequest;
 import org.apache.cocoon.servletservice.util.BlockCallHttpServletResponse;
-import org.easymock.MockControl;
 
 public class ServletServiceContextTest extends TestCase {
     
     private ServletServiceContext mainContext;
-    private Servlet servletA;
-    private Servlet servletB;
-    private MockControl servletAControl;
-    private MockControl servletBControl;
-    
-    private MockControl servletAConfigControl;
-    private ServletConfig servletAConfig;
+    private HttpServlet servletA;
+    private HttpServlet servletB;
     
     private ServletServiceContext servletAContext;
+    private ServletServiceContext servletBContext;
+    
+    BlockCallHttpServletRequest request;
+    BlockCallHttpServletResponse response;
     
     
+    /* (non-Javadoc)
+     * @see junit.framework.TestCase#setUp()
+     */
     protected void setUp() throws Exception {
         super.setUp();
         this.mainContext = new ServletServiceContext();
         
-        servletAControl = MockControl.createControl(Servlet.class);
-        servletA = (Servlet)servletAControl.getMock();
+        request = new BlockCallHttpServletRequest(new URI("dummy"), null);
+        request.setMethod("GET");
+        response = new BlockCallHttpServletResponse();
         
+        //creating and setting up ServletContexts
         servletAContext = new ServletServiceContext();
+        servletBContext = new ServletServiceContext();        
+    }
+    
+    /**
+     * Tests basic connection to the servlet.
+     * 
+     * @throws Exception
+     */
+    public void testBasicConnection() throws Exception {
+        servletA = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletAContext);
}
+
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+                response.setStatus(HttpServletResponse.SC_OK);
+            }
+        };
         servletAContext.setServlet(servletA);
+       
+        setMainConnection(servletA, "servletA");
         
-        servletAConfigControl = MockControl.createControl(ServletConfig.class);
-        servletAConfig = (ServletConfig)servletAConfigControl.getMock();
-        servletAConfig.getServletContext();
-        servletAConfigControl.setReturnValue(servletAContext);
+        RequestDispatcher dispatcher = mainContext.getNamedDispatcher("servletA");
+        dispatcher.forward(request, response);
         
-        servletA.getServletConfig();
-        servletAControl.setReturnValue(servletAConfig);
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+    }
+    
+    /**
+     * <p>Tests if explicit super call works and if ServletContexts are properly set
up when super call is performed.</p>
+     * 
+     * <p>Servlets are connected that way:</p>
+     * <pre>
+     *    ServletB
+     *       ^
+     *       |
+     *    ServletA
+     * </pre>
+     * @throws Exception
+     */
+    public void testExplicitSuperCall() throws Exception {
+        servletA = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletAContext);
}
+
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+                ServletContext context = CallStackHelper.getCurrentServletContext();
+                RequestDispatcher dispatcher = context.getNamedDispatcher("super");
+                dispatcher.forward(request, response);
+            }
+
+        };
         
-        Map connections = new HashMap();
-        connections.put("servletA", servletA);
-        mainContext.setConnections(connections);
+        servletB = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletBContext);
}
+            
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+                assertEquals(servletAContext, CallStackHelper.getBaseServletContext());
+                assertEquals(servletBContext, CallStackHelper.getCurrentServletContext());
+                response.setStatus(HttpServletResponse.SC_OK);
+            }
+        };
+        
+        servletAContext.setServlet(servletA);
+        servletBContext.setServlet(servletB);
+        
+        //connecting servlets
+        setMainConnection(servletA, "servletA");
+        connectServlets(servletA, servletB, "super");
+
+        RequestDispatcher dispatcher = mainContext.getNamedDispatcher("servletA");
+        dispatcher.forward(request, response);
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
     }
     
-    public void testBasicConnection() throws Exception {
-        ServletRequest request = new BlockCallHttpServletRequest(new URI("dummy"), null);
-        ServletResponse response = new BlockCallHttpServletResponse();
-       
-        servletA.service(request, response);
-        servletAControl.replay();
-        servletAConfigControl.replay();
+    /**
+     * <p>Tests if ServletContext is properly set up in a servlet (C) that is called
from super servlet (B) for another one(A).</p>
+     * 
+     * <p>Servlets are connected that way:</p>
+     * <pre>
+     *    ServletB --> ServletC
+     *       ^
+     *       |
+     *    ServletA
+     * </pre>
+     * 
+     * @throws Exception
+     */
+    public void testContextInServletCalledFromExplicitSuperCall() throws Exception {
+        final ServletServiceContext servletCContext = new ServletServiceContext();
+        
+        servletA = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletAContext);
}
+
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+                ServletContext context = CallStackHelper.getCurrentServletContext();
+                RequestDispatcher dispatcher = context.getNamedDispatcher("super");
+                dispatcher.forward(request, response);
+            }
+        };
+        
+        servletB = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletBContext);
}
+            
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+                ServletContext context = CallStackHelper.getBaseServletContext();
+                RequestDispatcher dispatcher = context.getNamedDispatcher("servletC");
+                dispatcher.forward(request, response);
+            }
+        };
+        
+        HttpServlet servletC = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletCContext);
}
+            
+            protected void service(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
+                super.service(req, response);
+                assertEquals(request, req);
+                assertEquals(response, res);
+                assertEquals(servletCContext, CallStackHelper.getBaseServletContext());
+                assertEquals(servletCContext, CallStackHelper.getCurrentServletContext());
+                res.setStatus(200);
+            }
+        };
+        servletAContext.setServlet(servletA);
+        servletBContext.setServlet(servletB);
+        servletCContext.setServlet(servletC);
         
+        //connecting servlets
+        setMainConnection(servletA, "servletA");
+        connectServlets(servletA, servletB, "super");
+        connectServlets(servletB, servletC, "servletC");
+
         RequestDispatcher dispatcher = mainContext.getNamedDispatcher("servletA");
         dispatcher.forward(request, response);
+        assertEquals(200, response.getStatus());
+    }
+    
+    /**
+     * <p>This test is very similar to the {@link #testContextInServletCalledFromExplicitSuperCall()}
but tries to use true OO approach so there is
+     *    no explicit super call. See COCOON-2038.</p>
+     * 
+     * <p>Servlets are connected that way:</p>
+     * <pre>
+     *    ServletB --> ServletC
+     *       ^
+     *       |
+     *    ServletA
+     * </pre>
+     * 
+     * @throws Exception
+     */
+    public void testtrueObjectOrientedBehaviour() throws Exception {
+        final ServletServiceContext servletCContext = new ServletServiceContext();
+        
+        servletA = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletAContext);
}
+
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+                super.service(request, response);
+                response.setStatus(500);
+            }
+
+        };
         
-        servletAControl.verify();
-        servletAConfigControl.verify();
+        servletB = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletBContext);
}
+            
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+                ServletContext context = CallStackHelper.getBaseServletContext();
+                RequestDispatcher dispatcher = context.getNamedDispatcher("servletC");
+                dispatcher.forward(request, response);
+            }
+        };
+        
+        HttpServlet servletC = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletCContext);
}
+            
+            protected void service(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
+                super.service(req, response);
+                assertEquals(request, req);
+                assertEquals(response, res);
+                assertEquals(servletCContext, CallStackHelper.getBaseServletContext());
+                assertEquals(servletCContext, CallStackHelper.getCurrentServletContext());
+                res.setStatus(200);
+            }
+        };
+        servletAContext.setServlet(servletA);
+        servletBContext.setServlet(servletB);
+        servletCContext.setServlet(servletC);
+        
+        //connecting servlets
+        setMainConnection(servletA, "servletA");
+        connectServlets(servletA, servletB, "super");
+        connectServlets(servletB, servletC, "servletC");
+
+        RequestDispatcher dispatcher = mainContext.getNamedDispatcher("servletA");
+        dispatcher.forward(request, response);
+        assertEquals(200, response.getStatus());
     }
+    
+    //-----------------------------------------------------------------------------------------
+    
+    private void connectServlets(HttpServlet connectFrom, HttpServlet connectTo, String connectionName)
{
+        ServletServiceContext context = (ServletServiceContext)connectFrom.getServletConfig().getServletContext();
+        Map connections = new HashMap();
+        connections.put(connectionName, connectTo);
+        context.setConnections(connections);
+    }
+    
+    private void setMainConnection(HttpServlet connectTo, String connectionName) {
+        Map connections = new HashMap();
+        connections.put(connectionName, connectTo);
+        mainContext.setConnections(connections);
+    }
+    
+    private class ServletConfigWithContext implements ServletConfig {
+        private ServletContext context;
+        
+        public ServletConfigWithContext(ServletContext context) {
+            this.context = context;
+        }
+        
+        public String getInitParameter(String arg0) { return null; }
+        public Enumeration getInitParameterNames() { return null; }
+        public String getServletName() { return null; }
+        
+        public ServletContext getServletContext() {
+            return context;
+        }
+    }
+
 }



Mime
View raw message