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