craigmcc    2002/07/01 15:10:36

  Added:       src/test/org/apache/struts/mock MockEnumeration.java
                        MockHttpServletRequest.java
                        MockHttpServletResponse.java MockHttpSession.java
                        MockPrincipal.java MockServletContext.java
  Log:
  Here are some initial mock objects for JUnit unit tests of things like
  RequestUtils methods when you don't need the full round-trip support
  provided by Cactus.
  
  I haven't actually used them in tests yet (that's next), wo beware of
  bugs I haven't found yet.
  
  Revision  Changes    Path
  1.1                  
jakarta-struts/src/test/org/apache/struts/mock/MockEnumeration.java
  
  Index: MockEnumeration.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockEnumeration.java,v 1.1 
2002/07/01 22:10:35 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/01 22:10:35 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.struts.mock;
  
  
  import java.util.Enumeration;
  import java.util.Iterator;
  
  
  /**
   * <p>General purpose <code>Enumeration</code> wrapper around an
   * <code>Iterator</code> specified to our controller.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/07/01 22:10:35 $
   */
  
  public class MockEnumeration implements Enumeration {
  
  
      public MockEnumeration(Iterator iterator) {
          this.iterator = iterator;
      }
  
  
      protected Iterator iterator;
  
  
      public boolean hasMoreElements() {
          return (iterator.hasNext());
      }
  
  
      public Object nextElement() {
          return (iterator.next());
      }
  
  
  }
  
  
  
  1.1                  
jakarta-struts/src/test/org/apache/struts/mock/MockHttpServletRequest.java
  
  Index: MockHttpServletRequest.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockHttpServletRequest.java,v 
