cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r930848 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/configuration/ main/java/org/apache/cayenne/configuration/server/ main/java/org/apache/cayenne/configuration/web/ test/java/org/apache/c...
Date Mon, 05 Apr 2010 12:12:26 GMT
Author: aadamchik
Date: Mon Apr  5 12:12:26 2010
New Revision: 930848

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

* in progress implementation

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/
    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/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/
    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
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule1.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule2.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockRequestHandler.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/WebUtilTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerRuntime.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java?rev=930848&r1=930847&r2=930848&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneRuntime.java
Mon Apr  5 12:12:26 2010
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.configuration;
 
+import java.util.Collection;
+
 import org.apache.cayenne.DataChannel;
 import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.di.DIBootstrap;
@@ -56,6 +58,27 @@ public abstract class CayenneRuntime {
     }
 
     /**
+     * Creates a CayenneRuntime with configuration based on supplied array of DI modules.
+     */
+    public CayenneRuntime(String name, Collection<Module> modules) {
+
+        if (name == null) {
+            throw new NullPointerException("Null runtime name");
+        }
+
+        this.name = name;
+
+        if (modules == null) {
+            this.modules = new Module[0];
+        }
+        else {
+            this.modules = modules.toArray(new Module[modules.size()]);
+        }
+
+        this.injector = DIBootstrap.createInjector(this.modules);
+    }
+
+    /**
      * Returns runtime name. By default a name of Cayenne project XML file contains a
      * runtime name in it in the form "cayenne-<name>.xml".
      */
@@ -71,6 +94,13 @@ public abstract class CayenneRuntime {
     }
 
     /**
+     * Returns DI injector used by this runtime.
+     */
+    public Injector getInjector() {
+        return injector;
+    }
+
+    /**
      * Returns the runtime {@link DataChannel}.
      */
     public DataChannel getDataChannel() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerRuntime.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerRuntime.java?rev=930848&r1=930847&r2=930848&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerRuntime.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerRuntime.java
Mon Apr  5 12:12:26 2010
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.server;
 
+import java.util.Collection;
+
 import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.configuration.CayenneRuntime;
 import org.apache.cayenne.di.Module;
