1 7 package com.inversoft.junit.internal.http; 8 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileNotFoundException ; 13 import java.io.InputStream ; 14 import java.net.URL ; 15 import java.net.MalformedURLException ; 16 import java.util.Enumeration ; 17 import java.util.Iterator ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.util.Set ; 21 import javax.servlet.RequestDispatcher ; 22 import javax.servlet.Servlet ; 23 import javax.servlet.ServletContext ; 24 25 26 33 public class MockServletContext implements ServletContext { 34 35 private Map attributes; 36 private Map initParameters; 37 38 39 42 public MockServletContext() { 43 attributes = new HashMap (); 44 initParameters = new HashMap (); 45 } 46 47 48 54 public Object getAttribute(String name) { 55 return attributes.get(name); 56 } 57 58 63 public Enumeration getAttributeNames(){ 64 final Iterator iter = attributes.keySet().iterator(); 65 return new Enumeration () { 66 public boolean hasMoreElements() { 67 return iter.hasNext(); 68 } 69 public java.lang.Object nextElement() { 70 return iter.next(); 71 } 72 }; 73 } 74 75 78 public ServletContext getContext(String uri) { 79 throw new UnsupportedOperationException (); 80 } 81 82 88 public String getInitParameter(String name) { 89 return (String ) initParameters.get(name); 90 } 91 92 94 public Enumeration getInitParameterNames() { 95 final Iterator iter = initParameters.keySet().iterator(); 96 return new Enumeration () { 97 public boolean hasMoreElements() { 98 return iter.hasNext(); 99 } 100 public java.lang.Object nextElement() { 101 return iter.next(); 102 } 103 }; 104 } 105 106 108 public int getMajorVersion() { 109 return 2; 110 } 111 112 114 public String getMimeType(String file) { 115 return null; 116 } 117 118 120 public int getMinorVersion() { 121 return 3; 122 } 123 124 126 public RequestDispatcher getNamedDispatcher(String uri) { 127 return getRequestDispatcher(uri); 128 } 129 130 132 public String getRealPath(String path) { 133 File file = new File (path); 134 return file.getAbsolutePath(); 135 } 136 137 139 public RequestDispatcher getRequestDispatcher(String uri) { 140 return new MockRequestDispatcher(uri); 141 } 142 143 149 public URL getResource(String path) { 150 File file = new File (convertPath(path)); 151 152 if (file.exists() && file.isFile()) { 153 try { 154 return file.toURL(); 155 } catch (MalformedURLException murle) { 156 return null; 157 } 158 } 159 160 return null; 161 } 162 163 169 public InputStream getResourceAsStream(String path) { 170 File file = new File (convertPath(path)); 171 172 if (file.exists() && file.isFile()) { 173 try { 174 return new FileInputStream (file); 175 } catch (FileNotFoundException fnfe) { 176 return null; 177 } 178 } 179 180 return null; 181 } 182 183 185 public Set getResourcePaths(String path) { 186 throw new UnsupportedOperationException (); 187 } 188 189 191 public String getServerInfo(){ 192 return "local"; 193 } 194 195 198 public Servlet getServlet(String name) { 199 throw new UnsupportedOperationException (); 200 } 201 202 204 public String getServletContextName() { 205 return "local"; 206 } 207 208 211 public Enumeration getServletNames() { 212 throw new UnsupportedOperationException (); 213 } 214 215 218 public Enumeration getServlets() { 219 throw new UnsupportedOperationException (); 220 } 221 222 225 public void log(Exception exception, String string) { 226 throw new UnsupportedOperationException (); 227 } 228 229 231 public void log(String msg) { 232 System.out.println(msg); 233 } 234 235 237 public void log(String msg, Throwable t) { 238 System.out.println(msg); 239 t.printStackTrace(System.out); 240 } 241 242 244 public void removeAttribute(String name) { 245 attributes.remove(name); 246 } 247 248 250 public void setAttribute(String name, Object value) { 251 attributes.put(name, value); 252 } 253 254 255 259 260 263 public void setInitParameter(String name, String value) { 264 initParameters.put(name, value); 265 } 266 267 275 public String convertPath(String path) { 276 277 if (File.separatorChar == '\\') { 278 return path.replace('/', '\\'); 279 } 280 281 return path.replace('\\', '/'); 282 } 283 } 284 | Popular Tags |