Author: alexsmirnov
Date: 2008-11-12 14:56:53 -0500 (Wed, 12 Nov 2008)
New Revision: 11124
Modified:
trunk/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java
trunk/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java
trunk/framework/jsf-test/src/main/java/org/richfaces/test/StagingConnection.java
trunk/framework/jsf-test/src/main/java/org/richfaces/test/StagingServer.java
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpRequest.java
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpResponse.java
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpSession.java
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingServletContext.java
Log:
Most of the Servlet specs have been implemented
Modified:
trunk/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java
===================================================================
---
trunk/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java 2008-11-12
18:35:56 UTC (rev 11123)
+++
trunk/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java 2008-11-12
19:56:53 UTC (rev 11124)
@@ -8,7 +8,9 @@
import javax.servlet.ServletException;
import org.apache.commons.httpclient.NameValuePair;
+import org.richfaces.test.staging.StagingHttpRequest;
+import com.gargoylesoftware.htmlunit.FormEncodingType;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
@@ -29,6 +31,18 @@
for (NameValuePair param : settings.getRequestParameters()) {
connection.addRequestParameter(param.getName(), param.getValue());
}
+ HttpMethod httpMethod = HttpMethod.valueOf(settings.getHttpMethod().toString());
+ connection.setMethod(httpMethod);
+ StagingHttpRequest request = connection.getRequest();
+ request.setCharacterEncoding(settings.getCharset());
+ String body = settings.getRequestBody();
+ String contentType = settings.getEncodingType().getName();
+ request.setRequestBody(body);
+ request.setContentType(contentType);
+ request.addHeaders(settings.getAdditionalHeaders());
+ if(null != body &&
FormEncodingType.URL_ENCODED.getName().equals(contentType)){
+ connection.parseFormParameters(body);
+ }
try {
connection.execute();
} catch (ServletException e) {
Modified: trunk/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java
===================================================================
---
trunk/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java 2008-11-12
18:35:56 UTC (rev 11123)
+++
trunk/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java 2008-11-12
19:56:53 UTC (rev 11124)
@@ -8,8 +8,10 @@
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Map.Entry;
import org.apache.commons.httpclient.NameValuePair;
@@ -75,7 +77,12 @@
}
public List<NameValuePair> getResponseHeaders() {
- // TODO Auto-generated method stub
- return Collections.emptyList();
+ ArrayList<NameValuePair> headers = new ArrayList<NameValuePair>(10);
+ for (Entry<String, String[]> entry :
serverConnection.getResponse().getHeaders().entrySet()) {
+ for (String value : entry.getValue()) {
+ headers.add(new NameValuePair(entry.getKey(),value));
+ }
+ };
+ return headers;
}
}
\ No newline at end of file
Modified:
trunk/framework/jsf-test/src/main/java/org/richfaces/test/StagingConnection.java
===================================================================
---
trunk/framework/jsf-test/src/main/java/org/richfaces/test/StagingConnection.java 2008-11-12
18:35:56 UTC (rev 11123)
+++
trunk/framework/jsf-test/src/main/java/org/richfaces/test/StagingConnection.java 2008-11-12
19:56:53 UTC (rev 11124)
@@ -4,9 +4,11 @@
package org.richfaces.test;
import java.io.IOException;
+import java.io.InvalidClassException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
@@ -66,7 +68,7 @@
private final String servletPath;
private boolean finished = false;
-
+
private boolean started = false;
private String queryString;
@@ -87,22 +89,37 @@
this.pathInfo);
this.request.setAttribute("javax.servlet.include.servlet_path",
this.servletPath);
- queryString = url.getQuery();
- if (null != queryString) {
- String[] queryParams = queryString.split("&");
- for (int i = 0; i < queryParams.length; i++) {
- String par = queryParams[i];
- int eqIndex = par.indexOf('=');
- if(eqIndex>=0){
- addRequestParameter(par.substring(0, eqIndex),
par.substring(eqIndex+1));
- } else {
- addRequestParameter(par, null);
- }
- }
- }
+ setQueryString(url.getQuery());
+ if (null != getQueryString()) {
+ parseFormParameters(queryString);
+ }
}
+ public void parseFormParameters(String queryString) {
+ String[] queryParams = queryString.split("&");
+ for (int i = 0; i < queryParams.length; i++) {
+ try {
+ String par = queryParams[i];
+ int eqIndex = par.indexOf('=');
+ if (eqIndex >= 0) {
+ // TODO - decode url-decoded values.
+ String name = URLDecoder.decode(par.substring(0, eqIndex),
+ request.getCharacterEncoding());
+ String value = URLDecoder.decode(
+ par.substring(eqIndex + 1), request
+ .getCharacterEncoding());
+ addRequestParameter(name, value);
+ } else {
+ addRequestParameter(URLDecoder.decode(par, request
+ .getCharacterEncoding()), null);
+ }
+ } catch (UnsupportedEncodingException e) {
+ throw new TestException(e);
+ }
+ }
+ }
+
/**
* @return the finished
*/
@@ -117,8 +134,6 @@
return started;
}
-
-
private void checkStarted() {
if (!isFinished()) {
throw new IllegalStateException("request have not been started");
@@ -127,7 +142,8 @@
public void execute() throws ServletException, IOException {
if (isStarted() || isFinished()) {
- throw new IllegalStateException("request have already been executed");
+ throw new IllegalStateException(
+ "request have already been executed");
}
start();
this.servlet.execute(request, response);
@@ -140,8 +156,9 @@
}
public void start() {
- log.fine("start "+getMethod()+" request processing for file
"+url.getFile());
- log.fine("request parameters: "+requestParameters);
+ log.fine("start " + getMethod() + " request processing for file "
+ + url.getFile());
+ log.fine("request parameters: " + requestParameters);
server.requestStarted(request);
started = true;
}
@@ -353,21 +370,21 @@
public HttpSession getSession(boolean create) {
return server.getSession(create);
}
-
+
@Override
public RequestDispatcher getRequestDispatcher(String path) {
RequestDispatcher dispatcher = null;
- if(!path.startsWith("/")){
+ if (!path.startsWith("/")) {
try {
- URL absoluteUrl = new URL(url,path);
+ URL absoluteUrl = new URL(url, path);
path = absoluteUrl.getFile();
} catch (MalformedURLException e) {
return null;
}
}
final RequestChain dispatchedServlet = server.getServlet(path);
- if(null != dispatchedServlet){
- dispatcher = new RequestDispatcher(){
+ if (null != dispatchedServlet) {
+ dispatcher = new RequestDispatcher() {
public void forward(ServletRequest request,
ServletResponse response) throws ServletException,
@@ -381,7 +398,7 @@
IOException {
dispatchedServlet.execute(request, response);
}
-
+
};
}
return dispatcher;
@@ -389,20 +406,20 @@
@Override
protected void attributeAdded(String name, Object o) {
- server.requestAttributeAdded(this,name,o);
-
+ server.requestAttributeAdded(this, name, o);
+
}
@Override
protected void attributeRemoved(String name, Object removed) {
- server.requestAttributeRemoved(this,name,removed);
-
+ server.requestAttributeRemoved(this, name, removed);
+
}
@Override
protected void attributeReplaced(String name, Object o) {
- server.requestAttributeReplaced(this,name,o);
-
+ server.requestAttributeReplaced(this, name, o);
+
}
}
@@ -442,4 +459,19 @@
return response.getErrorMessage();
}
+ /**
+ * @param queryString
+ * the queryString to set
+ */
+ public void setQueryString(String queryString) {
+ this.queryString = queryString;
+ }
+
+ /**
+ * @return the queryString
+ */
+ public String getQueryString() {
+ return queryString;
+ }
+
}
Modified: trunk/framework/jsf-test/src/main/java/org/richfaces/test/StagingServer.java
===================================================================
---
trunk/framework/jsf-test/src/main/java/org/richfaces/test/StagingServer.java 2008-11-12
18:35:56 UTC (rev 11123)
+++
trunk/framework/jsf-test/src/main/java/org/richfaces/test/StagingServer.java 2008-11-12
19:56:53 UTC (rev 11124)
@@ -31,11 +31,10 @@
import org.richfaces.test.staging.ServerResource;
import org.richfaces.test.staging.ServerResourcePath;
import org.richfaces.test.staging.ServerResourcesDirectory;
-import org.richfaces.test.staging.RequestChain;
import org.richfaces.test.staging.ServletContainer;
-import org.richfaces.test.staging.StaticServlet;
import org.richfaces.test.staging.StagingHttpSession;
import org.richfaces.test.staging.StagingServletContext;
+import org.richfaces.test.staging.StaticServlet;
/**
@@ -200,7 +199,7 @@
return getSession(true);
}
- public HttpSession getSession(boolean create){
+ public synchronized HttpSession getSession(boolean create){
if(null == this.session && create){
this.session = new ServerHttpSession();
// inform session listeners.
@@ -262,6 +261,7 @@
}
});
session.destroy();
+ session = null;
}
// Inform listeners
final ServletContextEvent event = new ServletContextEvent(context);
Modified:
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpRequest.java
===================================================================
---
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpRequest.java 2008-11-12
18:35:56 UTC (rev 11123)
+++
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpRequest.java 2008-11-12
19:56:53 UTC (rev 11124)
@@ -4,16 +4,22 @@
package org.richfaces.test.staging;
import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
+import java.text.DateFormat;
+import java.text.ParseException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
+import java.util.Set;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
@@ -35,8 +41,34 @@
public static final String LOCALHOST_IP = "127.0.0.1";
public static final String UTF8 = "UTF-8";
-
+ private String requestBody = null;
+ private String contentType;
+
+ private Map<String, Object> attributes = new HashMap<String, Object>();
+
+ private Map<String, String> headers = new HashMap<String, String>();
+
+ private Collection<Locale> locales = Arrays.asList(Locale.US,
+ Locale.GERMANY);
+
+ private String characterEncoding = UTF8;
+
+ /**
+ * @return the requestBody
+ */
+ public String getRequestBody() {
+ return requestBody;
+ }
+
+ /**
+ * @param requestBody
+ * the requestBody to set
+ */
+ public void setRequestBody(String requestBody) {
+ this.requestBody = requestBody;
+ }
+
/*
* (non-Javadoc)
*
@@ -57,7 +89,6 @@
return StagingServletContext.CONTEXT_PATH;
}
-
/*
* (non-Javadoc)
*
@@ -65,8 +96,14 @@
* javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
*/
public long getDateHeader(String name) {
- // TODO create headers support
- log.info("unimplemented request method getDateHeader");
+ String value = headers.get(name);
+ if(null != value){
+ try {
+ return DateFormat.getDateInstance(DateFormat.FULL,
getLocale()).parse(value).getTime();
+ } catch (ParseException e) {
+ throw new IllegalArgumentException(e.getMessage());
+ }
+ }
return -1;
}
@@ -76,9 +113,7 @@
* @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
*/
public String getHeader(String name) {
- // TODO create headers support
- log.info("unimplemented request method getHeader");
- return null;
+ return headers.get(name);
}
/*
@@ -88,9 +123,7 @@
*/
@SuppressWarnings("unchecked")
public Enumeration getHeaderNames() {
- // TODO create headers support
- log.info("unimplemented request method getHeaderNames");
- return null;
+ return Collections.enumeration(headers.keySet());
}
/*
@@ -100,9 +133,15 @@
*/
@SuppressWarnings("unchecked")
public Enumeration getHeaders(String name) {
- // TODO create headers support
- log.info("unimplemented request method getHeaders");
- return Collections.enumeration(Collections.EMPTY_LIST);
+ Set<String> values;
+ String value = headers.get(name);
+ if (null != value) {
+ values = Collections.singleton(value);
+
+ } else {
+ values = Collections.emptySet();
+ }
+ return Collections.enumeration(values);
}
/*
@@ -111,11 +150,21 @@
* @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
*/
public int getIntHeader(String name) {
- // TODO create headers support
- log.info("unimplemented request method getIntHeader");
+ String value = headers.get(name);
+ if(null != value){
+ return Integer.parseInt(value);
+ }
return -1;
}
+ public void addHeader(String name, String value) {
+ headers.put(name, value);
+ }
+
+ public void addHeaders(Map<String, String> headers) {
+ this.headers.putAll(headers);
+ }
+
/*
* (non-Javadoc)
*
@@ -126,7 +175,6 @@
return null;
}
-
/*
* (non-Javadoc)
*
@@ -138,7 +186,6 @@
return null;
}
-
/*
* (non-Javadoc)
*
@@ -159,8 +206,6 @@
return StagingHttpSession.SESSION_ID;
}
-
-
/*
* (non-Javadoc)
*
@@ -222,8 +267,6 @@
return false;
}
- private Map<String, Object> attributes = new HashMap<String, Object>();
-
/*
* (non-Javadoc)
*
@@ -258,7 +301,8 @@
* @see javax.servlet.ServletRequest#getContentLength()
*/
public int getContentLength() {
- return -1;
+ String body = getRequestBody();
+ return null == body ? -1 : body.length();
}
/*
@@ -267,18 +311,36 @@
* @see javax.servlet.ServletRequest#getContentType()
*/
public String getContentType() {
- log.info("unimplemented request method getContentType");
- return null;
+ return contentType;
}
+ /**
+ * @param contentType
+ * the contentType to set
+ */
+ public void setContentType(String contentType) {
+ this.contentType = contentType;
+ }
+
/*
* (non-Javadoc)
*
* @see javax.servlet.ServletRequest#getInputStream()
*/
public ServletInputStream getInputStream() throws IOException {
- // TODO implement post stream.
- log.info("unimplemented request method getInputStream");
+ String body = getRequestBody();
+ if(null != body){
+ final ByteArrayInputStream input = new
ByteArrayInputStream(body.getBytes(getCharacterEncoding()));
+ return new ServletInputStream(){
+
+ @Override
+ public int read() throws IOException {
+ // TODO Auto-generated method stub
+ return input.read();
+ }
+
+ };
+ }
return null;
}
@@ -318,9 +380,6 @@
return Locale.US;
}
- private Collection<Locale> locales = Arrays.asList(
- Locale.US, Locale.GERMANY);
-
/*
* (non-Javadoc)
*
@@ -331,8 +390,6 @@
return Collections.enumeration(locales);
}
- private String characterEncoding = UTF8;
-
/*
* (non-Javadoc)
*
@@ -348,8 +405,10 @@
* @see javax.servlet.ServletRequest#getReader()
*/
public BufferedReader getReader() throws IOException {
- // TODO implements request buffer.
- log.info("unimplemented request method getReader");
+ String body = getRequestBody();
+ if(null != body){
+ return new BufferedReader(new StringReader(body));
+ }
return null;
}
@@ -444,8 +503,8 @@
public void removeAttribute(String name) {
// TODO - inform listeners
Object removed = attributes.remove(name);
- if(null != removed){
- attributeRemoved(name,removed);
+ if (null != removed) {
+ attributeRemoved(name, removed);
}
}
@@ -458,14 +517,14 @@
* java.lang.Object)
*/
public void setAttribute(String name, Object o) {
- if(null == o){
+ if (null == o) {
removeAttribute(name);
} else {
Object oldValue = attributes.put(name, o);
- if(null != oldValue){
- attributeReplaced(name,o);
+ if (null != oldValue) {
+ attributeReplaced(name, o);
} else {
- attributeAdded(name,o);
+ attributeAdded(name, o);
}
}
@@ -482,7 +541,7 @@
*/
public void setCharacterEncoding(String env)
throws UnsupportedEncodingException {
- this.characterEncoding=env;
+ this.characterEncoding = env;
}
Modified:
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpResponse.java
===================================================================
---
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpResponse.java 2008-11-12
18:35:56 UTC (rev 11123)
+++
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpResponse.java 2008-11-12
19:56:53 UTC (rev 11124)
@@ -7,9 +7,13 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.text.DateFormat;
import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
import java.util.List;
import java.util.Locale;
+import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.ServletOutputStream;
@@ -53,6 +57,16 @@
private String encoding = StagingHttpRequest.UTF8;
+ private final Map<String, String[]> headers= new HashMap<String,
String[]>();
+
+
+ /**
+ * @return the headers
+ */
+ public Map<String, String[]> getHeaders() {
+ return headers;
+ }
+
/*
* (non-Javadoc)
*
@@ -61,9 +75,8 @@
* long)
*/
public void addDateHeader(String name, long date) {
- log.info("unimplemented response method addDateHeader");
- // TODO Auto-generated method stub
-
+ // TODO - locale support ?
+ addHeader(name, new Date(date).toString());
}
/*
@@ -73,8 +86,16 @@
* java.lang.String)
*/
public void addHeader(String name, String value) {
- // TODO Auto-generated method stub
- log.info("unimplemented response method addHeader");
+ String[] values = headers.get(name);
+ if (null == values) {
+ values = new String[1];
+ } else {
+ String[] newValues = new String[values.length + 1];
+ System.arraycopy(values, 0, newValues, 0, values.length);
+ values = newValues;
+ }
+ values[values.length - 1] = value;
+ headers.put(name, values);
}
@@ -86,9 +107,7 @@
* int)
*/
public void addIntHeader(String name, int value) {
- // TODO Auto-generated method stub
- log.info("unimplemented response method addIntHeader");
-
+ addHeader(name, String.valueOf(value));
}
/*
@@ -98,9 +117,7 @@
* javax.servlet.http.HttpServletResponse#containsHeader(java.lang.String)
*/
public boolean containsHeader(String name) {
- // TODO Auto-generated method stub
- log.info("unimplemented response method containsHeader");
- return false;
+ return headers.containsKey(name);
}
/*
@@ -182,8 +199,8 @@
* long)
*/
public void setDateHeader(String name, long date) {
- // TODO Auto-generated method stub
- log.info("unimplemented response method setDateHeader");
+ // TODO - locale support ?
+ setHeader(name, new Date(date).toString());
}
/*
@@ -193,8 +210,7 @@
* java.lang.String)
*/
public void setHeader(String name, String value) {
- // TODO Auto-generated method stub
- log.info("unimplemented response method setHeader");
+ headers.put(name, new String[]{value});
}
/*
@@ -205,8 +221,7 @@
* int)
*/
public void setIntHeader(String name, int value) {
- // TODO Auto-generated method stub
- log.info("unimplemented response method setIntHeader");
+ setHeader(name, String.valueOf(value));
}
/*
@@ -405,12 +420,14 @@
}
int i = type.indexOf(';');
if(i>=0){
+ setHeader("Content-Type", type);
contentType = type.substring(0, i).trim();
i=type.lastIndexOf('=');
if(i>=0){
setCharacterEncoding(type.substring(i+1).trim());
}
} else {
+ setHeader("Content-Type",
type+";charset="+getCharacterEncoding());
contentType = type;
}
}
Modified:
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpSession.java
===================================================================
---
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpSession.java 2008-11-12
18:35:56 UTC (rev 11123)
+++
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingHttpSession.java 2008-11-12
19:56:53 UTC (rev 11124)
@@ -9,6 +9,7 @@
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
@@ -26,11 +27,11 @@
public static final String SESSION_ID = "1234567890";
- private final Map<String, Object> attributes = new HashMap<String,
Object>();
+ private final Map<String, Object> attributes = new ConcurrentHashMap<String,
Object>();
private final long creationTime;
- private int inactiveTime = DEFAULT_INACTIVE_TIME;
+ private volatile int inactiveTime = DEFAULT_INACTIVE_TIME;
private boolean valid;
Modified:
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingServletContext.java
===================================================================
---
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingServletContext.java 2008-11-12
18:35:56 UTC (rev 11123)
+++
trunk/framework/jsf-test/src/main/java/org/richfaces/test/staging/StagingServletContext.java 2008-11-12
19:56:53 UTC (rev 11124)
@@ -13,6 +13,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -36,7 +37,7 @@
public static final String CONTEXT_PATH = "";
private static final String APPLICATION_NAME = "stub";
- private final Map<String, Object> attributes = new HashMap<String,
Object>();
+ private final Map<String, Object> attributes = new ConcurrentHashMap<String,
Object>();
private Map<String,String> initParameters = new HashMap<String, String>();
/* (non-Javadoc)