001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.configuration.web;
019    
020    import java.util.Enumeration;
021    import java.util.Properties;
022    
023    import javax.servlet.Servlet;
024    import javax.servlet.ServletConfig;
025    import javax.servlet.ServletContext;
026    import javax.servlet.http.HttpServlet;
027    
028    import org.apache.commons.configuration.AbstractConfiguration;
029    import org.apache.commons.configuration.TestAbstractConfiguration;
030    import org.junit.Test;
031    
032    import com.mockobjects.servlet.MockServletConfig;
033    import com.mockobjects.servlet.MockServletContext;
034    
035    /**
036     * Test case for the {@link ServletContextConfiguration} class.
037     *
038     * @author Emmanuel Bourg
039     * @version $Id: TestServletContextConfiguration.java 1222465 2011-12-22 21:32:56Z oheger $
040     */
041    public class TestServletContextConfiguration extends TestAbstractConfiguration
042    {
043        @Override
044        protected AbstractConfiguration getConfiguration()
045        {
046            final Properties parameters = new Properties();
047            parameters.setProperty("key1", "value1");
048            parameters.setProperty("key2", "value2");
049            parameters.setProperty("list", "value1, value2");
050            parameters.setProperty("listesc", "value1\\,value2");
051    
052            // create a servlet context
053            ServletContext context = new MockServletContext()
054            {
055                @Override
056                public String getInitParameter(String key)
057                {
058                    return parameters.getProperty(key);
059                }
060    
061                @Override
062                public Enumeration<?> getInitParameterNames()
063                {
064                    return parameters.keys();
065                }
066            };
067    
068            // create a servlet config
069            final MockServletConfig config = new MockServletConfig();
070            config.setServletContext(context);
071    
072            // create a servlet
073            Servlet servlet = new HttpServlet()
074            {
075                /**
076                 * Serial version UID.
077                 */
078                private static final long serialVersionUID = 1L;
079    
080                @Override
081                public ServletConfig getServletConfig()
082                {
083                    return config;
084                }
085            };
086    
087            return new ServletContextConfiguration(servlet);
088        }
089    
090        @Override
091        protected AbstractConfiguration getEmptyConfiguration()
092        {
093            // create a servlet context
094            ServletContext context = new MockServletContext()
095            {
096                @Override
097                public Enumeration<?> getInitParameterNames()
098                {
099                    return new Properties().keys();
100                }
101            };
102    
103            return new ServletContextConfiguration(context);
104        }
105    
106        @Override
107        @Test(expected = UnsupportedOperationException.class)
108        public void testAddPropertyDirect()
109        {
110            super.testAddPropertyDirect();
111        }
112    
113        @Override
114        @Test(expected = UnsupportedOperationException.class)
115        public void testClearProperty()
116        {
117            super.testClearProperty();
118        }
119    }