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  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.when;
23  
24  import java.util.Properties;
25  
26  import javax.servlet.Servlet;
27  import javax.servlet.ServletConfig;
28  import javax.servlet.http.HttpServlet;
29  
30  import org.apache.commons.configuration2.AbstractConfiguration;
31  import org.apache.commons.configuration2.TestAbstractConfiguration;
32  import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
33  import org.junit.jupiter.api.Test;
34  import org.mockito.ArgumentMatchers;
35  
36  /**
37   * Test case for the {@link ServletConfiguration} class.
38   */
39  public class TestServletConfiguration extends TestAbstractConfiguration {
40  
41      /**
42       * Creates a mocked {@link ServletConfig}.
43       *
44       * @param parameters the init parameters to use
45       * @return The created mock
46       */
47      private ServletConfig mockServletConfig(final Properties parameters) {
48          final ServletConfig config = mock(ServletConfig.class);
49          when(config.getInitParameterNames()).thenAnswer(invocation -> parameters.keys());
50          when(config.getInitParameter(ArgumentMatchers.any())).thenAnswer(invocation -> {
51              final String name = invocation.getArgument(0, String.class);
52              return parameters.getProperty(name);
53          });
54          return config;
55      }
56  
57      @Override
58      protected AbstractConfiguration getConfiguration() {
59          final Properties parameters = new Properties();
60          parameters.setProperty("key1", "value1");
61          parameters.setProperty("key2", "value2");
62          parameters.setProperty("list", "value1, value2");
63          parameters.setProperty("listesc", "value1\\,value2");
64  
65          final ServletConfig config = mockServletConfig(parameters);
66  
67          final Servlet servlet = new HttpServlet() {
68              /**
69               * Serial version UID.
70               */
71              private static final long serialVersionUID = 1L;
72  
73              @Override
74              public ServletConfig getServletConfig() {
75                  return config;
76              }
77          };
78  
79          final ServletConfiguration servletConfiguration = new ServletConfiguration(servlet);
80          servletConfiguration.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
81          return servletConfiguration;
82      }
83  
84      @Override
85      protected AbstractConfiguration getEmptyConfiguration() {
86          return new ServletConfiguration(mockServletConfig(new Properties()));
87      }
88  
89      @Override
90      @Test
91      public void testAddPropertyDirect() {
92          assertThrows(UnsupportedOperationException.class, super::testAddPropertyDirect);
93      }
94  
95      @Override
96      @Test
97      public void testClearProperty() {
98          assertThrows(UnsupportedOperationException.class, super::testClearProperty);
99      }
100 }