001    package com.mockrunner.mock.web;
002    
003    import java.io.IOException;
004    import java.io.Writer;
005    import java.util.Enumeration;
006    import java.util.HashMap;
007    import java.util.Iterator;
008    import java.util.NoSuchElementException;
009    import java.util.Stack;
010    
011    import javax.servlet.RequestDispatcher;
012    import javax.servlet.Servlet;
013    import javax.servlet.ServletConfig;
014    import javax.servlet.ServletContext;
015    import javax.servlet.ServletException;
016    import javax.servlet.ServletRequest;
017    import javax.servlet.ServletResponse;
018    import javax.servlet.http.HttpServletRequest;
019    import javax.servlet.http.HttpSession;
020    import javax.servlet.jsp.JspWriter;
021    import javax.servlet.jsp.PageContext;
022    import javax.servlet.jsp.el.ExpressionEvaluator;
023    import javax.servlet.jsp.el.VariableResolver;
024    import javax.servlet.jsp.tagext.BodyContent;
025    
026    /**
027     * Mock implementation of <code>PageContext</code>.
028     */
029    //Some methods of this class were copied from org.apache.struts.mock.MockPageContext
030    //and modified
031    public class MockPageContext extends PageContext
032    {
033        protected ServletConfig config;
034        protected ServletRequest request;
035        protected ServletResponse response;
036        private JspWriter jspWriter;
037        private Stack outStack;
038        private Exception exception;
039        private Object page;
040        private HashMap attributes;
041        private ExpressionEvaluator evaluator;
042        private VariableResolver resolver;
043        
044        public MockPageContext()
045        {
046            this(null, null, null);
047        }
048    
049        public MockPageContext(ServletConfig config, ServletRequest request, ServletResponse response)
050        {
051            this.config = config;
052            this.request = request;
053            this.response = response;
054            jspWriter = new MockJspWriter();
055            outStack = new Stack();
056            attributes = new HashMap();
057            evaluator = new MockExpressionEvaluator();
058            resolver = new MockVariableResolver();
059        }
060        
061        /**
062         * This method allows to set custom implementations
063         * of <code>JspWriter</code>. Per default, {@link MockJspWriter}
064         * is used.
065         * @param jspWriter the <code>JspWriter</code>
066         */
067        public void setJspWriter(JspWriter jspWriter)
068        {
069            this.jspWriter = jspWriter;
070        }
071        
072        public void setPage(Object page) 
073        {
074            this.page = page;
075        }
076        
077        public void setServletConfig(ServletConfig config)
078        {
079            this.config = config;
080        }
081        
082        public void setServletRequest(ServletRequest request)
083        {
084            this.request = request;
085        }
086        
087        public void setServletResponse(ServletResponse response)
088        {
089            this.response = response;
090        }
091        
092        public void setException(Exception exception) 
093        {
094            this.exception = exception;
095        }
096        
097        public Object findAttribute(String name) 
098        {
099            Object value = getAttribute(name, PageContext.PAGE_SCOPE);
100            if(value == null) 
101            {
102                value = getAttribute(name, PageContext.REQUEST_SCOPE);
103            }
104            if(value == null) 
105            {
106                value = getAttribute(name, PageContext.SESSION_SCOPE);
107            }
108            if(value == null) 
109            {
110                value = getAttribute(name, PageContext.APPLICATION_SCOPE);
111            }
112            return value;
113        }
114        
115        public Object getAttribute(String name) 
116        {
117            return getAttribute(name, PageContext.PAGE_SCOPE);
118        }
119    
120        public Object getAttribute(String name, int scope) 
121        {
122            if(scope == PageContext.PAGE_SCOPE) 
123            {
124                return attributes.get(name);
125            } 
126            else if(scope == PageContext.REQUEST_SCOPE) 
127            {
128                if(null == request) return null;
129                return request.getAttribute(name);
130            } 
131            else if(scope == PageContext.SESSION_SCOPE) 
132            {
133                if(null == getSession()) return null;
134                return getSession().getAttribute(name);
135            } 
136            else if(scope == PageContext.APPLICATION_SCOPE) 
137            {
138                if(null == getServletContext()) return null;
139                return getServletContext().getAttribute(name);
140            } 
141            else 
142            {
143                throw new IllegalArgumentException("Invalid scope " + scope);
144            }
145        }
146        
147        public void removeAttribute(String name) 
148        {
149            int scope = getAttributesScope(name);
150            if (scope != 0) 
151            {
152                removeAttribute(name, scope);
153            }
154        }
155    
156        public void removeAttribute(String name, int scope) 
157        {
158            if(scope == PageContext.PAGE_SCOPE) 
159            {
160                attributes.remove(name);
161            } 
162            else if(scope == PageContext.REQUEST_SCOPE) 
163            {
164                if(request != null) 
165                {
166                    request.removeAttribute(name);
167                }
168            } 
169            else if(scope == PageContext.SESSION_SCOPE) 
170            {
171                if(getSession() != null) 
172                {
173                    getSession().removeAttribute(name);
174                }
175            } 
176            else if(scope == PageContext.APPLICATION_SCOPE) 
177            {
178                if(getServletContext() != null) 
179                {
180                    getServletContext().removeAttribute(name);
181                }
182            } 
183            else 
184            {
185                throw new IllegalArgumentException("Invalid scope " + scope);
186            }
187        }
188        
189        public void setAttribute(String name, Object value) 
190        {
191            setAttribute(name, value, PageContext.PAGE_SCOPE);
192        }
193    
194    
195        public void setAttribute(String name, Object value, int scope) 
196        {
197            if(scope == PageContext.PAGE_SCOPE) 
198            {
199                attributes.put(name, value);
200            } 
201            else if(scope == PageContext.REQUEST_SCOPE) 
202            {
203                if(request != null) 
204                {
205                    request.setAttribute(name, value);
206                }
207            } 
208            else if(scope == PageContext.SESSION_SCOPE) 
209            {
210                if(getSession() != null) 
211                {
212                    getSession().setAttribute(name, value);
213                }
214            } 
215            else if(scope == PageContext.APPLICATION_SCOPE) 
216            {
217                if(getServletContext() != null) 
218                {
219                    getServletContext().setAttribute(name, value);
220                }
221            } 
222            else 
223            {
224                throw new IllegalArgumentException("Invalid scope " + scope);
225            }
226        }
227        
228        public int getAttributesScope(String name) 
229        {
230            if(getAttribute(name, PageContext.PAGE_SCOPE) != null) 
231            {
232                return PageContext.PAGE_SCOPE;
233            } 
234            else if(getAttribute(name, PageContext.REQUEST_SCOPE) != null) 
235            {
236                return PageContext.REQUEST_SCOPE;
237            } 
238            else if(getAttribute(name, PageContext.SESSION_SCOPE) != null)
239            {
240                return PageContext.SESSION_SCOPE;
241            } 
242            else if(getAttribute(name, PageContext.APPLICATION_SCOPE) != null) 
243            {
244                return PageContext.APPLICATION_SCOPE;
245            } 
246            return 0;
247        }
248        
249        public Enumeration getAttributeNamesInScope(int scope) 
250        {
251            if(scope == PageContext.PAGE_SCOPE) 
252            {
253                return new WrappedEnumeration(attributes.keySet().iterator());
254            } 
255            else if(scope == PageContext.REQUEST_SCOPE) 
256            {
257                if(request == null) return new NullEnumeration();
258                return request.getAttributeNames();
259            } 
260            else if(scope == PageContext.SESSION_SCOPE) 
261            {
262                if(getSession() == null) return new NullEnumeration();
263                return getSession().getAttributeNames();
264            } 
265            else if(scope == PageContext.APPLICATION_SCOPE) 
266            {
267                if(getServletContext() == null) return new NullEnumeration();
268                return getServletContext().getAttributeNames();
269            } 
270            else 
271            {
272                throw new IllegalArgumentException("Invalid scope " + scope);
273            }
274        }
275        
276        public JspWriter getOut()
277        {
278            return jspWriter;
279        }
280        
281        public Exception getException() 
282        {
283            return exception;
284        }
285        
286        public Object getPage() 
287        {
288            return page;
289        }
290    
291        public ServletRequest getRequest() 
292        {
293            return request;
294        }
295    
296        public ServletResponse getResponse() 
297        {
298            return response;
299        }
300    
301        public ServletConfig getServletConfig() 
302        {
303            return config;
304        }
305    
306        public ServletContext getServletContext() 
307        {
308            if(null == config) return null;
309            return config.getServletContext();
310        }
311    
312    
313        public HttpSession getSession() 
314        {
315            if(null == request) return null;
316            return ((HttpServletRequest)request).getSession();
317        }
318        
319        public void handlePageException(Exception exc) 
320        {
321            
322        }
323    
324        public void handlePageException(Throwable thr) 
325        {
326        
327        }
328        
329        public void forward(String path) throws ServletException, IOException
330        {
331            if(null != request)
332            {
333                RequestDispatcher dispatcher = request.getRequestDispatcher(path);
334                if(null != dispatcher)
335                {
336                    dispatcher.forward(request, response); 
337                }
338            }
339        }
340    
341        public void include(String path) throws ServletException, IOException
342        {
343            if(null != request)
344            {
345                RequestDispatcher dispatcher = request.getRequestDispatcher(path);
346                if(null != dispatcher)
347                {
348                    dispatcher.include(request, response); 
349                }
350            }
351        }
352        
353        public void include(String path, boolean flush) throws ServletException, IOException
354        {
355            if(flush)
356            {
357                jspWriter.flush();
358            }
359            include(path);
360        }
361    
362        public void initialize(Servlet servlet, ServletRequest request,
363                               ServletResponse response, String errorPageURL,
364                               boolean needsSession, int bufferSize,
365                               boolean autoFlush) 
366        {
367            this.config = servlet.getServletConfig();
368            this.request = request;
369            this.response = response;
370            jspWriter = new MockJspWriter();
371            outStack = new Stack();
372            attributes = new HashMap();
373        }
374    
375        public JspWriter popBody() 
376        {
377            jspWriter = (JspWriter)outStack.pop();
378            return jspWriter;
379        }
380        
381        public BodyContent pushBody() 
382        {
383            outStack.push(jspWriter);
384            jspWriter = new MockBodyContent(jspWriter);
385            return (BodyContent)jspWriter;
386        }
387        
388        public JspWriter pushBody(Writer writer)
389        {
390            outStack.push(jspWriter);
391            jspWriter = new MockJspWriter(writer);
392            return jspWriter;
393        }
394        
395        public void release() 
396        {
397            jspWriter = new MockJspWriter();
398            outStack = new Stack();
399        }
400        
401        /**
402         * Sets the expression evaluator. The default expression evaluator
403         * is {@link MockExpressionEvaluator}.
404         * @param evaluator the <code>ExpressionEvaluator</code>
405         */
406        public void setExpressionEvaluator(ExpressionEvaluator evaluator)
407        {
408            this.evaluator = evaluator;
409        }
410    
411        /**
412         * Sets the variable resolver. The default variable resolver
413         * is {@link MockVariableResolver}.
414         * @param resolver the <code>VariableResolver</code>
415         */
416        public void setVariableResolver(VariableResolver resolver)
417        {
418            this.resolver = resolver;
419        }
420        
421        public ExpressionEvaluator getExpressionEvaluator()
422        {
423            return evaluator;
424        }
425    
426        public VariableResolver getVariableResolver()
427        {
428            return resolver;
429        }
430        
431        private class NullEnumeration implements Enumeration 
432        {
433            public boolean hasMoreElements() 
434            {
435                return false;
436            }
437    
438            public Object nextElement() 
439            {
440                throw new NoSuchElementException();
441            }
442        }
443        
444        private class WrappedEnumeration implements Enumeration 
445        {
446            private Iterator iterator;
447            
448            public WrappedEnumeration(Iterator iterator) 
449            {
450                this.iterator = iterator;
451            }
452    
453            public boolean hasMoreElements() 
454            {
455                return iterator.hasNext();
456            }
457    
458            public Object nextElement() 
459            {
460                return iterator.next();
461            }
462        }
463    }