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

com.phloc.web.mock.MockServletConfig Maven / Gradle / Ivy

/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phloc.web.mock;

import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;

import com.phloc.commons.ValueEnforcer;
import com.phloc.commons.annotations.Nonempty;
import com.phloc.commons.collections.ContainerHelper;
import com.phloc.commons.state.EChange;
import com.phloc.commons.string.ToStringGenerator;

/**
 * Mock implementation of the {@link ServletConfig} interface.
 * 
 * @author Philip Helger
 */
@NotThreadSafe
public class MockServletConfig implements ServletConfig
{
  private final ServletContext m_aSC;
  private final String m_sServletName;
  private final Map  m_aServletInitParams = new LinkedHashMap  ();

  /**
   * Constructor without servlet init parameters.
   * 
   * @param aSC
   *        Base servlet context. May not be null.
   * @param sServletName
   *        Name of the servlet. May neither be null nor empty.
   */
  public MockServletConfig (@Nonnull final ServletContext aSC, @Nonnull @Nonempty final String sServletName)
  {
    this (aSC, sServletName, null);
  }

  /**
   * Constructor
   * 
   * @param aSC
   *        Base servlet context. May not be null.
   * @param sServletName
   *        Name of the servlet. May neither be null nor empty.
   * @param aServletInitParams
   *        The map with all servlet init parameters. May be null
   *        or empty.
   */
  public MockServletConfig (@Nonnull final ServletContext aSC,
                            @Nonnull @Nonempty final String sServletName,
                            @Nullable final Map  aServletInitParams)
  {
    m_aSC = ValueEnforcer.notNull (aSC, "ServletContext");
    m_sServletName = ValueEnforcer.notEmpty (sServletName, "ServletName");
    if (aServletInitParams != null)
      m_aServletInitParams.putAll (aServletInitParams);
  }

  @Nonnull
  @Nonempty
  public String getServletName ()
  {
    return m_sServletName;
  }

  @Nonnull
  public ServletContext getServletContext ()
  {
    return m_aSC;
  }

  @Nullable
  public String getInitParameter (@Nullable final String sName)
  {
    return m_aServletInitParams.get (sName);
  }

  @Nonnull
  public Enumeration  getInitParameterNames ()
  {
    return ContainerHelper.getEnumeration (m_aServletInitParams.keySet ());
  }

  public void addInitParameter (@Nonnull @Nonempty final String sName, @Nonnull final String sValue)
  {
    ValueEnforcer.notEmpty (sName, "Name");
    ValueEnforcer.notNull (sValue, "Value");
    m_aServletInitParams.put (sName, sValue);
  }

  @Nonnull
  public EChange removeInitParameter (@Nullable final String sName)
  {
    return EChange.valueOf (m_aServletInitParams.remove (sName) != null);
  }

  @Nonnull
  @Nonempty
  public Map  getAllInitParameters ()
  {
    return ContainerHelper.newMap (m_aServletInitParams);
  }

  @Override
  public String toString ()
  {
    return new ToStringGenerator (this).append ("servletContext", m_aSC)
                                       .append ("servletName", m_sServletName)
                                       .append ("servletInitParams", m_aServletInitParams)
                                       .toString ();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy