1 15 package org.apache.tapestry.test.mock; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.Collections ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import javax.servlet.RequestDispatcher ; 29 import javax.servlet.Servlet ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletException ; 32 33 39 40 public class MockContext extends AttributeHolder implements ServletContext , InitParameterHolder 41 { 42 private MockSession _session; 43 44 private static final Map _suffixToContentType = new HashMap (); 45 46 static { 47 _suffixToContentType.put("html", "text/html"); 48 _suffixToContentType.put("gif", "image/gif"); 49 _suffixToContentType.put("png", "image/png"); 50 } 51 52 private String _rootDirectory; 53 private String _servletContextName = "test"; 54 private Map _initParameters = new HashMap (); 55 56 public MockContext() 57 { 58 } 59 60 public MockContext(String testDirectory) 61 { 62 _rootDirectory = testDirectory + "/context"; 63 } 64 65 public ServletContext getContext(String name) 66 { 67 return null; 68 } 69 70 public int getMajorVersion() 71 { 72 return 2; 73 } 74 75 public int getMinorVersion() 76 { 77 return 1; 78 } 79 80 public String getMimeType(String path) 81 { 82 int lastx = path.lastIndexOf('.'); 83 String suffix = path.substring(lastx + 1); 84 85 return (String ) _suffixToContentType.get(suffix); 86 } 87 88 public Set getResourcePaths(String arg0) 89 { 90 return null; 91 } 92 93 public URL getResource(String path) throws MalformedURLException 94 { 95 if (path == null || !path.startsWith("/")) 96 throw new MalformedURLException ("Not a valid context path."); 97 98 String fullPath = _rootDirectory + path; 99 100 File file = new File (fullPath); 101 102 if (file.exists()) 103 return file.toURL(); 104 105 return null; 106 } 107 108 public InputStream getResourceAsStream(String path) 109 { 110 try 111 { 112 URL url = getResource(path); 113 114 if (url == null) 115 return null; 116 117 return url.openStream(); 118 } 119 catch (MalformedURLException ex) 120 { 121 return null; 122 } 123 catch (IOException ex) 124 { 125 return null; 126 } 127 } 128 129 135 136 public RequestDispatcher getRequestDispatcher(String path) 137 { 138 if (path.endsWith("/NULL")) 139 return null; 140 141 StringBuffer buffer = new StringBuffer (_rootDirectory); 142 buffer.append(path); 143 144 147 if (path.endsWith("/")) 148 buffer.append("index.html"); 149 150 return new MockRequestDispatcher(buffer.toString()); 151 } 152 153 public RequestDispatcher getNamedDispatcher(String name) 154 { 155 return null; 156 } 157 158 public Servlet getServlet(String name) throws ServletException 159 { 160 return null; 161 } 162 163 public Enumeration getServlets() 164 { 165 return null; 166 } 167 168 public Enumeration getServletNames() 169 { 170 return null; 171 } 172 173 public void log(String message) 174 { 175 log(message, null); 176 } 177 178 public void log(Exception exception, String message) 179 { 180 log(message, exception); 181 } 182 183 public void log(String message, Throwable exception) 184 { 185 } 186 187 public String getRealPath(String arg0) 188 { 189 return null; 190 } 191 192 public String getServerInfo() 193 { 194 return "Tapestry Mock Objects"; 195 } 196 197 public String getInitParameter(String name) 198 { 199 return (String ) _initParameters.get(name); 200 } 201 202 public Enumeration getInitParameterNames() 203 { 204 return Collections.enumeration(_initParameters.keySet()); 205 } 206 207 public void setInitParameter(String name, String value) 208 { 209 _initParameters.put(name, value); 210 } 211 212 public String getServletContextName() 213 { 214 return _servletContextName; 215 } 216 217 public MockSession createSession() 218 { 219 if (_session == null) 220 { 221 String id = Long.toHexString(System.currentTimeMillis()); 222 223 _session = new MockSession(this, id); 224 } 225 226 return _session; 227 } 228 229 public MockSession getSession() 230 { 231 return _session; 232 } 233 234 public void setServletContextName(String servletContextName) 235 { 236 _servletContextName = servletContextName; 237 } 238 239 public String getRootDirectory() 240 { 241 return _rootDirectory; 242 } 243 244 public void setRootDirectory(String rootDirectory) 245 { 246 _rootDirectory = rootDirectory; 247 } 248 249 } 250 | Popular Tags |