    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.commons.configuration.web;
    19
    20  import java.util.Enumeration;
    21  import java.util.List;
    22
    23  import javax.servlet.ServletRequest;
    24
    25  import com.mockobjects.servlet.MockHttpServletRequest;
    26  import org.apache.commons.collections.iterators.IteratorEnumeration;
    27  import org.apache.commons.configuration.AbstractConfiguration;
    28  import org.apache.commons.configuration.BaseConfiguration;
    29  import org.apache.commons.configuration.Configuration;
    30  import org.apache.commons.configuration.TestAbstractConfiguration;
    31  import org.apache.commons.lang.StringUtils;
    32
    33  /***
    34   * Test case for the {@link ServletRequestConfiguration} class.
    35   *
    36   * @author Emmanuel Bourg
    37   * @version $Revision: 515306 $, $Date: 2007-03-06 22:15:00 +0100 (Di, 06 Mrz 2007) $
    38   */
    39  public class TestServletRequestConfiguration extends TestAbstractConfiguration
    40  {
    41      protected AbstractConfiguration getConfiguration()
    42      {
    43          final Configuration configuration = new BaseConfiguration();
    44          ((BaseConfiguration) configuration).setListDelimiter('\0');
    45          configuration.setProperty("key1", "value1");
    46          configuration.setProperty("key2", "value2");
    47          configuration.addProperty("list", "value1");
    48          configuration.addProperty("list", "value2");
    49          configuration.addProperty("listesc", "value1//,value2");
    50
    51          return createConfiguration(configuration);
    52      }
    53
    54      protected AbstractConfiguration getEmptyConfiguration()
    55      {
    56          final Configuration configuration = new BaseConfiguration();
    57
    58          ServletRequest request = new MockHttpServletRequest()
    59          {
    60              public String getParameter(String key)
    61              {
    62                  return null;
    63              }
    64
    65              public Enumeration getParameterNames()
    66              {
    67                  return new IteratorEnumeration(configuration.getKeys());
    68              }
    69          };
    70
    71          return new ServletRequestConfiguration(request);
    72      }
    73
    74      /***
    75       * Returns a new servlet request configuration that is backed by the passed
    76       * in configuration.
    77       *
    78       * @param base the configuration with the underlying values
    79       * @return the servlet request configuration
    80       */
    81      private ServletRequestConfiguration createConfiguration(
    82              final Configuration base)
    83      {
    84          ServletRequest request = new MockHttpServletRequest()
    85          {
    86              public String[] getParameterValues(String key)
    87              {
    88                  return base.getStringArray(key);
    89              }
    90
    91              public Enumeration getParameterNames()
    92              {
    93                  return new IteratorEnumeration(base.getKeys());
    94              }
    95          };
    96
    97          return new ServletRequestConfiguration(request);
    98      }
    99
    100     public void testAddPropertyDirect()
    101     {
    102         try
    103         {
    104             super.testAddPropertyDirect();
    105             fail("addPropertyDirect should throw an UnsupportedException");
    106         }
    107         catch (UnsupportedOperationException e)
    108         {
    109             // ok
    110         }
    111     }
    112
    113     public void testClearProperty()
    114     {
    115         try
    116         {
    117             super.testClearProperty();
    118             fail("testClearProperty should throw an UnsupportedException");
    119         }
    120         catch (UnsupportedOperationException e)
    121         {
    122             // ok
    123         }
    124     }
    125
    126     /***
    127      * Tests a list with elements that contain an escaped list delimiter.
    128      */
    129     public void testListWithEscapedElements()
    130     {
    131         String[] values =
    132         { "test1", "test2//,test3", "test4//,test5" };
    133         final String listKey = "test.list";
    134         BaseConfiguration config = new BaseConfiguration();
    135         config.setListDelimiter('\0');
    136         config.addProperty(listKey, values);
    137         assertEquals("Wrong number of list elements", values.length, config
    138                 .getList(listKey).size());
    139         Configuration c = createConfiguration(config);
    140         List v = c.getList(listKey);
    141         assertEquals("Wrong number of elements in list", values.length, v
    142                 .size());
    143         for (int i = 0; i < values.length; i++)
    144         {
    145             assertEquals("Wrong value at index " + i, StringUtils.replace(
    146                     values[i], "//", StringUtils.EMPTY), v.get(i));
    147         }
    148     }
    149 }