1 package com.mockobjects.servlet; 2 3 import com.mockobjects.*; 4 5 import javax.servlet.RequestDispatcher ; 6 import javax.servlet.Servlet ; 7 import javax.servlet.ServletContext ; 8 import java.io.InputStream ; 9 import java.net.URL ; 10 import java.util.Enumeration ; 11 import java.util.HashMap ; 12 import java.util.Set ; 13 14 public class MockServletContext extends MockObject implements ServletContext { 15 private ReturnObjectBag returnAttributes = new ReturnObjectBag("attributes"); 16 private Set resourcePaths; 17 private ReturnObjectList realPaths = new ReturnObjectList("real path"); 18 private URL resource; 19 private HashMap initParameters = new HashMap (); 20 private ExpectationValue expectedLogValue = new ExpectationValue("log"); 21 private ExpectationValue expectedLogThrowable = 22 new ExpectationValue("log throwable"); 23 private ExpectationValue requestDispatcherURI = 24 new ExpectationValue("RequestDispatcher URI"); 25 private RequestDispatcher requestDispatcher; 26 private ExpectationSet attributes = new ExpectationSet("attributes"); 27 28 public Enumeration getServlets() { 29 return null; 30 } 31 32 public void log(String string) { 33 expectedLogValue.setActual(string); 34 } 35 36 public void setExpectedLog(String string) { 37 expectedLogValue.setExpected(string); 38 } 39 40 public void setupGetResource(URL resource) { 41 this.resource = resource; 42 } 43 44 public URL getResource(String string) { 45 return resource; 46 } 47 48 public void setupGetResourcePaths(Set resourcePaths) { 49 this.resourcePaths = resourcePaths; 50 } 51 52 public Set getResourcePaths(String string) { 53 return resourcePaths; 54 } 55 56 public ServletContext getContext(String string) { 57 return null; 58 } 59 60 public int getMinorVersion() { 61 return -1; 62 } 63 64 public void removeAttribute(String string) { 65 } 66 67 public void log(String string, Throwable t) { 68 log(string); 69 expectedLogThrowable.setActual(t); 70 } 71 72 public void setExpectedLogThrowable(Throwable throwable) { 73 expectedLogThrowable.setExpected(throwable); 74 } 75 76 public void addRealPath(String realPath) { 77 this.realPaths.addObjectToReturn(realPath); 78 } 79 80 public String getRealPath(String string) { 81 return realPaths.nextReturnObject().toString(); 82 } 83 84 public Enumeration getServletNames() { 85 return null; 86 } 87 88 public Servlet getServlet(String string) { 89 return null; 90 } 91 92 public void log(Exception exception, String string) { 93 } 94 95 public String getServerInfo() { 96 return null; 97 } 98 99 public void setExpectedRequestDispatcherURI(String uri) { 100 this.requestDispatcherURI.setExpected(uri); 101 } 102 103 public void setupGetRequestDispatcher( 104 RequestDispatcher requestDispatcher) { 105 106 this.requestDispatcher = requestDispatcher; 107 } 108 109 public RequestDispatcher getRequestDispatcher(String uri) { 110 requestDispatcherURI.setActual(uri); 111 return requestDispatcher; 112 } 113 114 public int getMajorVersion() { 115 return -1; 116 } 117 118 public Set getResourcePaths() { 119 return null; 120 } 121 122 public void setupGetAttribute(String string, Object object) { 123 returnAttributes.putObjectToReturn(string, object); 124 } 125 126 public String getMimeType(String string) { 127 return null; 128 } 129 130 public RequestDispatcher getNamedDispatcher(String string) { 131 return null; 132 } 133 134 public String getInitParameter(String paramName) { 135 return (String ) initParameters.get(paramName); 136 } 137 138 public void setInitParameter(String paramName, String paramValue) { 139 initParameters.put(paramName, paramValue); 140 } 141 142 public Object getAttribute(String string) { 143 return returnAttributes.getNextReturnObject(string); 144 } 145 146 public Enumeration getAttributeNames() { 147 return null; 148 } 149 150 public String getServletContextName() { 151 return null; 152 } 153 154 public InputStream getResourceAsStream(String string) { 155 return null; 156 } 157 158 public Enumeration getInitParameterNames() { 159 return null; 160 } 161 162 public void addExpectedAttribute(String key, Object value) { 163 attributes.addExpected(new MapEntry(key, value)); 164 } 165 166 public void setAttribute(String key, Object value) { 167 attributes.addActual(new MapEntry(key, value)); 168 } 169 } 170 | Popular Tags |