View Javadoc

1   /*
2    * $Id: StrutsMockServletContext.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.views.jsp;
23  
24  import java.io.InputStream;
25  import java.net.MalformedURLException;
26  import java.net.URL;
27  import java.util.Collections;
28  import java.util.Enumeration;
29  import java.util.HashMap;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import javax.servlet.RequestDispatcher;
34  import javax.servlet.Servlet;
35  import javax.servlet.ServletContext;
36  import javax.servlet.ServletException;
37  
38  
39  /***
40   * StrutsMockServletContext
41   *
42   */
43  public class StrutsMockServletContext implements ServletContext {
44  
45      String realPath;
46      String servletInfo;
47      String contextPath;
48      Map initParams = new HashMap();
49      Map attributes = new HashMap();
50      InputStream resourceAsStream;
51  
52      public void setInitParameter(String name, String value) {
53          initParams.put(name, value);
54      }
55  
56      public void setRealPath(String value) {
57          realPath = value;
58      }
59  
60      public String getRealPath(String string) {
61          return realPath;
62      }
63  
64      public ServletContext getContext(String s) {
65          return null;
66      }
67  
68      public int getMajorVersion() {
69          return 0;
70      }
71  
72      public int getMinorVersion() {
73          return 0;
74      }
75  
76      public String getMimeType(String s) {
77          return null;
78      }
79  
80      public Set getResourcePaths(String s) {
81          return null;
82      }
83  
84      public URL getResource(String s) throws MalformedURLException {
85          return null;
86      }
87  
88      public InputStream getResourceAsStream(String s) {
89          if (resourceAsStream != null) {
90              return resourceAsStream;
91          }
92          return null;
93      }
94  
95      public void setResourceAsStream(InputStream is) {
96          this.resourceAsStream = is;
97      }
98  
99      public RequestDispatcher getRequestDispatcher(String s) {
100         return null;
101     }
102 
103     public RequestDispatcher getNamedDispatcher(String s) {
104         return null;
105     }
106 
107     public Servlet getServlet(String s) throws ServletException {
108         return null;
109     }
110 
111     public Enumeration getServlets() {
112         return null;
113     }
114 
115     public Enumeration getServletNames() {
116         return null;
117     }
118 
119     public void log(String s) {
120     }
121 
122     public void log(Exception e, String s) {
123     }
124 
125     public void log(String s, Throwable throwable) {
126     }
127 
128     public String getServerInfo() {
129         return servletInfo;
130     }
131 
132     public String getInitParameter(String s) {
133         return (String) initParams.get(s);
134     }
135 
136     public Enumeration getInitParameterNames() {
137         return Collections.enumeration(initParams.keySet());
138     }
139 
140     public Object getAttribute(String s) {
141         return attributes.get(s);
142     }
143 
144     public Enumeration getAttributeNames() {
145         return Collections.enumeration(attributes.keySet());
146     }
147 
148     public void setAttribute(String s, Object o) {
149         attributes.put(s, o);
150     }
151 
152     public void removeAttribute(String s) {
153         attributes.remove(s);
154     }
155 
156     public String getServletContextName() {
157         return null;
158     }
159 
160     public void setServletInfo(String servletInfo) {
161         this.servletInfo = servletInfo;
162     }
163     
164     public String getContextPath() {
165         return contextPath;
166     } 
167 
168     public void setContextPath(String contextPath) {
169         this.contextPath = contextPath;
170     }
171 }