001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.chain.web.jakarta.servlet; 018 019import java.io.InputStream; 020import java.net.MalformedURLException; 021import java.net.URL; 022import java.util.Enumeration; 023import java.util.EventListener; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.Set; 027 028import org.apache.commons.chain.web.MockEnumeration; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032import jakarta.servlet.Filter; 033import jakarta.servlet.FilterRegistration; 034import jakarta.servlet.RequestDispatcher; 035import jakarta.servlet.Servlet; 036import jakarta.servlet.ServletContext; 037import jakarta.servlet.ServletException; 038import jakarta.servlet.ServletRegistration; 039import jakarta.servlet.ServletRegistration.Dynamic; 040import jakarta.servlet.SessionCookieConfig; 041import jakarta.servlet.SessionTrackingMode; 042import jakarta.servlet.descriptor.JspConfigDescriptor; 043 044/** 045 * Mock Object for {@code ServletContext} (Version 2.3) 046 */ 047public class MockServletContext implements ServletContext { 048 private Logger logger = LoggerFactory.getLogger(MockServletContext.class); 049 private Map<String, Object> attributes = new HashMap<>(); 050 private Map<String, String> parameters = new HashMap<>(); 051 052 // ----------------------------------------------------------- Constructors 053 054 /** 055 * The Default-Constructor for this class. 056 */ 057 public MockServletContext() { 058 } 059 060 // --------------------------------------------------------- Public Methods 061 062 public void addInitParameter(String name, String value) { 063 parameters.put(name, value); 064 } 065 066 // ------------------------------------------------- ServletContext Methods 067 068 @Override 069 public Object getAttribute(String name) { 070 return attributes.get(name); 071 } 072 073 @Override 074 public Enumeration<String> getAttributeNames() { 075 return new MockEnumeration<>(attributes.keySet().iterator()); 076 } 077 078 @Override 079 public ServletContext getContext(String uripath) { 080 throw new UnsupportedOperationException(); 081 } 082 083 @Override 084 public String getInitParameter(String name) { 085 return parameters.get(name); 086 } 087 088 @Override 089 public Enumeration<String> getInitParameterNames() { 090 return new MockEnumeration<>(parameters.keySet().iterator()); 091 } 092 093 @Override 094 public int getMajorVersion() { 095 return 4; 096 } 097 098 @Override 099 public String getMimeType(String path) { 100 throw new UnsupportedOperationException(); 101 } 102 103 @Override 104 public int getMinorVersion() { 105 return 0; 106 } 107 108 @Override 109 public RequestDispatcher getNamedDispatcher(String name) { 110 throw new UnsupportedOperationException(); 111 } 112 113 @Override 114 public String getRealPath(String path) { 115 throw new UnsupportedOperationException(); 116 } 117 118 @Override 119 public RequestDispatcher getRequestDispatcher(String path) { 120 throw new UnsupportedOperationException(); 121 } 122 123 @Override 124 public URL getResource(String path) throws MalformedURLException { 125 throw new UnsupportedOperationException(); 126 } 127 128 @Override 129 public InputStream getResourceAsStream(String path) { 130 throw new UnsupportedOperationException(); 131 } 132 133 @Override 134 public Set<String> getResourcePaths(String path) { 135 throw new UnsupportedOperationException(); 136 } 137 138 @Deprecated 139 @Override 140 public Servlet getServlet(String name) throws ServletException { 141 throw new UnsupportedOperationException(); 142 } 143 144 @Override 145 public String getServletContextName() { 146 return "MockServletContext"; 147 } 148 149 @Override 150 public String getServerInfo() { 151 return "MockServletContext"; 152 } 153 154 @Deprecated 155 @Override 156 public Enumeration<Servlet> getServlets() { 157 throw new UnsupportedOperationException(); 158 } 159 160 @Deprecated 161 @Override 162 public Enumeration<String> getServletNames() { 163 throw new UnsupportedOperationException(); 164 } 165 166 @Override 167 public void log(String message) { 168 logger.info(message); 169 } 170 171 @Deprecated 172 @Override 173 public void log(Exception exception, String message) { 174 logger.error(message, exception); 175 } 176 177 @Override 178 public void log(String message, Throwable exception) { 179 logger.error(message, exception); 180 } 181 182 @Override 183 public void removeAttribute(String name) { 184 attributes.remove(name); 185 } 186 187 @Override 188 public void setAttribute(String name, Object value) { 189 attributes.put(name, value); 190 } 191 192 @Override 193 public String getContextPath() { 194 throw new UnsupportedOperationException(); 195 } 196 197 @Override 198 public int getEffectiveMajorVersion() { 199 return getMajorVersion(); 200 } 201 202 @Override 203 public int getEffectiveMinorVersion() { 204 return getMinorVersion(); 205 } 206 207 @Override 208 public boolean setInitParameter(String name, String value) { 209 throw new UnsupportedOperationException(); 210 } 211 212 @Override 213 public Dynamic addServlet(String servletName, String className) { 214 throw new UnsupportedOperationException(); 215 } 216 217 @Override 218 public Dynamic addServlet(String servletName, Servlet servlet) { 219 throw new UnsupportedOperationException(); 220 } 221 222 @Override 223 public Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) { 224 throw new UnsupportedOperationException(); 225 } 226 227 @Override 228 public Dynamic addJspFile(String servletName, String jspFile) { 229 throw new UnsupportedOperationException(); 230 } 231 232 @Override 233 public <T extends Servlet> T createServlet(Class<T> clazz) throws ServletException { 234 throw new UnsupportedOperationException(); 235 } 236 237 @Override 238 public ServletRegistration getServletRegistration(String servletName) { 239 throw new UnsupportedOperationException(); 240 } 241 242 @Override 243 public Map<String, ? extends ServletRegistration> getServletRegistrations() { 244 throw new UnsupportedOperationException(); 245 } 246 247 @Override 248 public FilterRegistration.Dynamic addFilter(String filterName, String className) { 249 throw new UnsupportedOperationException(); 250 } 251 252 @Override 253 public FilterRegistration.Dynamic addFilter(String filterName, Filter filter) { 254 throw new UnsupportedOperationException(); 255 } 256 257 @Override 258 public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) { 259 throw new UnsupportedOperationException(); 260 } 261 262 @Override 263 public <T extends Filter> T createFilter(Class<T> clazz) throws ServletException { 264 throw new UnsupportedOperationException(); 265 } 266 267 @Override 268 public FilterRegistration getFilterRegistration(String filterName) { 269 throw new UnsupportedOperationException(); 270 } 271 272 @Override 273 public Map<String, ? extends FilterRegistration> getFilterRegistrations() { 274 throw new UnsupportedOperationException(); 275 } 276 277 @Override 278 public SessionCookieConfig getSessionCookieConfig() { 279 throw new UnsupportedOperationException(); 280 } 281 282 @Override 283 public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) { 284 throw new UnsupportedOperationException(); 285 } 286 287 @Override 288 public Set<SessionTrackingMode> getDefaultSessionTrackingModes() { 289 throw new UnsupportedOperationException(); 290 } 291 292 @Override 293 public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() { 294 throw new UnsupportedOperationException(); 295 } 296 297 @Override 298 public void addListener(String className) { 299 throw new UnsupportedOperationException(); 300 } 301 302 @Override 303 public <T extends EventListener> void addListener(T t) { 304 throw new UnsupportedOperationException(); 305 } 306 307 @Override 308 public void addListener(Class<? extends EventListener> listenerClass) { 309 throw new UnsupportedOperationException(); 310 } 311 312 @Override 313 public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException { 314 throw new UnsupportedOperationException(); 315 } 316 317 @Override 318 public JspConfigDescriptor getJspConfigDescriptor() { 319 throw new UnsupportedOperationException(); 320 } 321 322 @Override 323 public ClassLoader getClassLoader() { 324 return MockServletContext.class.getClassLoader(); 325 } 326 327 @Override 328 public void declareRoles(String... roleNames) { 329 throw new UnsupportedOperationException(); 330 } 331 332 @Override 333 public String getVirtualServerName() { 334 throw new UnsupportedOperationException(); 335 } 336 337 @Override 338 public int getSessionTimeout() { 339 throw new UnsupportedOperationException(); 340 } 341 342 @Override 343 public void setSessionTimeout(int sessionTimeout) { 344 throw new UnsupportedOperationException(); 345 } 346 347 @Override 348 public String getRequestCharacterEncoding() { 349 throw new UnsupportedOperationException(); 350 } 351 352 @Override 353 public void setRequestCharacterEncoding(String encoding) { 354 throw new UnsupportedOperationException(); 355 } 356 357 @Override 358 public String getResponseCharacterEncoding() { 359 throw new UnsupportedOperationException(); 360 } 361 362 @Override 363 public void setResponseCharacterEncoding(String encoding) { 364 throw new UnsupportedOperationException(); 365 } 366}