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