View Javadoc

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 static org.junit.Assert.assertEquals;
21  
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.ServletRequest;
27  
28  import org.apache.commons.configuration.AbstractConfiguration;
29  import org.apache.commons.configuration.BaseConfiguration;
30  import org.apache.commons.configuration.Configuration;
31  import org.apache.commons.configuration.ConfigurationMap;
32  import org.apache.commons.configuration.TestAbstractConfiguration;
33  import org.junit.Test;
34  
35  import com.mockobjects.servlet.MockHttpServletRequest;
36  
37  /**
38   * Test case for the {@link ServletRequestConfiguration} class.
39   *
40   * @author Emmanuel Bourg
41   * @version $Id: TestServletRequestConfiguration.java 1222465 2011-12-22 21:32:56Z oheger $
42   */
43  public class TestServletRequestConfiguration extends TestAbstractConfiguration
44  {
45      @Override
46      protected AbstractConfiguration getConfiguration()
47      {
48          final Configuration configuration = new BaseConfiguration();
49          ((BaseConfiguration) configuration).setListDelimiter('\0');
50          configuration.setProperty("key1", "value1");
51          configuration.setProperty("key2", "value2");
52          configuration.addProperty("list", "value1");
53          configuration.addProperty("list", "value2");
54          configuration.addProperty("listesc", "value1\\,value2");
55  
56          return createConfiguration(configuration);
57      }
58  
59      @Override
60      protected AbstractConfiguration getEmptyConfiguration()
61      {
62          ServletRequest request = new MockHttpServletRequest()
63          {
64              @Override
65              public String getParameter(String key)
66              {
67                  return null;
68              }
69  
70              @Override
71              public Map<?, ?> getParameterMap()
72              {
73                  return new HashMap<Object, Object>();
74              }
75          };
76  
77          return new ServletRequestConfiguration(request);
78      }
79  
80      /**
81       * Returns a new servlet request configuration that is backed by the passed
82       * in configuration.
83       *
84       * @param base the configuration with the underlying values
85       * @return the servlet request configuration
86       */
87      private ServletRequestConfiguration createConfiguration(final Configuration base)
88      {
89          ServletRequest request = new MockHttpServletRequest()
90          {
91              @Override
92              public String[] getParameterValues(String key)
93              {
94                  return base.getStringArray(key);
95              }
96  
97              @Override
98              public Map<?, ?> getParameterMap()
99              {
100                 return new ConfigurationMap(base);
101             }
102         };
103 
104         return new ServletRequestConfiguration(request);
105     }
106 
107     @Override
108     @Test(expected = UnsupportedOperationException.class)
109     public void testAddPropertyDirect()
110     {
111         super.testAddPropertyDirect();
112     }
113 
114     @Override
115     @Test(expected = UnsupportedOperationException.class)
116     public void testClearProperty()
117     {
118         super.testClearProperty();
119     }
120 
121     /**
122      * Tests a list with elements that contain an escaped list delimiter.
123      */
124     @Test
125     public void testListWithEscapedElements()
126     {
127         String[] values = { "test1", "test2\\,test3", "test4\\,test5" };
128         String listKey = "test.list";
129 
130         BaseConfiguration config = new BaseConfiguration();
131         config.setListDelimiter('\0');
132         config.addProperty(listKey, values);
133 
134         assertEquals("Wrong number of list elements", values.length, config.getList(listKey).size());
135 
136         Configuration c = createConfiguration(config);
137         List<?> v = c.getList(listKey);
138 
139         assertEquals("Wrong number of elements in list", values.length, v.size());
140 
141         for (int i = 0; i < values.length; i++)
142         {
143             assertEquals("Wrong value at index " + i, values[i].replaceAll("\\\\", ""), v.get(i));
144         }
145     }
146 }