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.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 ServletConfiguration} class.
39   */
40  public class TestJakartaServletConfiguration extends TestAbstractConfiguration {
41  
42      @Override
43      protected AbstractConfiguration getConfiguration() {
44          final Properties parameters = new Properties();
45          parameters.setProperty("key1", "value1");
46          parameters.setProperty("key2", "value2");
47          parameters.setProperty("list", "value1, value2");
48          parameters.setProperty("listesc", "value1\\,value2");
49  
50          final ServletConfig config = mockServletConfig(parameters);
51  
52          final Servlet servlet = new HttpServlet() {
53              /**
54               * Serial version UID.
55               */
56              private static final long serialVersionUID = 1L;
57  
58              @Override
59              public ServletConfig getServletConfig() {
60                  return config;
61              }
62          };
63  
64          final AbstractConfiguration servletConfiguration = new JakartaServletConfiguration(servlet);
65          servletConfiguration.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
66          return servletConfiguration;
67      }
68  
69      @Override
70      protected AbstractConfiguration getEmptyConfiguration() {
71          return new JakartaServletConfiguration(mockServletConfig(new Properties()));
72      }
73  
74      /**
75       * Creates a mocked {@link ServletConfig}.
76       *
77       * @param parameters the init parameters to use
78       * @return The created mock
79       */
80      private ServletConfig mockServletConfig(final Properties parameters) {
81          final ServletConfig config = mock(ServletConfig.class);
82          when(config.getInitParameterNames()).thenAnswer(invocation -> parameters.keys());
83          when(config.getInitParameter(ArgumentMatchers.any())).thenAnswer(invocation -> {
84              final String name = invocation.getArgument(0, String.class);
85              return parameters.getProperty(name);
86          });
87          return config;
88      }
89  
90      @Override
91      @Test
92      public void testAddPropertyDirect() {
93          assertThrows(UnsupportedOperationException.class, super::testAddPropertyDirect);
94      }
95  
96      @Override
97      @Test
98      public void testClearProperty() {
99          assertThrows(UnsupportedOperationException.class, super::testClearProperty);
100     }
101 
102     @Override
103     @Test
104     public void testContainsValue() {
105         assertFalse(getConfiguration().containsValue(null));
106         assertFalse(getConfiguration().containsValue(""));
107     }
108 }