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