1   /*
2    * Copyright 2004-2008 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.tags;
17  
18  import java.io.IOException;
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.Servlet;
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.ServletResponse;
29  import javax.servlet.http.HttpSession;
30  import javax.servlet.jsp.JspWriter;
31  import javax.servlet.jsp.PageContext;
32  import javax.servlet.jsp.el.ExpressionEvaluator;
33  import javax.servlet.jsp.el.VariableResolver;
34  
35  import org.seasar.framework.mock.servlet.MockHttpServletRequest;
36  import org.seasar.framework.mock.servlet.MockHttpServletRequestImpl;
37  import org.seasar.framework.mock.servlet.MockServletContext;
38  import org.seasar.framework.mock.servlet.MockServletContextImpl;
39  
40  public class MockJspContext extends PageContext {
41  
42  	private MockServletContext servletContext = new MockServletContextImpl("cubby");
43  	private MockHttpServletRequest request = new MockHttpServletRequestImpl(servletContext, "/mock");
44  	private MockJspWriter writer = new MockJspWriter();
45  	private Map<Integer, Map<String, Object>> attributes = new HashMap<Integer, Map<String,Object>>();
46  	private int[] FIND_ATTRIBUTE_SEQ = { PageContext.PAGE_SCOPE, PageContext.REQUEST_SCOPE, PageContext.SESSION_SCOPE, PageContext.APPLICATION_SCOPE };
47  	
48  	public MockJspContext() {
49  		for (int scope : FIND_ATTRIBUTE_SEQ) {
50  			attributes.put(scope, new HashMap<String, Object>());
51  		}
52  	}
53  	
54  	public MockJspWriter getMockJspWriter() {
55  		return this.writer;
56  	}
57  	
58  	public String getResult() {
59  		return getMockJspWriter().getResult();
60  	}
61  	
62  	@Override
63  	public Object findAttribute(String name) {
64  		Object value = null;
65  		for (int scope : FIND_ATTRIBUTE_SEQ) {
66  			value = getAttribute(name, scope);
67  			if (value != null) {
68  				return value;
69  			}
70  		}
71  		return null;
72  	}
73  
74  	@Override
75  	public Object getAttribute(String name) {
76  		// TODO 自動生成されたメソッド・スタブ
77  		return null;
78  	}
79  
80  	@Override
81  	public Object getAttribute(String name, int scope) {
82  		Map<String, Object> scopeMap = attributes.get(scope);
83  		return scopeMap != null ? scopeMap.get(name) : null;
84  	}
85  
86  	@SuppressWarnings("unchecked")
87  	@Override
88  	public Enumeration getAttributeNamesInScope(int scope) {
89  		throw new UnsupportedOperationException();
90  	}
91  
92  	@Override
93  	public int getAttributesScope(String name) {
94  		throw new UnsupportedOperationException();
95  	}
96  
97  	@Override
98  	public ExpressionEvaluator getExpressionEvaluator() {
99  		// TODO 自動生成されたメソッド・スタブ
100 		return null;
101 	}
102 
103 	@Override
104 	public JspWriter getOut() {
105 		// TODO 自動生成されたメソッド・スタブ
106 		return writer ;
107 	}
108 
109 	@Override
110 	public VariableResolver getVariableResolver() {
111 		throw new UnsupportedOperationException();
112 	}
113 
114 	@Override
115 	public void removeAttribute(String name) {
116 		throw new UnsupportedOperationException();
117 	}
118 
119 	@Override
120 	public void removeAttribute(String name, int scope) {
121 		this.attributes.get(scope).remove(name);
122 	}
123 
124 	@Override
125 	public void setAttribute(String name, Object value) {
126 		throw new UnsupportedOperationException();
127 	}
128 
129 	@Override
130 	public void setAttribute(String name, Object value, int scope) {
131 		attributes.get(scope).put(name, value);
132 	}
133 
134 	@Override
135 	public void forward(String relativeUrlPath) throws ServletException,
136 			IOException {
137 		// TODO 自動生成されたメソッド・スタブ
138 		
139 	}
140 
141 	@Override
142 	public Exception getException() {
143 		// TODO 自動生成されたメソッド・スタブ
144 		return null;
145 	}
146 
147 	@Override
148 	public Object getPage() {
149 		// TODO 自動生成されたメソッド・スタブ
150 		return null;
151 	}
152 
153 	@Override
154 	public ServletRequest getRequest() {
155 		return request;
156 	}
157 
158 	@Override
159 	public ServletResponse getResponse() {
160 		// TODO 自動生成されたメソッド・スタブ
161 		return null;
162 	}
163 
164 	@Override
165 	public ServletConfig getServletConfig() {
166 		// TODO 自動生成されたメソッド・スタブ
167 		return null;
168 	}
169 
170 	@Override
171 	public ServletContext getServletContext() {
172 		// TODO 自動生成されたメソッド・スタブ
173 		return null;
174 	}
175 
176 	@Override
177 	public HttpSession getSession() {
178 		// TODO 自動生成されたメソッド・スタブ
179 		return null;
180 	}
181 
182 	@Override
183 	public void handlePageException(Exception e) throws ServletException,
184 			IOException {
185 		// TODO 自動生成されたメソッド・スタブ
186 		
187 	}
188 
189 	@Override
190 	public void handlePageException(Throwable t) throws ServletException,
191 			IOException {
192 		// TODO 自動生成されたメソッド・スタブ
193 		
194 	}
195 
196 	@Override
197 	public void include(String relativeUrlPath) throws ServletException,
198 			IOException {
199 		// TODO 自動生成されたメソッド・スタブ
200 		
201 	}
202 
203 	@Override
204 	public void include(String relativeUrlPath, boolean flush)
205 			throws ServletException, IOException {
206 		// TODO 自動生成されたメソッド・スタブ
207 		
208 	}
209 
210 	@Override
211 	public void initialize(Servlet servlet, ServletRequest request,
212 			ServletResponse response, String errorPageURL,
213 			boolean needsSession, int bufferSize, boolean autoFlush)
214 			throws IOException, IllegalStateException, IllegalArgumentException {
215 		// TODO 自動生成されたメソッド・スタブ
216 		
217 	}
218 
219 	@Override
220 	public void release() {
221 		// TODO 自動生成されたメソッド・スタブ
222 		
223 	}
224 
225 }