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.configuration2.web;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.List;
28  
29  import javax.servlet.http.HttpServletRequest;
30  
31  import org.apache.commons.configuration2.AbstractConfiguration;
32  import org.apache.commons.configuration2.BaseConfiguration;
33  import org.apache.commons.configuration2.Configuration;
34  import org.apache.commons.configuration2.ConfigurationMap;
35  import org.apache.commons.configuration2.TestAbstractConfiguration;
36  import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
37  import org.junit.jupiter.api.Test;
38  import org.mockito.ArgumentMatchers;
39  
40  /**
41   * Test case for the {@link ServletRequestConfiguration} class.
42   */
43  public class TestServletRequestConfiguration extends TestAbstractConfiguration {
44      /**
45       * Returns a new servlet request configuration that is backed by the passed in configuration.
46       *
47       * @param base the configuration with the underlying values
48       * @return the servlet request configuration
49       */
50      private ServletRequestConfiguration createConfiguration(final Configuration base) {
51          final HttpServletRequest request = mock(HttpServletRequest.class);
52          when(request.getParameterMap()).thenAnswer(invocation -> new ConfigurationMap(base));
53          when(request.getParameterValues(ArgumentMatchers.any())).thenAnswer(invocation -> {
54              final String key = invocation.getArgument(0, String.class);
55              return base.getStringArray(key);
56          });
57  
58          final ServletRequestConfiguration config = new ServletRequestConfiguration(request);
59          config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
60          return config;
61      }
62  
63      @Override
64      protected AbstractConfiguration getConfiguration() {
65          final Configuration configuration = new BaseConfiguration();
66          configuration.setProperty("key1", "value1");
67          configuration.setProperty("key2", "value2");
68          configuration.addProperty("list", "value1");
69          configuration.addProperty("list", "value2");
70          configuration.addProperty("listesc", "value1\\,value2");
71  
72          return createConfiguration(configuration);
73      }
74  
75      @Override
76      protected AbstractConfiguration getEmptyConfiguration() {
77          final HttpServletRequest request = mock(HttpServletRequest.class);
78          when(request.getParameter(ArgumentMatchers.any())).thenReturn(null);
79          when(request.getParameterMap()).thenAnswer(invocation -> new HashMap<>());
80  
81          return new ServletRequestConfiguration(request);
82      }
83  
84      @Override
85      @Test
86      public void testAddPropertyDirect() {
87          assertThrows(UnsupportedOperationException.class, super::testAddPropertyDirect);
88      }
89  
90      @Override
91      @Test
92      public void testClearProperty() {
93          assertThrows(UnsupportedOperationException.class, super::testClearProperty);
94      }
95  
96      /**
97       * Tests a list with elements that contain an escaped list delimiter.
98       */
99      @Test
100     public void testListWithEscapedElements() {
101         final String[] values = {"test1", "test2\\,test3", "test4\\,test5"};
102         final String listKey = "test.list";
103 
104         final BaseConfiguration config = new BaseConfiguration();
105         config.addProperty(listKey, values);
106 
107         assertEquals(values.length, config.getList(listKey).size());
108 
109         final Configuration c = createConfiguration(config);
110         final List<?> v = c.getList(listKey);
111 
112         final List<String> expected = new ArrayList<>();
113         for (final String value : values) {
114             expected.add(value.replace("\\", ""));
115         }
116         assertEquals(expected, v);
117     }
118 }