Hi, I'd like to suggest a new configuration implementation that bridges between parameters commonly used in web applications (servlet, filter, application and request parameters) and our Configuration interface.

Because I'm tired of writting always the same parsing code like:

public void init(ServletConfig config) {
    try {
        param = Integer.parseInt(config.getInitParameter("param"));
    }
    catch (NumberFormatException e) { }
}

I tought it would be much easier to write instead:

public void init(ServletConfig config) {
    Configuration conf = new WebConfiguration(config);
    param = conf.getInt("param");
}

The WebConfiguration works with 4 types of objets specified in its constructor: a ServletConfig, a ServletContext, a FilterConfig or a ServletRequest.

I wrote a test case using mock objects covering the different cases except for the FilterConfig, the mock lacks a setInitParameter() method. Also the mock for the ServletRequest sends an exception for unknown parameters instead of returning null, most of its tests break.

Emmanuel Bourg

/*
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed 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.commons.configuration;

import org.apache.commons.collections.iterators.EnumerationIterator;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.FilterConfig;
import java.util.Iterator;

/**
 * A configuration suitable for reading the configuration a web components
 * (Servlets, filters, requests and applications).
 *
 * @author Emmanuel Bourg
 * @version $Revision$, $Date$
 */
public class WebConfiguration extends AbstractConfiguration {

    protected Object store;

    /**
     * Create a WebConfiguration using the request parameters.
     */
    public WebConfiguration(ServletRequest request) {
        store = request;
    }

    /**
     * Create a WebConfiguration using the servlet initialization parameters.
     */
    public WebConfiguration(ServletConfig config) {
        store = config;
    }

    /**
     * Create a WebConfiguration using the servlet context initialization
     * parameters.
     */
    public WebConfiguration(ServletContext context) {
        store = context;
    }

    /**
     * Create a WebConfiguration using the filter initialization parameters.
     */
    public WebConfiguration(FilterConfig config) {
        store = config;
    }

    protected Object getPropertyDirect(String key) {
        if (store instanceof ServletRequest) {
            return ((ServletRequest) store).getParameter(key);
        } else if (store instanceof ServletContext) {
            return ((ServletContext) store).getInitParameter(key);
        } else if (store instanceof ServletConfig){
            return ((ServletConfig) store).getInitParameter(key);
        } else {
            return ((FilterConfig) store).getInitParameter(key);
        }
    }

    protected void addPropertyDirect(String key, Object obj) {
        throw new UnsupportedOperationException("Read only configuration");
    }

    public boolean isEmpty() {
        return !getKeys().hasNext();
    }

    public boolean containsKey(String key) {
        return getPropertyDirect(key) != null;
    }

    public void clearProperty(String key) {
        throw new UnsupportedOperationException("Read only configuration");
    }

    public Iterator getKeys() {
        if (store instanceof ServletRequest) {
            return new EnumerationIterator(((ServletRequest) 
store).getParameterNames());
        } else if (store instanceof ServletContext) {
            return new EnumerationIterator(((ServletContext) 
store).getInitParameterNames());
        } else if (store instanceof ServletConfig){
            return new EnumerationIterator(((ServletConfig) 
store).getInitParameterNames());
        } else {
            return new EnumerationIterator(((FilterConfig) 
store).getInitParameterNames());
        }
    }
}
/*
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed 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.commons.configuration;

import com.mockobjects.servlet.MockHttpServletRequest;
import com.mockobjects.servlet.MockServletConfig;
import com.mockobjects.servlet.MockServletContext;
import junit.framework.TestCase;

import javax.servlet.FilterConfig;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * Test case for the [EMAIL PROTECTED] WebConfiguration} class.
 *
 * @author Emmanuel Bourg
 * @version $Revision$, $Date$
 */
public class TestWebConfiguration extends TestCase {

    protected HttpServletRequest request;
    protected ServletContext context;
    protected ServletConfig servlet;
    protected FilterConfig filter;

    protected void setUp() throws Exception {
        MockHttpServletRequest  request = new MockHttpServletRequest();
        request.setupAddParameter("key1", "value1");
        request.setupAddParameter("key2", "value2");
        this.request = request;

        MockServletContext context = new MockServletContext();
        context.setInitParameter("key1", "value1");
        context.setInitParameter("key2", "value2");
        this.context = context;

        MockServletConfig servlet = new MockServletConfig();
        servlet.setInitParameter("key1", "value1");
        servlet.setInitParameter("key2", "value2");
        this.servlet = servlet;
    }

