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