Here is the new implementation :
- the class WebConfiguration has been split into smaller classes for each type. Everything is in a web sub package.
- an AppletConfiguration has been introduced
- 100% test coverage this time :) But I had to tweak the mocks.
- I added a TestAbstractConfiguration class, this is an abstract test case that can be used to test AbstractConfiguration implementations. It might be useful for the 1.0 release even if the web configurations are added later.


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.web;

import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.collections.iterators.ArrayIterator;

import java.applet.Applet;
import java.util.Iterator;

/**
 * A configuration wrapper to read applet parameters.
 *
 * @author Emmanuel Bourg
 * @version $Revision$, $Date$
 */
public class AppletConfiguration extends AbstractConfiguration {

    protected Applet applet;

    public AppletConfiguration(Applet applet) {
        this.applet = applet;
    }

    protected Object getPropertyDirect(String key) {
        return applet.getParameter(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() {
        String[][] paramsInfo = applet.getParameterInfo();
        String[] keys = new String[paramsInfo != null ? paramsInfo.length : 0];
        for (int i = 0; i < keys.length; i++) {
            keys[i] = paramsInfo[i][0];
        }

        return new ArrayIterator(keys);
    }

}
/*
 * 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.web;

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

import javax.servlet.FilterConfig;
import java.util.Iterator;

/**
 * A configuration wrapper around a [EMAIL PROTECTED] FilterConfig}.
 *
 * @author Emmanuel Bourg
 * @version $Revision$, $Date$
 */
public class FilterConfiguration extends AbstractConfiguration {

    protected FilterConfig config;

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

    protected Object getPropertyDirect(String key) {
        return config.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() {
        return new EnumerationIterator(config.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.web;

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

import javax.servlet.ServletConfig;
import java.util.Iterator;

/**
 * A configuration wrapper around a [EMAIL PROTECTED] ServletConfig}.
 *
 * @author Emmanuel Bourg
 * @version $Revision$, $Date$
 */
public class ServletConfiguration extends AbstractConfiguration {

    protected ServletConfig config;

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

    protected Object getPropertyDirect(String key) {
        return config.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() {
        return new EnumerationIterator(config.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.web;

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

import javax.servlet.ServletContext;
import java.util.Iterator;

/**
 * A configuration wrapper to read the initialization parameters of a servlet context.
 *
 * @author Emmanuel Bourg
 * @version $Revision$, $Date$
 */
public class ServletContextConfiguration extends AbstractConfiguration {

    protected ServletContext context;

    /**
     * Create a ServletConfiguration using the servlet context initialization 
parameters.
     */
    public ServletContextConfiguration(ServletContext context) {
        this.context = context;
    }

    protected Object getPropertyDirect(String key) {
        return context.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() {
        return new EnumerationIterator(context.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.web;

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

import javax.servlet.ServletRequest;
import java.util.Iterator;

/**
 * A configuration wrapper to read the parameters of a servlet request.
 *
 * @author Emmanuel Bourg
 * @version $Revision$, $Date$
 */
public class ServletRequestConfiguration extends AbstractConfiguration {

    protected ServletRequest request;

    /**
     * Create a FilterConfiguration using the filter initialization parameters.
     */
    public ServletRequestConfiguration(ServletRequest request) {
        this.request = request;
    }

    protected Object getPropertyDirect(String key) {
        return request.getParameter(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() {
        return new EnumerationIterator(request.getParameterNames());
    }
}
/*
 * 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 junit.framework.TestCase;

import java.util.Iterator;

/**
 * Abstract TestCase for implementations of [EMAIL PROTECTED] AbstractConfiguration}.
 *
 * @author Emmanuel Bourg
 * @version $Revision$, $Date$
 */
public abstract class TestAbstractConfiguration extends TestCase {

    /**
     * Return an abstract configuration with 2 key/value pairs:<br>
     * <pre>
     * key1 = value1
     * key2 = value2
     * </pre>
     */
    protected abstract AbstractConfiguration getConfiguration();

    /**
     * Return an empty configuration.
     */
    protected abstract AbstractConfiguration getEmptyConfiguration();

    public void testGetPropertyDirect() {
        AbstractConfiguration config = getConfiguration();
        assertEquals("key1", "value1", config.getPropertyDirect("key1"));
        assertEquals("key2", "value2", config.getPropertyDirect("key2"));
        assertNull("key3", config.getPropertyDirect("key3"));
    }

    public void testAddPropertyDirect() {
        AbstractConfiguration config = getConfiguration();
        config.addPropertyDirect("key3", "value3");
        assertEquals("key3", "value3", config.getPropertyDirect("key3"));
    }

    public void testIsEmpty() {
        AbstractConfiguration config = getConfiguration();
        assertFalse("the configuration is empty", config.isEmpty());
        assertTrue("the configuration is not empty", 
getEmptyConfiguration().isEmpty());
    }

    public void testContainsKey() {
        AbstractConfiguration config = getConfiguration();
        assertTrue("key1 not found", config.containsKey("key1"));
        assertFalse("key3 found", config.containsKey("key3"));
    }

    public void testClearProperty() {
        AbstractConfiguration config = getConfiguration();
        config.clearProperty("key2");
        assertFalse("key2 not cleared", config.containsKey("key2"));
    }

    public void testGetKeys() {
        AbstractConfiguration config = getConfiguration();
        Iterator keys = config.getKeys();

        assertNotNull("null iterator", keys);
        String k = keys.next() + ":" + keys.next();
        assertTrue("elements", "key1:key2".equals(k) | "key2:key1".equals(k));
        assertFalse("too many elements", keys.hasNext());

        keys = getEmptyConfiguration().getKeys();
        assertNotNull("null iterator", keys);
        assertFalse("too many elements", keys.hasNext());
    }

}
/*
 * 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.web;

import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.TestAbstractConfiguration;

import java.applet.Applet;
import java.util.Properties;

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

    protected AbstractConfiguration getConfiguration() {
        final Properties parameters = new Properties();
        parameters.setProperty("key1", "value1");
        parameters.setProperty("key2", "value2");

        Applet applet = new Applet() {
            public String getParameter(String key) {
                return parameters.getProperty(key);
            }

            public String[][] getParameterInfo() {
                return new String[][]{
                    {"key1", "String", ""},
                    {"key2", "String", ""}
                };
            }
        };

        return new AppletConfiguration(applet);
    }

    protected AbstractConfiguration getEmptyConfiguration() {
        return new AppletConfiguration(new Applet());
    }

    public void testAddPropertyDirect() {
        // skipped, unsupported operation
    }

    public void testClearProperty() {
        // skipped, unsupported operation
    }
}
/*
 * 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.web;

import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.TestAbstractConfiguration;

import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.Properties;

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

    protected AbstractConfiguration getConfiguration() {
        MockFilterConfig config = new MockFilterConfig();
        config.setInitParameter("key1", "value1");
        config.setInitParameter("key2", "value2");

        return new FilterConfiguration(config);
    }

    protected AbstractConfiguration getEmptyConfiguration() {
        return new FilterConfiguration(new MockFilterConfig());
    }

    private class MockFilterConfig implements FilterConfig {

        private Properties parameters = new Properties();

        public String getFilterName() {
            return null;
        }

        public ServletContext getServletContext() {
            return null;
        }

        public String getInitParameter(String key) {
            return parameters.getProperty(key);
        }

        public Enumeration getInitParameterNames() {
            return parameters.keys();
        }

        public void setInitParameter(String key, String value) {
            parameters.setProperty(key, value);
        }
    }

    public void testAddPropertyDirect() {
        // skipped, unsupported operation
    }

    public void testClearProperty() {
        // skipped, unsupported operation
    }

}
/*
 * 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.web;

import com.mockobjects.servlet.MockServletConfig;
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.TestAbstractConfiguration;

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

    protected AbstractConfiguration getConfiguration() {
        MockServletConfig config = new MockServletConfig();
        config.setInitParameter("key1", "value1");
        config.setInitParameter("key2", "value2");

        return new ServletConfiguration(config);
    }

    protected AbstractConfiguration getEmptyConfiguration() {
        return new ServletConfiguration(new MockServletConfig());
    }

    public void testAddPropertyDirect() {
        // skipped, unsupported operation
    }

    public void testClearProperty() {
        // skipped, unsupported operation
    }

}
/*
 * 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.web;

import com.mockobjects.servlet.MockServletContext;
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.TestAbstractConfiguration;

import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.Properties;

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

    protected AbstractConfiguration getConfiguration() {

        final Properties parameters = new Properties();
        parameters.setProperty("key1", "value1");
        parameters.setProperty("key2", "value2");

        ServletContext context = new MockServletContext() {

            public String getInitParameter(String key) {
                return parameters.getProperty(key);
            }

            public Enumeration getInitParameterNames() {
                return parameters.keys();
            }
        };

        return new ServletContextConfiguration(context);
    }

    protected AbstractConfiguration getEmptyConfiguration() {

        ServletContext context = new MockServletContext() {
            public Enumeration getInitParameterNames() {
                return new Properties().keys();
            }
        };

        return new ServletContextConfiguration(context);
    }

    public void testAddPropertyDirect() {
        // skipped, unsupported operation
    }

    public void testClearProperty() {
        // skipped, unsupported operation
    }

}
/*
 * 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.web;

import com.mockobjects.servlet.MockHttpServletRequest;
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.TestAbstractConfiguration;

import javax.servlet.ServletRequest;
import java.util.Enumeration;
import java.util.Properties;

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

    protected AbstractConfiguration getConfiguration() {
        final Properties parameters = new Properties();
        parameters.setProperty("key1", "value1");
        parameters.setProperty("key2", "value2");

        ServletRequest request = new MockHttpServletRequest() {
            public String getParameter(String key) {
                return parameters.getProperty(key);
            }

            public Enumeration getParameterNames() {
                return parameters.keys();
            }
        };

        return new ServletRequestConfiguration(request);
    }

    protected AbstractConfiguration getEmptyConfiguration() {
        final Properties parameters = new Properties();

        ServletRequest request = new MockHttpServletRequest() {
            public String getParameter(String key) {
                return null;
            }

            public Enumeration getParameterNames() {
                return parameters.keys();
            }
        };

        return new ServletRequestConfiguration(request);
    }

    public void testAddPropertyDirect() {
        // skipped, unsupported operation
    }

    public void testClearProperty() {
        // skipped, unsupported operation
    }

}

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

Reply via email to