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.io.InputStream;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.util.Enumeration;
22  import java.util.Hashtable;
23  import java.util.Set;
24  
25  import javax.servlet.RequestDispatcher;
26  import javax.servlet.Servlet;
27  import javax.servlet.ServletContext;
28  import javax.servlet.ServletException;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * プラグインを初期化するためのサーブレットコンテキストのモックです。
35   * 
36   * @author baba
37   */
38  class MockServletContext implements ServletContext {
39  
40  	private static final Logger logger = LoggerFactory
41  			.getLogger(MockServletContext.class);
42  
43  	private Hashtable<String, String> initParameters = new Hashtable<String, String>();
44  
45  	private Hashtable<String, Object> attributes = new Hashtable<String, Object>();
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	public String getContextPath() {
51  		throw new UnsupportedOperationException();
52  	}
53  
54  	/**
55  	 * {@inheritDoc}
56  	 */
57  	public ServletContext getContext(String uripath) {
58  		throw new UnsupportedOperationException();
59  	}
60  
61  	/**
62  	 * {@inheritDoc}
63  	 */
64  	public int getMajorVersion() {
65  		return 2;
66  	}
67  
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	public int getMinorVersion() {
72  		return 5;
73  	}
74  
75  	/**
76  	 * {@inheritDoc}
77  	 */
78  	public String getMimeType(String file) {
79  		throw new UnsupportedOperationException();
80  	}
81  
82  	/**
83  	 * {@inheritDoc}
84  	 */
85  	@SuppressWarnings("unchecked")
86  	public Set getResourcePaths(String path) {
87  		throw new UnsupportedOperationException();
88  	}
89  
90  	/**
91  	 * {@inheritDoc}
92  	 */
93  	public URL getResource(String path) throws MalformedURLException {
94  		throw new UnsupportedOperationException();
95  	}
96  
97  	/**
98  	 * {@inheritDoc}
99  	 */
100 	public InputStream getResourceAsStream(String path) {
101 		throw new UnsupportedOperationException();
102 	}
103 
104 	/**
105 	 * {@inheritDoc}
106 	 */
107 	public RequestDispatcher getRequestDispatcher(String path) {
108 		throw new UnsupportedOperationException();
109 	}
110 
111 	/**
112 	 * {@inheritDoc}
113 	 */
114 	public RequestDispatcher getNamedDispatcher(String name) {
115 		throw new UnsupportedOperationException();
116 	}
117 
118 	/**
119 	 * {@inheritDoc}
120 	 */
121 	public Servlet getServlet(String name) throws ServletException {
122 		throw new UnsupportedOperationException();
123 	}
124 
125 	/**
126 	 * {@inheritDoc}
127 	 */
128 	@SuppressWarnings("unchecked")
129 	public Enumeration getServlets() {
130 		throw new UnsupportedOperationException();
131 	}
132 
133 	/**
134 	 * {@inheritDoc}
135 	 */
136 	@SuppressWarnings("unchecked")
137 	public Enumeration getServletNames() {
138 		throw new UnsupportedOperationException();
139 	}
140 
141 	/**
142 	 * {@inheritDoc}
143 	 */
144 	public void log(String msg) {
145 		logger.info(msg);
146 	}
147 
148 	/**
149 	 * {@inheritDoc}
150 	 */
151 	public void log(Exception exception, String msg) {
152 		this.log(msg, exception);
153 	}
154 
155 	/**
156 	 * {@inheritDoc}
157 	 */
158 	public void log(String message, Throwable throwable) {
159 		logger.info(message, throwable);
160 	}
161 
162 	/**
163 	 * {@inheritDoc}
164 	 */
165 	public String getRealPath(String path) {
166 		throw new UnsupportedOperationException();
167 	}
168 
169 	/**
170 	 * {@inheritDoc}
171 	 */
172 	public String getServerInfo() {
173 		throw new UnsupportedOperationException();
174 	}
175 
176 	/**
177 	 * {@inheritDoc}
178 	 */
179 	public String getInitParameter(String name) {
180 		return initParameters.get(name);
181 	}
182 
183 	/**
184 	 * {@inheritDoc}
185 	 */
186 	@SuppressWarnings("unchecked")
187 	public Enumeration getInitParameterNames() {
188 		return initParameters.keys();
189 	}
190 
191 	/**
192 	 * {@inheritDoc}
193 	 */
194 	public Object getAttribute(String name) {
195 		return attributes.get(name);
196 	}
197 
198 	/**
199 	 * {@inheritDoc}
200 	 */
201 	@SuppressWarnings("unchecked")
202 	public Enumeration getAttributeNames() {
203 		return attributes.keys();
204 	}
205 
206 	/**
207 	 * {@inheritDoc}
208 	 */
209 	public void setAttribute(String name, Object object) {
210 		attributes.put(name, object);
211 	}
212 
213 	/**
214 	 * {@inheritDoc}
215 	 */
216 	public void removeAttribute(String name) {
217 		attributes.remove(name);
218 	}
219 
220 	/**
221 	 * {@inheritDoc}
222 	 */
223 	public String getServletContextName() {
224 		throw new UnsupportedOperationException();
225 	}
226 
227 }