@@ -41,7 +43,7 @@ public class CayenneServerRuntime extend
     }
 
     /**
-     * Creates a server runtime object based on a set of custom DI modules. When
+     * Creates a server runtime object based on an array of custom DI modules. When
      * implementing custom modules, refer to {@link CayenneServerModule} for the minimal
      * set of services required by Cayenne. The easiest way to do it is to pass
      * {@link CayenneServerModule} or its subclass as one of the modules to this
@@ -52,6 +54,17 @@ public class CayenneServerRuntime extend
     }
 
     /**
+     * Creates a server runtime object based on a collection of DI modules. When
+     * implementing custom modules, refer to {@link CayenneServerModule} for the minimal
+     * set of services required by Cayenne. The easiest way to do it is to pass
+     * {@link CayenneServerModule} or its subclass as one of the modules to this
+     * constructor.
+     */
+    public CayenneServerRuntime(String name, Collection<Module> modules) {
+        super(name, modules);
+    }
+
+    /**
      * Returns the main runtime DataDomain. Note that by default the returned DataDomain
      * is the same as the main DataChannel returned by {@link #getDataChannel()}. Although
      * users may redefine DataChannel provider in the DI registry, for instance to

Added: 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=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneFilter.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,117 @@
+/*****************************************************************
+ *   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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.StringTokenizer;
+
+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 org.apache.cayenne.configuration.server.CayenneServerModule;
+import org.apache.cayenne.configuration.server.CayenneServerRuntime;
+import org.apache.cayenne.di.Module;
+import org.apache.cayenne.util.Util;
+
+/**
+ * A filter that creates a Cayenne server runtime, possibly including custom modules. By
+ * default runtime includes {@link CayenneServerModule} and {@link CayenneWebModule}. Any
+ * custom modules are loaded after the two standard ones to allow custom service
+ * overrides. Filter initialization parameters:
+ * <ul>
+ * <li>runtime-name - (optional) a name of Cayenne runtime. When looking for a Cayenne
+ * configuration XML file in the classpath, Cayenne derives the name of the file from the
+ * value of this parameter. If "runtime-name" is "foo", configuration file name is assumed
+ * to be "cayenne-foo.xml". By default filter name is used as runtime name.
+ * <li>extra-modules - (optional) a comma or space-separated list of class names, with
+ * each class implementing {@link Module} interface. These are the custom modules loaded
+ * 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>
+ * 
+ * @since 3.1
+ */
+public class CayenneFilter implements Filter {
+
+    static final String RUNTIME_NAME_PARAMETER = "runtime-name";
+    static final String EXTRA_MODULES_PARAMETER = "extra-modules";
+
+    public void init(FilterConfig config) throws ServletException {
+
+        String runtimeName = config.getInitParameter(RUNTIME_NAME_PARAMETER);
+        if (runtimeName == null) {
+            runtimeName = config.getFilterName();
+        }
+
+        if (runtimeName == null) {
+            throw new ServletException(
+                    "Can't initialize Cayenne runtime. CayenneFilter has no name and no '"
+                            + RUNTIME_NAME_PARAMETER
+                            + "' parameter");
+        }
+
+        Collection<Module> modules = new ArrayList<Module>(5);
+        modules.add(new CayenneServerModule(runtimeName));
+        modules.add(new CayenneWebModule());
+
+        String extraModules = config.getInitParameter(EXTRA_MODULES_PARAMETER);
+        if (extraModules != null) {
+
+            StringTokenizer toks = new StringTokenizer(extraModules, ", \n\r");
+
+            while (toks.hasMoreTokens()) {
+                String moduleName = toks.nextToken();
+                Module module;
+                try {
+                    module = (Module) Util.getJavaClass(moduleName).newInstance();
+                }
+                catch (Exception e) {
+                    String message = String
+                            .format(
+                                    "Error instantiating custom DI module '%s' by filter
'%s': %s",
+                                    moduleName,
+                                    getClass().getName(),
+                                    e.getMessage());
+                    throw new ServletException(message, e);
+                }
+
+                modules.add(module);
+            }
+        }
+
+        CayenneServerRuntime runtime = new CayenneServerRuntime(runtimeName, modules);
+        WebUtil.setCayenneRuntime(config.getServletContext(), runtime);
+    }
+
+    public void destroy() {
+        // noop for now...
+    }
+
+    public void doFilter(
+            ServletRequest request,
+            ServletResponse response,
+            FilterChain chain) throws IOException, ServletException {
+    }
+}

Added: 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=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,32 @@
+/*****************************************************************
+ *   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.di.Binder;
+import org.apache.cayenne.di.Module;
+
+/**
+ * @since 3.1
+ */
+public class CayenneWebModule implements Module {
+
+    public void configure(Binder binder) {
+        binder.bind(RequestHandler.class).to(DefaultRequestHandler.class);
+    }
+}