1.1 2002/07/01 22:10:35 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/01 22:10:35 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.struts.mock;
  
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.security.Principal;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.Locale;
  import java.util.Map;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletContext;
  import javax.servlet.ServletInputStream;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  
  
  
  /**
   * <p>Mock <strong>HttpServletRequest</strong> object for low-level unit tests
   * of Struts controller components.  Coarser grained tests should be
   * implemented in terms of the Cactus framework, instead of the mock
   * object classes.</p>
   *
   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
   * create unit tests is provided, plus additional methods to configure this
   * object as necessary.  Methods for unsupported operations will throw
   * <code>UnsupportedOperationException</code>.</p>
   *
   * <p><strong>WARNING</strong> - Because unit tests operate in a single
   * threaded environment, no synchronization is performed.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/07/01 22:10:35 $
   */
  
  public class MockHttpServletRequest implements HttpServletRequest {
  
  
  
      // ----------------------------------------------------------- Constructors
  
  
      public MockHttpServletRequest() {
          super();
      }
  
  
      public MockHttpServletRequest(HttpSession session) {
          super();
          setHttpSession(session);
      }
  
  
      public MockHttpServletRequest(String contextPath, String servletPath,
                                    String pathInfo, String queryString) {
          super();
          setPathElements(contextPath, servletPath, pathInfo, queryString);
      }
  
  
  
      public MockHttpServletRequest(String contextPath, String servletPath,
                                    String pathInfo, String queryString,
                                    HttpSession session) {
          super();
          setPathElements(contextPath, servletPath, pathInfo, queryString);
          setHttpSession(session);
      }
  
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The set of request attributes.
       */
      protected HashMap attributes = new HashMap();
  
  
      /**
       * The context path for this request.
       */
      protected String contextPath = null;
  
  
      /**
       * The preferred locale for this request.
       */
      protected Locale locale = null;
  
  
      /**
       * The set of arrays of parameter values, keyed by parameter name.
       */
      protected HashMap parameters = new HashMap();
  
  
      /**
       * The extra path information for this request.
       */
      protected String pathInfo = null;
  
  
      /**
       * The authenticated user for this request.
       */
      protected Principal principal = null;
  
  
      /**
       * The query string for this request.
       */
      protected String queryString = null;
  
  
      /**
       * The servlet path for this request.
       */
      protected String servletPath = null;
  
  
      /**
       * The HttpSession with which we are associated.
       */
      protected HttpSession session = null;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      public void addParameter(String name, String value) {
          String values[] = (String[]) parameters.get(name);
          if (values == null) {
              String results[] = new String[] { value };
              parameters.put(name, results);
              return;
          }
          String results[] = new String[values.length + 1];
          System.arraycopy(values, 0, results, 0, values.length);
          results[values.length] = value;
          parameters.put(name, results);
      }
  
  
      public void setHttpSession(HttpSession session) {
          this.session = session;
      }
  
  
      public void setLocale(Locale locale) {
          this.locale = locale;
      }
  
  
      public void setPathElements(String contextPath, String servletPath,
                                  String pathInfo, String queryString) {
  
          this.contextPath = contextPath;
          this.servletPath = servletPath;
          this.pathInfo = pathInfo;
          this.queryString = queryString;
  
      }
  
  
      public void setUserPrincipal(Principal principal) {
          this.principal = principal;
      }
  
  
  
      // --------------------------------------------- HttpServletRequest Methods
  
  
      public String getAuthType() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getContextPath() {
          return (contextPath);
      }
  
  
      public Cookie[] getCookies() {
          throw new UnsupportedOperationException();
      }
  
  
      public long getDateHeader(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public String getHeader(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public Enumeration getHeaderNames() {
          throw new UnsupportedOperationException();
      }
  
  
      public Enumeration getHeaders(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public int getIntHeader(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public String getMethod() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getPathInfo() {
          return (pathInfo);
      }
  
  
      public String getPathTranslated() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getQueryString() {
          return (queryString);
      }
  
  
      public String getRemoteUser() {
          if (principal != null) {
              return (principal.getName());
          } else {
              return (null);
          }
      }
  
  
      public String getRequestedSessionId() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getRequestURI() {
          StringBuffer sb = new StringBuffer();
          if (contextPath != null) {
              sb.append(contextPath);
          }
          if (servletPath != null) {
              sb.append(servletPath);
          }
          if (pathInfo != null) {
              sb.append(pathInfo);
          }
          if (sb.length() > 0) {
              return (sb.toString());
          }
          throw new UnsupportedOperationException();
      }
  
  
      public StringBuffer getRequestURL() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getServletPath() {
          return (servletPath);
      }
  
  
      public HttpSession getSession() {
          return (getSession(true));
      }
  
  
      public HttpSession getSession(boolean create) {
          if (create && (session == null)) {
              throw new UnsupportedOperationException();
          }
          return (session);
      }
  
  
      public Principal getUserPrincipal() {
          return (principal);
      }
  
  
      public boolean isRequestedSessionIdFromCookie() {
          throw new UnsupportedOperationException();
      }
  
  
      public boolean isRequestedSessionIdFromUrl() {
          throw new UnsupportedOperationException();
      }
  
  
      public boolean isRequestedSessionIdFromURL() {
          throw new UnsupportedOperationException();
      }
  
  
      public boolean isRequestedSessionIdValid() {
          throw new UnsupportedOperationException();
      }
  
  
      public boolean isUserInRole(String role) {
          if ((principal != null) && (principal instanceof MockPrincipal)) {
              return (((MockPrincipal) principal).isUserInRole(role));
          } else {
              return (false);
          }
      }
  
  
      // ------------------------------------------------- ServletRequest Methods
  
  
      public Object getAttribute(String name) {
          return (attributes.get(name));
      }
  
  
      public Enumeration getAttributeNames() {
          return (new MockEnumeration(attributes.keySet().iterator()));
      }
  
  
      public String getCharacterEncoding() {
          throw new UnsupportedOperationException();
      }
  
  
      public int getContentLength() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getContentType() {
          throw new UnsupportedOperationException();
      }
  
  
      public ServletInputStream getInputStream() {
          throw new UnsupportedOperationException();
      }
  
  
      public Locale getLocale() {
          return (locale);
      }
  
  
      public Enumeration getLocales() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getParameter(String name) {
          String values[] = (String[]) parameters.get(name);
          if (values != null) {
              return (values[0]);
          } else {
              return (null);
          }
      }
  
  
      public Map getParameterMap() {
          return (parameters);
      }
  
  
      public Enumeration getParameterNames() {
          return (new MockEnumeration(parameters.keySet().iterator()));
      }
  
  
      public String[] getParameterValues(String name) {
          return ((String[]) parameters.get(name));
      }
  
  
      public String getProtocol() {
          throw new UnsupportedOperationException();
      }
  
  
      public BufferedReader getReader() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getRealPath(String path) {
          throw new UnsupportedOperationException();
      }
  
  
      public String getRemoteAddr() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getRemoteHost() {
          throw new UnsupportedOperationException();
      }
  
  
      public RequestDispatcher getRequestDispatcher(String path) {
          throw new UnsupportedOperationException();
      }
  
  
      public String getScheme() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getServerName() {
          throw new UnsupportedOperationException();
      }
  
  
      public int getServerPort() {
          throw new UnsupportedOperationException();
      }
  
  
      public boolean isSecure() {
          throw new UnsupportedOperationException();
      }
  
  
      public void removeAttribute(String name) {
          attributes.remove(name);
      }
  
  
      public void setAttribute(String name, Object value) {
          if (value == null) {
              attributes.remove(name);
          } else {
              attributes.put(name, value);
          }
      }
  
  
      public void setCharacterEncoding(String name) {
          throw new UnsupportedOperationException();
      }
  
  
  }
  
  
  
  1.1                  
jakarta-struts/src/test/org/apache/struts/mock/MockHttpServletResponse.java
  
  Index: MockHttpServletResponse.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockHttpServletResponse.java,v
 1.1 2002/07/01 22:10:35 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/01 22:10:35 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.struts.mock;
  
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Locale;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServletResponse;
  
  
  
  /**
   * <p>Mock <strong>HttpServletResponse</strong> object for low-level unit tests
   * of Struts controller components.  Coarser grained tests should be
   * implemented in terms of the Cactus framework, instead of the mock
   * object classes.</p>
   *
   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
   * create unit tests is provided, plus additional methods to configure this
   * object as necessary.  Methods for unsupported operations will throw
   * <code>UnsupportedOperationException</code>.</p>
   *
   * <p><strong>WARNING</strong> - Because unit tests operate in a single
   * threaded environment, no synchronization is performed.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/07/01 22:10:35 $
   */
  
  public class MockHttpServletResponse implements HttpServletResponse {
  
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      // --------------------------------------------------------- Public Methods
  
  
  
  
      // -------------------------------------------- HttpServletResponse Methods
  
  
      public void addCookie(Cookie cookie) {
          throw new UnsupportedOperationException();
      }
  
  
      public void addDateHeader(String name, long value) {
          throw new UnsupportedOperationException();
      }
  
  
      public void addHeader(String name, String value) {
          throw new UnsupportedOperationException();
      }
  
  
      public void addIntHeader(String name, int value) {
          throw new UnsupportedOperationException();
      }
  
  
      public boolean containsHeader(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public String encodeRedirectUrl(String url) {
          throw new UnsupportedOperationException();
      }
  
  
      public String encodeRedirectURL(String url) {
          throw new UnsupportedOperationException();
      }
  
  
      public String encodeUrl(String url) {
          throw new UnsupportedOperationException();
      }
  
  
      public String encodeURL(String url) {
          throw new UnsupportedOperationException();
      }
  
  
      public void sendError(int status) {
          throw new UnsupportedOperationException();
      }
  
  
      public void sendError(int status, String message) {
          throw new UnsupportedOperationException();
      }
  
  
      public void sendRedirect(String location) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setDateHeader(String name, long value) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setHeader(String name, String value) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setIntHeader(String name, int value) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setStatus(int status) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setStatus(int status, String message) {
          throw new UnsupportedOperationException();
      }
  
  
      // ------------------------------------------------ ServletResponse Methods
  
  
      public void flushBuffer() {
          throw new UnsupportedOperationException();
      }
  
  
      public int getBufferSize() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getCharacterEncoding() {
          throw new UnsupportedOperationException();
      }
  
  
      public Locale getLocale() {
          throw new UnsupportedOperationException();
      }
  
  
      public ServletOutputStream getOutputStream() throws IOException {
          throw new UnsupportedOperationException();
      }
  
  
      public PrintWriter getWriter() throws IOException {
          throw new UnsupportedOperationException();
      }
  
  
      public boolean isCommitted() {
          throw new UnsupportedOperationException();
      }
  
  
      public void reset() {
          throw new UnsupportedOperationException();
      }
  
  
      public void resetBuffer() {
          throw new UnsupportedOperationException();
      }
  
  
      public void setBufferSize(int size) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setContentLength(int length) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setContentType(String type) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setLocale(Locale locale) {
          throw new UnsupportedOperationException();
      }
  
  
  }
  
  
  
  1.1                  
jakarta-struts/src/test/org/apache/struts/mock/MockHttpSession.java
  
  Index: MockHttpSession.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockHttpSession.java,v 1.1 
2002/07/01 22:10:35 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/01 22:10:35 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.struts.mock;
  
  
  import java.util.Enumeration;
  import java.util.HashMap;
  import javax.servlet.ServletContext;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpSessionContext;
  
  
  
  /**
   * <p>Mock <strong>HttpSession</strong> object for low-level unit tests
   * of Struts controller components.  Coarser grained tests should be
   * implemented in terms of the Cactus framework, instead of the mock
   * object classes.</p>
   *
   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
   * create unit tests is provided, plus additional methods to configure this
   * object as necessary.  Methods for unsupported operations will throw
   * <code>UnsupportedOperationException</code>.</p>
   *
   * <p><strong>WARNING</strong> - Because unit tests operate in a single
   * threaded environment, no synchronization is performed.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/07/01 22:10:35 $
   */
  
  public class MockHttpSession implements HttpSession {
  
  
  
      // ----------------------------------------------------------- Constructors
  
  
      public MockHttpSession() {
          super();
      }
  
  
      public MockHttpSession(ServletContext servletContext) {
          super();
          setServletContext(servletContext);
      }
  
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The set of session attributes.
       */
      protected HashMap attributes = new HashMap();
  
  
      /**
       * The ServletContext with which we are associated.
       */
      protected ServletContext servletContext = null;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      public void setServletContext(ServletContext servletContext) {
          this.servletContext = servletContext;
      }
  
  
      // ---------------------------------------------------- HttpSession Methods
  
  
      public Object getAttribute(String name) {
          return (attributes.get(name));
      }
  
  
      public Enumeration getAttributeNames() {
          return (new MockEnumeration(attributes.keySet().iterator()));
      }
  
  
      public long getCreationTime() {
          throw new UnsupportedOperationException();
      }
  
  
      public String getId() {
          throw new UnsupportedOperationException();
      }
  
  
      public long getLastAccessedTime() {
          throw new UnsupportedOperationException();
      }
  
  
      public int getMaxInactiveInterval() {
          throw new UnsupportedOperationException();
      }
  
  
      public ServletContext getServletContext() {
          return (this.servletContext);
      }
  
  
      public HttpSessionContext getSessionContext() {
          throw new UnsupportedOperationException();
      }
  
  
      public Object getValue(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public String[] getValueNames() {
          throw new UnsupportedOperationException();
      }
  
  
      public void invalidate() {
          throw new UnsupportedOperationException();
      }
  
  
      public boolean isNew() {
          throw new UnsupportedOperationException();
      }
  
  
      public void putValue(String name, Object value) {
          throw new UnsupportedOperationException();
      }
  
  
      public void removeAttribute(String name) {
          attributes.remove(name);
      }
  
  
      public void removeValue(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public void setAttribute(String name, Object value) {
          if (value == null) {
              attributes.remove(name);
          } else {
              attributes.put(name, value);
          }
      }
  
  
      public void setMaxInactiveInterval(int interval) {
          throw new UnsupportedOperationException();
      }
  
  
  }
  
  
  
  1.1                  
jakarta-struts/src/test/org/apache/struts/mock/MockPrincipal.java
  
  Index: MockPrincipal.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockPrincipal.java,v 1.1 
2002/07/01 22:10:35 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/01 22:10:35 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.struts.mock;
  
  
  import java.security.Principal;
  
  
  /**
   * <p>Mock <strong>Principal</strong> object for low-level unit tests
   * of Struts controller components.  Coarser grained tests should be
   * implemented in terms of the Cactus framework, instead of the mock
   * object classes.</p>
   *
   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
   * create unit tests is provided, plus additional methods to configure this
   * object as necessary.  Methods for unsupported operations will throw
   * <code>UnsupportedOperationException</code>.</p>
   *
   * <p><strong>WARNING</strong> - Because unit tests operate in a single
   * threaded environment, no synchronization is performed.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/07/01 22:10:35 $
   */
  
  public class MockPrincipal implements Principal {
  
  
      public MockPrincipal(String name) {
          super();
          this.name = name;
          this.roles = new String[0];
      }
  
  
      public MockPrincipal(String name, String roles[]) {
          super();
          this.name = name;
          this.roles = roles;
      }
  
  
      protected String name = null;
  
  
      protected String roles[] = null;
  
  
      public String getName() {
          return (this.name);
      }
  
  
      public boolean isUserInRole(String role) {
          for (int i = 0; i < roles.length; i++) {
              if (role.equals(roles[i])) {
                  return (true);
              }
          }
          return (false);
      }
  
  
  }
  
  
  
  1.1                  
jakarta-struts/src/test/org/apache/struts/mock/MockServletContext.java
  
  Index: MockServletContext.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockServletContext.java,v 1.1 
2002/07/01 22:10:35 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/01 22:10:35 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.struts.mock;
  
  
  import java.io.InputStream;
  import java.net.URL;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.Set;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.Servlet;
  import javax.servlet.ServletContext;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  
  /**
   * <p>Mock <strong>ServletContext</strong> object for low-level unit tests
   * of Struts controller components.  Coarser grained tests should be
   * implemented in terms of the Cactus framework, instead of the mock
   * object classes.</p>
   *
   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
   * create unit tests is provided, plus additional methods to configure this
   * object as necessary.  Methods for unsupported operations will throw
   * <code>UnsupportedOperationException</code>.</p>
   *
   * <p><strong>WARNING</strong> - Because unit tests operate in a single
   * threaded environment, no synchronization is performed.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/07/01 22:10:35 $
   */
  
  public class MockServletContext implements ServletContext {
  
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The set of servlet context attributes.
       */
      protected HashMap attributes = new HashMap();
  
  
      /**
       * Default destination for <code>log()</code> output.
       */
      protected Log log = LogFactory.getLog(MockServletContext.class);
  
  
      /**
       * The set of context initialization parameters.
       */
      protected HashMap parameters = new HashMap();
  
  
      // --------------------------------------------------------- Public Methods
  
  
      public void addInitParameter(String name, String value) {
          parameters.put(name, value);
      }
  
  
      public void setLog(Log log) {
          this.log = log;
      }
  
  
  
      // ------------------------------------------------- ServletContext Methods
  
  
      public Object getAttribute(String name) {
          return (attributes.get(name));
      }
  
  
      public Enumeration getAttributeNames() {
          return (new MockEnumeration(attributes.keySet().iterator()));
      }
  
  
      public ServletContext getContext(String uripath) {
          throw new UnsupportedOperationException();
      }
  
  
      public String getInitParameter(String name) {
          return ((String) parameters.get(name));
      }
  
  
      public Enumeration getInitParameterNames() {
          return (new MockEnumeration(parameters.keySet().iterator()));
      }
  
  
      public int getMajorVersion() {
          return (2);
      }
  
  
      public String getMimeType(String file) {
          throw new UnsupportedOperationException();
      }
  
  
      public int getMinorVersion() {
          return (3);
      }
  
  
      public RequestDispatcher getNamedDispatcher(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public String getRealPath(String path) {
          throw new UnsupportedOperationException();
      }
  
  
      public RequestDispatcher getRequestDispatcher(String path) {
          throw new UnsupportedOperationException();
      }
  
  
      public URL getResource(String path) {
          throw new UnsupportedOperationException();
      }
  
  
      public InputStream getResourceAsStream(String path) {
          throw new UnsupportedOperationException();
      }
  
  
      public Set getResourcePaths(String path) {
          throw new UnsupportedOperationException();
      }
  
  
      public String getServerInfo() {
          return ("MockServletContext/$Version$");
      }
  
  
      public Servlet getServlet(String name) {
          throw new UnsupportedOperationException();
      }
  
  
      public String getServletContextName() {
          return (getServerInfo());
      }
  
  
      public Enumeration getServletNames() {
          throw new UnsupportedOperationException();
      }
  
  
      public Enumeration getServlets() {
          throw new UnsupportedOperationException();
      }
  
  
      public void log(Exception exception, String message) {
          log(message, exception);
      }
  
  
      public void log(String message) {
          log.info(message);
      }
  
  
      public void log(String message, Throwable throwable) {
          log.error(message, throwable);
      }
  
  
      public void removeAttribute(String name) {
          attributes.remove(name);
      }
  
  
      public void setAttribute(String name, Object value) {
          if (value == null) {
              attributes.remove(name);
          } else {
              attributes.put(name, value);
          }
      }
  
  
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to