1 18 19 20 package org.apache.struts.mock; 21 22 23 import java.io.InputStream ; 24 import java.net.URL ; 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import javax.servlet.RequestDispatcher ; 29 import javax.servlet.Servlet ; 30 import javax.servlet.ServletContext ; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 35 51 52 public class MockServletContext implements ServletContext { 53 54 55 56 58 59 62 protected HashMap attributes = new HashMap (); 63 64 65 68 protected Log log = LogFactory.getLog(MockServletContext.class); 69 70 71 74 protected HashMap parameters = new HashMap (); 75 76 77 79 80 public void addInitParameter(String name, String value) { 81 parameters.put(name, value); 82 } 83 84 85 public void setLog(Log log) { 86 this.log = log; 87 } 88 89 90 91 93 94 public Object getAttribute(String name) { 95 return (attributes.get(name)); 96 } 97 98 99 public Enumeration getAttributeNames() { 100 return (new MockEnumeration(attributes.keySet().iterator())); 101 } 102 103 104 public ServletContext getContext(String uripath) { 105 throw new UnsupportedOperationException (); 106 } 107 108 109 public String getInitParameter(String name) { 110 return ((String ) parameters.get(name)); 111 } 112 113 114 public Enumeration getInitParameterNames() { 115 return (new MockEnumeration(parameters.keySet().iterator())); 116 } 117 118 119 public int getMajorVersion() { 120 return (2); 121 } 122 123 124 public String getMimeType(String file) { 125 throw new UnsupportedOperationException (); 126 } 127 128 129 public int getMinorVersion() { 130 return (3); 131 } 132 133 134 public RequestDispatcher getNamedDispatcher(String name) { 135 throw new UnsupportedOperationException (); 136 } 137 138 139 public String getRealPath(String path) { 140 throw new UnsupportedOperationException (); 141 } 142 143 144 public RequestDispatcher getRequestDispatcher(String path) { 145 throw new UnsupportedOperationException (); 146 } 147 148 149 public URL getResource(String path) { 150 return this.getClass().getResource(path); 151 } 153 154 155 public InputStream getResourceAsStream(String path) { 156 return this.getClass().getResourceAsStream(path); 157 } 159 160 161 public Set getResourcePaths(String path) { 162 throw new UnsupportedOperationException (); 163 } 164 165 166 public String getServerInfo() { 167 return ("MockServletContext/$Version$"); 168 } 169 170 171 public Servlet getServlet(String name) { 172 throw new UnsupportedOperationException (); 173 } 174 175 176 public String getServletContextName() { 177 return (getServerInfo()); 178 } 179 180 181 public Enumeration getServletNames() { 182 throw new UnsupportedOperationException (); 183 } 184 185 186 public Enumeration getServlets() { 187 throw new UnsupportedOperationException (); 188 } 189 190 191 public void log(Exception exception, String message) { 192 log(message, exception); 193 } 194 195 196 public void log(String message) { 197 log.info(message); 198 } 199 200 201 public void log(String message, Throwable throwable) { 202 log.error(message, throwable); 203 } 204 205 206 public void removeAttribute(String name) { 207 attributes.remove(name); 208 } 209 210 211 public void setAttribute(String name, Object value) { 212 if (value == null) { 213 attributes.remove(name); 214 } else { 215 attributes.put(name, value); 216 } 217 } 218 219 220 221 } 222 | Popular Tags |