cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r932683 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/ framework/ca...
Date Sat, 10 Apr 2010 09:19:22 GMT
Author: aadamchik
Date: Sat Apr 10 09:19:20 2010
New Revision: 932683

URL: http://svn.apache.org/viewvc?rev=932683&view=rev
Log:
CAY-1415 Replace WebApplicationContextFilter with a DI-enabled filter

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchModule.java
      - copied, changed from r932516, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchRequestHandler.java
      - copied, changed from r932516, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java
Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ServletUtil.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ServletUtilTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/WebApplicationContextFilterTest.java
Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilterTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneWebModuleTest.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=932683&r1=932682&r2=932683&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Sat Apr 10 09:19:20 2010
@@ -33,6 +33,7 @@ CAY-1350 Implement memorized sorting of 
 CAY-1374 Add parent dataMap property to Query interface
 CAY-1393 Update velocity to 1.6.3
 CAY-1394 DI-based replacement for JNDI hack
+CAY-1415 Replace WebApplicationContextFilter with a DI-enabled filter
 
 Bug Fixes Since 3.0:
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java?rev=932683&r1=932682&r2=932683&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/WebApplicationContextFilter.java
Sat Apr 10 09:19:20 2010
@@ -19,81 +19,29 @@
 
 package org.apache.cayenne.conf;
 
-import java.io.IOException;
-
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
 
