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