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