001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.configuration.web;
019    
020    import static org.junit.Assert.fail;
021    
022    import java.applet.Applet;
023    import java.util.Properties;
024    
025    import org.apache.commons.configuration.AbstractConfiguration;
026    import org.apache.commons.configuration.BaseConfiguration;
027    import org.apache.commons.configuration.MapConfiguration;
028    import org.apache.commons.configuration.TestAbstractConfiguration;
029    import org.junit.Before;
030    import org.junit.Test;
031    
032    /**
033     * Test case for the {@link AppletConfiguration} class.
034     *
035     * @author Emmanuel Bourg
036     * @version $Id: TestAppletConfiguration.java 1222465 2011-12-22 21:32:56Z oheger $
037     */
038    public class TestAppletConfiguration extends TestAbstractConfiguration
039    {
040        /** A flag whether tests with an applet can be run. */
041        boolean supportsApplet;
042    
043        /**
044         * Initializes the tests. This implementation checks whether an applet can
045         * be used. Some environments, which do not support a GUI, don't allow
046         * creating an <code>Applet</code> instance. If we are in such an
047         * environment, some tests need to behave differently or be completely
048         * dropped.
049         */
050        @Before
051        public void setUp() throws Exception
052        {
053            try
054            {
055                new Applet();
056                supportsApplet = true;
057            }
058            catch (Exception ex)
059            {
060                // cannot use applets
061                supportsApplet = false;
062            }
063        }
064    
065        @Override
066        protected AbstractConfiguration getConfiguration()
067        {
068            final Properties parameters = new Properties();
069            parameters.setProperty("key1", "value1");
070            parameters.setProperty("key2", "value2");
071            parameters.setProperty("list", "value1, value2");
072            parameters.setProperty("listesc", "value1\\,value2");
073    
074            if (supportsApplet)
075            {
076                Applet applet = new Applet()
077                {
078                    /**
079                     * Serial version UID.
080                     */
081                    private static final long serialVersionUID = 1L;
082    
083                    @Override
084                    public String getParameter(String key)
085                    {
086                        return parameters.getProperty(key);
087                    }
088    
089                    @Override
090                    public String[][] getParameterInfo()
091                    {
092                        return new String[][]
093                        {
094                        { "key1", "String", "" },
095                        { "key2", "String", "" },
096                        { "list", "String[]", "" },
097                        { "listesc", "String", "" } };
098                    }
099                };
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    }