All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.injection.ServletDependencies Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG. The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested in isolation from selected dependencies.

There is a newer version: 1.49
Show newest version
/*
 * Copyright (c) 2006 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.injection;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.annotation.*;
import javax.servlet.*;
import javax.servlet.descriptor.*;
import javax.servlet.http.*;
import static java.util.Collections.enumeration;

/**
 * Detects and resolves dependencies belonging to the {@code javax.servlet} API, namely {@code ServletContext}
 * and {@code HttpSession}.
 */
final class ServletDependencies
{
   static boolean isApplicable(@Nonnull Class dependencyType)
   {
      return dependencyType == HttpSession.class || dependencyType == ServletContext.class;
   }

   @Nonnull private final InjectionState injectionState;

   ServletDependencies(@Nonnull InjectionState injectionState) { this.injectionState = injectionState; }

   @Nonnull
   Object createAndRegisterDependency(@Nonnull Class dependencyType)
   {
      if (dependencyType == ServletContext.class) {
         return createAndRegisterServletContext();
      }

      return createAndRegisterHttpSession();
   }

   @Nonnull
   private ServletContext createAndRegisterServletContext()
   {
      ServletContext context = new ServletContext() {
         private final Map init = new HashMap();
         private final Map attrs = new HashMap();

         @Override public String getContextPath() { return ""; }
         @Override public ServletContext getContext(String uriPath) { return null; }
         @Override public int getMajorVersion() { return 3; }
         @Override public int getMinorVersion() { return 0; }
         @Override public int getEffectiveMajorVersion() { return 3; }
         @Override public int getEffectiveMinorVersion() { return 0; }
         @Override public String getMimeType(String file) { return null; }
         @Override public String getRealPath(String path) { return null; }
         @Override public Set getResourcePaths(String path) { return null; }
         @Override public URL getResource(String path) { return getClass().getResource(path); }
         @Override public InputStream getResourceAsStream(String path) { return getClass().getResourceAsStream(path); }
         @Override public RequestDispatcher getRequestDispatcher(String path) { return null; }
         @Override public RequestDispatcher getNamedDispatcher(String name) { return null; }
         @Override public String getServletContextName() { return null; }
         @Override public String getServerInfo() { return "JMockit 1.x"; }
         @Override public ClassLoader getClassLoader() { return getClass().getClassLoader(); }

         // Deprecated/logging methods: do nothing.
         @Override public Servlet getServlet(String name) { return null; }
         @Override public Enumeration getServlets() { return null; }
         @Override public Enumeration getServletNames() { return null; }
         @Override public void log(String msg) {}
         @Override public void log(Exception exception, String msg) { }
         @Override public void log(String message, Throwable throwable) {}

         // Context initialization parameters.
         @Override public Enumeration getInitParameterNames() { return enumeration(init.keySet()); }
         @Override public String getInitParameter(String name) { return init.get(name); }
         @Override public boolean setInitParameter(String name, String value) { return init.put(name, value) == null; }

         // Context attributes.
         @Override public Enumeration getAttributeNames() { return enumeration(attrs.keySet()); }
         @Override public Object getAttribute(String name) { return attrs.get(name); }
         @Override public void setAttribute(String name, Object value) { attrs.put(name, value); }
         @Override public void removeAttribute(String name) { attrs.remove(name); }

         // Un-implemented methods, which may get a non-empty implementation eventually.
         @Override public ServletRegistration.Dynamic addServlet(String name, String className) { return null; }
         @Override public ServletRegistration.Dynamic addServlet(String name, Servlet servlet) { return null; }
         @Override public ServletRegistration.Dynamic addServlet(String nm, Class c) { return null; }
         @Override public  T createServlet(Class clazz) { return null; }
         @Override public ServletRegistration getServletRegistration(String servletName) { return null; }
         @Override public Map getServletRegistrations() { return null; }
         @Override public FilterRegistration.Dynamic addFilter(String name, String className) { return null; }
         @Override public FilterRegistration.Dynamic addFilter(String name, Filter filter) { return null; }
         @Override public FilterRegistration.Dynamic addFilter(String name, Class cl) { return null; }
         @Override public  T createFilter(Class clazz) { return null; }
         @Override public FilterRegistration getFilterRegistration(String filterName) { return null; }
         @Override public Map getFilterRegistrations() { return null; }
         @Override public SessionCookieConfig getSessionCookieConfig() { return null; }
         @Override public void setSessionTrackingModes(Set sessionTrackingModes) {}
         @Override public Set getDefaultSessionTrackingModes() { return null; }
         @Override public Set getEffectiveSessionTrackingModes() { return null; }
         @Override public void addListener(String className) {}
         @Override public  void addListener(T t) {}
         @Override public void addListener(Class listenerClass) {}
         @Override public  T createListener(Class clazz) { return null; }
         @Override public JspConfigDescriptor getJspConfigDescriptor() { return null; }
         @Override public void declareRoles(String... roleNames) {}
         @Override public String getVirtualServerName() { return null; }
      };

      InjectionPoint injectionPoint = new InjectionPoint(ServletContext.class);
      injectionState.saveGlobalDependency(injectionPoint, context);
      return context;
   }

   @Nonnull
   private HttpSession createAndRegisterHttpSession()
   {
      HttpSession session = new HttpSession() {
         private final String id = String.valueOf(Math.abs(new Random().nextInt()));
         private final long creationTime = System.currentTimeMillis();
         private final Map attrs = new HashMap();
         private int maxInactiveInterval;
         private boolean invalidated;

         @Override public String getId() { return id; }

         @Override public int getMaxInactiveInterval() { return maxInactiveInterval; }
         @Override public void setMaxInactiveInterval(int interval) { maxInactiveInterval = interval; }

         @Override public long getCreationTime() { checkValid(); return creationTime; }
         @Override public long getLastAccessedTime() { checkValid(); return creationTime; }
         @Override public boolean isNew() { checkValid(); return false; }

         @Override public Enumeration getAttributeNames() { checkValid(); return enumeration(attrs.keySet()); }
         @Override public Object getAttribute(String name) { checkValid(); return attrs.get(name); }
         @Override public void setAttribute(String name, Object value) { checkValid(); attrs.put(name, value); }
         @Override public void removeAttribute(String name) { checkValid(); attrs.remove(name); }

         @Override
         public void invalidate()
         {
            checkValid();
            attrs.clear();
            invalidated = true;
         }

         private void checkValid()
         {
            if (invalidated) {
               throw new IllegalStateException("Session is invalid");
            }
         }

         @Override
         public ServletContext getServletContext()
         {
            ServletContext context = injectionState.getGlobalDependency(new InjectionPoint(ServletContext.class));

            if (context == null) {
               context = createAndRegisterServletContext();
            }

            return context;
         }

         // Deprecated methods: do nothing.
         @Override public Object getValue(String name) { return null; }
         @Override public void putValue(String name, Object value) {}
         @Override public void removeValue(String name) {}
         @Override public String[] getValueNames() { return null; }
         @SuppressWarnings("deprecation") @Override public HttpSessionContext getSessionContext() { return null; }
      };

      InjectionPoint injectionPoint = new InjectionPoint(HttpSession.class);
      injectionState.saveInstantiatedDependency(injectionPoint, session);
      return session;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy