1 5 6 13 package org.exoplatform.test.mocks.servlet; 14 import javax.servlet.ServletContext ; 15 import javax.servlet.RequestDispatcher ; 16 import javax.servlet.Servlet ; 17 import javax.servlet.ServletException ; 18 import java.util.*; 19 import java.net.URL ; 20 import java.net.MalformedURLException ; 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 24 public class MockServletContext implements ServletContext { 25 26 private String name_ ; 27 private HashMap initParams_ ; 28 private HashMap attributes_ ; 29 private String contextPath_ ; 30 private StringBuffer logBuffer = new StringBuffer (); 31 32 public MockServletContext() { 33 this("portlet_app_1"); 34 } 35 36 public MockServletContext(String name) { 37 name_ = name; 38 initParams_ = new HashMap() ; 39 attributes_ = new HashMap(); 40 } 41 42 public MockServletContext(String name, String path) { 43 this(name); 44 contextPath_ = path; 45 attributes_.put("javax.servlet.context.tempdir", path); 46 } 47 48 public void setName(String name){ 49 name_ = name; 50 } 51 52 public String getLogBuffer() { 53 try { 54 return logBuffer.toString(); 55 } finally { 56 logBuffer = new StringBuffer (); 57 } 58 } 59 60 public ServletContext getContext(String s) { 61 return null; 62 } 63 64 public int getMajorVersion() { 65 return 2; 66 } 67 68 public int getMinorVersion() { 69 return 3; 70 } 71 72 public String getMimeType(String s) { 73 return "text/html"; 74 } 75 76 public Set getResourcePaths(String s) { 77 Set set = new HashSet(); 78 set.add("/test1"); 79 set.add("/WEB-INF"); 80 set.add("/test2"); 81 return set; 82 } 83 84 public URL getResource(String s) throws MalformedURLException { 85 String path = "file:" + contextPath_ + s ; 86 URL url = new URL (path) ; 87 return url ; 88 } 89 90 public InputStream getResourceAsStream(String s) { 91 try { 92 return getResource(s).openStream(); 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 return null; 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 logBuffer.append(s); 121 } 122 123 public void log(Exception e, String s) { 124 logBuffer.append(s + e.getMessage()); 125 } 126 127 public void log(String s, Throwable throwable) { 128 logBuffer.append(s + throwable.getMessage()); 129 } 130 131 public void setContextPath(String s) { 132 contextPath_ = s ; 133 } 134 135 public String getRealPath(String s) { 136 return contextPath_ + s ; 137 } 138 139 public String getServerInfo() { 140 return null; 141 } 142 143 public void setInitParameter(String name, String value) { 144 initParams_.put(name, value) ; 145 } 146 147 public String getInitParameter(String name) { 148 return (String ) initParams_.get(name) ; 149 } 150 151 public Enumeration getInitParameterNames() { 152 Vector keys = new Vector(initParams_.keySet()) ; 153 return keys.elements() ; 154 } 155 156 public Object getAttribute(String name) { 157 return attributes_.get(name) ; 158 } 159 160 public Enumeration getAttributeNames() { 161 Vector keys = new Vector(attributes_.keySet()) ; 162 return keys.elements() ; 163 } 164 165 public void setAttribute(String name, Object value) { 166 attributes_.put(name, value) ; 167 } 168 169 public void removeAttribute(String name) { 170 attributes_.remove(name) ; 171 } 172 173 public String getServletContextName() { 174 return name_ ; 175 } 176 } 177 | Popular Tags |