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