1 package test.crispy.mock;
2
3 import java.io.InputStream;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.Enumeration;
7 import java.util.HashSet;
8 import java.util.Hashtable;
9 import java.util.Properties;
10 import java.util.Set;
11
12 import javax.servlet.RequestDispatcher;
13 import javax.servlet.Servlet;
14 import javax.servlet.ServletContext;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 public class MockServletContext implements ServletContext {
20
21 protected static final Log log = LogFactory.getLog (MockServletContext.class);
22
23 private final String resourceBasePath = "";
24 private final Properties initParameters = new Properties();
25 private final Hashtable attributes = new Hashtable();
26 private String servletContextName = "MockServletContext";
27
28
29 public MockServletContext() {
30
31 }
32
33 protected String getResourceLocation(String path) {
34 if (!path.startsWith("/")) {
35 path = "/" + path;
36 }
37 return this.resourceBasePath + path;
38 }
39
40
41 public ServletContext getContext(String name) {
42 throw new UnsupportedOperationException("getContext " + name);
43 }
44
45 public int getMajorVersion() {
46 return 2;
47 }
48
49 public int getMinorVersion() {
50 return 4;
51 }
52
53 public String getMimeType(String filePath) {
54 throw new UnsupportedOperationException("getMimeType " + filePath);
55 }
56
57 public Set getResourcePaths(String path) {
58 return new HashSet();
59 }
60
61 public URL getResource(String path) throws MalformedURLException {
62 URL lvUrl = new URL("http://localhost");
63 return lvUrl;
64 }
65
66 public InputStream getResourceAsStream(String path) {
67 return null;
68 }
69
70 public RequestDispatcher getRequestDispatcher(String path) {
71 return null;
72 }
73
74 public RequestDispatcher getNamedDispatcher(String path) {
75 throw new UnsupportedOperationException("getNamedDispatcher " + path);
76 }
77
78 public Servlet getServlet(String name) {
79 throw new UnsupportedOperationException("getServlet " + name);
80 }
81
82 public Enumeration getServlets() {
83 throw new UnsupportedOperationException("getServlets");
84 }
85
86 public Enumeration getServletNames() {
87 throw new UnsupportedOperationException("getServletNames");
88 }
89
90 public void log(String message) {
91 log.info(message);
92 }
93
94 public void log(Exception e, String message) {
95 log.info(message, e);
96 }
97
98 public void log(String message, Throwable t) {
99 log.info(message, t);
100 }
101
102 public String getRealPath(String path) {
103 return "/";
104 }
105
106 public String getServerInfo() {
107 return "MockServletContext";
108 }
109
110 public String getInitParameter(String name) {
111 return this.initParameters.getProperty(name);
112 }
113
114 public void addInitParameter(String name, String value) {
115 this.initParameters.setProperty(name, value);
116 }
117
118 public Enumeration getInitParameterNames() {
119 return this.initParameters.keys();
120 }
121
122 public Object getAttribute(String name) {
123 return this.attributes.get(name);
124 }
125
126 public Enumeration getAttributeNames() {
127 return this.attributes.keys();
128 }
129
130 public void setAttribute(String name, Object value) {
131 if (value != null) {
132 this.attributes.put(name, value);
133 }
134 else {
135 this.attributes.remove(name);
136 }
137 }
138
139 public void removeAttribute(String name) {
140 this.attributes.remove(name);
141 }
142
143 public void setServletContextName(String servletContextName) {
144 this.servletContextName = servletContextName;
145 }
146
147 public String getServletContextName() {
148 return servletContextName; }
149
150 }