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