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