View Javadoc

1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.unit;
17  
18  import java.util.Enumeration;
19  import java.util.Properties;
20  
21  import javax.servlet.FilterConfig;
22  import javax.servlet.ServletContext;
23  
24  import org.junit.Assert;
25  
26  /**
27   * {@link FilterConfig} のモック。
28   * 
29   * @author baba
30   */
31  class MockFilterConfig implements FilterConfig {
32  
33  	private final ServletContext servletContext;
34  
35  	private final String filterName;
36  
37  	private final Properties initParameters = new Properties();
38  
39  	/**
40  	 * Create a new MockFilterConfig.
41  	 * 
42  	 * @param servletContext
43  	 *            the ServletContext that the servlet runs in
44  	 */
45  	public MockFilterConfig(ServletContext servletContext) {
46  		this(servletContext, "");
47  	}
48  
49  	/**
50  	 * Create a new MockFilterConfig.
51  	 * 
52  	 * @param servletContext
53  	 *            the ServletContext that the servlet runs in
54  	 * @param filterName
55  	 *            the name of the filter
56  	 */
57  	public MockFilterConfig(ServletContext servletContext, String filterName) {
58  		this.servletContext = (servletContext != null ? servletContext
59  				: new MockServletContext());
60  		this.filterName = filterName;
61  	}
62  
63  	/**
64  	 * {@inheritDoc}
65  	 */
66  	public String getFilterName() {
67  		return filterName;
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 */
73  	public ServletContext getServletContext() {
74  		return servletContext;
75  	}
76  
77  	/**
78  	 * {@inheritDoc}
79  	 */
80  	public String getInitParameter(String name) {
81  		Assert.assertNotNull(name, "Parameter name must not be null");
82  		return this.initParameters.getProperty(name);
83  	}
84  
85  	/**
86  	 * {@inheritDoc}
87  	 */
88  	@SuppressWarnings("unchecked")
89  	public Enumeration getInitParameterNames() {
90  		return this.initParameters.keys();
91  	}
92  
93  }