Added: 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/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java?rev=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/DefaultRequestHandler.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,36 @@
+/*****************************************************************
+ *   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;
+
+/**
+ * Default implementation of the {@link RequestHandler}.
+ * 
+ * @since 3.1
+ */
+public class DefaultRequestHandler implements RequestHandler {
+
+    public void requestEnd(ServletRequest request, ServletResponse response) {
+    }
+
+    public void requestStart(ServletRequest request, ServletResponse response) {
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java?rev=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/RequestHandler.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,35 @@
+/*****************************************************************
+ *   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;
+
+/**
+ * A service invoked by {@link CayenneFilter} that provides a callback mechanism to bind
+ * appropriate ObjectContext to the request thread, handle transactions, etc.
+ * 
+ * @since 3.1
+ */
+public interface RequestHandler {
+
+    void requestStart(ServletRequest request, ServletResponse response);
+
+    void requestEnd(ServletRequest request, ServletResponse response);
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java?rev=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/WebUtil.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,53 @@
+/*****************************************************************
+ *   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.ServletContext;
+
+import org.apache.cayenne.configuration.CayenneRuntime;
+
+/**
+ * A helper class to retrieve and store {@link CayenneRuntime} in the
+ * {@link ServletContext}. All Cayenne web configuration objects are relying on this class
+ * to access runtime.
+ * 
+ * @since 3.1
+ */
+public class WebUtil {
+
+    static final String CAYENNE_RUNTIME_KEY = WebUtil.class.getName()
+            + ".CAYENNE_RUNTIME";
+
+    /**
+     * Retrieves CayenneRuntime previously stored in provided context via
+     * {@link #setCayenneRuntime(ServletContext, CayenneRuntime)}. May return null if no
+     * runtime was stored.
+     */
+    public static CayenneRuntime getCayenneRuntime(ServletContext context) {
+        return (CayenneRuntime) context.getAttribute(CAYENNE_RUNTIME_KEY);
+    }
+
+    /**
+     * Stores {@link CayenneRuntime} in the servlet context. It can be later retrieve via
+     * {@link #getCayenneRuntime(ServletContext)}.
+     */
+    public static void setCayenneRuntime(ServletContext context, CayenneRuntime runtime)
{
+        context.setAttribute(CAYENNE_RUNTIME_KEY, runtime);
+    }
+}

Added: 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=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilterTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneFilterTest.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,121 @@
+/*****************************************************************
+ *   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.configuration.CayenneRuntime;
+import org.apache.cayenne.configuration.server.CayenneServerModule;
+
+import com.mockrunner.mock.web.MockFilterConfig;
+import com.mockrunner.mock.web.MockServletContext;
+
+import junit.framework.TestCase;
+
+public class CayenneFilterTest extends TestCase {
+
+    public void testInitWithFilterName() throws Exception {
+
+        MockFilterConfig config = new MockFilterConfig();
+        config.setFilterName("abc");
+
+        MockServletContext context = new MockServletContext();
+        config.setupServletContext(context);
+
+        CayenneFilter filter = new CayenneFilter();
+
+        assertNull(WebUtil.getCayenneRuntime(context));
+        filter.init(config);
+
+        CayenneRuntime runtime = WebUtil.getCayenneRuntime(context);
+        assertNotNull(runtime);
+
+        assertEquals("abc", runtime.getName());
+    }
+
+    public void testInitWithRuntimeName() throws Exception {
+
+        MockFilterConfig config = new MockFilterConfig();
+        config.setFilterName("abc");
+        config.setInitParameter(CayenneFilter.RUNTIME_NAME_PARAMETER, "xyz");
+
+        MockServletContext context = new MockServletContext();
+        config.setupServletContext(context);
+
+        CayenneFilter filter = new CayenneFilter();
+        filter.init(config);
+
+        CayenneRuntime runtime = WebUtil.getCayenneRuntime(context);
+        assertNotNull(runtime);
+
+        assertEquals("xyz", runtime.getName());
+    }
+
+    public void testInitWithStarndardModules() throws Exception {
+
+        MockFilterConfig config = new MockFilterConfig();
+        config.setFilterName("abc");
+
+        MockServletContext context = new MockServletContext();
+        config.setupServletContext(context);
+
+        CayenneFilter filter = new CayenneFilter();
+
+        assertNull(WebUtil.getCayenneRuntime(context));
+        filter.init(config);
+
+        CayenneRuntime runtime = WebUtil.getCayenneRuntime(context);
+        assertNotNull(runtime);
+
+        assertEquals("abc", runtime.getName());
+        assertEquals(2, runtime.getModules().length);
+        assertTrue(runtime.getModules()[0] instanceof CayenneServerModule);
+        assertTrue(runtime.getModules()[1] instanceof CayenneWebModule);
+
+        RequestHandler handler = runtime.getInjector().getInstance(RequestHandler.class);
+        assertTrue(handler instanceof DefaultRequestHandler);
+    }
+
+    public void testInitWithExtraModules() throws Exception {
+
+        MockFilterConfig config = new MockFilterConfig();
+        config.setFilterName("abc");
+        config.setInitParameter(CayenneFilter.EXTRA_MODULES_PARAMETER, MockModule1.class
+                .getName()
+                + ","
+                + MockModule2.class.getName());
+
+        MockServletContext context = new MockServletContext();
+        config.setupServletContext(context);
+
+        CayenneFilter filter = new CayenneFilter();
+        filter.init(config);
+
+        CayenneRuntime runtime = WebUtil.getCayenneRuntime(context);
+        assertNotNull(runtime);
+
+        assertEquals(4, runtime.getModules().length);
+
+        assertTrue(runtime.getModules()[0] instanceof CayenneServerModule);
+        assertTrue(runtime.getModules()[1] instanceof CayenneWebModule);
+        assertTrue(runtime.getModules()[2] instanceof MockModule1);
+        assertTrue(runtime.getModules()[3] instanceof MockModule2);
+
+        RequestHandler handler = runtime.getInjector().getInstance(RequestHandler.class);
+        assertTrue(handler instanceof MockRequestHandler);
+    }
+}

Added: 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=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneWebModuleTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/CayenneWebModuleTest.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,37 @@
+/*****************************************************************
+ *   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.di.Injector;
+import org.apache.cayenne.di.spi.DefaultInjector;
+
+import junit.framework.TestCase;
+
+public class CayenneWebModuleTest extends TestCase {
+
+    public void testBind_Scopes() {
+
+        Injector injector = new DefaultInjector(new CayenneWebModule());
+        RequestHandler handler = injector.getInstance(RequestHandler.class);
+        assertTrue(handler instanceof DefaultRequestHandler);
+
+        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/MockModule1.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule1.java?rev=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule1.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule1.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,30 @@
+/*****************************************************************
+ *   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.di.Binder;
+import org.apache.cayenne.di.Module;
+
+
+public class MockModule1 implements Module {
+
+    public void configure(Binder binder) {
+    }
+
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule2.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule2.java?rev=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule2.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockModule2.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,30 @@
+/*****************************************************************
+ *   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.di.Binder;
+import org.apache.cayenne.di.Module;
+
+public class MockModule2 implements Module {
+
+    public void configure(Binder binder) {
+        binder.bind(RequestHandler.class).to(MockRequestHandler.class);
+    }
+
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockRequestHandler.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockRequestHandler.java?rev=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockRequestHandler.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/MockRequestHandler.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,33 @@
+/*****************************************************************
+ *   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;
+
+
+public class MockRequestHandler implements RequestHandler {
+
+    public void requestEnd(ServletRequest request, ServletResponse response) {
+    }
+
+    public void requestStart(ServletRequest request, ServletResponse response) {
+    }
+
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/WebUtilTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/WebUtilTest.java?rev=930848&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/WebUtilTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/WebUtilTest.java
Mon Apr  5 12:12:26 2010
@@ -0,0 +1,49 @@
+/*****************************************************************
+ *   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.configuration.CayenneRuntime;
+
+import com.mockrunner.mock.web.MockServletContext;
+
+import junit.framework.TestCase;
+
+public class WebUtilTest extends TestCase {
+
+    public void testGetCayenneRuntime() {
+        MockServletContext context = new MockServletContext();
+
+        assertNull(WebUtil.getCayenneRuntime(context));
+
+        CayenneRuntime runtime = new CayenneRuntime("xx") {
+        };
+
+        WebUtil.setCayenneRuntime(context, runtime);
+        assertSame(runtime, WebUtil.getCayenneRuntime(context));
+
+        CayenneRuntime runtime1 = new CayenneRuntime("yy") {
+        };
+
+        WebUtil.setCayenneRuntime(context, runtime1);
+        assertSame(runtime1, WebUtil.getCayenneRuntime(context));
+
+        WebUtil.setCayenneRuntime(context, null);
+        assertNull(WebUtil.getCayenneRuntime(context));
+    }
+}



Mime
View raw message