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