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  
23  import java.applet.Applet;
24  import java.util.Properties;
25  
26  import org.apache.commons.configuration2.AbstractConfiguration;
27  import org.apache.commons.configuration2.BaseConfiguration;
28  import org.apache.commons.configuration2.MapConfiguration;
29  import org.apache.commons.configuration2.TestAbstractConfiguration;
30  import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Test case for the {@link AppletConfiguration} class.
36   */
37  public class TestAppletConfiguration extends TestAbstractConfiguration {
38  
39      /** A flag whether tests with an applet can be run. */
40      private boolean supportsApplet;
41  
42      @Override
43      protected AbstractConfiguration getConfiguration() {
44          final AbstractConfiguration config;
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          if (supportsApplet) {
52              final Applet applet = new Applet() {
53  
54                  /**
55                   * Serial version UID.
56                   */
57                  private static final long serialVersionUID = 1L;
58  
59                  @Override
60                  public String getParameter(final String key) {
61                      return parameters.getProperty(key);
62                  }
63  
64                  @Override
65                  public String[][] getParameterInfo() {
66                      return new String[][] {{"key1", "String", ""}, {"key2", "String", ""}, {"list", "String[]", ""}, {"listesc", "String", ""}};
67                  }
68              };
69  
70              config = new AppletConfiguration(applet);
71          } else {
72              config = new MapConfiguration(parameters);
73          }
74  
75          config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
76          return config;
77      }
78  
79      @Override
80      protected AbstractConfiguration getEmptyConfiguration() {
81          if (supportsApplet) {
82              return new AppletConfiguration(new Applet());
83          }
84          return new BaseConfiguration();
85      }
86  
87      /**
88       * Initializes the tests. This implementation checks whether an applet can be used. Some environments, which do not
89       * support a GUI, don't allow creating an {@code Applet} instance. If we are in such an environment, some tests need to
90       * behave differently or be completely dropped.
91       */
92      @BeforeEach
93      public void setUp() throws Exception {
94          try {
95              new Applet();
96              supportsApplet = true;
97          } catch (final Exception ex) {
98              // cannot use applets
99              supportsApplet = false;
100         }
101     }
102 
103     @Override
104     @Test
105     public void testAddPropertyDirect() {
106         if (supportsApplet) {
107             assertThrows(UnsupportedOperationException.class, super::testAddPropertyDirect);
108         }
109     }
110 
111     @Override
112     @Test
113     public void testClearProperty() {
114         if (supportsApplet) {
115             assertThrows(UnsupportedOperationException.class, super::testClearProperty);
116         }
117     }
118 
119     @Override
120     @Test
121     public void testContainsValue() {
122         assertFalse(getConfiguration().containsValue(null));
123         assertFalse(getConfiguration().containsValue(""));
124     }
125 }