-import org.apache.cayenne.BaseContext;
-import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.configuration.web.CayenneFilter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
- * A Servlet Filter that binds session DataContext to the current request thread. During
- * the request application code without any knowledge of the servlet environment can
- * access DataContext via {@link DataContext#getThreadDataContext()} method. <p/> To
- * enable the filter add XML similar to this in the <code>web.xml</code> descriptor
of a
- * web application:
- * 
- * <pre>
- *  &lt;filter&gt;
- *   &lt;filter-name&gt;CayenneFilter&lt;/filter-name&gt;
- *   &lt;filter-class&gt;org.apache.cayenne.conf.WebApplicationContextFilter&lt;/filter-class&gt;
- *   &lt;/filter&gt;
- *   &lt;filter-mapping&gt;
- *   &lt;filter-name&gt;CayenneFilter&lt;/filter-name&gt;
- *   &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
- *   &lt;/filter-mapping&gt;
- * </pre>
+ * This is a legacy version of the filter
  * 
  * @since 1.2
+ * @deprecated since 3.0 see {@link CayenneFilter}
  */
-public class WebApplicationContextFilter implements Filter {
+public class WebApplicationContextFilter extends CayenneFilter {
 
-    public void init(FilterConfig filterConfig) throws ServletException {
-        ServletUtil.initializeSharedConfiguration(filterConfig.getServletContext());
-    }
+    private Log logger;
 
-    /**
-     * Cleanup callback method that does nothing, as the filter doesn't store any state.
-     */
-    // TODO: andrus 9/17/2006 - should we shut down Cayenne stack? I.e. should it be
-    // complimentary to "init"?
-    public void destroy() {
-        // noop
-    }
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+        logger = LogFactory.getLog(WebApplicationContextFilter.class);
+        logger
+                .warn("**** WebApplicationContextFilter is deprecated. Use CayenneFilter
instead");
 
-    /**
-     * The main worker method that binds a DataContext to the current thread on entry and
-     * unbinds it on exit (regardless of whether any exceptions occured in the request).
-     */
-    public void doFilter(
-            ServletRequest request,
-            ServletResponse response,
-            FilterChain chain) throws IOException, ServletException {
-
-        boolean reset = false;
-
-        if (request instanceof HttpServletRequest) {
-            reset = true;
-
-            HttpSession session = ((HttpServletRequest) request).getSession(true);
-            DataContext context = ServletUtil.getSessionContext(session);
-            BaseContext.bindThreadObjectContext(context);
-        }
-
-        try {
-            chain.doFilter(request, response);
-        }
-        finally {
-            if (reset) {
-                BaseContext.bindThreadObjectContext(null);
-            }
-        }
+        super.init(filterConfig);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java?rev=932683&r1=932682&r2=932683&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java
Sat Apr 10 09:19:20 2010
@@ -26,10 +26,12 @@ import java.util.StringTokenizer;
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
+import org.apache.cayenne.configuration.CayenneRuntime;
 import org.apache.cayenne.configuration.server.CayenneServerModule;
 import org.apache.cayenne.configuration.server.CayenneServerRuntime;
 import org.apache.cayenne.di.Module;
@@ -50,6 +52,10 @@ import org.apache.cayenne.util.Util;
  * after the two standard ones that allow users to override any Cayenne runtime aspects,
  * e.g. {@link RequestHandler}. Each custom module must have a no-arg constructor.
  * </ul>
+ * <p>
+ * CayenneFilter is a great utility to quickly start a Cayenne application. More advanced
+ * apps most likely will not use it, relying on their own configuration mechanism (such as
+ * Guice, Spring, etc.)
  * 
  * @since 3.1
  */
@@ -58,8 +64,12 @@ public class CayenneFilter implements Fi
     static final String RUNTIME_NAME_PARAMETER = "runtime-name";
     static final String EXTRA_MODULES_PARAMETER = "extra-modules";
 
+    protected ServletContext servletContext;
+
     public void init(FilterConfig config) throws ServletException {
 
+        this.servletContext = config.getServletContext();
+
         String runtimeName = config.getInitParameter(RUNTIME_NAME_PARAMETER);
         if (runtimeName == null) {
             runtimeName = config.getFilterName();
@@ -113,5 +123,16 @@ public class CayenneFilter implements Fi
             ServletRequest request,
             ServletResponse response,
             FilterChain chain) throws IOException, ServletException {
+
+        CayenneRuntime runtime = WebUtil.getCayenneRuntime(servletContext);
+        RequestHandler handler = runtime.getInjector().getInstance(RequestHandler.class);
+
+        handler.requestStart(request, response);
+        try {
+            chain.doFilter(request, response);
+        }
+        finally {
+            handler.requestEnd(request, response);
+        }
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java?rev=932683&r1=932682&r2=932683&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
Sat Apr 10 09:19:20 2010
@@ -27,6 +27,6 @@ import org.apache.cayenne.di.Module;
 public class CayenneWebModule implements Module {
 
     public void configure(Binder binder) {
-        binder.bind(RequestHandler.class).to(DefaultRequestHandler.class);
+        binder.bind(RequestHandler.class).to(SessionContextRequestHandler.class);
     }
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java?rev=932683&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/SessionContextRequestHandler.java
Sat Apr 10 09:19:20 2010
@@ -0,0 +1,76 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.configuration.web;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.apache.cayenne.BaseContext;
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.di.Injector;
+
+/**
+ * Default implementation of the {@link RequestHandler} that stores per-user
+ * {@link ObjectContext} in a web session and binds it to request thread. Note that using
+ * this handler would force {@link HttpSession} creation, that may not be desirable in
+ * many cases. Also session-bound context may result in a race condition with two user
+ * requests updating the same periststent objects in parallel.
+ * <p>
+ * User applications in most cases should provide a custom RequestHandler that implements
+ * a smarter app-specific strategy for providing ObjectContext.
+ * 
+ * @since 3.1
+ */
+public class SessionContextRequestHandler implements RequestHandler {
+
+    static final String SESSION_CONTEXT_KEY = SessionContextRequestHandler.class
+            .getName()
+            + ".SESSION_CONTEXT";
+
+    @Inject
+    private Injector injector;
+
+    public void requestStart(ServletRequest request, ServletResponse response) {
+        if (request instanceof HttpServletRequest) {
+
+            // this forces session creation if it does not exist yet
+            HttpSession session = ((HttpServletRequest) request).getSession();
+
+            ObjectContext context;
+            synchronized (session) {
+                context = (ObjectContext) session.getAttribute(SESSION_CONTEXT_KEY);
+
+                if (context == null) {
+                    context = injector.getInstance(ObjectContext.class);
+                    session.setAttribute(SESSION_CONTEXT_KEY, context);
+                }
+            }
+
+            BaseContext.bindThreadObjectContext(context);
+        }
+    }
+
+    public void requestEnd(ServletRequest request, ServletResponse response) {
+        BaseContext.bindThreadObjectContext(null);
+    }
+
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilterTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilterTest.java?rev=932683&r1=932682&r2=932683&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilterTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilterTest.java
Sat Apr 10 09:19:20 2010
@@ -18,14 +18,17 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.web;
 
+import junit.framework.TestCase;
+
 import org.apache.cayenne.configuration.CayenneRuntime;
 import org.apache.cayenne.configuration.server.CayenneServerModule;
 
+import com.mockrunner.mock.web.MockFilterChain;
 import com.mockrunner.mock.web.MockFilterConfig;
+import com.mockrunner.mock.web.MockHttpServletRequest;
+import com.mockrunner.mock.web.MockHttpServletResponse;
 import com.mockrunner.mock.web.MockServletContext;
 
-import junit.framework.TestCase;
-
 public class CayenneFilterTest extends TestCase {
 
     public void testInitWithFilterName() throws Exception {
@@ -87,7 +90,7 @@ public class CayenneFilterTest extends T
         assertTrue(runtime.getModules()[1] instanceof CayenneWebModule);
 
         RequestHandler handler = runtime.getInjector().getInstance(RequestHandler.class);
-        assertTrue(handler instanceof DefaultRequestHandler);
+        assertTrue(handler instanceof SessionContextRequestHandler);
     }
 
     public void testInitWithExtraModules() throws Exception {
@@ -118,4 +121,40 @@ public class CayenneFilterTest extends T
         RequestHandler handler = runtime.getInjector().getInstance(RequestHandler.class);
         assertTrue(handler instanceof MockRequestHandler);
     }
+
+    public void testDoFilter() throws Exception {
+        MockFilterConfig config = new MockFilterConfig();
+        config.setFilterName("abc");
+        config.setInitParameter(
+                CayenneFilter.EXTRA_MODULES_PARAMETER,
+                CayenneFilter_DispatchModule.class.getName());
+
+        MockServletContext context = new MockServletContext();
+        config.setupServletContext(context);
+
+        CayenneFilter filter = new CayenneFilter();
+        filter.init(config);
+
+        CayenneRuntime runtime = WebUtil.getCayenneRuntime(context);
+        CayenneFilter_DispatchRequestHandler handler = (CayenneFilter_DispatchRequestHandler)
runtime
+                .getInjector()
+                .getInstance(RequestHandler.class);
+
+        assertEquals(0, handler.getStarted());
+        assertEquals(0, handler.getEnded());
+
+        filter.doFilter(
+                new MockHttpServletRequest(),
+                new MockHttpServletResponse(),
+                new MockFilterChain());
+        assertEquals(1, handler.getStarted());
+        assertEquals(1, handler.getEnded());
+
+        filter.doFilter(
+                new MockHttpServletRequest(),
+                new MockHttpServletResponse(),
+                new MockFilterChain());
+        assertEquals(2, handler.getStarted());
+        assertEquals(2, handler.getEnded());
+    }
 }

Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchModule.java
(from r932516, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchModule.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchModule.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java&r1=932516&r2=932683&rev=932683&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchModule.java
Sat Apr 10 09:19:20 2010
@@ -20,13 +20,14 @@ package org.apache.cayenne.configuration
 
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.Scopes;
 
-/**
- * @since 3.1
- */
-public class CayenneWebModule implements Module {
+public class CayenneFilter_DispatchModule implements Module {
 
     public void configure(Binder binder) {
-        binder.bind(RequestHandler.class).to(DefaultRequestHandler.class);
+        binder
+                .bind(RequestHandler.class)
+                .to(CayenneFilter_DispatchRequestHandler.class)
+                .in(Scopes.SINGLETON);
     }
 }

Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchRequestHandler.java
(from r932516, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchRequestHandler.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchRequestHandler.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java&r1=932516&r2=932683&rev=932683&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilter_DispatchRequestHandler.java
Sat Apr 10 09:19:20 2010
@@ -21,16 +21,25 @@ package org.apache.cayenne.configuration
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
-/**
- * Default implementation of the {@link RequestHandler}.
- * 
- * @since 3.1
- */
-public class DefaultRequestHandler implements RequestHandler {
+public class CayenneFilter_DispatchRequestHandler implements RequestHandler {
+
+    private int started;
+    private int ended;
 
     public void requestEnd(ServletRequest request, ServletResponse response) {
+        ended++;
     }
 
     public void requestStart(ServletRequest request, ServletResponse response) {
+        started++;
+    }
+
+    public int getStarted() {
+        return started;
     }
+
+    public int getEnded() {
+        return ended;
+    }
+
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneWebModuleTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneWebModuleTest.java?rev=932683&r1=932682&r2=932683&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneWebModuleTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneWebModuleTest.java
Sat Apr 10 09:19:20 2010
@@ -29,7 +29,7 @@ public class CayenneWebModuleTest extend
 
         Injector injector = new DefaultInjector(new CayenneWebModule());
         RequestHandler handler = injector.getInstance(RequestHandler.class);
-        assertTrue(handler instanceof DefaultRequestHandler);
+        assertTrue(handler instanceof SessionContextRequestHandler);
 
         RequestHandler handler1 = injector.getInstance(RequestHandler.class);
         assertNotSame("Incorrect singleton scope for request handler", handler, handler1);

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java?rev=932683&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java
Sat Apr 10 09:19:20 2010
@@ -0,0 +1,112 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.configuration.web;
+
+import org.apache.cayenne.BaseContext;
+import org.apache.cayenne.MockObjectContext;
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.DIBootstrap;
+import org.apache.cayenne.di.Injector;
+import org.apache.cayenne.di.Module;
+
+import com.mockrunner.mock.web.MockHttpServletRequest;
+import com.mockrunner.mock.web.MockHttpServletResponse;
+import com.mockrunner.mock.web.MockHttpSession;
+
+import junit.framework.TestCase;
+
+public class ServletContextHandlerTest extends TestCase {
+
+    public void testRequestStart_bindContext() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(ObjectContext.class).to(MockObjectContext.class);
+            }
+        };
+        Injector injector = DIBootstrap.createInjector(module);
+        SessionContextRequestHandler handler = new SessionContextRequestHandler();
+        injector.injectMembers(handler);
+
+        MockHttpSession session = new MockHttpSession();
+
+        BaseContext.bindThreadObjectContext(null);
+
+        try {
+
+            MockHttpServletRequest request1 = new MockHttpServletRequest();
+            MockHttpServletResponse response1 = new MockHttpServletResponse();
+            request1.setSession(session);
+            handler.requestStart(request1, response1);
+
+            ObjectContext c1 = BaseContext.getThreadObjectContext();
+            assertNotNull(c1);
+
+            handler.requestEnd(request1, response1);
+
+            try {
+                BaseContext.getThreadObjectContext();
+                fail("thread context not null");
+            }
+            catch (IllegalStateException e) {
+                // expected
+            }
+
+            MockHttpServletRequest request2 = new MockHttpServletRequest();
+            MockHttpServletResponse response2 = new MockHttpServletResponse();
+            request2.setSession(session);
+            handler.requestStart(request2, response2);
+
+            ObjectContext c2 = BaseContext.getThreadObjectContext();
+            assertSame(c1, c2);
+
+            handler.requestEnd(request2, response2);
+            try {
+                BaseContext.getThreadObjectContext();
+                fail("thread context not null");
+            }
+            catch (IllegalStateException e) {
+                // expected
+            }
+
+            MockHttpServletRequest request3 = new MockHttpServletRequest();
+            MockHttpServletResponse response3 = new MockHttpServletResponse();
+            request3.setSession(new MockHttpSession());
+            handler.requestStart(request3, response3);
+
+            ObjectContext c3 = BaseContext.getThreadObjectContext();
+            assertNotNull(c3);
+            assertNotSame(c1, c3);
+
+            handler.requestEnd(request3, response3);
+            try {
+                BaseContext.getThreadObjectContext();
+                fail("thread context not null");
+            }
+            catch (IllegalStateException e) {
+                // expected
+            }
+        }
+        finally {
+            BaseContext.bindThreadObjectContext(null);
+        }
+    }
+}



Mime
View raw message