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 jakarta.servlet.Servlet;
28  import jakarta.servlet.ServletConfig;
29  import jakarta.servlet.ServletContext;
30  import jakarta.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 TestJakartaServletContextConfiguration 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              /**
62               * Serial version UID.
63               */
64              private static final long serialVersionUID = 1L;
65  
66              @Override
67              public ServletConfig getServletConfig() {
68                  return config;
69              }
70          };
71  
72          final AbstractConfiguration resultConfig = new JakartaServletContextConfiguration(servlet);
73          resultConfig.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
74          return resultConfig;
75      }
76  
77      @Override
78      protected AbstractConfiguration getEmptyConfiguration() {
79          // create a servlet context
80          final ServletContext context = mockServletConfig(new Properties());
81  
82          return new JakartaServletContextConfiguration(context);
83      }
84  
85      /**
86       * Creates a mocked {@link ServletConfig}.
87       *
88       * @param parameters the init parameters to use
89       * @return The created mock
90       */
91      private ServletContext mockServletConfig(final Properties parameters) {
92          final ServletContext context = mock(ServletContext.class);
93          when(context.getInitParameterNames()).thenAnswer(invocation -> parameters.keys());
94          when(context.getInitParameter(ArgumentMatchers.any())).thenAnswer(invocation -> {
95              final String name = invocation.getArgument(0, String.class);
96              return parameters.getProperty(name);
97          });
98          return context;
99      }
100 
101     @Override
102     @Test
103     public void testAddPropertyDirect() {
104         assertThrows(UnsupportedOperationException.class, super::testAddPropertyDirect);
105     }
106 
107     @Override
108     @Test
109     public void testClearProperty() {
110         assertThrows(UnsupportedOperationException.class, super::testClearProperty);
111     }
112 
113     @Override
114     @Test
115     public void testContainsValue() {
116         assertFalse(getConfiguration().containsValue(null));
117         assertFalse(getConfiguration().containsValue(""));
118     }
119 }