struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject svn commit: r365272 - in /struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock: MockHttpServletRequest.java MockHttpServletResponse.java MockServletContext.java
Date Mon, 02 Jan 2006 01:34:27 GMT
Author: craigmcc
Date: Sun Jan  1 17:34:24 2006
New Revision: 365272

URL: http://svn.apache.org/viewcvs?rev=365272&view=rev
Log:
Support HTTP headers on request and response mock objects, update copyright
dates on modified files.

Modified:
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletRequest.java
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletResponse.java
    struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletRequest.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletRequest.java?rev=365272&r1=365271&r2=365272&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletRequest.java
(original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletRequest.java
Sun Jan  1 17:34:24 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,10 +18,18 @@
 
 import java.io.BufferedReader;
 import java.security.Principal;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.TimeZone;
+import java.util.Vector;
 
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletInputStream;
@@ -81,6 +89,27 @@
     // ----------------------------------------------------- Mock Object Methods
 
 
+    public void addDateHeader(String name, long value) {
+
+        headers.add(name + ": " + formatDate(value));
+
+    }
+
+
+    public void addHeader(String name, String value) {
+
+        headers.add(name + ": " + value);
+
+    }
+
+
+    public void addIntHeader(String name, int value) {
+
+        headers.add(name + ": " + value);
+
+    }
+
+
     public void addParameter(String name, String value) {
 
         String values[] = (String[]) parameters.get(name);
@@ -134,6 +163,7 @@
 
     private HashMap attributes = new HashMap();
     private String contextPath = null;
+    private List headers = new ArrayList();
     private Locale locale = null;
     private HashMap parameters = new HashMap();
     private String pathInfo = null;
@@ -169,35 +199,80 @@
 
     public long getDateHeader(String name) {
 
-        throw new UnsupportedOperationException();
+        String match = name + ":";
+        Iterator headers = this.headers.iterator();
+        while (headers.hasNext()) {
+            String header = (String) headers.next();
+            if (header.startsWith(match)) {
+                return parseDate(header.substring(match.length() + 1).trim());
+            }
+        }
+        return (long) -1;
 
     }
 
 
     public String getHeader(String name) {
 
-        throw new UnsupportedOperationException();
+        String match = name + ":";
+        Iterator headers = this.headers.iterator();
+        while (headers.hasNext()) {
+            String header = (String) headers.next();
+            if (header.startsWith(match)) {
+                return header.substring(match.length() + 1).trim();
+            }
+        }
+        return null;
 
     }
 
 
     public Enumeration getHeaderNames() {
 
-        throw new UnsupportedOperationException();
+        Vector values = new Vector();
+        Iterator headers = this.headers.iterator();
+        while (headers.hasNext()) {
+            String header = (String) headers.next();
+            int colon = header.indexOf(':');
+            if (colon >= 0) {
+                String name = header.substring(0, colon).trim();
+                if (!values.contains(name)) {
+                    values.add(name);
+                }
+            }
+        }
+        return values.elements();
 
     }
 
 
     public Enumeration getHeaders(String name) {
 
-        throw new UnsupportedOperationException();
+        String match = name + ":";
+        Vector values = new Vector();
+        Iterator headers = this.headers.iterator();
+        while (headers.hasNext()) {
+            String header = (String) headers.next();
+            if (header.startsWith(match)) {
+                values.add(header.substring(match.length() + 1).trim());
+            }
+        }
+        return values.elements();
 
     }
 
 
     public int getIntHeader(String name) {
 
-        throw new UnsupportedOperationException();
+        String match = name + ":";
+        Iterator headers = this.headers.iterator();
+        while (headers.hasNext()) {
+            String header = (String) headers.next();
+            if (header.startsWith(match)) {
+                return Integer.parseInt(header.substring(match.length() + 1).trim());
+            }
+        }
+        return -1;
 
     }
 
@@ -553,6 +628,45 @@
 
         throw new UnsupportedOperationException();
 
+    }
+
+
+    // --------------------------------------------------------- Support Methods
+
+
+    /**
+     * <p>The date formatting helper we will use in <code>httpTimestamp()</code>.
+     * Note that usage of this helper must be synchronized.</p>
+     */
+    private static SimpleDateFormat format =
+            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
+    static {
+        format.setTimeZone(TimeZone.getTimeZone("GMT"));
+    }
+
+
+    /**
+     * <p>Return a properly formatted String version of the specified
+     * date/time, formatted as required by the HTTP specification
+     *
+     * @param date Date/time, expressed as milliseconds since the epoch
+     */
+    private String formatDate(long date) {
+        return format.format(new Date(date));
+    }
+
+
+    /**
+     * <p>Return a date/time value, parsed from the specified String.</p>
+     *
+     * @param date Date/time, expressed as a String
+     */
+    private long parseDate(String date) {
+        try {
+            return format.parse(date).getTime();
+        } catch (ParseException e) {
+            throw new IllegalArgumentException(date);
+        }
     }
 
 

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletResponse.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletResponse.java?rev=365272&r1=365271&r2=365272&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletResponse.java
(original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockHttpServletResponse.java
Sun Jan  1 17:34:24 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,7 +20,13 @@
 import java.io.CharArrayWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
+import java.util.TimeZone;
 
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.Cookie;
@@ -48,6 +54,41 @@
 
 
     /**
+     * <p>Retrieve the first value that was set for the specified header,
+     * if any.  Otherwise, return <code>null</code>.</p>
+     *
+     * @param name Header name to look up
+     */
+    public String getHeader(String name) {
+        String match = name + ":";
+        Iterator headers = this.headers.iterator();
+        while (headers.hasNext()) {
+            String header = (String) headers.next();
+            if (header.startsWith(match)) {
+                return header.substring(match.length() + 1).trim();
+            }
+        }
+        return null;
+    }
+
+
+    /**
+     * <p>Return the text message for the HTTP status that was set.</p>
+     */
+    public String getMessage() {
+        return this.message;
+    }
+
+
+    /**
+     * <p>Return the HTTP status code that was set.</p>
+     */
+    public int getStatus() {
+        return this.status;
+    }
+
+
+    /**
      * <p>Set the <code>ServletOutputStream</code> to be returned by a
call to
      * <code>getOutputStream()</code>.</p>
      *
@@ -80,6 +121,9 @@
 
     private String encoding = "ISO-8859-1";
     private String contentType = "text/html";
+    private List headers = new ArrayList();
+    private String message = null;
+    private int status = HttpServletResponse.SC_OK;
     private ServletOutputStream stream = null;
     private PrintWriter writer = null;
 
@@ -96,28 +140,28 @@
 
     public void addDateHeader(String name, long value) {
 
-        throw new UnsupportedOperationException();
+        headers.add(name + ": " + formatDate(value));
 
     }
 
 
     public void addHeader(String name, String value) {
 
-        throw new UnsupportedOperationException();
+        headers.add(name + ": " + value);
 
     }
 
 
     public void addIntHeader(String name, int value) {
 
-        throw new UnsupportedOperationException();
+        headers.add(name + ": " + value);
 
     }
 
 
     public boolean containsHeader(String name) {
 
-        throw new UnsupportedOperationException();
+        return getHeader(name) != null;
 
     }
 
@@ -152,42 +196,47 @@
 
     public void sendError(int status) {
 
-        throw new UnsupportedOperationException();
+        this.status = status;
 
     }
 
 
     public void sendError(int status, String message) {
 
-        throw new UnsupportedOperationException();
+        this.status = status;
+        this.message = message;
 
     }
 
 
     public void sendRedirect(String location) {
 
-        throw new UnsupportedOperationException();
+        this.status = HttpServletResponse.SC_MOVED_TEMPORARILY;
+        this.message = location;
 
     }
 
 
     public void setDateHeader(String name, long value) {
 
-        throw new UnsupportedOperationException();
+        removeHeader(name);
+        addDateHeader(name, value);
 
     }
 
 
     public void setHeader(String name, String value) {
 
-        throw new UnsupportedOperationException();
+        removeHeader(name);
+        addHeader(name, value);
 
     }
 
 
     public void setIntHeader(String name, int value) {
 
-        throw new UnsupportedOperationException();
+        removeHeader(name);
+        addIntHeader(name, value);
 
     }
 
@@ -323,6 +372,49 @@
 
         throw new UnsupportedOperationException();
 
+    }
+
+
+    // --------------------------------------------------------- Private Methods
+
+
+    /**
+     * <p>The date formatting helper we will use in <code>httpTimestamp()</code>.
+     * Note that usage of this helper must be synchronized.</p>
+     */
+    private static SimpleDateFormat format =
+            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
+    static {
+        format.setTimeZone(TimeZone.getTimeZone("GMT"));
+    }
+
+
+    /**
+     * <p>Return a properly formatted String version of the specified
+     * date/time, formatted as required by the HTTP specification
+     *
+     * @param date Date/time, expressed as milliseconds since the epoch
+     */
+    private String formatDate(long date) {
+        return format.format(new Date(date));
+    }
+
+
+    /**
+     * <p>Remove any header that has been set with the specific name.</p>
+     *
+     * @param name Header name to look up
+     */
+    private void removeHeader(String name) {
+        String match = name + ":";
+        Iterator headers = this.headers.iterator();
+        while (headers.hasNext()) {
+            String header = (String) headers.next();
+            if (header.startsWith(match)) {
+                headers.remove();
+                return;
+            }
+        }
     }
 
 

Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java?rev=365272&r1=365271&r2=365272&view=diff
==============================================================================
--- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java
(original)
+++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockServletContext.java
Sun Jan  1 17:34:24 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -62,6 +62,18 @@
 
 
     /**
+     * <p>Add a new MIME type mapping to the set of mappings
+     * recognized by this instance.</p>
+     *
+     * @param extension Extension to check for (without the period)
+     * @param contentType Corresponding content type
+     */
+    public void addMimeType(String extension, String contentType) {
+        mimeTypes.put(extension, contentType);
+    }
+
+
+    /**
      * <p>Set the document root for <code>getRealPath()</code>
      * resolution.  This parameter <strong>MUST</strong> represent
      * a directory.</p>
@@ -78,6 +90,7 @@
 
     private Hashtable attributes = new Hashtable();
     private File documentRoot = null;
+    private Hashtable mimeTypes = new Hashtable();
     private Hashtable parameters = new Hashtable();
 
 
@@ -128,7 +141,12 @@
 
     public String getMimeType(String path) {
 
-        throw new UnsupportedOperationException();
+        int period = path.lastIndexOf('.');
+        if (period < 0) {
+            return null;
+        }
+        String extension = path.substring(period + 1);
+        return (String) mimeTypes.get(extension);
 
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Mime
View raw message