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.configuration.web;
19  
20  import org.apache.commons.configuration.AbstractConfiguration;
21  import org.apache.commons.configuration.BaseConfiguration;
22  import org.apache.commons.configuration.MapConfiguration;
23  import org.apache.commons.configuration.TestAbstractConfiguration;
24  
25  import java.applet.Applet;
26  import java.util.Properties;
27  
28  /***
29   * Test case for the {@link AppletConfiguration} class.
30   *
31   * @author Emmanuel Bourg
32   * @version $Revision: 439648 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb
33   * 2005) $
34   */
35  public class TestAppletConfiguration extends TestAbstractConfiguration
36  {
37      /*** A flag whether tests with an applet can be run. */
38      boolean supportsApplet;
39  
40      /***
41       * Initializes the tests. This implementation checks whether an applet can
42       * be used. Some environments, which do not support a GUI, don't allow
43       * creating an <code>Applet</code> instance. If we are in such an
44       * environment, some tests need to behave differently or be completely
45       * dropped.
46       */
47      protected void setUp() throws Exception
48      {
49          try
50          {
51              new Applet();
52              supportsApplet = true;
53          }
54          catch (Exception ex)
55          {
56              // cannot use applets
57              supportsApplet = false;
58          }
59      }
60  
61      protected AbstractConfiguration getConfiguration()
62      {
63          final Properties parameters = new Properties();
64          parameters.setProperty("key1", "value1");
65          parameters.setProperty("key2", "value2");
66          parameters.setProperty("list", "value1, value2");
67  
68          if (supportsApplet)
69          {
70              Applet applet = new Applet()
71              {
72                  public String getParameter(String key)
73                  {
74                      return parameters.getProperty(key);
75                  }
76  
77                  public String[][] getParameterInfo()
78                  {
79                      return new String[][]
80                      {
81                      { "key1", "String", "" },
82                      { "key2", "String", "" },
83                      { "list", "String[]", "" } };
84                  }
85              };
86  
87              return new AppletConfiguration(applet);
88          }
89          else
90          {
91              return new MapConfiguration(parameters);
92          }
93      }
94  
95      protected AbstractConfiguration getEmptyConfiguration()
96      {
97          if (supportsApplet)
98          {
99              return new AppletConfiguration(new Applet());
100         }
101         else
102         {
103             return new BaseConfiguration();
104         }
105     }
106 
107     public void testAddPropertyDirect()
108     {
109         if (supportsApplet)
110         {
111             try
112             {
113                 super.testAddPropertyDirect();
114                 fail("addPropertyDirect should throw an UnsupportedException");
115             }
116             catch (UnsupportedOperationException e)
117             {
118                 // ok
119             }
120         }
121     }
122 
123     public void testClearProperty()
124     {
125         if (supportsApplet)
126         {
127             try
128             {
129                 super.testClearProperty();
130                 fail("testClearProperty should throw an UnsupportedException");
131             }
132             catch (UnsupportedOperationException e)
133             {
134                 // ok
135             }
136         }
137     }
138 }