    public void testGetPropertyDirectRequest() {
        WebConfiguration config = new WebConfiguration(request);
        assertEquals("request key1", "value1", config.getPropertyDirect("key1"));
        assertEquals("request key2", "value2", config.getPropertyDirect("key2"));
        assertNull("request key3", config.getPropertyDirect("key3"));
    }

    public void testGetPropertyDirectContext() {
        WebConfiguration config = new WebConfiguration(context);
        assertEquals("servlet context key1", "value1", 
config.getPropertyDirect("key1"));
        assertEquals("servlet context key2", "value2", 
config.getPropertyDirect("key2"));
        assertNull("servlet context key3", config.getPropertyDirect("key3"));
    }

    public void testGetPropertyDirectServlet() {
        WebConfiguration config = new WebConfiguration(servlet);
        assertEquals("servlet config key1", "value1", 
config.getPropertyDirect("key1"));
        assertEquals("servlet config key2", "value2", 
config.getPropertyDirect("key2"));
        assertNull("servlet config key3", config.getPropertyDirect("key3"));
    }

    public void testIsEmptyRequest() {
        WebConfiguration config = new WebConfiguration(request);
        assertFalse("non empty request parameters", config.isEmpty());
        assertTrue("empty request parameters", new WebConfiguration(new 
MockHttpServletRequest()).isEmpty());
    }

    public void testIsEmptyContext() {
        WebConfiguration config = new WebConfiguration(context);
        assertFalse("non empty servlet context parameters", config.isEmpty());
        assertTrue("empty servlet context parameters", new WebConfiguration(new 
MockServletContext()).isEmpty());
    }

    public void testIsEmptyServlet() {
        WebConfiguration config = new WebConfiguration(servlet);
        assertFalse("non empty servlet config parameters", config.isEmpty());
        assertTrue("empty servlet config parameters", new WebConfiguration(new 
MockServletConfig()).isEmpty());
    }

    public void testContainsKeyRequest() {
        WebConfiguration config = new WebConfiguration(request);
        assertTrue("key1 not found in request parameters", config.containsKey("key1"));
        assertFalse("key3 found in request parameters", config.containsKey("key3"));
    }

    public void testContainsKeyContext() {
        WebConfiguration config = new WebConfiguration(context);
        assertTrue("key1 not found in servlet context parameters", 
config.containsKey("key1"));
        assertFalse("key3 found in servlet context parameters", 
config.containsKey("key3"));
    }

    public void testContainsKeyServlet() {
        WebConfiguration config = new WebConfiguration(servlet);
        assertTrue("key1 not found in servlet config parameters", 
config.containsKey("key1"));
        assertFalse("key3 found in servlet config parameters", 
config.containsKey("key3"));
    }

    public void testGetKeysRequest() {
        WebConfiguration config = new WebConfiguration(request);
        assertNotNull("null iterator on request parameters", config.getKeys());
        assertNotNull("null iterator on request parameters", new WebConfiguration(new 
MockHttpServletRequest()).getKeys());
    }

    public void testGetKeysContext() {
        WebConfiguration config = new WebConfiguration(context);
        assertNotNull("null iterator on servlet context parameters", config.getKeys());
        assertNotNull("null iterator on servlet context parameters", new 
WebConfiguration(new MockServletContext()).getKeys());
    }

    public void testGetKeysServlet() {        
        WebConfiguration config = new WebConfiguration(servlet);
        assertNotNull("null iterator on servlet config parameters", config.getKeys());
        assertNotNull("null iterator on servlet config parameters", new 
WebConfiguration(new MockServletConfig()).getKeys());
    }
}
Index: project.xml
===================================================================
RCS file: /home/cvspublic/jakarta-commons/configuration/project.xml,v
retrieving revision 1.10
diff -u -r1.10 project.xml
--- project.xml 15 Feb 2004 11:58:37 -0000      1.10
+++ project.xml 1 Mar 2004 18:00:00 -0000
@@ -193,6 +193,11 @@
       </properties>  
     </dependency>
 
+    <dependency>
+      <id>servletapi</id>
+      <version>2.3</version>
+    </dependency>
+
     <!-- Needed for testing -->
     <dependency>
       <id>junit</id>
@@ -229,6 +234,16 @@
     <dependency>
       <id>exml</id>
       <version>dbunit1.5.1</version>
+    </dependency>
+
+    <dependency>
+      <id>mockobjects:mockobjects-core</id>
+      <version>0.09</version>
+    </dependency>
+
+    <dependency>
+      <id>mockobjects:mockobjects-jdk1.4-j2ee1.3</id>
+      <version>0.09</version>
     </dependency>
 
   </dependencies>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to