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 java.util.Enumeration;
21  import java.util.Properties;
22  import javax.servlet.Servlet;
23  import javax.servlet.ServletConfig;
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServlet;
26  
27  import com.mockobjects.servlet.MockServletConfig;
28  import com.mockobjects.servlet.MockServletContext;
29  import org.apache.commons.configuration.AbstractConfiguration;
30  import org.apache.commons.configuration.TestAbstractConfiguration;
31  
32  /***
33   * Test case for the {@link ServletContextConfiguration} class.
34   *
35   * @author Emmanuel Bourg
36   * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
37   */
38  public class TestServletContextConfiguration extends TestAbstractConfiguration
39  {
40      protected AbstractConfiguration getConfiguration()
41      {
42          final Properties parameters = new Properties();
43          parameters.setProperty("key1", "value1");
44          parameters.setProperty("key2", "value2");
45          parameters.setProperty("list", "value1, value2");
46  
47          // create a servlet context
48          ServletContext context = new MockServletContext()
49          {
50              public String getInitParameter(String key)
51              {
52                  return parameters.getProperty(key);
53              }
54  
55              public Enumeration getInitParameterNames()
56              {
57                  return parameters.keys();
58              }
59          };
60  
61          // create a servlet config
62          final MockServletConfig config = new MockServletConfig();
63          config.setServletContext(context);
64  
65          // create a servlet
66          Servlet servlet = new HttpServlet()
67          {
68              public ServletConfig getServletConfig()
69              {
70                  return config;
71              }
72          };
73  
74          return new ServletContextConfiguration(servlet);
75      }
76  
77      protected AbstractConfiguration getEmptyConfiguration()
78      {
79          // create a servlet context
80          ServletContext context = new MockServletContext()
81          {
82              public Enumeration getInitParameterNames()
83              {
84                  return new Properties().keys();
85              }
86          };
87  
88          return new ServletContextConfiguration(context);
89      }
90  
91      public void testAddPropertyDirect()
92      {
93          try
94          {
95              super.testAddPropertyDirect();
96              fail("addPropertyDirect should throw an UnsupportedException");
97          }
98          catch (UnsupportedOperationException e)
99          {
100             // ok
101         }
102     }
103 
104     public void testClearProperty()
105     {
106         try
107         {
108             super.testClearProperty();
109             fail("testClearProperty should throw an UnsupportedException");
110         }
111         catch (UnsupportedOperationException e)
112         {
113             // ok
114         }
115     }
116 
117 }