1 13 package com.tonbeller.wcf.controller; 14 15 import java.util.Enumeration ; 16 import java.util.HashMap ; 17 import java.util.Locale ; 18 import java.util.Map ; 19 import java.util.Vector ; 20 21 import javax.servlet.ServletContext ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.http.HttpSession ; 25 import javax.servlet.http.HttpSessionBindingEvent ; 26 import javax.servlet.http.HttpSessionBindingListener ; 27 28 import org.apache.commons.fileupload.FileItem; 29 30 import com.tonbeller.tbutils.res.Resources; 31 import com.tonbeller.wcf.convert.Converter; 32 import com.tonbeller.wcf.convert.ConverterFactory; 33 import com.tonbeller.wcf.expr.ExprContext; 34 import com.tonbeller.wcf.expr.ExprUtils; 35 import com.tonbeller.wcf.format.Formatter; 36 import com.tonbeller.wcf.format.FormatterFactory; 37 38 43 public class TestContext extends RequestContext { 44 45 Formatter formatter = FormatterFactory.instance(getLocale()); 46 Converter converter = ConverterFactory.instance(formatter); 47 ExprContext exprContext; 48 Map parameters = new HashMap (); 49 HttpSession session = new TestSession(); 50 51 54 public HttpServletRequest getRequest() { 55 return null; 56 } 57 58 61 public HttpServletResponse getResponse() { 62 return null; 63 } 64 65 68 public ServletContext getServletContext() { 69 return null; 70 } 71 72 75 public HttpSession getSession() { 76 return session; 77 } 78 79 82 public Converter getConverter() { 83 return converter; 84 } 85 86 89 public Formatter getFormatter() { 90 return formatter; 91 } 92 93 public Map getParameters() { 94 return parameters; 95 } 96 97 public void setParameters(Map map) { 98 this.parameters = map; 99 } 100 101 public String [] getParameters(String name) { 102 return (String [])parameters.get(name); 103 } 104 105 public String getParameter(String name) { 106 String [] values = getParameters(name); 107 if (values != null && values.length > 0) 108 return values[0]; 109 return null; 110 } 111 112 public Locale getLocale() { 113 return Locale.US; 114 } 115 116 120 public ExprContext getExprContext() { 121 return exprContext; 122 } 123 124 128 public void setExprContext(ExprContext exprContext) { 129 this.exprContext = exprContext; 130 } 131 132 135 public Object getModelReference(String expr) { 136 return ExprUtils.getModelReference(exprContext, expr); 137 } 138 139 142 public void setModelReference(String expr, Object value) { 143 ExprUtils.setModelReference(exprContext, expr, value); 144 } 145 146 public boolean isUserInRole(String roleExpr) { 147 return false; 148 } 149 150 public Resources getResources() { 151 return Resources.instance(getLocale()); 152 } 153 154 public Resources getResources(String bundleName) { 155 return Resources.instance(getLocale(), bundleName); 156 } 157 158 public Resources getResources(Class clasz) { 159 return Resources.instance(getLocale(), clasz); 160 } 161 162 public String getRemoteUser() { 163 return "guest"; 164 } 165 166 public String getRemoteDomain() { 167 return null; 168 } 169 170 public boolean isAdmin() { 171 return false; 172 } 173 public class TestSession implements HttpSession { 174 HashMap attrs = new HashMap (); 175 176 public long getCreationTime() { 177 return 0; 178 } 179 180 public String getId() { 181 return "testSessionID"; 182 } 183 184 public long getLastAccessedTime() { 185 return 0; 186 } 187 188 public ServletContext getServletContext() { 189 return null; 190 } 191 192 public void setMaxInactiveInterval(int arg0) { 193 } 194 195 public int getMaxInactiveInterval() { 196 return 0; 197 } 198 199 200 public javax.servlet.http.HttpSessionContext getSessionContext() { 201 return null; 202 } 203 204 public Object getAttribute(String id) { 205 return attrs.get(id); 206 } 207 208 209 public Object getValue(String id) { 210 return getAttribute(id); 211 } 212 213 public Enumeration getAttributeNames() { 214 Vector v = new Vector (); 215 v.addAll(attrs.keySet()); 216 return v.elements(); 217 } 218 219 220 public String [] getValueNames() { 221 return (String []) attrs.keySet().toArray(new String [0]); 222 } 223 224 public void setAttribute(String id, Object att) { 225 removeAttribute(id); 226 attrs.put(id, att); 227 if (att instanceof HttpSessionBindingListener ) { 228 HttpSessionBindingEvent e = new HttpSessionBindingEvent (this, id); 229 ((HttpSessionBindingListener )att).valueBound(e); 230 } 231 } 232 233 234 public void putValue(String id, Object attr) { 235 setAttribute(id, attr); 236 } 237 238 public void removeAttribute(String id) { 239 Object attr = attrs.get(id); 240 if (attr instanceof HttpSessionBindingListener ) { 241 HttpSessionBindingEvent e = new HttpSessionBindingEvent (this, id); 242 ((HttpSessionBindingListener )attr).valueUnbound(e); 243 } 244 } 245 246 247 public void removeValue(String id) { 248 removeAttribute(id); 249 } 250 251 public void invalidate() { 252 } 253 254 public boolean isNew() { 255 return false; 256 } 257 } 258 259 public Object findBean(String name) { 260 return exprContext.findBean(name); 261 } 262 263 public void setBean(String name, Object bean) { 264 exprContext.setBean(name, bean); 265 } 266 267 public void setLocale(Locale locale) { 268 } 269 270 public FileItem getFileItem(String name) { 271 return null; 272 } 273 274 public Map getFileParameters() { 275 return null; 276 } 277 } 278 | Popular Tags |