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.assertThrows;
21  
22  import java.util.Enumeration;
23  import java.util.Properties;
24  
25  import javax.servlet.FilterConfig;
26  import javax.servlet.ServletContext;
27  
28  import org.apache.commons.configuration2.AbstractConfiguration;
29  import org.apache.commons.configuration2.TestAbstractConfiguration;
30  import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Test case for the {@link ServletFilterConfiguration} class.
35   */
36  public class TestServletFilterConfiguration extends TestAbstractConfiguration {
37      private static final class MockFilterConfig implements FilterConfig {
38          private final Properties parameters = new Properties();
39  
40          @Override
41          public String getFilterName() {
42              return null;
43          }
44  
45          @Override
46          public String getInitParameter(final String key) {
47              return parameters.getProperty(key);
48          }
49  
50          @Override
51          public Enumeration<?> getInitParameterNames() {
52              return parameters.keys();
53          }
54  
55          @Override
56          public ServletContext getServletContext() {
57              return null;
58          }
59  
60          public void setInitParameter(final String key, final String value) {
61              parameters.setProperty(key, value);
62          }
63      }
64  
65      @Override
66      protected AbstractConfiguration getConfiguration() {
67          final MockFilterConfig config = new MockFilterConfig();
68          config.setInitParameter("key1", "value1");
69          config.setInitParameter("key2", "value2");
70          config.setInitParameter("list", "value1, value2");
71          config.setInitParameter("listesc", "value1\\,value2");
72  
73          final ServletFilterConfiguration resultConfig = new ServletFilterConfiguration(config);
74          resultConfig.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
75          return resultConfig;
76      }
77  
78      @Override
79      protected AbstractConfiguration getEmptyConfiguration() {
80          return new ServletFilterConfiguration(new MockFilterConfig());
81      }
82  
83      @Override
84      @Test
85      public void testAddPropertyDirect() {
86          assertThrows(UnsupportedOperationException.class, super::testAddPropertyDirect);
87      }
88  
89      @Override
90      @Test
91      public void testClearProperty() {
92          assertThrows(UnsupportedOperationException.class, super::testClearProperty);
93      }
94  }