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